Project 3 — Strings
Complete the following problems.
Setup
To begin, use PyCharm to create a project called project3
. You should do all
your work there.
Next, download project3.zip. This is a zip file, so you
will need to extract its contents. You should put the files inside of your
project3
folder.
Most of the problems will have you use the string_functions.py
file.
For all functions, be sure to write documentation and doctests. Be sure to write tests that cover a variety of situations. We will be automatically grading your code with a complete set of tests, so the better you test your code the better your score!
Non-Alpha
Write a function in string_functions.py
called has_non_alpha(s)
which
returns true if the string contains non-alphabetic characters. For example:
>>> has_non_alpha('abcd')
False
>>> has_non_alpha('as90')
True
Censor
Write a function in string_functions.py
called censor(text)
. This takes one
parameter:
text
: a string
If text
contains ‘SUPER SECRET’, then the whole string becomes asterisks, but
if not, all UPPERCASE characters become asterisks. For example:
>>> censor('This is a SUPER SECRET message')
'******************************'
>>> censor('That is SUPER crazy')
'*hat is ***** crazy'
Reverse
Write a function in string_functions.py
called reverse_case(s)
. This
function takes one parameter:
s
: a string
It returns a new string, where all lowercase characters in s
become UPPERCASE
and all UPPERCASE characters become lowercase, with all other characters staying
the same. For example:
>>> reverse_case('abC!')
'ABc!'
Remove prompt
Write a function in string_functions.py
called remove_prompt(s)
. This
function takes one parameter:
s
: a string
It returns a new string, which contains all characters after the first occurence
of :
. If there is no prompt, it returns the text unmodified. For example:
>>> remove_prompt('anna: stuff')
' stuff'
>>> remove_prompt('hello')
'hello'
>>> remove_prompt('name:')
''
Important
Write a function in string_functions.py
called is_important(s)
. This
function takes one parameter:
s
: a string
If the string starts with ‘important’, return the string with ‘important’ replaced with ‘THIS IS IMPORTANT’, otherwise do nothing (note: the string may contain ‘important’ in other parts, and these should be ignored. For example:
>>> is_important('important: meeting tomorrow')
'THIS IS IMPORTANT: meeting tomorrow'
>>> is_important('there is an important meeting tomorrow')
'there is an important meeting tomorrow'
Crazy
Write a function in string_functions.py
called crazy(s)
. This function takes
one parameter:
s
: a string
It returns a new string where every alphabetic character is doubled, every digit is tripled, every space is replaced with _, and everything else is left unchanged. For example:
>>> crazy("It's 90 degrees outside!")
"IItt'ss_999000_ddeeggrreeeess_oouuttssiiddee!"
Vowel count
Write a function in string_functions.py
called vowel_count(s)
. This function
takes one parameter:
s
: a string
The function returns a count of the number of vowels in the string. For example:
>>> vowel_count('What a beautiful day!')
8
Many times
Write a function called many_times(s, maximum)
. This function takes two
parameters:
s
: a stringmaximum
: an integer
The function returns a new string that has s
repeated until the maximum length
of maximum
characters is exceeded. For example:
>>> many_times('hurray',10)
'hurrayhurray'
>>> many_times('yes',20)
'yesyesyesyesyesyesyes'
Remember that you can use ”+” to concatenate strings, so 'hello'+'hello'
is
'hellohello'
.
Collect
Write a function in string_functions.py
called collect(s, stop)
. This
function takes two parameters:
s
: a stringstop
: another string
The function returns all characters in s
before the first time the string in
stop
occurs. If stop
is the empty string or if stop
does not occur, all
characters in s
are returned.
For example:
>>> collect('what a lovely day it is', 'lovely')
'what a '
>>> collect('tomorrow is the first day of the rest of your life','rest')
'tomorrow is the first day of the '
Replace
Write a function in string_functions.py
called replace(s, phrase1, phrase2)
.
This function takes three parameters:
s
: a stringphrase1
: a string to replacephrase2
: a string to put in place of phrase2
The function returns a new string that removes the first occurrence of phrase
in s
and replaces it with phrase2
.
For example:
>>> replace('tomorrow is the first day of the rest of your life','rest','best')
'tomorrow is the first day of the best of your life'
>>> replace('tomorrow is the first day of the rest of your life', 'of the rest of your life', 'of realizing your greatest potential')
'tomorrow is the first day of realizing your greatest potential'
Wordle
If you have not played Wordle before,
you should play it enough so that you understand how it works. In Chrome you can
use File->New Incognito Window
to replay the current game so you can see how
it works. Note that you are always guessing a five letter word, and the game
tells you if a letter is correct and in the right spot (green), correct but in
the wrong spot (yellow) or incorrect (gray).
For this problem, you will be working in wordle_helper.py
. You will see three
functions there that you need to test and write. We have provided the
documentation for you, but be sure to create doctests!
Check for a valid guess
The first function is valid_guess(word)
. This function takes one parameter:
word
: a string
The function checks whether a guess is valid — the length of the word must be exactly 5 characters, and all characters must be alphabetic. It returns True only if the word is a valid guess, False otherwise.
Filter letters
The second function is filter_letters(word, alphabet)
. This function helps us
to keep track of which letters the player has not guessed yet. It takes two
parameters:
word
: a word the user guessesalphabet
: a list of letters the user has not guessed yet
The function returns a new string, which contains all the letters in alphabet
that are NOT in `word.
Score the guess
The third function is score_guess(guess, truth, wrong, close, correct)
. This
function scores a player’s guess. It takes five parameters:
guess
: the word the player guessestruth
: the word being guessedwrong
: the character used to indicate a wrong guessclose
: the character used to indicate a close guesscorrect
: the character used to indicate a correct guess
The function should return a new string that is a sequence of wrong
, close
,
and correct
characters that indicate the score for the player’s guess. For
example:
>>> score_guess("abcde", "abcde", '-', '*', '#')
'#####'
>>> score_guess("abcde", "annex", '-', '*', '#')
'#---*'
Here, we are using -
for wrong, *
for close and #
for correct. They really
could be any characters. In the first example, the user guesses the word
correctly, so the returned string is #####
. In the second example, the user
has two letters, one close and one correct. The a
is correct, but the e
is
in the wrong position. All the other letters are wrong.
You know you can loop over the characters in a string. Did you know you can loop
over two strings simultaneously? If strings first
and second
have the same
length, then:
for i in range(len(first)):
character_in_first = first[i]
character_in_second = second[i]
Playing wordle
Once you have these functions tested, you can use:
python wordle.py
and play the game. Our version uses a red square emoji for the wrong letters, a yellow square for the close letters (wrong position), and a green square for correct letters. Enjoy!
Submit
You need to submit:
string_functions.py
wordle_helper.py
Points
This project is worth 40 points.
Task | Description | Points |
---|---|---|
Non-alpha | Your solution works | 2 |
Censor | Your solution works | 2 |
Reverse | Your solution works | 2 |
Remove prompt | Your solution works | 2 |
Important | Your solution works | 2 |
Crazy | Your solution works | 2 |
Vowel count | Your solution works | 2 |
Many times | Your solution works | 3 |
Collect | Your solution works | 3 |
Replace | Your solution works | 3 |
Wordle valid guess | Your solution works | 2 |
Wordle filter letters | Your solution works | 3 |
Wordle score guess | Your solution works | 5 |
Docstrings | You have well-written docstrings | 2 |
Doctests | You have doctests that cover a good range of test cases | 5 |
The auto grader on GradeScope will grade whether your solution works. The TAs will grade your docstrings and decomposition as follows:
- Docstrings
- 2 points: You have docstrings that clearly explain waht the function does, what each parameter does and the return value
- 1 points: Your docstrings need some improvement
- 0 points: No docstrings or only a few docstrings
- Doctests
- 5 points: Your doctests cover a good range of test cases
- 3 points: You have doctests but need to test more cases
- 0 points: You do not have doctests
The TAs may award points between 0 and 3 and between 3 and 5 if you are in betweeen these scores.