computing science 1p
DESCRIPTION
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. - PowerPoint PPT PresentationTRANSCRIPT
Computing Science 1P
Large Group Tutorial: Lab Exam & Class Test
Simon GayDepartment of Computing Science
University of Glasgow
2006/07
2006/07 Computing Science 1P Tutorial on Lab Exam / Class Test - Simon Gay 2
Remember the lab exam…
A sequence of items, each of weight between 1 and 20, are tobe 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 manyapplications of practical importance. In general, working outthe best packing given all of the items is an example of anNP-complete problem. No-one knows a better way thantrying all possible packings.
As we are allocating each item as it appears, it’s an online bin-packing problem.
2006/07 Computing Science 1P Tutorial on Lab Exam / Class Test - Simon Gay 3
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 inor: -1, if it won’t fit in any of the bins
2006/07 Computing Science 1P Tutorial on Lab Exam / Class Test - Simon Gay 4
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-fitalgorithm and one for the best-fit algorithm.
BEWARE: does findBin return the number of the bin (in therange 1…) or its index position (in the range 0…) ?
I will use the index position.
2006/07 Computing Science 1P Tutorial on Lab Exam / Class Test - Simon Gay 5
Bin-packing
Examples:
findBin([15,17],2)
should return 0 or 1, depending on whether we are usingfirst-fit or best-fit.
findBin([15,17],6)
should return -1.
Assume that we have findBin.
2006/07 Computing Science 1P Tutorial on Lab Exam / Class Test - Simon Gay 6
Bin-packing: main program
Obviously we need a variable to store the list of bins beingused, with the weight of each bin.
Example: [ 10, 5, 16 ]
The values in this list will never be 0, because we won’tstart 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 ofall of the items.
2006/07 Computing Science 1P Tutorial on Lab Exam / Class Test - Simon Gay 7
Do we need a variable to store a list of the weights of all the items?
• Yes• No• Don't know
2006/07 Computing Science 1P Tutorial on Lab Exam / Class Test - Simon Gay 8
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 familiarstructure:
bins = []weight = input(“Enter weight: ”)while weight != 0:
## do something with weight#weight = input(“Enter weight: ”)
2006/07 Computing Science 1P Tutorial on Lab Exam / Class Test - Simon Gay 9
Bin-packing: main program
What do we do with weight ? We have to callfindBin(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] + weightweight = input(“Enter weight: ”)
2006/07 Computing Science 1P Tutorial on Lab Exam / Class Test - Simon Gay 10
Bin-packing: main program
To output the report of the latest allocation:
bins = []item = 0weight = input(“Enter weight: ”) while weight != 0:
item = item + 1bin = 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+1weight = input(“Enter weight: ”)
2006/07 Computing Science 1P Tutorial on Lab Exam / Class Test - Simon Gay 11
Do you prefer bins = [ ] or bins = [ 0 ] ?
• bins = [ ]• bins = [ 0 ]
2006/07 Computing Science 1P Tutorial on Lab Exam / Class Test - Simon Gay 12
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
2006/07 Computing Science 1P Tutorial on Lab Exam / Class Test - Simon Gay 13
Bin-packing: final output
The very easy way:
print “Bin weights:”, bins
But it prints square brackets, which we don’t want.
2006/07 Computing Science 1P Tutorial on Lab Exam / Class Test - Simon Gay 14
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.
2006/07 Computing Science 1P Tutorial on Lab Exam / Class Test - Simon Gay 15
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.
2006/07 Computing Science 1P Tutorial on Lab Exam / Class Test - Simon Gay 16
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]
2006/07 Computing Science 1P Tutorial on Lab Exam / Class Test - Simon Gay 17
Bin-packing: almost everythingbins = []item = 0weight = input(“Enter weight: ”)while weight != 0:
item = item + 1bin = 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+1weight = 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]
2006/07 Computing Science 1P Tutorial on Lab Exam / Class Test - Simon Gay 18
Defining findBin for first-fit
def findBin(bins,weight):b = 0while b < len(bins):
if bins[b]+weight <= 20:return b
else:b = b + 1
return -1
2006/07 Computing Science 1P Tutorial on Lab Exam / Class Test - Simon Gay 19
Defining findBin for best-fit
def findBin(bins,weight):b = 0best = -1 # index of best so farwhile b < len(bins):
if bins[b]+weight <= 20:if best == -1 or bins[b] > bins[best]:
best = bb = b + 1
return best
2006/07 Computing Science 1P Tutorial on Lab Exam / Class Test - Simon Gay 20
Class Test: Question 5
Input a string and output the number of occurrences of eachletter, in a tabular format.
this is the day of the cs1p class test
a: 3 b: 0 c: 2 d: 2e: 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
2006/07 Computing Science 1P Tutorial on Lab Exam / Class Test - Simon Gay 21
Part (a)
Define a function which takes a character and a string asparameters and returns the number of times that thecharacter appears in the string.
def count(c,s):n = 0for nextC in s:
if nextC == c:n = n + 1
return n
Exam technique: the function should DO WHAT THE QUESTION ASKED
2006/07 Computing Science 1P Tutorial on Lab Exam / Class Test - Simon Gay 22
Part (a)
Alternatively:
def count(c,s):n = 0i = 0while i < len(s):
if s[i] == c:n = n + 1
i = i + 1return n
A good example of when for … in is simpler: we don’t need to do anything with the position within the string, just thecharacter at each position.
2006/07 Computing Science 1P Tutorial on Lab Exam / Class Test - Simon Gay 23
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”:
2006/07 Computing Science 1P Tutorial on Lab Exam / Class Test - Simon Gay 24
Part (b)
Of course we need to input the string to analyze:
text = raw_input(“Enter the string: ”)
for c in “abcdefghijklomnopqrstuvwxyz”:
2006/07 Computing Science 1P Tutorial on Lab Exam / Class Test - Simon Gay 25
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 ...
2006/07 Computing Science 1P Tutorial on Lab Exam / Class Test - Simon Gay 26
Part (b)
So we need to keep track of which column we are on, andstart a new line when necessary:
text = raw_input(“Enter the string: ”)
column = 0for c in “abcdefghijklomnopqrstuvwxyz”:
print c+“:”, count(c,text), “ ”,column = column + 1if column = 4:
column = 0print
2006/07 Computing Science 1P Tutorial on Lab Exam / Class Test - Simon Gay 27
Part (b)
Alternatively:
text = raw_input(“Enter the string: ”)
column = 0for c in “abcdefghijklomnopqrstuvwxyz”:
print c+“:”, count(c,text), “ ”,column = column + 1if column % 4 == 0:
2006/07 Computing Science 1P Tutorial on Lab Exam / Class Test - Simon Gay 28
Part (b)
Another approach avoids using a string of the whole alphabet.It uses the functions ord and chr, which you might not know.
2006/07 Computing Science 1P Tutorial on Lab Exam / Class Test - Simon Gay 29
Do you know what ord and chr do?
• Yes• No
2006/07 Computing Science 1P Tutorial on Lab Exam / Class Test - Simon Gay 30
Part (b)
The function chr is given an integer and returns thecorresponding 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 canuse the following expression to give us the n’th letter of thealphabet, starting the numbering from 0:
chr(n+ord(‘a’))
Of course we could wrap this up in a function (exercise…)
Look up ASCII