full-speed-python 習題解答(四)
5.1 Exercise with the for loop
1.Create a function “add” that receives a list as parameter and returns the sum of all elements in the list. Use the “for” loop to iterate over the elements of the list.
def add(alist): sum = 0 for each in alist: sum += each return sum add([1,2,3])
6
2.Create a function that receives a list as parameter and returns the maximum value in the list. As you iterate over the list you may want to keep the maximum value found so far in order to keep comparing it with the next elements of the list.
def max(alist): max_value = alist[0] for i in range(len(alist)): if alist[i] >= max_value: max_value = alist[i] return max_value max([10,2,3,4,5])
10
3.Modify the previous function such that it returns a list with the first element being the maximum value and the second being the index of the maximum value in the list. Besides keep the last maximum value found so far, you need to keep also the position where it occured.
def max(alist): max_value_index = 0 max_value = alist[max_value_index] for i,value in enumerate(alist): if value >= max_value: max_value_index = i max_value = value print( 'maxvalue',max_value, 'index:',max_value_index) max([10,2,3,4,5])
maxvalue 10 index: 0
4.Implement a function that returns the reverse of a list received as parameter. You may create an empty list and keep adding the values in reversed order as they come from the original list. Check what you can do with lists at https://docs.python. org/3/tutorial/datastructures.html#more-on-lists.
def reverse(alist): reverse_list = [] for i in range(len(alist)): reverse_list.append(alist[len(alist)-1-i]) return reverse_list print(reverse([1,2,3,4,5]))
method_2
alist = [1,2,3,4,5] alist.reverse() print(alist)
[5, 4, 3, 2, 1] [5, 4, 3, 2, 1]
5.Make the function “is_sorted” that receives a list as parameter and returns True if the list is sorted by increasing order. For instance [1, 2, 2, 3] is ordered while [1, 2, 3, 2] is not. Suggestion: you have to compare a number in the list with the next one, so you can use indexes or you need to keep the previous number in a variable as you iterate over the list.
def is_sorted(alist): t = alist[0] for i in range(1, len(alist)): if alist[i] < t: return False else: t = alist[i] i+=1 if i==len(alist): return True print(is_sorted([1, 2, 3, 3]))
True
6.Implement the function “is_sorted_dec” which is similar to the previous one but all items must be sorted by decreasing order
def is_sorted_dec(alist): t = alist[0] for i in range(1, len(alist)): if alist[i] > t: return False else: t = alist[i] i+=1 if i==len(alist): return True print(is_sorted_dec([3, 2, 2, 1]))
True
7.Implement the “has_duplicates” function which verifies if a list has duplicate values. You may have to use two “for” loops, where for each value you have to check for duplicates on the rest of the list.
def has_duplicates(alist): for i in range(len(alist)): complist = [] for each in alist[i+1:len(alist)]: t = alist[i] - each if t == 0: complist.append(t) if(len(complist)>0): print(alist[i],'has duplicate') has_duplicates([1,2,3,3,3,1,2])
1 has duplicate 2 has duplicate 3 has duplicate 3 has duplicate
method_2
def has_duplicate(alist): set_alist = set(alist) if len(set_alist)!=len(alist): return 'has_duplicate' else: return 'No_duplicate' has_duplicate([1,2,4,3])
‘No_duplicate’
method3
def has_duplicate(alist): set_alist = set(alist) for item in set_alist: print(item,'has been found',alist.count(item),'times') has_duplicate([1,2,2,2,3,3,78,78])
1 has been found 1 times 2 has been found 3 times 3 has been found 2 times 78 has been found 2 times
method_4
from collections import Counter Counter([1,2,2,2,3,3,78,78])
Counter({1: 1, 2: 3, 3: 2, 78: 2})