Lab 13 — Strings, Part 3
In this lab you will gain some additional practice with strings. Start by
downloading lab13.zip. Write all your code in
strings_part3.py
.
Emoji Replace
Write a function called emoji_replace(sentence)
. This function takes one
parameter:
sentence
: a string
The function returns a new string that replaces each of the following strings with its corresponding emoji:
'fire'
with'\U0001F525'
'love'
with'\u2764'
'birthday'
with'\U0001F382'
'plane'
with'\U00002708'
Note that this should not operate on words but strings, so planet
will still
have plane
replaced with its corresponding emoji.
You should use the str.replace()
function discussed in lecture.
Emoji Word Replace
Write a function called emoji_word_replace(sentence)
. It does the same thing
as above, but only replaces complete words. So planet
will remain planet
and
only plane
will be replaced with its corresponding emoji.
You should use str.split()
to convert the sentence into words. Now you will
have a list of words you can cycle through, and you can replace items in the
list directly, since lists are mutable. For example:
for index in range(len(words)):
if words[index] == 'hello':
words[index] = 'goodbye'
Once you have the list of new words setup, you can turn them back into a string
using str.join()
.
To keep things simple, we will not worry about punctuation for this function.
Stock Trades
This is a repeat of a problem from Lab 9, except you will use
str.split()
to make your job easier. You are given a file with trades of
GameStop shares, using the following format:
tradeID,stockSymbol,tradeType,numberOfShares,entity
For example, the file might contain:
4965,GME,SELL,8,KAREL_CO
2725,GME,SELL,13,KAREL_CO
9543,GME,SELL,4,J_DOE
8390,GME,BUY,3,KAREL_CO
9114,GME,SELL,5,NEMO
Total Trades
Write a function called total_trades(filename)
. It takes one parameter:
filename
: the name of a file containing trades for the NYSE stock Gamestop (GME)
The function determines the total number of shares bought and sold. For example:
>>> total_trades('gamestop_trades.txt')
3 shares bought
30 shares sold
You can use str.split()
to split each line as you loop through the lines in
the file. Remember, you may want to use strip()
to remove the trailing newline
from the end of a line. You can do this with:
line = line.strip()
Reminder: Remember you will need to convert string values to integer values when you want to sum integers.
Market Manipulators
Often, when a single entity or group of entities decide to sell a large amount of stock at once, the price of that stock falls. With the rise in automated trading, concerns arise over large groups taking advantage of this trend and dumping stock to deliberately tank a stock price.
Write a function called find_percent_seller(filename, entity_name)
. It takes
two parameters:
filename
: the name of a file containing trades for the NYSE stock Gamestop (GME)entity_name
: the name of an entity in this data set
The function calculates the percentage of stock sold by the named entity. For example:
>>> find_percent_seller('gamestop_trades.txt', 'KAREL_CO')
KAREL_CO sold 70.0 percent of GME stock sold
The percentages for the doctests are rounded to two decimal places. You can do
this with the round()
function:
percent = round(percent, 2)
Uppercase Words
Write a function called uppercase_words(sentence, letter)
. This function takes
two parameters:
sentence
: a stringletter
: a single character
The function finds every word that starts with the given letter
and changes
that word to uppercase. The rest of the words are unchanged. The function
returns a new string that is the same as the original string but with some words
in uppercase.
You will want to use the same pattern for this function — use str.split()
to
get the words, loop through the list to change words, and then join them back
together again.
Uppercase Many Words
Write a function called capitalize_many_words(sentence, letters)
. This
function works the same as above, except the letters
parameter is a string.
The function returns a new string that is the same as the original string, but
any word that starts with any of the letters in letters
is in uppercase.
Total Grade
Write a function called total_grade(filename, last_name, first_name)
. This
function takes three parameters:
filename
: the name of a filelast_name
: the last name of a studentfirst_name
: the first name of a student
This function reads the file given by filename
, finds all of the records for
the student with the given last_name
and first_name
, and returns the total
points earned by that student across all assignments. The format of the file is:
last,first,assignment,total,score
Lessons
What we want you to get from this lab:
-
You can use the new string functions we have covered
-
You can parse the lines in a file, one line at a time, using
split()
-
You can convert between string and integer types as needed
-
You can use
split()
andjoin()
to parse strings and create new strings -
You can figure out what went wrong when something unexpected happens
-
Hopefully you had fun!
Points
Turn in a zip file that has your code.
Task | Description | Points |
---|---|---|
Emoji Replace | Your solution works | 1 |
Emoji Word Replace | Your solution works | 1 |
Total Trades | Your solution works | 2 |
Find Percent Seller | Your solution works | 2 |
Uppercase Words | Your solution works | 2 |
Uppercase Many Words | Your solution works | 2 |