Section #2: Functions, Strings, and Grids

April 11th, 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.

Questions About Doctests

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

Bubble Up

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
          


Playing with Strings

Exclaim

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!!!!!.

Word Guess

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:

  • guessed_word: what we've guessed so far in the word; unguessed letters represented by "-"
  • secret_word: the word we are trying to guess
  • guess: the letter we are guessing this round
The function should return a new_obscured_word that represents our guessed word updated by the most recent guess. Make sure to build up a new string letter by letter!

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:


Opening the Black Box

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:

  • def in_range(n, low, high), which takes in three integer parameters and returns True if n is between low and high (inclusive of both ends of the range) and False otherwise.
  • def is_even(n), which takes in an integer parameter n and returns True if n is even and False otherwise. You may wish to use the % operator, which returns the remainder when dividing one number by the other. For instance, 7 % 3 evaluates to 1, since the remainder when dividing 7 by 3 is 1.
    • Note: to solve this problem you will need to use the % operator, known as the mod operator. Check out the modulus section of the Python reader. Mod will not be on the first quiz.
  • def only_one_even(num1, num2), which takes in two integer parameters and returns True if and only if exactly one of them is even.
  • def longer_str(str1, str2), which takes in two strings and returns whichever string is longer. If they are the same, it can return either string.
  • def is_phone_number(str1), which takes in a string and returns True if a string is a phone number, and False otherwise. We say a string is a phone number if it is made of only digits, and has 9 or 10 characters in the string.