Further Exercises#
(Click here for the German version of this page)
Fizz buzz#
A popular exercise for beginners is the fizz buzz problem:
The numbers 1 up to (including) 100 are to be printed, each in their own line.
Numbers, which are divisible by 3 are to be replaced with the string
Fizz
Numbers, which are divisible by 5 are to be replaced with the string
Buzz
Numbers, which are divisible by both 3 and 5 are to be replaced with the string
FizzBuzz
The output should look like this: (Note that the right column is just included for clarity)
Output (Number)
1 1
2 2
Fizz 3
4 4
Buzz 5
Fizz 6
7 7
8 8
Fizz 9
Buzz 10
11 11
Fizz 12
13 13
14 14
FizzBuzz 15
16 16
... ...
98 98
Fizz 99
Buzz 100
Your solution#
# Here you can solve the fizz buzz problem...
Solution for fizz buzz
for number in range(1, 101):
if number % 3 == 0 and number % 5 == 0:
print("FizzBuzz")
elif number % 3 == 0:
print("Fizz")
elif number % 5 == 0:
print("Buzz")
else:
print(number)
Explanation:
range(1, 101)
is used, since the sequence starts at 1
and ends at 100
. Since the second parameter for range()
is the excluded upper boundary, 101
has to be used. (100
inclusive –> 101
exclusive)
Number is divisible by n
–> number % n == 0
.
I.e., there is no remained for the division number / n
.
There are many solutions for this famous problem. One could, e.g., work with strings and concatenate Fizz and Buzz for cases where the number is divisible by both 3 and 5.
Palindromes#
A palindrome is a word, which is read the same no matter if one reads from left to right or right to left.
I.e., the word can be mirrored in the middle without change.
Examples: radar, kayak
Word: R A D A R
Index: 0 1 2 3 4
Word: R A D A R
Index: 4 3 2 1 0
It does not matter whether you read “RADAR” from left to right or right to left, the result is the same.
Your task is to write a function, which takes a string as input and returns True
if the provided string is a palindrome and False
if it is not. To reduce complexity, you can assume that only lowercase letters without punctuation or spaces are provided.
# Here you can solve the palindrome problem...
Solution for palindromes
Probably the easiest version is to just reverse the string and check if it is still equal to the original string. To reverse the string, one can create an empty string and concatenate all letters to the empty string, starting from the back. This can be done using reversed(range())
. Since strings are immutable, strings must be concatenated and there is no way to modify a character at a given index.
def is_palindrome(string):
reversed_string = ""
for index in reversed(range(0, len(string))):
reversed_string += string[index]
return string == reversed_string
For another variant, the string does not have to reversed. All comparisons can be done in-place. A check is performed whether the first character is equal to the last character, then a check is performed whether the second character is equal to the character before last, and so on until one arrives in the middle. If one arrives at the middle and has not encountered a pair that does not match up, then it is a palindrome. If a pair does not match up somewhere, then it is not a palindrome.
Explanation:
it_is_a_palindrome
is initially set to True
. The index variable will have the values in range(0, len(string) // 2)
. With len(string)
one can get the length of the word. //
is the integer division operator, which rounds the result to the next-lowest integer if it is not an integer already.
To access both the letter on the left side and on the right side, one can use negative indices Index
and -(Index+1)
. -Index
is the same as len(string) - Index
.
The connected pairs of characters would be {0, -1}, {1, -2}, {2, -3}, {3, -4}, …
. More generally: {index, -(index+1)}
.
If there is a spot where the pair does not match up, it is not a palindrome.
it_is_a_palindrome
will be set to False
. Optionally to save time, the loop is cancelled using the break
keyword, since it has already been shown that the word is not a palindrome.
Finally, the function returns whether the word is a palindrome or not.
def is_palindrome_from_both_sides(string):
it_is_a_palindrome = True
for index in range(0, len(string) // 2):
if string[index] != string[-(index+1)]:
it_is_a_palindrome = False
break
return it_is_a_palindrome