question archive Debug the function flips() and partition() so that it works properly
Subject:Computer SciencePrice: Bought3
Debug the function flips() and partition() so that it works properly. You are allowed to add any other print statements that you need to help you with your work. When you have fixed the function, it should pass all tests in tests.py . When you are done, remember to remove all debugging code (the print statement and the limiting code) from your function.
FUNCTION PAGE - Includes my code at the bottom where I keep getting an inifite loop error instead of passing the tests located in tests.py (also shared at the bottom) - Please help with debugging this function !
import random
def flips():
"""
Returns the number of coin flips until seeing tails.
Keeps flipping a coin ('h' for heads. 't' for tails). It stops once it sees the
first tails, returning the number of heads.
"""
counter = 0
going = True
limit = 10
while going:
print(going)
if limit > 0:
limit = limit - 1
else:
break
return counter
def partition(s):
"""
Returns a tuple containing the vowels in s and the consonants in s.
Vowels are defined to be the letters 'a', 'e', 'i', 'o', and 'u'. This function
returns a tuple of two elements, the vowels in s and the the consonants in s.
The vowels and consonants are presented in the same order that they occur in s,
duplicates included.
Examples:
partition('hello') returns ('eo','hll')
partition('superstar') returns ('uea','sprstr')
partition('scary') returns ('a','scry')
partition('a') returns ('a','')
partition('k') returns ('','k')
Parameter s: The string to partition
Precondition: s is nonempty string of only lowercase letters
"""
# TWO accumulators
left = ''
right = ''
pos = 0
while pos < len(s):
letter = s[pos]
if letter in 'aeiou':
left = left+letter
else:
right = right+letter
pos = pos+1
return (left,right)
TESTS.PY PAGE -
import funcs
import introcs
import random
def test_flips():
"""
Test procedure for the function flips()
"""
print('Testing flips()')
# This allows us to "derandomize" random for testing
# See https://www.statisticshowto.datasciencecentral.com/random-seed-definition/
random.seed(20)
result = funcs.flips()
introcs.assert_equals(1,result)
result = funcs.flips()
introcs.assert_equals(1,result)
result = funcs.flips()
introcs.assert_equals(2,result)
result = funcs.flips()
introcs.assert_equals(0,result)
result = funcs.flips()
introcs.assert_equals(3,result)
result = funcs.flips()
introcs.assert_equals(0,result)
result = funcs.flips()
introcs.assert_equals(0,result)
def test_partition():
"""
Test procedure for the function partition()
"""
print('Testing partition()')
result = funcs.partition('hello')
introcs.assert_equals(('eo','hll'),result)
result = funcs.partition('superstar')
introcs.assert_equals(('uea','sprstr'),result)
result = funcs.partition('scary')
introcs.assert_equals(('a','scry'),result)
result = funcs.partition('grxlpk')
introcs.assert_equals(('','grxlpk'),result)
result = funcs.partition('aeiou')
introcs.assert_equals(('aeiou',''),result)
# Uncomment these test procedures when ready
if __name__ == '__main__':
test_flips()
#test_partition()
print('Module funcs passed all tests.')