Skip to content
Snippets Groups Projects
Commit c31dff5a authored by Jöran Frey's avatar Jöran Frey
Browse files

made minor adjustment

parent 76d43d17
No related branches found
No related tags found
No related merge requests found
%% Cell type:markdown id:becc5c8b tags:
# Week 4: Control Strucutres
%% Cell type:markdown id:59608955 tags:
## 1 While loops
%% Cell type:markdown id:5d4d9321 tags:
## 1.2 Basic While loop
Write a Python program that prints numbers from 1 to 5 using a while loop.
%% Cell type:code id:ca902be9 tags:
``` python
```
%% Cell type:markdown id:55995920 tags:
## 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.
The correct password is ``pythonrocks``.
%% Cell type:code id:8e316f1b tags:
``` python
```
%% Cell type:markdown id:0e3d6582 tags:
## 2. For loops
%% Cell type:markdown id:b1521cf0 tags:
#### 2.1 Ausgabe einer Liste
Given a list of animals. Write a program that outputs the contents of the list
%% Cell type:code id:9d700e0f tags:
``` python
animals = ['dog', 'cat', 'bird', 'fish']
```
%% Cell type:code id:9e37533d tags:
``` python
```
%% Cell type:markdown id:f00bbc4f tags:
#### 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``
Output the newly created list with a print command. Hint: the method ```type(varaiable)``` returns then type name.
%% Cell type:code id:046a9e3f tags:
``` python
List = [ 3.1, "Hallo Welt", True]
```
%% Cell type:code id:4255b571 tags:
``` python
for i in List:
print(type(i))
```
%% Output
<class 'float'>
<class 'str'>
<class 'bool'>
%% Cell type:markdown id:800d6106 tags:
#### 2.3 Prime number determination
Write a program that checks whether a given number is a prime number. <br>
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>
**TIP:** Use the function ``range(start, stop, step)`` to generate divisors to be checked.<br>
for that the ``break`` statement can be used
%% Cell type:code id:7f9daf9a tags:
``` python
```
%% Cell type:markdown id:259d8ec3 tags:
### 3 Advanced tasks
### 3 Functions
%% Cell type:markdown id:53685910 tags:
#### 3.1 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.
**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
%% Cell type:code id:bcc82bd9 tags:
``` python
```
%% Cell type:markdown id:ec6bf765 tags:
#### 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.
Implementation notes:
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.
> start with ``fibonacci = [0,1]`` to make programming easier
%% Cell type:code id:565ad93a tags:
``` python
```
%% Cell type:markdown id:21082eed tags:
#### 3.3 Sum of digits
Task:
Write a function called sum_of_digits that takes an integer as a parameter and returns the sum of its digits.
Implementation notes:
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:
``` python
```
%% Cell type:markdown id:a15c9045 tags:
#### 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.
> **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:
``` python
```
%% Cell type:markdown id:3811ae84 tags:
# Voluntary extended tasks
%% Cell type:markdown id:77c5124c tags:
#### 4.1 Faculty calculation
- 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.
> **TIP:** Use a for loop to calculate the product
%% Cell type:code id:db0682bc tags:
``` python
```
%% Cell type:markdown id:5abd2d86 tags:
#### 4.2 Bubblesort algarythm: **for Geeks!**
Bubblesort algarythmus:
Write a function called bubble_sort that takes a list of numbers as parameters.
Implement algorithm:
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.
Test:
Test the function with multiple lists of numbers to make sure it sorts correctly.
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.
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]
%% Cell type:code id:7d4e526f tags:
``` 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