October 3rd, 2021
Written by Juliette Woodrow and Anna Mistele
If you'd like to try your solutions in PyCharm, download a starter project to import here. We've included some doctests for you to test with, but you should also add more.
In class this week, we discussed doctests: ways of specifying the expected output of a function on certain parameters, which provide a convenient way of testing our functions as we write them. Doctests are an incredibly effective tool for programmers, but require careful consideration as we write them. As you go through the quarter, you'll develop a stronger sense of how to test your code, but it's never too early to start thinking about it. While you go through your section problems, consider adding doctests to ensure your code handles all inputs properly.
Syntax for Doctests:
As shown above, the syntax is three greater than signs followed by a space and then the name of the function and the specific parameters you want to test. On the next line you put the output you expect from those parameters. To run a doctest, right click on the greater than signs and hit "Run 'Doctest name_of_function'".
>>> name_of_function(param1, param2) 'output of function goes here'
Juliette loves to type text messages with lots of punctuation. She's very enthusiastic, so she'll often repeat the exclamation a bunch... Here's one of her recent text conversations:
Juliette: "Did you see Amanda Gorman at the inauguration?!?!?!?!" "Yes, her poem was great. Did you like it?" Juliette: "It was wicked cool!!!" "Agreed! And she is only 22. What will you have accomplished by the age of 22?" Juliette: "......."
Juliette is getting tired of repeating the punctuation so much. Help her out by writing a Python function that does it for her! The function should accept a message, msg, the ending punctuation, end, and the number of times to repeat the punctuation, n. The function takes in a string msg, an int n, and a string end. The function definition looks as follows: def exclaim(msg, end, n) The function should print out the resulting message to the terminal, so Juliette can copy it and paste it into her text messages. For example, calling exclaim('Did you see Amanda Gorman at the inauguration', '?!', 6) would print Did you see Amanda Gorman at the inauguration?!?!?!?!?!?! and calling exclaim('106A is awesome', '!', 5) would print 106A is awesome!!!!!.
Write a function, def wordguess_helper(guessed_word, secret_word, guess): that helps us play wordguess. The function takes in 3 paramters:
As an example, if the word the user is trying to guess is 'Python' and they have not guessed any
letters so far,
guessed_word = '------' and secret_word = 'Python'. If the user guesses 'o': then wordguess_helper('------', 'Python', 'o') would return '----o-'.
Below is some art that walks through the above example:
Suppose there is a grid where every square is either a bubble 'b', rock 'r', or empty None. Implement a function, can_bubble_up(grid, x, y), that takes in a grid and an x y location that is guaranteed to be in bounds and checks if given grid location can be bubbled up. Bubbled up means we move the bubble at that square to the one above it. A gird location can be bubbled up if: the grid at that location is a bubble 'b', and the location directly above it is in bounds and empty.
Here is some nice art to help you visualize the problem.
As a first step to the problem, walk through each cell of the grid and write down by hand (not using code) if it can be bubbled and why. This should help you determine the cases you will need to check in the code. Once you have completed this step, you can move on to coding the solution.
Here is an example run of can_bubble_up(grid, x, y) in Doctest format:
>>> grid = Grid.build([[None, 'r', 'b'], ['b', 'b', 'b']]) >>> can_bubble_up(grid, 0, 1) True >>> can_bubble_up(grid, 0, 0) False >>> can_bubble_up(grid, 2, 1) False
Now implement a function, has_bubble_up(grid, y), which takes in a grid object named grid and an int y representing a row in the grid. The function should return True if at least one square in row y can be bubbled up and False otherwise. Hint: use the nice function you just wrote as a helper.
These series of problems are for people who want to increase their familiarity with parameters and return statements. Implement the following functions and include at least one doc test per function: