Skip to content

Commit 47b4b09

Browse files
author
abregman
committed
Add a couple of exercises
1 parent 90ece0d commit 47b4b09

19 files changed

+222
-0
lines changed

README.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,18 @@
3939
|Name|Objective & Instructions|Solution|Comments|
4040
|--------|--------|------|----|
4141
| 22 times | [Exercise](exercises/hello_world/22_times.md) | | |
42+
| What is the result? | [Exercise](exercises/strings/what_is_the_result.md) | [Solution](solutions/strings/what_is_the_result.md) | |
4243
| Starts with a letter | [Exercise](exercises/strings/letter_start.md) | [Solution](solutions/strings/letter_start.md) | |
4344
| All Digits | [Exercise](exercises/strings/all_digits.md) | [Solution](solutions/strings/all_digits.md) | |
4445
| Removing Characters | [Exercise](exercises/strings/removing_characters.md) | [Solution](solutions/strings/removing_characters.md) | |
46+
| Reverse a String | [Exercise](exercises/strings/reverse_string.md) | [Solution](solutions/strings/reverse_string.md) | |
47+
| Compress Strings | [Exercise](exercises/strings/compress_strings.md) | | |
48+
49+
## Numbers
50+
51+
|Name|Objective & Instructions|Solution|Comments|
52+
|--------|--------|------|----|
53+
| Palindrome | [Exercise](exercises/numbers/palindrome.md) | [Solution](solutions/numbers/palindrome.md) | |
4554

4655
## Lists
4756

@@ -50,6 +59,9 @@
5059
| Access List Items | [Exercise](exercises/hello_world/access_list_items.md) | [Solution](solutions/hello_world/access_list_items.md) | |
5160
| Equal Lists | [Exercise](exercises/lists/equal_lists.md) | [Solution](solutions/lists/equal_lists.md) | |
5261
| Length | [Exercise](exercises/hello_world/length.md) | | |
62+
| Min Max | [Exercise](exercises/lists/min_max.md) | [Solution](solutions/lists/min_max.md) | |
63+
| Three Biggest Items | [Exercise](exercises/lists/three_biggest_items.md) | [Solution](solutions/lists/three_biggest_items.md) | |
64+
| What is the result? | [Exercise](exercises/lists/what_is_the_result.md) | [Solution](solutions/lists/what_is_the_result.md) | |
5365

5466
## Loops
5567

@@ -61,7 +73,9 @@
6173

6274
|Name|Objective & Instructions|Solution|Comments|
6375
|--------|--------|------|----|
76+
| My First Function | [Exercise](exercises/functions/my_first_function.md) | [Solution](solutions/functions/my_first_function.md) | |
6477
| Calculator | [Exercise](exercises/functions/calculator.md) | [Solution](solutions/functions/calculator.md) | |
78+
| First Class Objects | [Exercise](exercises/functions/first_class_objects.md) | [Solution](solutions/functions/first_class_objects.md) | |
6579

6680
## Magic Methods
6781

@@ -104,6 +118,12 @@
104118
| File exists | [Exercise](exercises/os/file_exists.md) | | |
105119
| Print all the files in a directory | [Exercise](exercises/os/print_all_files_in_dir.md) | | |
106120

121+
## Improve the Code
122+
123+
|Name|Objective & Instructions|Solution|Comments|
124+
|--------|--------|------|----|
125+
| Vowel Letters | [Exercise](exercises/improve_the_code/vowel_letters.md) | [Solution](solutions/improve_the_code/vowel_letters.md) | |
126+
107127
## Misc
108128

109129
|Name|Objective & Instructions|Solution|Comments|
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
## First Class Objects
2+
3+
1. In Python, functions are first-class objects. What does it mean?
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
## My First Function
2+
3+
1. Write a function to print "Hello World"
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
## Vowel Letters
2+
3+
1. Improve the following code
4+
5+
```python
6+
char = input("Insert a character: ")
7+
if char == "a" or char == "o" or char == "e" or char =="u" or char == "i":
8+
print("It's a vowel!")
9+
```

exercises/lists/min_max.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
## Lists - Min Max
2+
3+
How do you get the maximum and minimum values from a list?
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
## Three Biggest Items
2+
3+
1. Print three biggest items of a list

exercises/numbers/palindrome.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
## Palindrome
2+
3+
1. Write code that determines if a number is a Palindrome

exercises/strings/compress_strings.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
## Compress Strings
2+
3+
1. Write code that compresses a string
4+
- 'aaaabbccc' -> 'a4b2c3'
5+
- 'abbbc' -> 'a1b3c1'
6+
2. Write code that decompresses a string
7+
- 'a4b2c3' -> 'aaaabbccc'
8+
- 'a1b3c1' -> 'abbbc'

exercises/strings/reverse_string.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
## Reverse String
2+
3+
1. Reverse a string
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
## What is the result?
2+
3+
1. What is the result of each of the following statements?
4+
5+
- "abc"*3
6+
- "abc"*2.5
7+
- "abc"*2.0
8+
- "abc"*True
9+
- "abc"*False
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
## First Class Objects
2+
3+
1. In Python, functions are first-class objects. What does it mean?
4+
5+
### Solution
6+
7+
In general, first class objects in programming languages are objects which can be assigned to variable, used as a return value and can be used as arguments or parameters.<br>
8+
In python you can treat functions this way. Let's say we have the following function
9+
10+
```
11+
def my_function():
12+
return 5
13+
```
14+
15+
You can then assign a function to a variables like this `x = my_function` or you can return functions as return values like this `return my_function`
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
## My First Function
2+
3+
1. Write a function to print "Hello World"
4+
5+
### Solution
6+
7+
```python
8+
def print_hello_world():
9+
print("hello world")
10+
```
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
## Vowel Letters
2+
3+
1. Improve the following code
4+
5+
```python
6+
char = input("Insert a character: ")
7+
if char == "a" or char == "o" or char == "e" or char =="u" or char == "i":
8+
print("It's a vowel!")
9+
```
10+
11+
### Solution
12+
13+
```python
14+
char = input("Insert a character: ") # For readablity
15+
if lower(char[0]) in "aieou": # Takes care of multiple characters and separate cases
16+
print("It's a vowel!")
17+
```
18+
OR
19+
```python
20+
if lower(input("Insert a character: ")[0]) in "aieou": # Takes care of multiple characters and small/Capital cases
21+
print("It's a vowel!")
22+
```

solutions/lists/min_max.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
## Lists - Min Max
2+
3+
How do you get the maximum and minimum values from a list?
4+
5+
### Solution
6+
7+
Maximum: max(some_list)
8+
Minimum: min(some_list)
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
## Three Biggest Items
2+
3+
1. Print three biggest items of a list
4+
5+
### Solution
6+
7+
```python
8+
sorted(some_list, reverse=True)[:3]
9+
```
10+
11+
Or
12+
13+
```python
14+
some_list.sort(reverse=True)
15+
some_list[:3]
16+
```

solutions/lists/what_is_the_result.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
## What is the result?
2+
3+
1. What is the result of the following block of code?
4+
5+
```python
6+
x = ['a', 'b', 'c']
7+
for i in x:
8+
if i == 'b':
9+
x = ['z', 'y']
10+
print(i)
11+
```
12+
13+
### Solution
14+
15+
```
16+
a
17+
b
18+
c
19+
```

solutions/numbers/palindrome.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
## Palindrome
2+
3+
1. Write code that determines if a number is a Palindrome
4+
5+
## Solution
6+
7+
1.
8+
9+
```
10+
from typing import Union
11+
12+
def isNumberPalindrome(number: Union[int, str]) -> bool:
13+
if isinstance(number, int):
14+
number = str(number)
15+
return number == number[::-1]
16+
17+
print(isNumberPalindrome("12321"))
18+
```
19+
20+
- Using Python3.10 that accepts using bitwise operator '|'.
21+
22+
```
23+
def isNumberPalindrome(number: int | str) -> bool:
24+
if isinstance(number, int):
25+
number = str(number)
26+
return number == number[::-1]
27+
28+
print(isNumberPalindrome("12321"))
29+
```
30+
31+
Note: Using slicing to reverse a list could be slower than other options like `reversed` that return an iterator.

solutions/strings/reverse_string.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
## Reverse String
2+
3+
1. Reverse a string
4+
5+
### Solution
6+
7+
```
8+
my_string[::-1]
9+
```
10+
11+
A more visual way is:<br>
12+
<i>Careful: this is very slow</i>
13+
14+
```
15+
def reverse_string(string):
16+
temp = ""
17+
for char in string:
18+
temp = char + temp
19+
return temp
20+
```
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
## What is the result?
2+
3+
1. What is the result of each of the following statements?
4+
5+
- "abc"*3
6+
- "abc"*2.5
7+
- "abc"*2.0
8+
- "abc"*True
9+
- "abc"*False
10+
11+
### Solution
12+
13+
* abcabcabc
14+
* TypeError
15+
* TypeError
16+
* "abc"
17+
* ""

0 commit comments

Comments
 (0)