.py
files containing your work. (If you upload a screenshot or other file format, you won't get credit.)This homework assignment must be submitted in Gradescope by Noon central time on Tuesday 8 February 2022.
Collaboration is prohibited, and you may only access resources (books, online, etc.) listed below.
The course materials you may refer to for this homework are:
This homework assignment has two problems, numbered 2 and 3. The grading breakdown is:
Points | Item |
---|---|
2 | Autograder |
4 | Problem 2 |
4 | Problem 3 |
10 | Total |
The part marked "autograder" reflects points assigned to your submission based on some simple automated checks for Python syntax, etc.. The result of these checks is shown immediately after you submit.
Ask your instructor or TA a question by email, in office hours, or on discord.
In Gradescope, the score assigned to your homework submission by the autograder (checking for syntax and docstrings) will be recorded as "Problem 1". Therefore, the numbering of the actual problems begins with 2.
Here is a function that takes two strings s
and t
and returns a string that contains all the letters that appear in both s
and t
. The order in which the characters appear in the return value isn't important, but the function is written in such a way that it will return them in the order they appear in s
.
def common_letters2(s,t):
"Return a string with distinct characters, consisting of the characters that appear in both s and t"
common = []
for c in s:
if c in common:
continue
if c in t:
common.append(c)
return "".join(common)
# This isn't the only way, nor the most efficient or natural way to accomplish this in Python
# We'll return to this function later and learn another way to do it!
Here is an example of its use:
common_letters2("evolving situation","inferior sandwich")
common_letters2("aabbccddeeff","global foxtrot aaaaa")
Use this function to write another function common_letters
that accepts any number of strings as arguments, and which returns a string that contains the letters that appear in every string that was given as an argument. Put your function in a file hwk4prob2.py
.
Note: Your solution must make essential use of common_letters2
.
Sample usage of common_letters
:
common_letters("abacus","diagonal","ocean madness")
common_letters("Python expert","noisome ruminant","tenured onion")
common_letters("a-b-c-d","b^c^d^e","c_d_e_f")
def common_letters(*args):
'''Given any number of strings as args, returns single
string containing letters appearing in every string'''
# Handle special cases where there are only 0 or 1 argument(s)
if len(args)==0:
return ""
if len(args)==1:
return args[0]
common = args[0] # Start with every letter from the first arg
for arg in args[1:]: # Iterate over each subsequent arg
common = common_letters2(common, arg) # Update common to only have letters in the current arg
return common
If a function attempts to open a file that doesn't exist, a FileNotFoundError
exception will be raised.
Write a decorator ignore_missing
that, if attached to such a function, will just ignore the FileNotFoundError
. Thus, with the decorator applied, the function will return as soon as it fails to open a file, with no exception. Put your decorator in a file hwk4prob3.py
.
For example, the following code raises an exception if "asdf.txt" does not exist:
def openit():
"Open 'asdf.txt' for reading"
with open("asdf.txt","r") as fobj:
return "Success!"
openit() # Exception because "asdf.txt" does not exist
print("Hey look, this line never runs")
But if you apply the decorator, it should no longer do so:
@ignore_missing
def openit():
"Open 'asdf.txt' for reading"
with open("asdf.txt","r") as fobj:
return "Success!"
openit() # Will do nothing because the `FileNotFound` is caught
print("Hey look, it ignored the missing file.")
def ignore_missing(f):
'''Decorator for ignoring any FileNotFoundErrors'''
def inner(*args, **kwargs):
'''Inner function to be returned by ignore_missing'''
try:
return f(*args, **kwargs) # Run the decorated function as normal
except FileNotFoundError:
pass # <- Do absolutely nothing!
return inner # Note the lack of parentheses after inner. Function is being returned, but not being run.
And to test the decorator...
@ignore_missing
def openit():
"Open 'asdf.txt' for reading"
with open("asdf.txt","r") as fobj:
return "Success!"
openit() # Will do nothing because the `FileNotFound` is caught
print("Hey look, it ignored the missing file.")