Development - Intermediate, exercise 2
Text
Write the body of the Python function def do_it(string, number)
that takes a string and a number in input, and returns the string "Oh no!"
if the number of characters that are consonants (i.e. that are not “a”, “e”, “i”, “o”, “u”) in string
is less than number
, otherwise it returns a queue containing all the consonants in string
. In this latter case, the consonants are inserted in the queue in the same order they appear in string
. Example of execution:
def do_it(string, number):
pass # To implement
my_string = "yes mama"
my_number = 3
do_it(my_string, my_number) # returns deque(["y", "s", "m", "m"])
Solution
from collections import deque
# Test case for the function
def test_do_it(string, number, expected):
result = do_it(string, number)
if expected == result:
return True
else:
return False
# Code of the function
def do_it(string, number):
result = deque()
for c in string:
if c != " " and c not in "aeiou":
result.append(c)
if len(result) < number:
return "Oh no!"
else:
return result
# Tests
print(test_do_it("just a string", 10, "Oh no!"))
print(test_do_it("just a string", 2, deque(["j", "s", "t", "s", "t", "r", "n", "g"])))
Additional material
The runnable Python file is available online.