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
if __name__ == "__main__":
words = ["cod", "s", "r"]
print(verbing(words)) # expected ['coding', 'sing', 'ring']
print(verbing(["work", "play"])) # expected ["working", "playing"]
print(verbing(words)) # expected ['coding', 'sing', 'ring']
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 Runtime error) and cause of each bug. You don't need to say how fix the bugs and feel free to be 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
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 example,
second_largest([20, -1, -10])
should return -1 and second_largest([8, 8, 7, 6, 5])
should return 8.
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. Hint: you do not
need to sort the list to solve this problem!
You do not need to handle the case where elements in the provided list are not numbers.
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.
if __name__ == '__main__':
block which
includes at least 1 test that was not explicitly providedsecond_largest
function is implemented correctly
Next Exercise: Survey