Skip to content
Snippets Groups Projects
Commit 9a9fca51 authored by Felix Nyffenegger's avatar Felix Nyffenegger
Browse files

update on exercises

parent f70f1744
No related branches found
No related tags found
No related merge requests found
This diff is collapsed.
%% Cell type:markdown id:becc5c8b tags: %% Cell type:markdown id:becc5c8b tags:
# Week 4: Control Strucutres # Week 4: Control Strucutres
%% Cell type:markdown id:59608955 tags: %% Cell type:markdown id:59608955 tags:
## 1 While loops ## 1 While loops
%% Cell type:markdown id:5d4d9321 tags: %% Cell type:markdown id:5d4d9321 tags:
## 1.2 Basic While loop ## 1.2 Basic While loop
Write a Python program that prints numbers from 1 to 5 using a while loop. Write a Python program that prints numbers from 1 to 5 using a while loop.
%% Cell type:code id:ca902be9 tags: %% Cell type:code id:ca902be9 tags:
``` python ``` python
``` ```
%% Cell type:markdown id:55995920 tags: %% Cell type:markdown id:55995920 tags:
## 1.2 Password attempt ## 1.2 Password attempt
Write a Python program that simulates alogin system. The program should allow the user to attempt entering a password up to 3 times. If the user enters the correct password, the program should display a success message and stop. If the user fails 3 times, it should lock the user out with a failure message. Write a Python program that simulates alogin system. The program should allow the user to attempt entering a password up to 3 times. If the user enters the correct password, the program should display a success message and stop. If the user fails 3 times, it should lock the user out with a failure message.
The correct password is ``pythonrocks``. The correct password is ``pythonrocks``.
%% Cell type:code id:8e316f1b tags: %% Cell type:code id:8e316f1b tags:
``` python ``` python
``` ```
%% Cell type:markdown id:0e3d6582 tags: %% Cell type:markdown id:0e3d6582 tags:
## 2. for loops ## 2. For loops
%% Cell type:markdown id:b1521cf0 tags: %% Cell type:markdown id:b1521cf0 tags:
#### 2.1 Ausgabe einer Liste #### 2.1 Ausgabe einer Liste
Given a list of animals. Write a program that outputs the contents of the list Given a list of animals. Write a program that outputs the contents of the list
%% Cell type:code id:9d700e0f tags: %% Cell type:code id:9d700e0f tags:
``` python ``` python
animals = ['dog', 'cat', 'bird', 'fish'] animals = ['dog', 'cat', 'bird', 'fish']
``` ```
%% Cell type:code id:9e37533d tags: %% Cell type:code id:9e37533d tags:
``` python ``` python
``` ```
%% Cell type:markdown id:f00bbc4f tags: %% Cell type:markdown id:f00bbc4f tags:
#### 2.2 Determine the data types of list elements #### 2.2 Determine the data types of list elements
Given a list with data, extend the code so that the data types of the individual variables are listed in a new list called ``type_list`` Given a list with data, extend the code so that the data types of the individual variables are listed in a new list called ``type_list``
Output the newly created list with a print command Output the newly created list with a print command. Hint: the method ```type(varaiable)``` returns then type name.
%% Cell type:code id:046a9e3f tags: %% Cell type:code id:046a9e3f tags:
``` python ``` python
List = [ 3.1, "Hallo Welt", True] List = [ 3.1, "Hallo Welt", True]
``` ```
%% Cell type:code id:4255b571 tags: %% Cell type:code id:4255b571 tags:
``` python ``` python
for i in List:
print(type(i))
``` ```
%% Output
<class 'float'>
<class 'str'>
<class 'bool'>
%% Cell type:markdown id:800d6106 tags: %% Cell type:markdown id:800d6106 tags:
#### 2.3 Prime number determination #### 2.3 Prime number determination
Write a program that checks whether a given number is a prime number. <br> Write a program that checks whether a given number is a prime number. <br>
Tip: **int()** can be used to convert values into integers Tip: **int()** can be used to convert values into integers
**Logik** A prime number is a natural number which is greater than 1 and is only divisible by itself and by 1 without remainder. <br> **Logik** A prime number is a natural number which is greater than 1 and is only divisible by itself and by 1 without remainder. <br>
**TIP:** Use the function ``range(start, stop, step)`` to generate divisors to be checked.<br> **TIP:** Use the function ``range(start, stop, step)`` to generate divisors to be checked.<br>
for that the ``break`` statement can be used for that the ``break`` statement can be used
%% Cell type:code id:7f9daf9a tags: %% Cell type:code id:7f9daf9a tags:
``` python ``` python
``` ```
%% Cell type:markdown id:259d8ec3 tags: %% Cell type:markdown id:259d8ec3 tags:
### 3 Advanced tasks ### 3 Advanced tasks
%% Cell type:markdown id:53685910 tags: %% Cell type:markdown id:53685910 tags:
#### 3.1 Palindrome checker #### 3.1 Palindrome checker
Implement a function called Palindrome_checker Implement a function called Palindrome_checker
A palindrome is a word, a sentence, a number or another sequence of characters that can be read the same forwards and backwards. A palindrome is a word, a sentence, a number or another sequence of characters that can be read the same forwards and backwards.
**TIP** with the slicing syntax [::-1], a string can be reversed <br> **TIP** with the slicing syntax [::-1], a string can be reversed <br>
You can use the ``.lower()`` function to ignore upper and lower case letters You can use the ``.lower()`` function to ignore upper and lower case letters
%% Cell type:code id:bcc82bd9 tags: %% Cell type:code id:bcc82bd9 tags:
``` python ``` python
``` ```
%% Cell type:markdown id:ec6bf765 tags: %% Cell type:markdown id:ec6bf765 tags:
#### 3.2 Fibonacci #### 3.2 Fibonacci
Implement a function called fibonacci that takes a number n as a parameter and returns the first n numbers of the Fibonacci sequence in a list. Implement a function called fibonacci that takes a number n as a parameter and returns the first n numbers of the Fibonacci sequence in a list.
Implementation notes: Implementation notes:
The Fibonacci sequence starts with 0 and 1. Each additional number is the sum of the two previous numbers. The Fibonacci sequence starts with 0 and 1. Each additional number is the sum of the two previous numbers.
Use a loop to generate the number sequence. Use a loop to generate the number sequence.
> start with ``fibonacci = [0,1]`` to make programming easier > start with ``fibonacci = [0,1]`` to make programming easier
%% Cell type:code id:565ad93a tags: %% Cell type:code id:565ad93a tags:
``` python ``` python
``` ```
%% Cell type:markdown id:21082eed tags: %% Cell type:markdown id:21082eed tags:
#### 3.3 Sum of digits #### 3.3 Sum of digits
Task: Task:
Write a function called sum_of_digits that takes an integer as a parameter and returns the sum of its digits. Write a function called sum_of_digits that takes an integer as a parameter and returns the sum of its digits.
Implementation notes: Implementation notes:
You can convert the number to a string to extract each digit, or use math operations to isolate the digits. You can convert the number to a string to extract each digit, or use math operations to isolate the digits.
%% Cell type:code id:2db44c44 tags: %% Cell type:code id:2db44c44 tags:
``` python ``` python
``` ```
%% Cell type:markdown id:a15c9045 tags: %% Cell type:markdown id:a15c9045 tags:
#### 3.4 Number of vowels in a string #### 3.4 Number of vowels in a string
Write a function ``count_vowels`` that takes a string as a parameter and returns the number of vowels in the string. Write a function ``count_vowels`` that takes a string as a parameter and returns the number of vowels in the string.
> **TIP:** You can use ``.lower()`` to make the function case-insensitive, ensuring it counts both uppercase and lowercase vowels. > **TIP:** You can use ``.lower()`` to make the function case-insensitive, ensuring it counts both uppercase and lowercase vowels.
%% Cell type:code id:97e09fcc tags: %% Cell type:code id:97e09fcc tags:
``` python ``` python
``` ```
%% Cell type:markdown id:3811ae84 tags: %% Cell type:markdown id:3811ae84 tags:
# Voluntary extended tasks # Voluntary extended tasks
%% Cell type:markdown id:77c5124c tags: %% Cell type:markdown id:77c5124c tags:
#### 4.1 Faculty calculation #### 4.1 Faculty calculation
- Write a function called factorial that takes a number n as a parameter and returns the factorial of this number. - Write a function called factorial that takes a number n as a parameter and returns the factorial of this number.
- **Logic** The factorial of a number n is the product of all positive integers less than or equal to n. - **Logic** The factorial of a number n is the product of all positive integers less than or equal to n.
> **TIP:** Use a for loop to calculate the product > **TIP:** Use a for loop to calculate the product
%% Cell type:code id:db0682bc tags: %% Cell type:code id:db0682bc tags:
``` python ``` python
``` ```
%% Cell type:markdown id:5abd2d86 tags: %% Cell type:markdown id:5abd2d86 tags:
#### 4.2 Bubblesort algarythm: **for Geeks!** #### 4.2 Bubblesort algarythm: **for Geeks!**
Bubblesort algarythmus: Bubblesort algarythmus:
Write a function called bubble_sort that takes a list of numbers as parameters. Write a function called bubble_sort that takes a list of numbers as parameters.
Implement algorithm: Implement algorithm:
Inside the function, use the bubble sort algorithm to sort the list. Inside the function, use the bubble sort algorithm to sort the list.
The algorithm should compare and swap neighboring elements until the entire list is sorted. The algorithm should compare and swap neighboring elements until the entire list is sorted.
Test: Test:
Test the function with multiple lists of numbers to make sure it sorts correctly. Test the function with multiple lists of numbers to make sure it sorts correctly.
Implementation notes: Implementation notes:
Bubble Sort Logic: start at the beginning of the list and compare each neighboring pair of elements. Swap the elements if the first element is greater than the second. Repeat this process for each pairing in the list, then start again from the beginning until a run without swaps has been completed. Bubble Sort Logic: start at the beginning of the list and compare each neighboring pair of elements. Swap the elements if the first element is greater than the second. Repeat this process for each pairing in the list, then start again from the beginning until a run without swaps has been completed.
Optimization option: You can use a variable to track whether swaps have been made. If a complete run ends without swaps, you can end the sorting as the list is already sorted. Optimization option: You can use a variable to track whether swaps have been made. If a complete run ends without swaps, you can end the sorting as the list is already sorted.
test_array = [64, 34, 25, 12, 22, 11, 90] test_array = [64, 34, 25, 12, 22, 11, 90]
%% Cell type:code id:7d4e526f tags: %% Cell type:code id:7d4e526f tags:
``` python ``` python
``` ```
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment