310 likes | 452 Views
Computing Science 1P. Large Group Tutorial: Lab Exam & Class Test. Simon Gay Department of Computing Science University of Glasgow. 2006/07. Remember the lab exam…. A sequence of items, each of weight between 1 and 20, are to be packed into bins, each of capacity 20.
E N D
Computing Science 1P Large Group Tutorial: Lab Exam & Class Test Simon Gay Department of Computing Science University of Glasgow 2006/07
Remember the lab exam… A sequence of items, each of weight between 1 and 20, are to be packed into bins, each of capacity 20. As each item appears, work out which bin to put it in: - using the “first fit” algorithm - using the “best fit” algorithm This is an example of a bin-packing problem, with many applications of practical importance. In general, working out the best packing given all of the items is an example of an NP-complete problem. No-one knows a better way than trying all possible packings. As we are allocating each item as it appears, it’s an online bin-packing problem. Computing Science 1P Tutorial on Lab Exam / Class Test - Simon Gay
Bin-packing The question tells us to define a function findBin which is given: a list of the weights of the bins in use the weight of the next item and returns: either:the number of the bin to put the next item in or:-1, if it won’t fit in any of the bins Computing Science 1P Tutorial on Lab Exam / Class Test - Simon Gay
Bin-packing The function definition will start with def findBin(bins,weight): and it must return an integer value, including the possibility of -1 There will be two versions of findBin, one for the first-fit algorithm and one for the best-fit algorithm. BEWARE: does findBinreturn the number of the bin (in the range 1…) or its index position (in the range 0…) ? I will use the index position. Computing Science 1P Tutorial on Lab Exam / Class Test - Simon Gay
Bin-packing Examples: findBin([15,17],2) should return 0 or 1, depending on whether we are using first-fit or best-fit. findBin([15,17],6) should return -1. Assume that we have findBin. Computing Science 1P Tutorial on Lab Exam / Class Test - Simon Gay
Bin-packing: main program Obviously we need a variable to store the list of bins being used, with the weight of each bin. Example: [ 10, 5, 16 ] The values in this list will never be 0, because we won’t start a new bin unless there is something to put in it. Initially this list will be empty. bins = [] It’s worth asking whether we need to store the weights of all of the items. Computing Science 1P Tutorial on Lab Exam / Class Test - Simon Gay
Do we need a variable to store a list of the weights of all the items? • Yes • No • Don't know Computing Science 1P Tutorial on Lab Exam / Class Test - Simon Gay
Bin-packing: main program All we need to do is consider each item in turn, as it is entered; there is no need to store them. The program has a familiar structure: bins = [] weight = input(“Enter weight: ”) while weight != 0: # # do something with weight # weight = input(“Enter weight: ”) Computing Science 1P Tutorial on Lab Exam / Class Test - Simon Gay
Bin-packing: main program What do we do with weight ? We have to call findBin(bins,weight) to find out which bin to put it in. If the result is -1 then that’s a special case: a new bin. bins = [] weight = input(“Enter weight: ”) while weight != 0: bin = findBin(bins,weight) if bin == -1: bins = bins + [weight] else: bins[bin] = bins[bin] + weight weight = input(“Enter weight: ”) Computing Science 1P Tutorial on Lab Exam / Class Test - Simon Gay
Bin-packing: main program To output the report of the latest allocation: bins = [] item = 0 weight = input(“Enter weight: ”) while weight != 0: item = item + 1 bin = findBin(bins,weight) if bin == -1: bin = len(bins) bins = bins + [weight] else: bins[bin] = bins[bin] + weight print “Item”, item, “weight”, weight, “bin”, bin+1 weight = input(“Enter weight: ”) Computing Science 1P Tutorial on Lab Exam / Class Test - Simon Gay
Do you prefer bins = [ ] or bins = [ 0 ] ? • bins = [ ] • bins = [ 0 ] Computing Science 1P Tutorial on Lab Exam / Class Test - Simon Gay
How many bins are needed to pack 0 items? • None • One, but it is empty • Two, but they are both empty • Some other number • Don't know Computing Science 1P Tutorial on Lab Exam / Class Test - Simon Gay
Bin-packing: final output The very easy way: print “Bin weights:”, bins But it prints square brackets, which we don’t want. Computing Science 1P Tutorial on Lab Exam / Class Test - Simon Gay
Bin-packing: final output The easy way without the square brackets: print “Bin weights:”, for i in bins: print i, But it doesn’t print commas between the numbers. Computing Science 1P Tutorial on Lab Exam / Class Test - Simon Gay
Bin-packing: final output With commas but not quite right: print “Bin weights:”, for i in bins: print i, “,”, It prints an unwanted comma after the final number. Computing Science 1P Tutorial on Lab Exam / Class Test - Simon Gay
Bin-packing: final output The right way: print “Bin weights:”, for i in range(len(bins)-1): print str(bins[i]) + “,”, if len(bins) > 0: print bins[len(bins)-1] Computing Science 1P Tutorial on Lab Exam / Class Test - Simon Gay
Bin-packing: almost everything bins = [] item = 0 weight = input(“Enter weight: ”) while weight != 0: item = item + 1 bin = findBin(bins,weight) if bin == -1: bin = len(bins) bins = bins + [weight] else: bins[bin] = bins[bin] + weight print “Item”, item, “weight”, weight, “bin”, bin+1 weight = input(“Enter weight: ”) print “Bin weights:”, for i in range(len(bins)-1): print str(bins[i]) + “,”, if len(bins) > 0: print bins[len(bins)-1] Computing Science 1P Tutorial on Lab Exam / Class Test - Simon Gay
Defining findBin for first-fit def findBin(bins,weight): b = 0 while b < len(bins): if bins[b]+weight <= 20: return b else: b = b + 1 return -1 Computing Science 1P Tutorial on Lab Exam / Class Test - Simon Gay
Defining findBin for best-fit def findBin(bins,weight): b = 0 best = -1 # index of best so far while b < len(bins): if bins[b]+weight <= 20: if best == -1 or bins[b] > bins[best]: best = b b = b + 1 return best Computing Science 1P Tutorial on Lab Exam / Class Test - Simon Gay
Class Test: Question 5 Input a string and output the number of occurrences of each letter, in a tabular format. this is the day of the cs1p class test a: 3 b: 0 c: 2 d: 2 e: 3 f: 1 g: 0 h: 2 i: 1 j: 0 k: 0 l: 1 m: 0 n: 0 o: 2 p: 1 q: 0 r: 0 s: 5 t: 5 u: 0 v: 0 w: 0 x: 0 y: 2 z: 0 Computing Science 1P Tutorial on Lab Exam / Class Test - Simon Gay
Part (a) Define a function which takes a character and a string as parameters and returns the number of times that the character appears in the string. def count(c,s): n = 0 for nextC in s: if nextC == c: n = n + 1 return n Exam technique: the function should DO WHAT THE QUESTION ASKED Computing Science 1P Tutorial on Lab Exam / Class Test - Simon Gay
Part (a) Alternatively: def count(c,s): n = 0 i = 0 while i < len(s): if s[i] == c: n = n + 1 i = i + 1 return n A good example of when for … in is simpler: we don’t need to do anything with the position within the string, just the character at each position. Computing Science 1P Tutorial on Lab Exam / Class Test - Simon Gay
Part (b) A simple way to do this is to loop over a string (or a list) of all the letters in the alphabet. for c in “abcdefghijklomnopqrstuvwxyz”: Computing Science 1P Tutorial on Lab Exam / Class Test - Simon Gay
Part (b) Of course we need to input the string to analyze: text = raw_input(“Enter the string: ”) for c in “abcdefghijklomnopqrstuvwxyz”: Computing Science 1P Tutorial on Lab Exam / Class Test - Simon Gay
Part (b) Now we can use the function from part (a): text = raw_input(“Enter the string: ”) for c in “abcdefghijklomnopqrstuvwxyz”: print c+“:”, count(c,text), “ ”, This prints the output on one long line: a: 3 b: 0 c: 2 d: 2 e: 3 ... Computing Science 1P Tutorial on Lab Exam / Class Test - Simon Gay
Part (b) So we need to keep track of which column we are on, and start a new line when necessary: text = raw_input(“Enter the string: ”) column = 0 for c in “abcdefghijklomnopqrstuvwxyz”: print c+“:”, count(c,text), “ ”, column = column + 1 if column = 4: column = 0 print Computing Science 1P Tutorial on Lab Exam / Class Test - Simon Gay
Part (b) Alternatively: text = raw_input(“Enter the string: ”) column = 0 for c in “abcdefghijklomnopqrstuvwxyz”: print c+“:”, count(c,text), “ ”, column = column + 1 if column % 4 == 0: print Computing Science 1P Tutorial on Lab Exam / Class Test - Simon Gay
Part (b) Another approach avoids using a string of the whole alphabet. It uses the functions ord and chr, which you might not know. Computing Science 1P Tutorial on Lab Exam / Class Test - Simon Gay
Do you know what ord and chr do? • Yes • No Computing Science 1P Tutorial on Lab Exam / Class Test - Simon Gay
Part (b) The function chr is given an integer and returns the corresponding character. chr(97) = ‘a’ and so on. The function ord does the opposite: ord(‘a’) = 97 To avoid having to remember the magic number 97, we can use the following expression to give us the n’th letter of the alphabet, starting the numbering from 0: Look up ASCII chr(n+ord(‘a’)) Of course we could wrap this up in a function (exercise…) Computing Science 1P Tutorial on Lab Exam / Class Test - Simon Gay
Part (b) This gives an alternative solution: text = raw_input(“Enter the string: ”) column = 0 for i in range(26): c = chr(i + ord(‘a’)) print c+“:”, count(c,text), “ ”, column = column + 1 if column % 4 == 0: print Computing Science 1P Tutorial on Lab Exam / Class Test - Simon Gay