Section #4: String Parsing, Lists, Drawing, & Main

February 7th, 2021


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

This week in section you will gain practice with string parsing, building lists, drawing, and the main function. These problems are meant to prepare you for both homework 5 and quiz #2. Solutions will be posted at the end of the week. 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 Juliette!

String Parsing and Lists


Exclaim Words

Implement a function, exlaim_words(s), which takes in a string and returns a list of the exclamatory words 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_words('x123hello!32happy!day') would return ['hello', 'happy'].

Parse Madlib Categories

Implement a function, parse_unique_categories(line), which takes in a line from a Pylib, return a list of all unique categories listed in that line (omitting the delimiting brackets). For example, inputting the string "I cannot wait to [verb] when I get to [place]!" should return ["verb", "place"]. Each category will begin and end with a [ and ], respectively, and there can be multiple categories in the string; all close brackets ] will be preceeded by an open bracket [. If there are no categories in the string, an empty list should be returned.

Parse Names from Emails

Implement a function, parse_words(s), which takes in a string representing an email in the format firstname.lastname@domainname.com, and returns a list of the email broken into three parts: the first name, the last name, and the domain name. For example, parse_words('theodor.geisel@seuss.com') would return ['theodor', 'geisel', 'seuss'] and parse_words('maya.angelou@gmail.com') would return ['maya', 'angelou', 'gmail'].

Parse out Hashtags

Implement a function, parse_out_hashtags(s), which takes in a string representing a single tweet and returns a list of all hashtags within the tweet. 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 '#'. Hashtags are not guaranteed to be separated by spaces. For example, parse_out_hashtags('I am going to #wear #my #mask') would return ['wear', 'my', 'mask'] and parse_out_hashtags("I miss Stanford #backtocampus #ResX?") would return ['backtocampus', 'ResX'].

Parse Phone Number

Implement a function, parse_phone_number(s), which takes in a string representing a ten digit phone number separated into parts by dashes 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. For example, parse_phone_number('212-225-9876') would return ['212', '2259876'].


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.

To accomplish this task, decompose into a function called draw_stanford_flag(canvas, left_x, top_y, width, height) where left_x should be the starting x value of the top left corner of this flag on the canvas and top_y should be the starting y value of the top left corner of this flag on the canvas. Once you have that working, write a function called draw_flag(canvas, width, height) which draws the three flags at their locations specified above. Below is an example of what one call to draw_flag(canvas, 450, 600) should look like. You can ignore the black outline on parts of the image.


The main function

Your job is to write a program that emulates the 3 calculator functions shown below:

          
$ python3 calculator.py -square 42
1764 # prints the square of the number passed in

$ python3 calculator.py -exp 2 10
1024 # prints the first number raised to the power of the second number

$ python3 calculator.py -add 1 2 3 4 5
15   # prints the sum of all the numbers typed in
          
        
You may assume that you are provided with a main function that takes as a parameter the list of arguments typed in the console, as below:
          
import sys

def main(args):
    # your code here
    pass

if __name__ == "__main__":
    main(sys.argv)
          
        
Thus, your job is to decompose and implement the main function so that your program produces the sample output above.

Notes