# # Changes in lists # # Practice # Preliminary notes: - Based on Chapter 6 [ ] for assignment, del, append(), extend(), insert(), remove(), sort() - Required knowledge: Chapters 1-6 - On the surface: toolbox; yet much more - Use only studied material. # Rational Given a string, replace all spaces with a comma. s = "a b c" s[1] = ',' # 1 s = "a b c" newS = "" for c in s: if c != " ": newS = newS + c else: newS = newS + "," print(newS) print(s) # 2 s = "a b c" newS = s.replace(' ', ',') print(newS) print(s) # Q. 1 # Write a program that receives a list # of integers and replaces all integers # greater than 99 by "NA". lst = [5, 150, -32, 250, 0] for i in range(len(lst)): if lst[i] > 99: lst[i] = "NA" print(lst) # Q. 2 # Write a program that given the following list: # [ ['ID', 1, 2, 3, 4], # ['GENDER', 1, 1, 2, 1], # ['INCOME', 5326, 2136, 7632, 6661] ] # which represents the following table: # ID GENDER INCOME # 1 1 5326 # 2 1 2136 # 3 2 7632 # 4 1 6661 # adds 100 to the income of all GENDER == 2. table = [ ['ID', 1, 2, 3, 4], ['GENDER', 1, 1, 2, 1], ['INCOME', 5326, 2136, 7632, 6661] ] for i in range(len(table[1])): if table[1][i] == 2: table[2][i] = table[2][i] + 100 print(table) # Q. 3 # Write a program that receives 10 integers # in range 1-5. The program finds how many times # each number in the range was entered. nums = [0] * 5 for i in range(10): num = int(input("Please enter an integer in range 1-5: ")) nums[num - 1] = nums[num - 1] + 1 print("Number of 1 entered is:", nums[0]) # Q. 4 # Which name should Yasmin give her daughter, # (1) Su, (2) Sun, (3) Suny, (4) or Sunia? # Write a program that iteratively receives a number in # range 1-4 indicating a name, until q is entered. # The program then prints the name which was # chosen the greatest number of times. names = ['Su', 'Sun', 'Suny', 'Sunia'] counts = [0] * 4 num = input("Please enter your choice 1-4: ") while num != 'q': choice = int(num) counts [choice - 1] = counts [choice - 1] + 1 num = input("Please enter your choice 1-4: ") print(counts) print(names[counts.index(max(counts))]) # Q. 5 # Which destination should Dan choose for # holiday: (1) Paris, (2) London, (3) Tokyo # or (4) Berlin? Write a program that receives # a number in range 1-4 indicating a destination, # and the number of points the user gives it, # until q is entered. The program then prints # the destination that received the greatest # number of points. # Challenge: what would we do if the # points were multiplied and not added? names = ['Paris', 'London', 'Tokyo', 'Berlin'] sums = [0] * 4 num = input("Please enter your choice 1-4: ") while num != 'q': choice = int(num) points = int(input("Please enter number of points: ")) sums [choice - 1] = sums [choice - 1] + points num = input("Please enter your choice 1-4: ") print(sums) print(names[sums.index(max(sums))]) # Q. 6 - append() # Write a program that produces a list of # 10 random numbers in range 5-15. # with + import random lst = [] for i in range(10): lst = lst + [random.randint(5, 15)] print(lst) # with append() import random lst = [] for i in range(10): lst.append(random.randint(5, 15)) print(lst) Comprensions lst = [random.randint(5, 15) for i in range(10)] # A note on list comprehension # Q. 7 - extend() # Write a program that receives lists of # pairs of integers until q is entered. # THe program produces a list including # all the integers in the list. lst = [] yourLst = input("Please enter a list of two integers: ") while yourLst != 'q': lst.extend(eval(yourLst)) yourLst = input("Please enter a list of two integers: ") print(lst) # Q. 8 - insert() # Write a program that receives a list of # integers and rearranges it such that # all negative integers come before 0, # which itself comes before all positive # integers. #lst = eval(input("Please enter any list of integers: ")) lst = [-3, -1, 10, 0, 0, -7, 5] newLst = [] negNum = 0 for num in lst: if num > 0: newLst.append(num) elif num < 0: newLst.insert(0, num) negNum = negNum + 1 else: newLst.insert(negNum, 0) print(newLst) # Q. 9 - del # Write a program that given a list of integers, # deletes all integers greater than 50. lst = [100, 20, 30, 70] newLst = [] for num in lst: if num <= 50: newLst.append(num) print(newLst) lst = [100, 20, 30, 70] i = 0 while i < len(lst) : print("i = ", i, lst, "len =", len(lst)) if lst[i] > 50: del lst[i] else: i = i + 1 print(lst) # Q. 10 - remove() # Write a program that given a list of # integers, removes all duplicates. lst = [5, 5, 5, 8, 8, 3, -1, -1, -1, 5] for num in lst: while lst.count(num) > 1: lst.remove(num) print(lst) # old solution lst = [5, 5, 5, 8, 8, 3, -1, -1, -1, 5] newLst = [] for num in lst: if num not in newLst: newLst.append(num) print(newLst) Q. ? - sort() The following string contains the text of the first sentence in James Joyce-s Ulysses. sent = "Stately, plump Buck Mulligan came from the stairhead, bearing a bowl of lather on which a mirror and a razor lay crossed. A yellow dressinggown, ungirdled, was sustained gently behind him on the mild morning air. He held the bowl aloft and intoned:" find the most freqent letter in this sentence. import string sent = "Stately, plump Buck Mulligan came from the stairhead, bearing a bowl of lather on which a mirror and a razor lay crossed. A yellow dressinggown, ungirdled, was sustained gently behind him on the mild morning air. He held the bowl aloft and intoned:" freqs = [] for c in sent.lower(): if c != " " and c not in string.punctuation: freqs.append([sent.count(c), c]) freqs.sort(reverse = True) print(freqs[0]) print(string.punctuation)