jagomart
digital resources
picture1_Python Pdf 185740 | Ex Item Download 2023-02-01 18-28-02


 133x       Filetype PDF       File size 0.08 MB       Source: www.danielzingaro.com


File: Python Pdf 185740 | Ex Item Download 2023-02-01 18-28-02
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 ...

icon picture PDF Filetype PDF | Posted on 01 Feb 2023 | 2 years ago
Partial capture of text on file.
                      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
The words contained in this file might help you see if this file matches what you are looking for:

...Cshspythonexercises daniel zingaro university of toronto utoronto ca july exercise dice rolling here s an that uses numbers and while loops a few things useimport random to import the module gives you access number generation is used any time want like include in c i guess but closer java use print b two items on same line not equal operator triple quoted comment at top function called docstring doc string python convention helps people learn certain game person turn involves until one shows or both show for example possible as above there are ways end make sure understand other too write following so it continues roll their values over ll generate between each can randint do note output should consist integers separated by space sample given def take pairs rolls repeating characters complete according its description rep chars num return consisting character repeated times abc returns aabbcc valid passwords designed ask password presumably user setting up new account some sort reaches...

no reviews yet
Please Login to review.