133x Filetype PDF File size 0.08 MB Source: www.danielzingaro.com
CS4HSPythonExercises Daniel Zingaro University of Toronto daniel.zingaro@utoronto.ca July 13-14, 2011 1 Exercise1: Dice-Rolling Here’s an exercise that uses numbers and while-loops. A few things: • Useimport random to import the module that gives you access to random-number generation. import is used any time you want access to a module (like #include in C, I guess; but closer to import in Java) • Use print a, b to print two items on the same line • != is the not-equal operator • The triple-quoted comment at the top of the function is called a docstring. A doc- string is a Python convention that helps people learn to use a function In a certain dice game, a person’s turn involves rolling two dice at a time, until: • one of the dice shows a 6, or • both dice show the same number For example, here is a possible person’s turn. (As above, there are two ways for a person’s turn to end. Make sure you understand the other one, too.) 3 4 4 5 2 1 3 6 Write the following function so that it continues to roll two dice and print their values, until the person’s turn is over. You’ll want to generate a random number between 1 and 6 for each roll; you can use random.randint (1, 6) to do so. Note that each line of output should consist of two integers separated by a space, as in the sample given above. def take_turn (): ’’’Print pairs of dice rolls until the turn is over.’’’ 1 2 Exercise2: Repeating Characters Complete the following function according to its docstring description. def rep_chars (s, num): ’’’Return a string consisting of each character of s repeated num times. For example, rep_chars (’abc’, 2) returns aabbcc’’’ 3 Exercise3: Valid Passwords Complete the following function according to its docstring description. The function is designed to ask for a valid password (presumably for a user setting up a new account of some sort) until one is given, or the user reaches the maximum guesses allowed. You must use a while-loop in your solution. You may assume that max_attempts is at least 1. • Use pw = raw_input ("Enter password: ") to prompt • True and False are the Python boolean literals • The empty string is ’’ • len(pw) gives the length of string pw def get_password (max_attempts): ’’’Prompt the user for a valid password until a valid one is given or they have entered max_attempts invalid passwords. A valid password is one that is at least length 6 and does not start with the string "password". Each time the user gives an invalid password, tell them that it was invalid and prompt again if they have not reached max_attempts tries. If the user gives max_attempts invalid passwords, return the empty string; otherwise return the valid password they gave’’’. 2 4 Exercise4: too Much Tetris? Diane has been known to spend hours and hours on end playing Tetris. However, she also enjoys spending as much time as possible helping students with CSC108 in office hours. But where does Diane spend more time? To find out, Diane’s colleagues have observed her behavior for a certain number of consecutive days and have created two parallel Python lists: a tetris list and an office_hours list. For day i, tetris[i] holds the number of hours spent on Tetris, and office_hours[i] holds the number of hours spent in office hours. Both lists are of equal length and contain only integers. Write the following function according to its docstring. def too_much_tetris (tetris, office_hours): ’’’tetris and office_hours are parallel lists of integers, as described above. Return True if Diane has spent at least one day where the number of tetris hours is more than the number of office hours. Return False otherwise.’’’ 3 5 Exercise5: One-HopFlights Aflightdictionary is a dictionary where: • The keys are strings. Each key is the name of a city. • Thevalues are lists of strings. For a key k, the value is the list of cities reachable on adirect flight from k. Every city in the list is also a key in the dictionary. Here is an example flight dictionary: flights = {’Montreal’: [’Toronto’, ’Tampa Bay’], ’Toronto’: [’Montreal’, ’Tampa Bay’], ’Tampa Bay’: [’Atlanta’, ’Toronto’], ’Atlanta’: [’Tampa Bay’]} Aone-hop between two cities a and b exists when it is possible to get from city a to city b by taking two flights: one from city a to some intermediate city c, and then a second flight from c to the final destination b. For example, in the above dictionary, there is a one-hop from Toronto to Atlanta (through Tampa Bay), and a one-hop from Montreal to Atlanta (again through Tampa Bay), but there is not a one-hop from Atlanta to Montreal. Write the following function according to its docstring. def one_hop (flights, city1, city2): ’’’flights is a flight dictionary. city1 and city2 are keys in flights. Return True if and only if there is at least one one-hop from city1 to city2.’’’ 4
no reviews yet
Please Login to review.