Section #5: String Parsing, Lists, & Drawing

July 19th, 2021


Written by Juliette Woodrow, Brahm Capoor, Nick Parlante, Anna Mistele, and John Dalloul

This week in section you will gain practice with string parsing, building lists, and drawing. These problems are meant to prepare you for both homework 5 and quiz #2. Solutions are linked. There are more problems on this than we expect you to get through in section. Feel free to use the other ones as practice! If you have any questions, post on Ed or email Tara!

Tracing Problem

This is a tracing problem. Rather than copying it into Pycharm and running it to see what it prints out, we are going to trace through it by hand to see how values are passed between functions. This may seem like a silly execrcise but tracing through code is a good skill to have, especially when you take the quizzes.

        
def main():
    x = 4
    y = 8
    z = x + y
    y = b(x, z)
    print("Main variables: ", x, y, z)
​
def b(y, x):
    z = x + y
    print("b variables: ", x, y, z)
    if z % 2 == 0: # if (x+y) is even
        return z - (x/y)
    return z // 2
    

        
      

String Parsing and Lists


Parse Time

Implement a function, find_time(str) that takes in a string and, if it contains one, returns a time written in the string. Times will be of the format "XX:XX" and you don't need to include any AM or PM designations. For example, given the input string "Let's go to the movies at 09:30 tomorrow" your function should return "09:30". If there are no times in the string, return the empty string.

Parse out Hashtag

Implement a function, parse_out_hashtag(s), which takes in a string representing a single tweet and returns the hashtag within the tweet. For this problem, each tweet will have only one hashtag. A Hashtag can be defined as a string of 1 or more alphanumeric characters immadiately following a "#" character. A single hashtag ends at the first non-alphanumeric character following the '#'. For example, parse_out_hashtag('I am going to #wearmymask everywhere') would return 'wearmymask' and parse_out_hashtag('what is #ResX?') would return 'ResX'.

Here are some drawings for looping over the word following the '#' character (also known as an octothorpe).

Before:

After:

Find the Price

Implement a function, find_price(s, currency) that takes in a string and a currency symbol and returns an integer price if that is mentioned in the string s. We want to look through a line and find the place where a price is mentioned. You can assume that the currency symbol specified will only show up once in the line provided. To find the price, locate the currency symbol and then find all the digits AFTER the symbol (not all currencies have the symbol come first--think about how we could adjust our code if the symbol came last, instead). Stop reading once the last digit has been read. Return the price as an integer (without the currency symbol).

For example, find_price("Une cramique au chocolat coûte €3", "€") would return 3.

Exclaim Word

Implement a function, exclaim_word(s), which takes in a string and returns the exclamatory word in that string. Consider an exclamatory word the "word" substring of one or more alphabetic chars which are immediately to the left of the '!' in s. For example: exclaim_word('x123hello!cs106a') would return 'hello' and exclaim_word(32happy!day') would return 'happy'. If there are no exclamatory words, return the empty string.

Parse Info From Emails

  1. Implement a function, parse_username(s),which takes in a string representing a single email address, and returns the username. The username is all alphabetic characters, digits, and '.' characters that come before the '@' character in the email address. For example: parse_username('maya.angelou1@gmail.com') would return 'maya.angelou1'
  2. Implement a function, parse_hostname(s),which takes in a string representing a single email address, and returns the host name for that email address. The host name for a given email is all alphabetic characters, digits, and '.' characters that come after the '@' character in the email address. For example: parse_username('nick@cs.stanford.edu') would return 'cs.stanford.edu'

Parse Phone Number

Wait until after Friday lecture to try this one.

Implement a function, parse_phone_number(s), which takes in a string that has a ten digit phone number somewhere in it and returns a list of the number in two parts: the area code and the rest of the number. The area code is the part of the phone number up until the first '-' character. The string can have text before or after the phone number. You may assume the following: that the only digits in the string will be part of the phone number. If there are any digits in the string, they will make up a complete phone number. If there is no phone number in the string, return an empty list. There may be '-' outside of the phone number. For example, parse_phone_number('Zoom is too much-call me instead: 212-225-9876') would return ['212', '2259876'], parse_phone_number('so call-me beep-me if you wanna reach me-at 650-555-5555 ') would return ['650', '5555555'], and parse_phone_number('The weekend is not long enough') would return [].


Drawing

Stanford Flag

You have been hired by Stanford to create a digital version of their new flag that they are desiging to welcome all students back to campus in the fall. They have given you the following description of the flag:

They want you to draw three of these flags on the canvas, one on the bottom half of the left third, one on the top half of the middle third, and one on the bottom half of the right third. In the parts of the canvas without the Stanford flag (top half of left third, bottom half of middle third, and top half of the right third), you will draw a different patch called a spider patch. In a spider patch, draw a series of lines distributed across the top edge of the figure, all terminating at the lower-right corner. The first line should start at the upper left corner, the last line should start at the upper right corner, with the other lines distributed evenly in between. Draw a green rectangle at the outer edge of each spider patch.

To accomplish this task, decompose into a function called draw_stanford_flag(canvas, left, top, width, height, num_stripes) where left should be the starting x value of the top left corner of this flag on the canvas and top should be the starting y value of the top left corner of this flag on the canvas.

You should also decompose into a function called draw_spider_patch(canvas, left, top, width, height, n) where left should be the starting x value of the top left corner of this spider patch on the canvas and top should be the starting y value of the top left corner of this spider patch on the canvas. n is the number of lines to draw and is guaranteed to be 2 or more.

Once you have that working, write a function called draw_flag(canvas, width, height, num_stripes, n) which draws the three flags at their locations specified above. Below is an example of what one call to draw_flag(canvas, 600, 600, 9, 4) should look like. You can ignore the black outline on parts of the image.