Presentation is loading. Please wait.

Presentation is loading. Please wait.

Computing Science 1P Large Group Tutorial: Lab Exam & Class Test Simon Gay Department of Computing Science University of Glasgow 2006/07.

Similar presentations


Presentation on theme: "Computing Science 1P Large Group Tutorial: Lab Exam & Class Test Simon Gay Department of Computing Science University of Glasgow 2006/07."— Presentation transcript:

1 Computing Science 1P Large Group Tutorial: Lab Exam & Class Test Simon Gay Department of Computing Science University of Glasgow 2006/07

2 Computing Science 1P Tutorial on Lab Exam / Class Test - Simon Gay2 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.

3 2006/07Computing Science 1P Tutorial on Lab Exam / Class Test - Simon Gay3 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

4 2006/07Computing Science 1P Tutorial on Lab Exam / Class Test - Simon Gay4 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 findBin return the number of the bin (in the range 1…) or its index position (in the range 0…) ? I will use the index position.

5 2006/07Computing Science 1P Tutorial on Lab Exam / Class Test - Simon Gay5 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.

6 2006/07Computing Science 1P Tutorial on Lab Exam / Class Test - Simon Gay6 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.

7 2006/07Computing Science 1P Tutorial on Lab Exam / Class Test - Simon Gay7 Do we need a variable to store a list of the weights of all the items? Yes No Don't know

8 2006/07Computing Science 1P Tutorial on Lab Exam / Class Test - Simon Gay8 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: ”)

9 2006/07Computing Science 1P Tutorial on Lab Exam / Class Test - Simon Gay9 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: ”)

10 2006/07Computing Science 1P Tutorial on Lab Exam / Class Test - Simon Gay10 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: ”)

11 2006/07Computing Science 1P Tutorial on Lab Exam / Class Test - Simon Gay11 Do you prefer bins = [ ] or bins = [ 0 ] ? bins = [ ] bins = [ 0 ]

12 2006/07Computing Science 1P Tutorial on Lab Exam / Class Test - Simon Gay12 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

13 2006/07Computing Science 1P Tutorial on Lab Exam / Class Test - Simon Gay13 Bin-packing: final output The very easy way: print “Bin weights:”, bins But it prints square brackets, which we don’t want.

14 2006/07Computing Science 1P Tutorial on Lab Exam / Class Test - Simon Gay14 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.

15 2006/07Computing Science 1P Tutorial on Lab Exam / Class Test - Simon Gay15 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.

16 2006/07Computing Science 1P Tutorial on Lab Exam / Class Test - Simon Gay16 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]

17 2006/07Computing Science 1P Tutorial on Lab Exam / Class Test - Simon Gay17 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]

18 2006/07Computing Science 1P Tutorial on Lab Exam / Class Test - Simon Gay18 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

19 2006/07Computing Science 1P Tutorial on Lab Exam / Class Test - Simon Gay19 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

20 2006/07Computing Science 1P Tutorial on Lab Exam / Class Test - Simon Gay20 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

21 2006/07Computing Science 1P Tutorial on Lab Exam / Class Test - Simon Gay21 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

22 2006/07Computing Science 1P Tutorial on Lab Exam / Class Test - Simon Gay22 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.

23 2006/07Computing Science 1P Tutorial on Lab Exam / Class Test - Simon Gay23 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”:

24 2006/07Computing Science 1P Tutorial on Lab Exam / Class Test - Simon Gay24 Part (b) Of course we need to input the string to analyze: text = raw_input(“Enter the string: ”) for c in “abcdefghijklomnopqrstuvwxyz”:

25 2006/07Computing Science 1P Tutorial on Lab Exam / Class Test - Simon Gay25 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...

26 2006/07Computing Science 1P Tutorial on Lab Exam / Class Test - Simon Gay26 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

27 2006/07Computing Science 1P Tutorial on Lab Exam / Class Test - Simon Gay27 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

28 2006/07Computing Science 1P Tutorial on Lab Exam / Class Test - Simon Gay28 Part (b) Another approach avoids using a string of the whole alphabet. It uses the functions ord and chr, which you might not know.

29 2006/07Computing Science 1P Tutorial on Lab Exam / Class Test - Simon Gay29 Do you know what ord and chr do? Yes No

30 2006/07Computing Science 1P Tutorial on Lab Exam / Class Test - Simon Gay30 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: chr(n+ord(‘a’)) Of course we could wrap this up in a function (exercise…) Look up ASCII

31 2006/07Computing Science 1P Tutorial on Lab Exam / Class Test - Simon Gay31 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


Download ppt "Computing Science 1P Large Group Tutorial: Lab Exam & Class Test Simon Gay Department of Computing Science University of Glasgow 2006/07."

Similar presentations


Ads by Google