Skip to content

Commit

Permalink
Add a couple of exercises
Browse files Browse the repository at this point in the history
  • Loading branch information
abregman committed Apr 23, 2022
1 parent 54403cb commit d4b77c1
Show file tree
Hide file tree
Showing 25 changed files with 272 additions and 7 deletions.
18 changes: 14 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

:information_source:  This repo contains questions and exercises to learn and practice Python

:bar_chart:  There are currently **60** exercises and questions
:bar_chart:  There are currently **70** exercises and questions

# Python Exercises

Expand Down Expand Up @@ -50,13 +50,17 @@
|Name|Objective & Instructions|Solution|Comments|
|--------|--------|------|----|
| 22 times | [Exercise](exercises/hello_world/22_times.md) | | |
| Variables | [Exercise](exercises/strings/variables.md) | [Solution](solutions/strings/variables.md) | |
| What is the result? - Level 1 | [Exercise](exercises/strings/what_is_the_result_lvl_1.md) | [Solution](solutions/strings/what_is_the_result_lvl_1.md) | |
| What is the result? - Level 2 | [Exercise](exercises/strings/what_is_the_result_lvl_2.md) | [Solution](solutions/strings/what_is_the_result_lvl_2.md) | |
| Starts with a letter | [Exercise](exercises/strings/letter_start.md) | [Solution](solutions/strings/letter_start.md) | |
| Change Strings | [Exercise](exercises/strings/change_strings.md) | [Solution](solutions/strings/change_strings.md) | |
| All Digits | [Exercise](exercises/strings/all_digits.md) | [Solution](solutions/strings/all_digits.md) | |
| Removing Characters | [Exercise](exercises/strings/removing_characters.md) | [Solution](solutions/strings/removing_characters.md) | |
| Reverse a String | [Exercise](exercises/strings/reverse_string.md) | [Solution](solutions/strings/reverse_string.md) | |
| Compress Strings | [Exercise](exercises/strings/compress_strings.md) | | |
| Slicing - Level 1 | [Exercise](exercises/strings/slicing_lvl_1.md) | [Solution](solutions/strings/slicing_lvl_1.md) | |
| Slicing - Level 2 | [Exercise](exercises/strings/slicing_lvl_2.md) | [Solution](solutions/strings/slicing_lvl_2.md) | |

## Numbers

Expand All @@ -68,7 +72,7 @@
| Bases | [Exercise](exercises/numbers/bases.md) | [Solution](solutions/numbers/bases.md) | |
| Palindrome | [Exercise](exercises/numbers/palindrome.md) | [Solution](solutions/numbers/palindrome.md) | |

## Lists
## Lists & Tuples

|Name|Objective & Instructions|Solution|Comments|
|--------|--------|------|----|
Expand All @@ -77,12 +81,17 @@
| Length | [Exercise](exercises/hello_world/length.md) | | |
| Min Max | [Exercise](exercises/lists/min_max.md) | [Solution](solutions/lists/min_max.md) | |
| Three Biggest Items | [Exercise](exercises/lists/three_biggest_items.md) | [Solution](solutions/lists/three_biggest_items.md) | |
| What is the result? | [Exercise](exercises/lists/what_is_the_result.md) | [Solution](solutions/lists/what_is_the_result.md) | |
| What is the result? - Level 1 | [Exercise](exercises/lists/what_is_the_result_lvl_1.md) | [Solution](solutions/lists/what_is_the_result_lvl_1.md) | |
| What is the result? - Level 2 | [Exercise](exercises/lists/what_is_the_result_lvl_2.md) | [Solution](solutions/lists/what_is_the_result_lvl_2.md) | |

## Loops

|Name|Objective & Instructions|Solution|Comments|
|--------|--------|------|----|
| Break Out | [Exercise](exercises/loops/break_out.md) | [Solution](solutions/loops/break_out.md) | |
| Break Out 2 | [Exercise](exercises/loops/break_out.md) | [Solution](solutions/loops/break_out.md) | |
| Every character | [Exercise](exercises/loops/every_char.md) | [Solution](solutions/loops/every_char.md) | |
| Stream of Numbers | [Exercise](exercises/loops/numbers_stream.md) | [Solution](solutions/loops/numbers_stream.md) | |
| Refactor-1 | [Exercise](exercises/loops/refactor_1.md) | [Solution](solutions/loops/refactor_1.py) | |

## Functions
Expand Down Expand Up @@ -123,7 +132,8 @@
|Name|Objective & Instructions|Solution|Comments|
|--------|--------|------|----|
| Dividing by Zero | [Exercise](exercises/exceptions/divide_by_zero.md) | [Solution](solutions/exceptions/divide_by_zero.md) | |
| What exception is raised? | [Exercise](exercises/exceptions/what_exception_is_raised.md) | [Solution](solutions/exceptions/what_exception_is_raised.md) | |
| What exception is raised? - Level 1 | [Exercise](exercises/exceptions/what_exception_is_raised_lvl_1.md) | [Solution](solutions/exceptions/what_exception_is_raised_lvl_1.md) | |
| What exception is raised? - Level 2 | [Exercise](exercises/exceptions/what_exception_is_raised_lvl_2.md) | [Solution](solutions/exceptions/what_exception_is_raised_lvl_2.md) | |

## Regex

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
## What Exception is Raised?
## What Exception is Raised? - Level 1

For each of the following statement, specify what exception will be raised

Expand Down
9 changes: 9 additions & 0 deletions exercises/exceptions/what_exception_is_raised_lvl_2.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
## What Exception is Raised? - Level 2

For each of the following statement, specify what exception will be raised

1. `chars = "abc";chars[0]='h'`

### Solution

1. TypeError
15 changes: 15 additions & 0 deletions exercises/lists/what_is_the_result_lvl_1.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
## What is the result?

1. What is the result of the following block of code?

```python
x = ['a', 'b', 'c']
for i in x:
if i == 'b':
x = ['z', 'y']
print(i)
```

### Solution

Click [here](solutions/lists/what_is_the_result_lvl_1.md) to view the solution.
15 changes: 15 additions & 0 deletions exercises/lists/what_is_the_result_lvl_2.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
## What is the result? - Level 2

1. What is the result of the following block of code?

```python
x = ['a', 'b', 'c']
for i in x:
if i == 'b':
x = ['z', 'y']
print(i)
```

### Solution

Click [here](solutions/lists//what_is_the_result_lvl_2.md) to view the solution.
7 changes: 7 additions & 0 deletions exercises/loops/break_out.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
## Break Out

Write a loop that will keep reversing user's input until he enters the letter 'q' to quit

## Solution

Click [here](solutions/loops/break_out.md) to view the solution.
7 changes: 7 additions & 0 deletions exercises/loops/break_out_2.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
## Break Out 2

Iterate over a given list of numbers and break out of it if one the numbers is 2 or 4. If you didn't any of these numbers, print a message saying so.

### Solution

Click [here](solutions/loops/break_out_2.md) to view the solution.
7 changes: 7 additions & 0 deletions exercises/loops/every_char.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
## Every Character

Given a variable of a string, print character of the string on its own line.

### Solution

Click [here](solutions/loops/every_char.md) to view the solution.
8 changes: 8 additions & 0 deletions exercises/loops/numbers_stream.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
## Stream of Numbers

1. Print the numbers between 0 and 10
2. Print the number between 0 and -10

### Solution

Click [here](solutions/loops/numbers_stream.md) to view the solution.
7 changes: 7 additions & 0 deletions exercises/strings/change_strings.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
## Change Strings

Modify the string "cat" to "bat"

### Solution

Click [here](soltuions/strings/change_strings.md) to view the solution.
13 changes: 13 additions & 0 deletions exercises/strings/slicing_lvl_1.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
## Slicing - Level 1

What is the result of each of the following statements if `x = 'abcdefgh'`

1. `x[:]`
2. `x[2:]`
3. `x[2:2]`
4. `x[2:3]`
5. `x[-1:]`

### Solution

Click [here](solutions/strings/slicing_lvl_1.md) to view the solution.
15 changes: 15 additions & 0 deletions exercises/strings/slicing_lvl_2.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
## Slicing - Level 2

What is the result of each of the following statements if `x = 'abcdefgh'`

1. `x[-1:-2]`
2. `x[3:-1]`
3. `x[::1]`
4. `x[::3]`
5. `x[1:2:3]`
6. `x[::-1]`
7. `x[1::-1]`

### Solution

Click [here](solutions/strings/slicing_lvl_2.md) to view the solution.
12 changes: 12 additions & 0 deletions exercises/strings/variables.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
## Strings & Variables

Using the variable `fruit = "strawberry"` print the following:

1. `Best fruit is: strawberry`
2. `Yes, strawberry. Only strawberry`
3. `Hello, my name is Mr. Strawberry`
4. `fruit = 'strawberry'`

### Solution

Click [here](solutions/strings/variables.md) to view the solution.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
## What Exception is Raised?
## What Exception is Raised? - Level 1

For each of the following statement, specify what exception will be raised

Expand Down
9 changes: 9 additions & 0 deletions solutions/exceptions/what_exception_is_raised_lvl_2.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
## What Exception is Raised? - Level 2

For each of the following statement, specify what exception will be raised

1. `chars = "abc";chars[0]='h'`

### Solution

1. TypeError
13 changes: 13 additions & 0 deletions solutions/lists/what_is_the_result_lvl_1.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
## What is the result?

1. `'x = 'pizza,';print(x)`
2. `x = ('pizza',);print(x)`
3. `x = ('pizza');print(x)`
4. `x = 'pizza', 'pasta';print(x)`

### Solution

1. `pizza,`
2. `('pizza')`
3. `pizza`
4. `('pizza', 'pasta')`
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
## What is the result?
## What is the result? - Level 2

1. What is the result of the following block of code?

Expand Down
13 changes: 13 additions & 0 deletions solutions/loops/break_out.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
## Break Out

Write a loop that will keep reversing user's input until he enters the letter 'q' to quit

## Solution

```python
while True:
ans = input("Please insert a string to reverse: ")
if ans == 'q':
break
print(ans[::-1])
```
14 changes: 14 additions & 0 deletions solutions/loops/break_out_2.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
## Break Out 2

Iterate over a given list of numbers and break out of it if one the numbers is 2 or 4. If you didn't any of these numbers, print a message saying so.

### Solution

```python
for i in li:
if i == 2 or i == 4:
print("Found it!")
break
else:
print("Didn't find 2 nor 4")
```
11 changes: 11 additions & 0 deletions solutions/loops/every_char.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
## Every Character

Given a variable of a string, print character of the string on its own line.

### Solution

```python
string = "python"
for char in string:
print(char)
````
20 changes: 20 additions & 0 deletions solutions/loops/numbers_stream.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
## Stream of Numbers

1. Print the numbers between 0 and 10
2. Print the number between 0 and -10

### Solution

1.

```python
for i in range(0, 11):
print(i)
```

2.

```python
for i in range (0, -11, -1):
print(i)
```
7 changes: 7 additions & 0 deletions solutions/strings/change_strings.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
## Change Strings

Modify the string "cat" to "bat"

### Soltuion

`"cat".replace("c", "b")` OR `"b" + "cat"[1:]`
17 changes: 17 additions & 0 deletions solutions/strings/slicing_lvl_1.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
## Slicing - Level 1

What is the result of each of the following statements if `x = 'abcdefgh'`

1. `x[:]`
2. `x[2:]`
3. `x[2:2]`
4. `x[2:3]`
5. `x[-1:]`

### Solution

1. `'abcdefgh'`
2. `'cdefgh'`
3. `''`
4. `'c'`
5. `'h'`
21 changes: 21 additions & 0 deletions solutions/strings/slicing_lvl_2.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
## Slicing - Level 2

What is the result of each of the following statements if `x = 'abcdefgh'`

1. `x[-1:-2]`
2. `x[3:-1]`
3. `x[::1]`
4. `x[::3]`
5. `x[1:2:3]`
6. `x[::-1]`
7. `x[1::-1]`

### Solution

1. `''`
2. `'defg'`
3. `'abcdefgh'`
4. `'adg'`
5. `'b'`
6. `'hgfedcba'`
7. `'ba'`
15 changes: 15 additions & 0 deletions solutions/strings/variables.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
## Strings & Variables

Using the variable `fruit = "strawberry"` print the following:

1. `Best fruit is: strawberry`
2. `Yes, strawberry. Only strawberry`
3. `Hello, my name is Mr. Strawberry`
4. `fruit = 'strawberry'`

### Solution

1. `print(f'Best fruit is: {fruit}')`
2. `print(f'Yes, {fruit}. Only {fruit}')`
3. `print(f'Hello, my name is Mr. {fruit.capitalize()}')`
4. `print(f'{fruit = }')` # Supported only using Python 3.8+

0 comments on commit d4b77c1

Please sign in to comment.