Section #3: Strings & File Reading

January 31st, 2021


Written by Juliette Woodrow, Anna Mistele, John Dalloul, and Parth Sarin

String Slicing

          
                s = 'PythonTime'
    [0123456789]
          
        

How would you slice the string to receive the following results?

Remember, strings in Python are 0-indexed. In addition, the slice s[1:8] is inclusive of the first index, and exculsive of the second (that is, it will get the string beginning at index 1 and up to, but not including, index 8, i.e. 'ythonTi').

String Searching

Implement the following functions:

String Construction

Implement the following functions:

Word Puzzles

In these problems, we'll investigate properties of words in the English language. In each problem, we'll define a special rule and write a function to determine whether a word obeys that rule or violates that rule. For this problem, you can assume that word will be a string containing uppercase alphabetic characters only.

Peaceful Words

We say that a word is peaceful if its letters are in alphabetical order. For example, ALMOST, CHIPS, DIRTY, FIRST, and HOST are all peaceful words. Write a function is_peaceful(word) that returns True if a word is peaceful and False otherwise. You may assume you have access to a constant ALPHABET which is a string of the uppercase letters in the alphabet, in sequential order, i.e., ALPHABET = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'.

Stacatto Words

We say that a word is a stacatto word if all of the letters in even positions are vowels (i.e., the second, fourth, sixth, etc. letters are vowels). For this problem, the vowels are A, E, I, O, U, and Y. For example, AUTOMATIC, CAFETERIA, HESITATE, LEGITIMATE, and POPULATE are stacatto words. Write a function is_stacatto(word) that returns True if a word is a stacatto word and False otherwise.

File Reading

No More Counting Dollars, We'll Be Counting Words

Suppose you're given a file that contains all the words in the English language, where each one is on a different line. Write the following functions, using the functions you wrote in the previous problem:

GameStop Trades

Write a function, parse_trades(filename) that takes in the name of a file containing trades for the NYSE stock Gamestop (GME) and determines the total number of shares bought and sold. Each line of the file is a trade, consisting of: a trade ID, stock symbol, trade type, and number of shares transacted. The trade type and number of shares for a given trade are delimited by the '|' and '$' characters, respectively. The only two trade types are "BUY" and "SELL." It may be helpful to write a helper function that given a line of the file and delimeter, gives back the value between two of that delimeter in the line.

Consider the file: gamestop_trades.txt

        
4965 GME |BUY| $8$
2725 GME |BUY| $3$
9543 GME |BUY| $11$
8390 GME |SELL| $7$
9114 GME |BUY| $8$
      
    

If you run parse_trades('gamestop_trades.txt'), it would print:

30 shares bought.
7 shares sold.
    

Py-Libs

Write a function, create_pylib(filename) that reads a PyLib (like a Madlib but with help from you and your Python skills) from the given filename and prints out a completed story! For each line in the file replace any bracketed categories (like [noun]) with a word from that category. We've provided you with a get_word(category) function that given a category gives you back a random word of that category. There will be exactly one bracketed category per line. Print out the completed Pylib line by line.

Here is an example file to call your function on: story.txt

When covid ends the first place I am traveling to is [location].
I cannot wait to [verb] when I get there! 
I will make sure to bring [noun].
Until then, I will [adverb] await the trip.