NOTE: there are many possible solutions!
def multiply(x, y):
'''given integers x and y, multiply x by y without using the "*" operator'''
if x == 0 or y == 0:
return 0
elif x < 0 and y < 0:
return abs(x) + multiply(abs(x), abs(y) - 1)
elif y < 0:
return y + multiply(x, y + 1)
return x + multiply(x, y - 1)
assert multiply(1, 1) == 1
assert multiply(2, 3) == 6
assert multiply(-2, 3) == -6
assert multiply(2, -3) == -6
assert multiply(-2, -3) == 6
assert multiply(3, 0) == 0
def reverse(x):
'''given a list x, return x in reverse order'''
if len(x) == 0:
return []
return [x[-1]] + reverse(x[:-1])
assert reverse([]) == []
assert reverse([1]) == [1]
assert reverse([1, 2]) == [2, 1]
assert reverse([1, 2, 3]) == [3, 2, 1]
def isPalindrome(x):
'''given a string x, return True if x is a palindrome, otherwise return False'''
if len(x) <= 1:
return True
elif x[0] != x[-1]:
return False
return isPalindrome(x[1:-1])
assert isPalindrome("") == True
assert isPalindrome("a") == True
assert isPalindrome("aa") == True
assert isPalindrome("ab") == False
assert isPalindrome("aba") == True
assert isPalindrome("abc") == False
assert isPalindrome("abba") == True
assert isPalindrome("abcba") == True