Back to CS 106A Homepage
Written by Brahm Capoor, Juliette Woodrow and Parth Sarin
January 27, 2020
s = 'PythonTime'
How would you slice into this string to obtain the following results?
'ython''Py''Tim''Time''T''PythonTime'
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').
Implement the following functions:
defront(s): If the string parameter has more than 1
character, return it without its first two characters. Otherwise,
return the original string. What would we expect this function to
return if the string is exactly 2 characters long?
x_end(s): If the string parameter contains the
'x' character, return the string,
from the first 'x' to the end of the string.
Otherwise, return an empty string. For example, calling
x_end('excited') would return the string
'xcited'.
pre_colon(s): If the string parameter contains a
colon (':'), return the substring leading to but not
including the first ocurrence of this character. For example,
calling pre_colon('hello:world') would return the
string 'hello'.
As an extension, write a version of this function
pre_colon_ext(s, before) that takes in a boolean
parameter before. If this parameter is
True, return the substring up to the first
colon. If this parameter is False, return the
substring after the first colon.
pre_colon_ext('hello:world', True) would return
'hello'.
pre_colon_ext('hello:world', False) would return
'world'.
at_word(s): If the string parameter contains 2 or
more '@' characters, return the substring between the
first two such characters. Otherwise, return the empty string. For
example, calling at_word('xx@hello@xx') returns the
string 'hello'.
You might find the 2-parameter version of the
s.find(target, start) function useful. This function
returns the index in s of the first instance of the
string target, searching the range
s[start: len(s)]
Test your solutions here .
Implement the following functions:
only_one_first_char(s): removes all occurrences of the
first character of s except the first character itself.
For example, only_one_first_char('recurrence') returns
'recuence'. You may assume s has at least
one character.
make_gerund(s): which adds 'ing' to the
end of the given string s and returns this new word.
If s already ends with 'ing', add an
'ly' to the end of s instead. You may
assume that s is at least 3 characters long.
put_in_middle(outer, inner): which returns a string
where inner has been inserted into the middle of the
string outer. To find the middle of a string,
take the length of the string and divide it by 2 using integer
division. The first half of the string should be
all characters leading up to, but not including, the character at
this index. The second half should start with the character at this
index and include the rest of the characters in the string.
If you'd like to test your solutions, download the PyCharm project, which includes doctests, here from the digital version of this handout.
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.
We say that a word is a palindrome if it is the same read forwards and backwards.
For example, CIVIC, DEED, RACECAR, and ROTATOR are palindromes.
Write a function is_palindrome(word) that returns True if a word is a
palindrome and False otherwise.
We say that a word is a tridrome if the first three letters of the word are the same as the
last three letters of the word (and appear in the same order). All tridromes must be at least
6 letters long. For example, ENTERTAINMENT, MURMUR, and UNDERGROUND are tridromes. Write a function
is_tridrome(word) that returns True if a word is a tridrome and
False otherwise.
We say that a word is peaceful if its letters are in alphabetical order. For example,
ABORT, 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'.
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.
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:
count_palindromes(filename) which returns the number of English words that are palindromes.
count_tridromes(filename) which returns the number of English words that are tridromes.
count_peaceful(filename) which returns the number of English words that are peaceful.
count_stacatto(filename) which returns the number of English words that are stacatto words.
A few things to note:
\n. You can remove this character from a string
using the strip function, i.e. s.strip().
You can actually do this on your computer! We've provided a file of all the words in the English language
called words.txt in the PyCharm project, which you can download here
from the digital version of this handout.
(with our apologies to Stanford R&DE)
Brahm is making an app that delivers food from CoHo to students so that they don't have to leave the comfort of their dorm room. He is hiring Stanford students to pick up the food and deliver it. When a student orders food, they get matched with a delivery person, and communicate with them using the app's built in chat platform. Brahm pays the delivery people per order, but keeps some of the profit for himself.
Early on, he finds out that some of the delivery people are sending their phone numbers or email addresses to students through his app chat platform and offering a lower price in order to keep the entire profit. He needs to find a way to detect when delivery people are trying to send their phone numbers or email addresses. Unfortunately, he's too busy to write this program right now, so he wants you to help implement a function that can alert him when someone is trying to circumvent the system.
Specifically, your job is to implement the following function:
def is_message_valid(filename)
which takes as a parameter a filename string corresponding to a
text file containing a single message sent by a delivery person. If the message
contains a phone number or email address, it is considered invalid and the
function should return False. If it contains neither a phone number or email
address, the message is valid and you should return True. For the sake of this
problem, you can assume that any sequence of 10 consecutive digits is a phone
number and an email address is any sequence of characters including the '@'
character and at least one character before and after it. You can make the
following assumptions:
This is a lab-style problem. To get started, download the starter PyCharm project here. You will first spend some time discussing various approaches to decomposing a solution to this problem, and will then implement and test it on your laptops. On the digital version of this handout, we've linked a PyCharm project you can download with some starter code for this problem. In section, you will spend some time discussing approaches to these problems, and then will write and test your code in PyCharm.