Python Problems Flashcards
Small problems / kata from Codewars
1
Q
Given a string containing the digits of a number, such as s = “501”, print out the sum of the individual digits. In this case, the output should be 6 = 5 + 0 + 1. Hint: int(‘9’) yields value 9.
A
Sum digits in string
s = "501" n = 0 for d in s: n += int(d) print(n)
2
Q
Given a string containing the digits of a number, such as s = “501”, print out the sum of the individual digits. In this case, the output should be 6 = 5 + 0 + 1. Hint: int(‘9’) yields value 9.
A
Reverse list
A = [1, 9, 2, 4] """ example swap (this is a common pattern also): t = A[3] A[3] = A[0] A[0] = t """ print(A) n = len(A) for i in range(n//2): # 0..n/2-1 (stop halfway!) t = A[n-i-1] A[n-i-1] = A[i] A[i] = t
print(A)