# Extra Practice For Quiz 2
# Extra Practice For Quiz 2
'''
Instructions: Read and attempt the question yourself without looking at the answer.
After doing this, uncomment and compare with the solution provided underneath.
'''
# Q1. Write a function, printLongestWord, that takes one argument, myList, which is
a list of strings. printLongestWord should print the longest string in the list. If
there are multiple strings with the
# same maximal length, print the one that appears last in the list. If there
are no elements in myList, print the empty string.
def printLongestWord(myList)
longestWord = ''
for i in myList:
if len(i) > len(longestWord):
longestWord = i
print(longestWord)
'''
# ANSWER:
def printLongestWord(myList):
longestWord = ''
for i in myList:
if len(i) >= len(longestWord):
longestWord = i
print(longestWord)
# Q2. Write a function, returnLowestValue, that takes one argument, myList, which
is a list of floats. returnLowestValue should return the smallest value in a list.
def returnLowestValue(myList)
lowestValue = 1001
for element in myList:
if (element < lowestValue and element >= 0 and element <= 1000):
lowestValue = element
return lowestValue
'''
# ANSWER:
def returnLowestValue(myList):
lowestValue = 1001
for element in myList:
if (element < lowestValue and element >= 0 and element <= 1000):
lowestValue = element
return lowestValue
# Q3. Write code for 'myFunction' that allows the user to increase the value of the
global variable my_var by n.
my_var = 0
myFunction(my_var)
global my_var
myvar += n
'''
my_var = 0
def myFunction(n):
# YOUR CODE HERE
'''
'''
# ANSWER:
my_var = 0
def myFunction(n):
global my_var
my_var += n
myPrintingFunction(myList)
for i in range(0, len(myList), 2):
print(myList[i])
'''
# ANSWER:
def myPrintingFunction(myList):
for i in range(0, len(myList), 2):
print(myList[i])
'''
def myPrintingFunction(myList):
for i in range(len(myList)-1, -1, -2): # the second argument must be -1 - if it
is 0, it is 0 exclusive, so it won't print the element in the very first position
print(myList[i])
# Q6. Given a list of strings, myList, print only the strings that contain 3 or
fewer characters
'''
# ANSWER:
myList = ['abc', 'abcde', 'a', '']
# Q7. Write a function, returnAverageValue, that takes one argument, myList, which
is a list of floats. returnAverageValue should return the average value in a list.
As an added twist, assume that only numbers
# between 0 and 500 (inclusive) are 'valid'. Anything above or below is
considered 'invalid' and should not be included in our search for the average value
of a list. If no values exist, return '0'
'''
# ANSWER:
def returnAverageValue(myList):
averageValue = 0 # the numerator (the summed result of all valid values)
numbersConsidered = 0 # we need to keep track of the denominator because it
won't always be len(myList) since some floats may be invalid
for element in myList:
if (element >= 0 and element <= 500):
averageValue += element
numbersConsidered += 1
return averageValue/numbersConsidered