0% found this document useful (0 votes)
12 views5 pages

# Extra Practice For Quiz 2

Uploaded by

Ryan Tan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views5 pages

# Extra Practice For Quiz 2

Uploaded by

Ryan Tan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 5

# 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)

# let's try calling the function


printLongestWord(['a', 'ab', 'abc', 'def'])
'''

# 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.

As an added twist, assume that only numbers


# between 0 and 1000 (inclusive) are 'valid'. Anything above or below is
considered 'invalid' and should not be included in our search for the minimum value
in myList. If no lowest value is found, return 1001

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

# let's try calling the function


print(returnLowestValue([1, 2, 3, 999, -10, -20]))
'''

# 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

# let's try calling the function


myFunction(10)
print(my_var)
'''
# Q4. Write a function, myPrintingFunction, that, given a list (myList) as an
argument, prints every second element (starting with element 0)

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])

# let's try calling the function


myPrintingFunction([1, 2, 3, 4, 5, 6, 7])
'''

# Q5. Do the same as above, but backwards


def myPrintingFunction(myList):
myList = reversed(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])

# let's try calling the function


myPrintingFunction([1, 2, 3, 4, 5, 6, 7])
'''

# Q6. Given a list of strings, myList, print only the strings that contain 3 or
fewer characters

'''
# ANSWER:
myList = ['abc', 'abcde', 'a', '']

for element in myList:


if (len(element) <= 3):
print(element)
'''

# 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

# let's try calling the function


print(returnAverageValue([1, 2, 3, 999, -10, -20]))
'''

# Q8. Write a function, getWordsWithVowels, that takes a list of strings, myList,


as an argument, and returns a list containing the elements of myList that had at
least one vowel within it
# Note that there are a ton of different (and easier) ways to do this - we will
approach it using syntax we have discussed in class
'''
# ANSWER:
def getWordsWithVowels(myList):
myNewList = []
for element in myList:
if ('a' in element or 'e' in element or 'i' in element or 'o' in element or
'u' in element):
myNewList.append(element)
return myNewList

# let's try calling the function


print(getWordsWithVowels(['cow', 'late', 'cat', 'xyz', 'class', '123']))
'''

You might also like