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

Slides:



Advertisements
Similar presentations
Computing Science 1P Large Group Tutorial 19 Simon Gay Department of Computing Science University of Glasgow 2006/07.
Advertisements

Introduction to Computing Science and Programming I
String and Lists Dr. Benito Mendoza. 2 Outline What is a string String operations Traversing strings String slices What is a list Traversing a list List.
16-May-15 Sudden Python Drinking from the Fire Hose.
1 9/29/06CS150 Introduction to Computer Science 1 Loops Section Page 255.
Introduction to Computer Science Exam Information Unit 20.
R-1 University of Washington Computer Programming I Lecture 17: Multidimensional Arrays © 2000 UW CSE.
Chapter 2 Writing Simple Programs
Python. What is Python? A programming language we can use to communicate with the computer and solve problems We give the computer instructions that it.
CMPT 120 Lists and Strings Summer 2012 Instructor: Hassan Khosravi.
Group practice in problem design and problem solving
An Introduction to Textual Programming
Computing Science 1P Large Group Tutorial 17 Simon Gay Department of Computing Science University of Glasgow 2006/07.
Lists in Python.
Computing Science 1P Lecture 21: Friday 20 th April Simon Gay Department of Computing Science University of Glasgow 2006/07.
Handling Lists F. Duveau 16/12/11 Chapter 9.2. Objectives of the session: Tools: Everything will be done with the Python interpreter in the Terminal Learning.
Instructor: Chris Trenkov Hands-on Course Python for Absolute Beginners (Spring 2015) Class #002 (January 17, 2015)
FUNCTIONS. Function call: >>> type(32) The name of the function is type. The expression in parentheses is called the argument of the function. Built-in.
Computing Science 1P Lecture 13: Friday 26 th January Simon Gay Department of Computing Science University of Glasgow 2006/07.
CS190/295 Programming in Python for Life Sciences: Lecture 3 Instructor: Xiaohui Xie University of California, Irvine.
Computing Science 1P Lecture 18: Friday 2 nd March Simon Gay Department of Computing Science University of Glasgow 2006/07.
Strings The Basics. Strings can refer to a string variable as one variable or as many different components (characters) string values are delimited by.
Collecting Things Together - Lists 1. We’ve seen that Python can store things in memory and retrieve, using names. Sometime we want to store a bunch of.
Variables and Expressions CMSC 201 Chang (rev )
Computing Science 1P Lecture 15: Friday 9 th February Simon Gay Department of Computing Science University of Glasgow 2006/07.
Computing Science 1P Lecture 21: Wednesday 18 th April Simon Gay Department of Computing Science University of Glasgow 2006/07.
Computing Science 1P Large Group Tutorial 20 Simon Gay Department of Computing Science University of Glasgow 2006/07.
Lists CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
Functions. Built-in functions You’ve used several functions already >>> len("ATGGTCA")‏ 7 >>> abs(-6)‏ 6 >>> float("3.1415")‏ >>>
Computing Science 1P Lecture 14: Friday 2 nd February Simon Gay Department of Computing Science University of Glasgow 2006/07.
More about Strings. String Formatting  So far we have used comma separators to print messages  This is fine until our messages become quite complex:
1 CSC 221: Introduction to Programming Fall 2011 Lists  lists as sequences  list operations +, *, len, indexing, slicing, for-in, in  example: dice.
1Computer Sciences Department. Book: INTRODUCTION TO THE THEORY OF COMPUTATION, SECOND EDITION, by: MICHAEL SIPSER Reference 3Computer Sciences Department.
Computing Science 1P Large Group Tutorial 13 Simon Gay Department of Computing Science University of Glasgow 2006/07.
Computing Science 1P Large Group Tutorial 14 Simon Gay Department of Computing Science University of Glasgow 2006/07.
Nonvisual Arrays by Chris Brown under Prof. Susan Rodger Duke University June 2012.
Loops and Simple Functions CS303E: Elements of Computers and Programming.
Arrays. The array data structure Array is a collection of elements, that have the same data type Integers (int) Floating point numbers (float, double)
Midterm Review Important control structures Functions Loops Conditionals Important things to review Binary Boolean operators (and, or, not) Libraries (import.
More Sequences. Review: String Sequences  Strings are sequences of characters so we can: Use an index to refer to an individual character: Use slices.
3 Basics © 2010 David A Watt, University of Glasgow Accelerated Programming 2 Part I: Python Programming.
Python Programing: An Introduction to Computer Science
CSC 4630 Meeting 17 March 21, Exam/Quiz Schedule Due to ice, travel, research and other commitments that we all have: –Quiz 2, scheduled for Monday.
Functions CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
Loops and Simple Functions COSC Review: While Loops Typically used when the number of times the loop will execute is indefinite Typically used when.
Announcements No Labs / Recitation this week On Friday we will talk about Project 3 Release late afternoon / evening tomorrow Cryptography.
CSC 4630 Perl 3 adapted from R. E. Beck. Problem But we worked on it first: Input: Read from a text file named in a command line argument Output: List.
CompSci Today’s Topics Computer Science Noncomputability Upcoming Special Topic: Enabled by Computer -- Decoding the Human Genome Reading Great.
String and Lists Dr. José M. Reyes Álamo. 2 Outline What is a string String operations Traversing strings String slices What is a list Traversing a list.
ENGINEERING 1D04 Tutorial 2. What we’re doing today More on Strings String input Strings as lists String indexing Slice Concatenation and Repetition len()
String and Lists Dr. José M. Reyes Álamo.
CS 115 Lecture 8 Structured Programming; for loops
Dictionaries, File operations
JavaScript Functions.
Engineering Innovation Center
Python I/O.
Using files Taken from notes by Dr. Neil Moore
The backslash is used to escape characters that are used in Python
ARRAYS 1 GCSE COMPUTER SCIENCE.
CEV208 Computer Programming
String and Lists Dr. José M. Reyes Álamo.
Remembering lists of values lists
Class Examples.
Rocky K. C. Chang September 18, 2018 (Based on Zelle and Dierbach)
CS190/295 Programming in Python for Life Sciences: Lecture 3
functions: argument, return value
Introduction to Computer Science
Loops and Simple Functions
String Encodings and Penny Math
Presentation transcript:

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

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.

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

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.

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.

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.

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

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: ”)

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: ”)

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: ”)

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

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

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.

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.

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.

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]

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]

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

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

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

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

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.

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”:

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”:

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...

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

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

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.

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

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

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