July 5th, 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:
>>> name_of_function(param1, param2)
'output of function goes here'
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'".
Grids will not be on the first quiz.
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
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 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!!!!!.
This problem might be a bit more difficult than what you would see on the quiz this Friday, but it is good practice. This problem has many things that you have learned so far: parameters, return, if/else, booleans, and strings so it is a good problem to work through in preparation for Friday's quiz.
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:
In this program, you will tackle a series of problems intended to increase your familiarity with parameters and return statements. Implement the following functions and include at least one doc test per function: