# lecture 4 code
print(sum([1, 2, 3]))
print(sum([3, 4.2, 8]))
#print(sum(["Mark","a","z"])) # error!
def hasNoNegatives(n):
for myNumber in n:
if myNumber < 0:
return False
return True
a = [1,3,-9,27,42]
print (hasNoNegatives(a))
def average(l):
#return sum(l)/len(l)
total = 0
for i in range(len(l)):
total += l[i]
return total/(i+1)
print(average([1,2,3]))
def copyList(l):
# this code creates and returns an alias - incorrect
#a = l
#return a
myExtraSpecialList = []
for i in range(len(l)):
myExtraSpecialList += [l[i]]
return myExtraSpecialList
arr = [1,2,3]
arr2 = copyList(arr)
arr2[2] = 42
print(arr, arr2)
def sumLists(l1, l2):
if len(l1) != len(l2):
return [] # could also return None to indicate it was unable to execute
#sumList = [] # sumList is the incorrect length (length == 0)
sumList = [0] * len(l1) # sumList has the correct number of elements
for i in range(len(l2)):
#sumList += [l1[i] + l2[i]] # correct code is sumList initialized to []
sumList[i] = l1[i] + l2[i]
return sumList
print(sumLists(arr,arr2))