Assignment 3
This is an ungraded assignment!
Deadline: 8th Oct - 15:00 - After this time, don't change your notebook. It'll be automatically collected by CoCalc.
Advanced debugging with f-strings
Python 3.6 and later have something called f-strings. They are very useful for debugging. To see if we can use them first we check which Python version we are using.
The syntax is simple. Just put an 'f' in front of your string to turn it into an f-string. In an f-string anything that is inside curly braces will be evaluated as Python code. The value in the brackets will then be replaced by the value of your expression. The cell below shows an example.
Task 1 - for
-loop (1 Point)
Write a for-loop that creates a string containing all uneven numbers from 1 to 100 without spaces (you start at 1, so 1 should be included). Store this string in the variable solution1
.
HINT: Use range()
.
Success!
Task 2 Advanced for
-loop and range()
(2 Points)
Write a for
-loop that creates a string containing the digit sum of every third number from 1 to 1000 without spaces (you start at the number 1). Store this string in the variable solution2
.
To calculate the digit sum you sum up all the digits in a number.
An example for the numbers from 1 to 13 is given below:
Number | Digit Sum |
---|---|
1 | 1 |
4 | 4 |
7 | 7 |
10 | 1 + 0 = 1 |
13 | 1 + 3 = 4 |
The solution here is the string "14714"
. You should not calculate the digit sum of the string containing every third number from 1 to 1000. For every third number, calculate the digit sum, then add it to solution2
as a string
.
Don't confuse it with the digital root. The digital sum of 99
is 18
, the digital root of 99
is 9
. You obtain the digital root by calculating the digital sum until you reach a single digit number.
HINT: Use range()
.
HINT: Iterate over the digits of each number inside the for
-loop with another for
-loop.
Success!
Task 3 - prime checking (2 Points):
Write a function is_prime
that takes a positive integer as argument and returns True
if the integer is a prime number and False
if it is not.
HINT: To check if a number is dividable by another number you can use modulo (%).
HINT: Don't overcomplicate things: Check if the number your function gets as an argument is dividable by another number (other than 1 and itself) and if it is not then you have found a prime number.
Success!
Success!
Task 4 - function with default parameters (1 Point)
Define a function sum_numbers
that takes three arguments: start
, end
and step
. It should sum up all numbers between start
(inclusive) and end
(exclusive), with a step size of step
and return this sum.
Example: start = 1
, end = 10
, step = 2
should sum up every second number between 1 and 10.
1 + 3 + 5 + 7 + 9 = 25
The parameters should be optional with default values as in the example above.
HINT: You can use sum()
to sum up all values in an iterable (such as a list, or a tuple, ...).
Success!
Success!
Success!
Task 5 - FizzBuzz (5 Points)
Write a function fizzbuzz
with one argument limit
. Your function should return a list with limit + 1
items (so if limit = 10
your list should have length 11 - remember that lists start counting at 0!) where for each index the value in the list should be "Fizz"
if the index is a multiple of three, "Buzz"
if the index is a multiple of five and in case it is a multiple of both three and five it should contain "FizzBuzz"
, in all other cases it should be the index itself.
Example:
fizzbuzz(15)
should return a list with 16 elements (this means it goes up to lst[15]
).
2 is not a multiple of three or five, so
lst[2] == 2
(the integer 2, not the string '2')3 is a multiple of three, so
lst[3] == "Fizz"
15 is a multiple of three and five, so
lst[15] == "FizzBuzz"
HINT: Be careful when using if
and elif
, as a number can be a multiple of both three and five!
HINT: Don't forget to handle the case when a number is not a multiple of three or five.
Success!
Success!
Success!
Success!
Task 6 - function interaction (3 Point)
Write a function unique_elements
that has one argument elems
and returns a sorted list containing the the elements from elems
without duplicates. If a value appears multiple times in elems
you should return a list that contains only one copy of it. In other words: Remove the duplicates from elems
.
Write a second function tuple_firsts
that has one argument tpls
. This argument will be a list of tuples. Your function should then call your unique_elements
functions with a list that contains only the first elements from tpls
and returns the value that is returned by unique_elements
.
For the list of elements given above in the get_testing_elements()
function this means that:
your
tuple_firsts
function is called with the list of tuples from_elements()
inside your
tuple_firsts
function you extract the first values and store them in a list:["django", "django", "pandas", "pandas", "NumPy"]
you call your
unique_elements
function with the list from the previous stepunique_elements
returns a (sorted) list containing the values["django", "pandas", "NumPy"]
your
tuple_firsts
functions returns the value thatunique_elements
returns when you call it
HINT: Start with the unique_elements
function! Then, when you think you have implemented the function correctly, try calling it with lists that contain duplicates and check if it works!
HINT: For the tuple_firsts
function first find a way to get the first element from a list of tuples. Again, try calling your function and check if it works by using print()
statements. Once you are sure this works you can call your unique_elements
function from inside your tuple_firsts
function!
Success!