1.Write a function which takes in a dictionary are returns the sum of the keys plus the sum of the values, but only if all the keys and all the values are integers. Otherwise it returns False. >>>f({'a':1,'b':4,'c':7,'d':11}) False >>>f({1:2,3:4,5:6,7:8}) 36 2.Write a function to quickly compute the recaman sequence. 3. The Hofstadter Conway sequence is defined by a(1)=a(2)=1 and (for n>2 by) a(n)=a(a(n-1))+a(n-a(n-1)). Write a function to quickly compute this sequence.
>>> [hc(i) for i in range(1,20)] [1, 1, 2, 2, 3, 4, 4, 4, 5, 6, 7, 7, 8, 8, 8, 8, 9, 10, 11]
Question 1
def f(dic):
# if key / value is a string it returns a error
try:
# sum of all keys
keys = sum(dic.keys())
# sum of all values
values = sum(dic.values())
# sum of both keys and values
sumAll = keys + values
# returns sum
return sumAll
except:
# if it is not a integer returns False
return False
Code
Output
Question 2
def hc(n):
# if n is less than 0 it is invalid so returns 0
if n < 0:
return 0
# if n=1 and n=2 it returns 1
if n <= 2:
return 1
# remaining values will follow the formula
return hc(hc(n-1)) + hc(n - hc(n-1))
Code
Output
Get Answers For Free
Most questions answered within 1 hours.