- def secondLargest(L):
- return secondLargestHelper(L, None, None)
- def secondLargestHelper(L, largest, secondLargest):
- if L == []:
- return secondLargest
- first = L[0]
- rest = L[1:]
- if largest == None or first > largest:
- secondLargest = largest
- largest = first
- elif secondLargest == None or (first > secondLargest):
- secondLargest = first
- return secondLargestHelper(rest, largest, secondLargest)
- def vowelRun(s):
- if s == '' or s[0] not in "aeiouAEIOU":
- return 0
- else:
- return 1 + vowelRun(s[1:])
- def longestVowelRun(s):
- if s == '':
- return 0
- vowelLength = vowelRun(s)
- bestRun = longestVowelRun(s[1:])
- if vowelLength > bestRun:
- return vowelLength
- return bestRun
- def testSecondLargest():
- print('Testing secondLargest()...', end= '')
- assert(secondLargest([2, 5, 3, 4, 1]) == 4)
- assert(secondLargest([6, 5, 6, 6, 6]) == 6)
- assert(secondLargest([1]) == None)
- assert(secondLargest([]) == None)
- assert(secondLargest([2, 24, 120, 39]) == 39)
- print("Passed!")
- def testLongestVowelRun():
- print('Testing longestVowelRun()...', end= '')
- assert(longestVowelRun('AbcAeiD aEoa ba') == 4)
- assert(longestVowelRun('cnsnts') == 0)
- assert(longestVowelRun('aeiouAEIOU') == 10)
- assert(longestVowelRun('') == 0)
- print("Passed!")
- testSecondLargest()
- testLongestVowelRun()
Raw Paste