# Write a function that takes a list of numbers as a parameter, and # returns the sum of the list. def listsum1(mylist): thesum = 0 for i in range(len(mylist)): thesum += mylist[i] return thesum print listsum1([1, 2, 3]) print listsum1([-1, -2, -3]) print listsum1([3, -3]) print listsum1(4 * [0]) print listsum1([]) def listsum2(mylist): thesum = 0 for val in mylist: thesum += val return thesum print listsum2([1, 2, 3]) print listsum2([-1, -2, -3]) print listsum2([3, -3]) print listsum2(4 * [0]) print listsum2([]) def listsum3(mylist): #there's a built-in sum function! #you won't be allowed to use in exam 2 most probably return sum(mylist) print listsum3([1, 2, 3]) print listsum3([-1, -2, -3]) print listsum3([3, -3]) print listsum3(4 * [0]) print listsum3([]) # Write a function that takes two parameters: # - a list of strings and # - an integer minlen. # The function should return a count of all the # strings in the list that are longer than minlen. def stringCount(mylist, minlen): '''(list, int) -> int Return a count of the number of strings inmylist longer than minlen. ''' count = 0 for str in mylist: if len(str) > minlen: count += 1 return count # Write another version of this function that returns # a list of all the strings that are longer than minlen def stringFilter(mylist, minlen): '''(list, int) -> list Return a list with all strings from mylist longer than minlen. ''' newlist = [] for s in mylist: if len(s) > minlen: newlist += [ s ] return newlist #Write a function that takes an integer N as a parameter and #constructs and returns a list of positive integers from #1 to N that evenly divide into that number. #For example if N is 4, the function should return [1,2,4]. def getDivisors(N): '''Return a list of divisors of the positive integer N.''' divisors = [] for i in range(1,N+1): if N % i == 0: divisors = divisors + [i] return divisors #Write a function that takes a list of integers representing #daily temperatures. Compute and return a list that contains #the [minimum, average, maximum] values. def daily_temperatures(temperature_list): '''''Compute the min, average, and max of a list of daily temps. Return a list of 3 items: min, average, and max.''' min_value = min(temperature_list) max_value = max(temperature_list) average = sum(temperature_list) / float(len(temperature_list)) return [min_value, average, max_value]