Please solve the following exercises to make sure you understand everything from the Python basics tutorial.
The solutions can be found in the next notebook, but you should really try to solve all exercises yourself before looking! ;-)
For all exercises there are some tests provided as a quick check if you solved the exercise correctly. In software development it is good practice to test all your code rigorously, usually with so called "unit tests", to make sure your code also works for possible edge cases. The tests provided here are a bit simpler than regular unit tests, relying instead on simple assert statements. An assert statement is a one liner that raises an AssertionError
if e.g. a function does not return the expected output for some input:
assert something that should evaluate to True, "Error message displayed otherwise"
Make sure that for the functions you write in the exercises, all the assert tests pass, otherwise you're not done!
# if the assert test passes, you don't see anything
assert 1 + 1 == 2, "Python should know basic math"
# if it doesn't pass, you'll get an error and the code stops there
assert "Hello" == "hello", "Python is case sensitive"
--------------------------------------------------------------------------- AssertionError Traceback (most recent call last) <ipython-input-1-d4e42cd345c6> in <module> 2 assert 1 + 1 == 2, "Python should know basic math" 3 # if it doesn't pass, you'll get an error and the code stops there ----> 4 assert "Hello" == "hello", "Python is case sensitive" AssertionError: Python is case sensitive
Check if all elements in the given list are the same.
Input: A list with some elements.
Output: True, if all elements are the same; False if there is at least one element different from the others.
def all_the_same(a_list):
# write code here to return True if all elements are the same, False otherwise
return False
# some basic tests with assert statements
assert all_the_same([1, 1, 1]) == True, "all the same"
assert all_the_same([1, 2, 1]) == False, "some different"
assert all_the_same(['a', 'a', 'a']) == True, "all the same - letters"
assert all_the_same([]) == True, "no elements = the same elements"
Check if a given password fulfills certain conditions to be considered strong enough:
Input: A string with a password. You are assured that it does not contain any whitespace.
Output: True if the password fulfills all the above conditions, False otherwise.
def strong_password(password):
# check if the password fulfills all conditions and return True or False
return False
# some basic tests with assert statements
assert strong_password('A1213pokl') == False, "to short"
assert strong_password('bAse730onE') == True, "good password"
assert strong_password('asasasasasasasaas') == False, "only lowercase letters"
assert strong_password('QWERTYqwerty') == False, "no digits"
assert strong_password('123456123456') == False, "no letters"
assert strong_password('QwErTy911poqqqq') == True, "good password"
Find and return the most frequent letter in a given string of letters and other symbols.
Casing should be ignored (i.e. "A" == "a"
) and the returned letter should be in lower case. Make sure you do not count punctuation symbols, digits, or whitespaces - only letters.
If you have two or more letters with the same frequency, then return the letter which comes first in the latin alphabet. For example -- "one" contains "o", "n", "e" only once, so return "e".
Input: The string that should be analyzed.
Output: The most frequent letter in lower case as a string.
def most_freq_letter(text):
# write code here to return the right letter from the given text
return "a"
# print the output of your function for "Hello World!"
print("Example:")
print(most_freq_letter("Hello World!"))
# some basic tests with assert statements
assert most_freq_letter("Hello World!") == "l", "Hello test"
assert most_freq_letter("How do you do?") == "o", "O is most wanted"
assert most_freq_letter("One") == "e", "All letter only once."
assert most_freq_letter("Oops!") == "o", "Don't forget about lower case."
assert most_freq_letter("AAaooo!!!!") == "a", "Only letters."
assert most_freq_letter("abe") == "a", "The First."
# see how efficient your function is - can it handle a string with 10k letters?
print("Start the long test")
assert most_freq_letter("a" * 1000 + "b" * 9000) == "b", "Long."
print("The local tests are done.")
Lists can be nested, i.e., you can have a list that itself contains another list. Here we want to flatten such nested lists, i.e., get all elements in a single list.
Input: A possibly nested list, i.e., a list that can have other lists inside it.
Output: A flattened list with the same elements as the input list.
def flat_list(a_list):
# some code here to create a flattened version of the list
return a_list
assert flat_list([1, 2, 3]) == [1, 2, 3], "flat list"
assert flat_list([[1, 2, 3]]) == [1, 2, 3], "single nested list"
assert flat_list([]) == [], "empty list"
assert flat_list([[], [[]]]) == [], "empty nested list"
assert flat_list([[1, 2, [], 3]]) == [1, 2, 3], "empty additional list"
assert flat_list([1, [2, 2, 2], 4]) == [1, 2, 2, 2, 4], "list with the same elements"
assert flat_list([[[2]], [4, [5, 6, [6], 6, 6, 6], 7]]) == [2, 4, 5, 6, 6, 6, 6, 6, 7], "very nested"
assert flat_list([-1, [1, [-2], 1], -1]) == [-1, 1, -2, 1, -1], "symmetric"