Second Largest
Please Log In for full access to the web site.
Note that this link will take you to an external site (https://shimmer.mit.edu) to authenticate, and then you will be redirected back to this page.
1) Part 1
Larry has written the following piece of code, designed to solve the first part of the ing assignment from week 2.
def verbing(worsd)
"""
This function should take in a list of strings and return a new list of strings with "ing" added to the end of each word.
"""
for w in len(words):
words[w] = w + "ing"
return words
words = ["cod", "s", "r"]
print(verbing(words)) # expected ['coding', 'sing', 'ring']
print(verbing(["work", "sleep", "play"])) # expected ['working', 'sleeping', 'playing']
For example, one of the five
bugs is that "In line 1, Larry forgot the colon ":" at the end of the definition of the
For full credit, include the type (Semantic error, Syntax error, or the specific
runtime error) and cause of each bug. You don't need to say how fix the bugs
and feel free to be very brief (1-2 sentences per bug).
verbing
function, which causes a SyntaxError".
Formatting Help
2) Part 2
In a file on your own machine, write and test a function called
second_largest_number
that takes as input a single list containing 0
or more numbers. If there are 0 or 1 numbers in the given list, your
function should return None
. Otherwise, it should return the
second largest number in the list.
For full credit, your code should not use built-in functions such as max
or sorted
. It should also not mutate (change) the input list.
You do not need to handle the case where elements in the provided list are not numbers.
For example, running the following code:
print(second_largest_number([]))
print(second_largest_number([2]))
print(second_largest_number([94, 87, 20, 35]))
print(second_largest_number([20, -1, -10]))
print(second_largest_number([8, 8, 7, 6, 5]))
should print:
None
None
87
-1
8
Pay particular attention to the last example. It does not print 7
since 8
is both the largest and second largest number.
Start by making a plan and running through some test cases of your own devising on paper. Once you are confident in your plan, implement your function and test using the Python interpreter. When you are ready, submit your file twice below. In order to get manual style credit, your code must pass at least half of the automated tests in the second file submission below.
Please upload your code here. This question will be manually graded for good code style according to the following rubric / 5 points: Solutions that use max, imports, sorted, or other similar advanced Python features
can earn max half credit (the point of this exercise is to help you to develop
your programming skills by writing the explicit logic yourself!)
if __name__ == '__main__':
block which
includes at least 1 manual test case that uses an f-string
Please upload your code below to test for correctness:
Next Exercise: Survey