Back to CS 106A Homepage
Written by Brahm Capoor, Andrew Tierno and Juliette Woodrow
October 28th, 2019
Write the following function:
def make_pyramid(image)
that takes in a source image and returns a new image which copies over pixels from the source image in a centered triangular pattern. That is, only the middle pixel should be taken from the first row, the middle three pixels from the second row, etc. Any pixels that aren't copied from the source image should be white in our triangular image. You may assume that the image is at least twice as wide as it is tall and that for images with even widths that the 'middle pixel' of a row is defined as the pixel whose column is the width of the image divided by two and rounded down. Your function should produce images that look like the image below, if passed an all-black image. The border is provided for your own convenience and need not be present in your returned image.
Implement the following functions:
def find_numbers(s): Given a string s,
return a list of strings representing all the numbers in
s. A number is defined as any sequence of consecutive
digits, with at most one period representing a decimal point. For
example, find_numbers('42 abc100.75def50') would
return the list ['42', '100.75', '50'].
def parse_phone_number(s): Given a string s
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'].
def find_letter_runs(s): Given a string
s, return a list of strings representing all the
'character runs' in s. A 'character run' is defined
as a sequence of 2 or more consecutive appearances of the same
letter (disregarding case) in a string. For example,
find_letter_runs('a bcdeef gghhhijjjklmnnnnnooooo')
would return the list
['ee', 'gg', 'hhh', 'jjj', 'nnnnn', 'ooooo'].
def parse_out_hashtags(string s) Given a string
s 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 #crush the midterm! #106_A #python')
would return ['crush', '106', 'python'] and
parse_out_hashtags("I can't wait for thanksgiving break! #too#much#work")
would return ['too', 'much', 'work'].
def find_quotes(s): Given a string s representing
text that may contain some quotes or dialogue represented by a string of
characters between quotation marks and returns a list of strings
where each string in the list is one of the quotes from s. For example:
>>> pooh_quote = '"When you wake up in the morning, Pooh," said Piglet at last, "What\'s the first thing you say to yourself?" "What\'s for breakfast?" said Pooh. "What do you say Piglet?" "I say, I wonder what\'s going to happen exciting today!" Pooh nodded thoughtfully. "It\'s the same thing," he said.'
>>> no_quote = 'happy days happy ways'
>>> find_quotes(pooh_quote)
['When you wake up in the morning, Pooh,', "What's the first thing you say to yourself?", "What's for breakfast?", 'What do you say Piglet?', "I say, I wonder what's going to happen exciting today!", "It's the same thing,"]
>>> find_quotes(no_quote)
[]
Write the following function:
def pig_latin(word)
that converts a single lowercase word to a simplified version of Pig Latin, a (profoundly silly) variant of English The rules for translating a word to Pig Latin are as follows:
pig_latin('elephant') returns
'elephantyay' and
pig_latin('aardvark') returns
'aardvarkyay'.
pig_latin('switch') returns 'itchsway' and
pig_latin('string') returns 'ingstray'.
You may assume that the word has at least one character and is comprised of only lowercase letters.
is_alpha_orderImplement the following function:
def is_in_alpha_order(s):
which takes as a parameter a string of one word and determines if all characters in the given word appear in alphabetical order. The function returns true if all letters are in alphabetical order and false otherwise. If a letter is repeated twice in a row, that is considered in alphabetical order.
As a hint, you can check to see if a letter appears before or after another letter by using
the <, >, <=, or >= operators,
just as you would for integers.
>>> is_in_alpha_order('ghost')
True
>>> is_in_alpha_order('door')
True
>>> is_in_alpha_order('Juliette')
False
Consider the following program:
def dundees(pam, michael, dwight):
jim = 5
for i in range(1, pam, 2):
jim += i
if michael:
battlestar_galactica(pam, jim)
jim = scotts_tots(jim, pam, dwight)
else:
if not dwight:
jim = scotts_tots(pam, jim, not michael)
battlestar_galactica(jim, 42)
return jim // pam
def battlestar_galactica(toby, kevin):
creed = toby
toby = kevin
kevin = creed
def scotts_tots(oscar, angela, jim):
kelly = 2 * oscar
if jim:
kelly -= angela
return kelly
def main():
args = sys.argv[1:]
michael = False
holly = False
if '-scott' in args:
michael = True
if '-flax' in args:
holly = True
mifflin = int(args[0])
print(dundees(mifflin, holly, michael))
if __name__ == '__main__':
main()
For each of the following calls, trace through the program's execution to determine what the program prints:
$ python3 the-office.py 2 -flax
$ python3 the-office.py 5 -scott
$ python3 the-office.py 4
Write a function named collapse that accepts a list of integers as a
parameter and returns a new list containing the result of replacing
each pair of integers with the sum of that pair. For example, if a
list called nums stores the values
[7, 2, 8, 9, 4, 13, 7, 1, 9, 10], then the call of
collapse(nums) should return a new list containing
[9, 17, 17, 8, 19]. The first pair from the original list
is collapsed into 9 (7 + 2), the second pair is collapsed into 17 (8 +
9), and so on. If the list stores an odd number of elements, the final
element is not collapsed. For example, if the list had been
[1, 2, 3, 4, 5], then the call would return
[3, 7, 5]. Your function should not change the list that
is passed as a parameter.
Write the following function:
def rotate_list_right(numbers, n)
that takes in a list of integers called numbers and an
integer called n and returns a 'rotate' version of the
list that rotates numbers to the right
n times. Each element in numbers is shifted
forward n places, and the last n elements
are moved to the start of the list. For example, calling
rotate_list_right([1, 2, 3, 4, 5], 2) would return the
list [4, 5, 1, 2, 3]. Your function should not change the
list that is passed as a parameter.
Your job is to write a full python program, including a
main function, for a file called
special-count.py. This program should implement the
following behavior:
If special-count.py is called with just a filename
(e.g. python3 special-count.py myfile.txt) then it
should read in the file named myfile.txt (you may assume that this
file is formatted as single words separated by newlines) and
produce counts of words that share the same consonants in order.
For example, if we had the following text in
myfile.txt:
great
grate
greet
teeny
tiny
bump
Your program should produce the following output. Note that the output is in sorted order.
$ python3 special-count.py myfile.txt
bmp -> 1
grt -> 3
tny -> 2
special-count.py is called with the additional flag
-vowels (e.g.
python3 special-count.py -vowels myfile.txt) then it
should produce the same output, only grouping words by their vowels
rather than their consonants. So, using the same file as before
produces the following output:
$ python3 special-count.py -vowels myfile.txt
ae -> 1
ea -> 1
ee -> 2
i -> 1
u -> 1
For even more practice, check out the sample midterm and midterm preparation handout, linked on the course website .