CS5001 / CS5003:
Intensive Foundations of Computer Science
I didn't mean for you to write the get_choice()
function -- oops! But, here is the sample output, and it should be straightforward to figure out how to write the function:
def get_choice():
"""
Ask the user to make a selection between Print friend list, Add a friend,
Unfriend someone, and Quit
:return: 'p', 'a', 'u', or 'q'
"""
def get_choice():
"""
Ask the user to make a selection between Print friend list, Add a friend,
Unfriend someone, and Quit
:return: 'p', 'a', 'u', or 'q'
"""
print("Please choose an option below:")
print("\tP: Print a friend list")
print("\tA: Add a friend")
print("\tU: Unfriend Someone")
print("\tQ: Quit")
choice = ''
while choice not in ['p', 'a', 'u', 'q']:
choice = input("Your selection? (P/A/U/Q): ").lower()
return choice
'\t'
means "tab" (you could have put a couple of spaces)def read_friends(filename):
"""
Reads in a Dwarf Facebook list from filename. Returns a dict,
with each dwarf as the keys, and a list of dwarfs as the value,
representing the dwarf's friends.
The file should be formatted as follows:
dwarf_name1 friend1 friend2 friend3 ...
dwarf_name2 friend1 friend2 friend3 ...
...
The names will be space-separated.
A particular Dwarf's dict key/value might look like this:
dwarf_friends = {
'Happy': ['Dopey', 'Bashful', 'Sneezy', 'Sleepy', 'Doc', 'Grumpy'],
}
:param filename: The file name of the file to read from
:return: A dict of dwarf friends
"""
database = {}
try:
with open(filename, "r") as f:
pass
return database
except FileNotFoundError:
print(f"Could not open the database file, '{filename}'")
return None
We need to read in the space separated words, with the first as the key and the rest as the friends.
def read_friends(filename):
database = {}
try:
with open(filename, "r") as f:
for line in f:
line = line[:-1] # remove newline
dwarfs = line.split(' ') # space separated
database[dwarfs[0]] = dwarfs[1:] # add first as key, rest as friends
return database
except FileNotFoundError:
print(f"Could not open the database file, '{filename}'")
return None
.split()
method when you have data that is separated by a particular characterdef print_friends(friend_database, dwarf):
"""
Prints out the friends for a particular dwarf from friend_database. The list
should print as follows, for example:
Happy, your friends are:
Dopey
Bashful
Sneezy
Sleepy
Doc
Grumpy
:param friend_database The database of friends
:param dwarf: The dwarf whose friends will get printed
:return: True if the dwarf is in the database, False otherwise
"""
pass
print_friends()
function:def print_friends(friend_database, dwarf):
"""
Prints out the friends for a particular dwarf from friend_database. The list
should print as follows, for example:
Happy, your friends are:
Dopey
Bashful
Sneezy
Sleepy
Doc
Grumpy
:param friend_database The database of friends
:param dwarf: The dwarf whose friends will get printed
:return: True if the dwarf is in the database, False otherwise
"""
print(f"{dwarf}, your friends are:")
friend_list = friend_database[dwarf] # get friend list from database
for friend in friend_list:
print(friend)
print_friends()
function:def add_friend(friend_database, dwarf, new_friend):
"""
Adds new_friend to the dwarf's friend list, but only if the friend is not
already a friend
:param friend_database:
:param dwarf: The dwarf who is adding a new friend
:param new_friend: The new friend to add
:return: True if the friend was added, False otherwise
"""
pass
add_friend()
function:def add_friend(friend_database, dwarf, new_friend):
"""
Adds new_friend to the dwarf's friend list, but only if the friend is not
already a friend
:param friend_database:
:param dwarf: The dwarf who is adding a new friend
:param new_friend: The new friend to add
:return: True if the friend was added, False otherwise
"""
friend_list = friend_database[dwarf] # get friend list from database
if new_friend in friend_list:
print(f"{new_friend} is already in your friend list. No change.made!")
return False
else:
friend_list.append(new_friend)
return True
add_friend()
function:def unfriend(friend_database, dwarf, current_friend):
"""
Unfriends current_friend from dwarf's friend list.
:param friend_database: The database of friends
:param dwarf: The dwarf whose friend will be unfriended
:param current_friend: The friend of the dwarf who will be unfriended
:return: True if the dwarf had the friend, False if the dwarf did not
have the friend
"""
pass
unfriend()
function:Let's use try/except! How does a list removal fail? I don't know -- let's check:
>>> a = [1,5,7]
>>> a.remove(5)
>>> a.remove(2)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: list.remove(x): x not in list
It looks like we have a ValueError.
def unfriend(friend_database, dwarf, current_friend):
"""
Unfriends current_friend from dwarf's friend list.
:param friend_database: The database of friends
:param dwarf: The dwarf whose friend will be unfriended
:param current_friend: The friend of the dwarf who will be unfriended
:return: True if the dwarf had the friend, False if the dwarf did not
have the friend
"""
friend_list = friend_database[dwarf] # get friend list from database
try:
friend_list.remove(current_friend)
return True
except ValueError:
print(f"{current_friend} is not your friend! No change made.")
return False
unfriend()
function:try/except
it is a very straightforward function.def save_friends(friend_database, filename):
"""
Saves friend_database to filename
:param friend_database: The friend database
:param filename: The file to save the database to
:return: True if the database was saved, False otherwise
"""
pass
save_friends()
function:save_friends()
function:def save_friends(friend_database, filename):
"""
Saves friend_database to filename
:param friend_database: The friend database
:param filename: The file to save the database to
:return: True if the database was saved, False otherwise
"""
try:
with open(filename, "w") as f:
for dwarf, friends in friend_database.items():
f.write(" ".join([dwarf] + friends) + '\n')
return True
except FileNotFoundError:
return False
def save_friends(friend_database, filename):
"""
Saves friend_database to filename
:param friend_database: The friend database
:param filename: The file to save the database to
:return: True if the database was saved, False otherwise
"""
try:
with open(filename, "w") as f:
for dwarf, friends in friend_database.items():
f.write(dwarf)
f.write(" ")
for friend in friends[:-1]: # handle last as special case (no space)
f.write(friend)
f.write(" ")
f.write(friends[-1]) # last does not have a space
f.write("\n") # but it does have a newline
return True
except FileNotFoundError:
return False
save_friends()
function:a
, b
, and c
, and returns a
multiplied by b
and then added to c
:
def linear_combination(a, b, c):
"""
:return The value of a multiplied by b and added to c
"""
pass
a
, b
, and c
, and returns a
multiplied by b
and then added to c
:
def linear_combination(a, b, c):
"""
:return The value of a multiplied by b and added to c
"""
return a * b + c
>>> def first_and_last(lst):
... first = lst[0]
... last = lst[-1]
... return (first, last)
...
>>> result = first_and_last([5,10,15,20])
>>> print(result)
(5, 20)
>>> print(first)
5
>>>
10
>>>
[5, 10, 15, 20]
>>>
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'first' is not defined
>>>
>>> def first_and_last(lst):
... first = lst[0]
... last = lst[-1]
... return (first, last)
...
>>> result = first_and_last([5, 10, 15, 20])
>>> print(result)
(5, 20)
>>> print(first)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'first' is not defined
>>>
def mystery_func(n):
if n < 2:
return False
for i in range(2, n):
if n % i == 0:
return False
return True
def main():
count = 0
lst = [2, 3, 4]
for num in lst:
if mystery_func(num):
count += 1
if count == 0:
print("None")
elif count < len(lst):
print("Some")
else:
print("All")
if __name__ == "__main__":
main()
A. What does mystery_func
do?
B. Write a non-trivial doctest for mystery_func.
C. What does main() print out for this program?
def mystery_func(n):
if n < 2:
return False
for i in range(2, n):
if n % i == 0:
return False
return True
def main():
count = 0
lst = [2, 3, 4]
for num in lst:
if mystery_func(num):
count += 1
if count == 0:
print("None")
elif count < len(lst):
print("Some")
else:
print("All")
if __name__ == "__main__":
main()
A. What does mystery_func
do?
B. Write two non-trivial doctests for mystery_func.
C. What does main() print out for this program?
To solve this problem, I suggest trying a few numbers in the function, and going through each line. Let's try mystery_func(5):
def mystery_func(n):
if n < 2:
return False
for i in range(2, n):
if n % i == 0:
return False
return True
def main():
count = 0
lst = [2, 3, 4]
for num in lst:
if mystery_func(num):
count += 1
if count == 0:
print("None")
elif count < len(lst):
print("Some")
else:
print("All")
if __name__ == "__main__":
main()
A. What does mystery_func
do?
B. Write two non-trivial doctests for mystery_func.
C. What does main() print out for this program?
Let's try mystery_func(9):
Based on mystery_func(3) returning True and mystery_func(9) returning False, can you figure out what the function is doing? If not, try some more:
mystery_func(2) returns False, mystery_func(3) returns True, mystery_func(4) returns False, mystery_func(6) returns False, mystery_func(7) returns True.
def mystery_func(n):
if n < 2:
return False
for i in range(2, n):
if n % i == 0:
return False
return True
def main():
count = 0
lst = [2, 3, 4]
for num in lst:
if mystery_func(num):
count += 1
if count == 0:
print("None")
elif count < len(lst):
print("Some")
else:
print("All")
if __name__ == "__main__":
main()
A. What does mystery_func
do?
B. Write two non-trivial doctests for mystery_func.
C. What does main() print out for this program?
At this point, hopefully you can see that the function returns whether or not n is prime.
The doctests:
def mystery_func(n):
"""
>>> mystery_func(7)
True
>>> mystery_func(6)
False
"""
if n < 2:
return False
for i in range(2, n):
if n % i == 0:
return False
return True
def main():
count = 0
lst = [2, 3, 4]
for num in lst:
if mystery_func(num):
count += 1
if count == 0:
print("None")
elif count < len(lst):
print("Some")
else:
print("All")
if __name__ == "__main__":
main()
main() prints:
Some
(because 4 is not prime, and 2 and 3 are prime.
def main():
count = 0
lst = [2, 3, 4]
for num in lst:
if mystery_func(num):
count += 1
if count == 0:
print("None")
elif count < len(lst):
print("Some")
else:
print("All")
if __name__ == "__main__":
main()
Write the output for the following values of the parameters a, b, and c. (2 pts. each)
a: 3, b: 3, c: 9
a: 8, b: 7, c: 9
a: 1, b: 4, c: 4
a: 4, b: 7, c: 2
a: 2, b: 4, c: 7
def batman(a, b, c):
if a > 3:
if a < c:
print("Robin")
else:
print("Batman")
elif b == 4:
if b != c:
print("Batmobile")
else:
print("Toolbelt")
else:
print("Alfred")
Write the output for the following values of the parameters a, b, and c. (2 pts. each)
a: 3, b: 3, c: 9
a: 8, b: 7, c: 9
a: 1, b: 4, c: 4
a: 4, b: 7, c: 2
a: 2, b: 4, c: 7
def batman(a, b, c):
if a > 3:
if a < c:
print("Robin")
else:
print("Batman")
elif b == 4:
if b != c:
print("Batmobile")
else:
print("Toolbelt")
else:
print("Alfred")
Alfred
Robin
Toolbelt
Batman
Batmobile
while
and for
loops for your code. You should also understand how the enumerate function works.def sum_product(n):
print(f"Please provide {n} numbers:")
pass
Please provide 4 numbers:
Number 0: 2
Number 1: 4
Number 2: 5
Number 3: 7
Sum: 18, Product: 280
while
and for
loops for your code. You should also understand how the enumerate function works.def sum_product(n):
print(f"Please provide {n} numbers:")
sum = 0
product = 1
for i in range(n):
next_num = int(input(f"Number {i}: "))
sum += next_num
product *= next_num
return sum, product
Please provide 4 numbers:
Number 0: 2
Number 1: 4
Number 2: 5
Number 3: 7
Sum: 18, Product: 280
def count_list_range(lst, min, max):
"""
:return a count of numbers in the list
between (and including) min and max
"""
>>> print(count_list_range([1, 5, 2, 11, 6, 18, 4, 9], 5, 10))
3
def count_list_range(lst, min, max):
"""
:return a count of numbers in the list
between (and including) min and max
"""
count = 0
for value in lst:
if min <= value <= max:
count += 1
return count
def count_list_range(lst, min, max):
"""
:return a count of numbers in the list
between (and including) min and max
"""
return len([x for x in lst if min <= x <= max])
a // b
) to produce the final average. You may assume that the array is length 3 or more. You can use the sum()
, min()
, and max()
functions.def centered_average(lst):
pass
a // b
) to produce the final average. You may assume that the array is length 3 or more. You can use the sum()
, min()
, and max()
functions.def centered_average(lst):
lst_cpy = list(lst)
lst_cpy.remove(min(lst_cpy))
lst_cpy.remove(max(lst_cpy))
return sum(lst_cpy) // len(lst_cpy)
def div_by_seven(n):
pass
def div_by_seven(n):
return [x for x in range(1, n) if x % 7 == 0]
def remove_vowels(str):
pass
def remove_vowels(str):
return "".join([x for x in str if x.lower() not in "aeiou"])
def words_not_the(sentence):
"""Words not 'the'
Given a sentence, produce a list of the lengths of each word in the sentence,
but only if the word is not 'the'.
>>> words_not_the('the quick brown fox jumps over the lazy dog')
[5, 5, 3, 5, 4, 4, 3]
"""
pass
def words_not_the(sentence):
"""Words not 'the'
Given a sentence, produce a list of the lengths of each word in the sentence,
but only if the word is not 'the'.
>>> words_not_the('the quick brown fox jumps over the lazy dog')
[5, 5, 3, 5, 4, 4, 3]
"""
return [len(x) for x in sentence.split(' ') if x != 'the']
def recursive_sum(n):
pass
def recursive_sum(n):
if n == 1:
return 1
return n + recursive_sum(n - 1)
def mystery(i):
if i < 1:
return
print(i)
mystery(i - 1)
print(i)
def mystery(i):
if i < 1:
return
print(i)
mystery(i - 1)
print(i)
Example:
printFun(3)
3
2
1
1
2
3
The function prints i
down to 1
, and then 1
back up to i
.
def histogram(lst):
pass
Example:
>>> histogram([12, 5, 3, 19, 28, 15, 14, 1, 23])
{0: 3, 10: 4, 20: 2}
def histogram(lst):
hist = {}
for value in lst:
if value // 10 * 10 not in hist:
hist[value // 10 * 10] = 1
else:
hist[value // 10 * 10] += 1
return hist
Short, generic solution:
>>> histogram([12, 5, 3, 19, 28, 15, 14, 1, 23])
{0: 3, 10: 4, 20: 2}
def histogram2(lst):
hist = {}
for value in lst:
if 0 <= value < 10:
if 0 not in hist:
hist[0] = 1
else:
hist[0] += 1
elif 10 <= value < 20:
if 10 not in hist:
hist[10] = 1
else:
hist[10] += 1
elif 20 <= value <= 30:
if 20 not in hist:
hist[20] = 1
else:
hist[20] += 1
return hist
Verbose solution:
Example:
with open(filename, "r") as f:
for line in f:
line = line[:-1] # remove newline
# do something with the line
with open(filename, "r") as f:
allText = f.readlines() # read all lines into a list (will still have newlines)
allText = [x[:-1] for x in allText] # remove newlines (if necessary)
# now allText has a list of all the lines in the file with no newlines at the end
with open(filename, "w") as f:
f.write(mydata)
f.write('\n') # each line should end with a newline
with open(filename, "r") as f:
for line in f:
allText = f.read() # read all data
# now allText has the entire file
def reverse_lines(filename):
pass
def reverse_lines(filename):
with open(filename, "r") as f:
lines = f.readlines()
lines = lines[::-1]
with open(filename, "w") as f:
for line in lines:
f.write(line)
try/except
to catch errors. We will give you the error types (e.g., FileNotFoundError
)def get(dict, key):
pass
try/except
to catch errors. We will give you the error types (e.g., FileNotFoundError
)def get(dict, key):
try:
return dict[key]
except KeyError:
return None
help(dict.get)
get(self, key, default=None, /)
Return the value for key if key is in the dictionary, else default.
>>> d = {'name': 'Chris'}
>>> age = d.get('age')
>>> print(age)
None
>>> age = d['age']
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
KeyError: 'age'
>>>
def letter_histogram(s):
freqs = {}
for c in s:
if c not in freqs:
freqs[c] = 1
else:
freqs[c] += 1
return freqs
def letter_histogram(s):
freqs = {}
for c in s:
freqs[c] = freqs.get(c, 0) + 1
return freqs
>>> letter_histogram("the quick brown fox jumps over the lazy dog")
{'t': 2, 'h': 2, 'e': 3, ' ': 8, 'q': 1, 'u': 2, 'i': 1, 'c': 1, 'k': 1, 'b': 1, 'r': 2, 'o': 4,
'w': 1, 'n': 1, 'f': 1, 'x': 1, 'j': 1, 'm': 1, 'p': 1, 's': 1, 'v': 1, 'l': 1, 'a': 1, 'z': 1,
'y': 1, 'd': 1, 'g': 1}
get()
:
The midterm will be 2.5 hours long, from 7pm-9:30pm next Tuesday. It will be using a computerized testing system called BlueBook -- more details about that will follow.