January 24th, 2021
Written by Juliette Woodrow, Brahm Capoor, and Parth Sarin
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 to 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'".
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:
Write the following function: def remove_all_occurrences(s, to_remove) which removes all occurences of the length-one string to_remove from the string s. Do not use the replace function, instead build up a new string. Here are some sample inputs and outputs for this function:
>>> remove_all_occurrences('This is a test', 't')
'This is a es'
>>> remove_all_occurrence('----O----', '-')
'O'
Write the following function: def is_palindrome(s) which returns True if s is a palindrome and False otherwise.
We say that a word is a palindrome if it reads the same forwards as backwards. For example, "Abba" is a palindrome because it is the same word read forwards and backwards. Here are some more examples:
>>> is_palindrome('juliette')
False
>>> is_palindrome('maps spam')
True
>>> is_palindrome('Maps spam')
False # uppercase 'M' and lowercase 'm' are not the same character
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 excalim(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 is straight from an old CS106A midterm :) 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. Write code for a bubble_up(grid, y) function that tries to move every bubble in a row up one square as follows: given a grid and an in-bounds y value. Look at every square with that y. If the square contains a bubble 'b', and the square above it is empty, move the bubble up one square, changing its original square to be empty.
Here is an example run of bubble_up(grid, y) for the row y=1 in Doctest format:
>>> grid = Grid.build([[None, 'r', 'b'], ['b', 'b', 'b']])
>>> bubble_up(grid, 1)
[['b', 'r', 'b'], [None, 'b', 'b']]