Skip to content

Commit e5cac66

Browse files
committed
new exercises
1 parent 159d64a commit e5cac66

19 files changed

Lines changed: 650 additions & 15 deletions
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
## Development - Advanced, exercise 32
2+
3+
### Text
4+
A **primality test** is an algorithm for determining whether an input integer greater than zero is prime. The simplest primality test is trial division: given an input number `n`, one must check whether it is divisible by any number between `2` and `√n` (square root of `n`). If so, then `n` is composite; otherwise, it is prime.
5+
6+
Write an algorithm in Python – `def is_prime(n)` – which takes a positive integer `n` and returns `True` if `n` is prime, otherwise it returns `False`. In the implementation, you must follow strictly the approach introduced above (i.e. checking whether `n` is divisible by any number between `2` and `√n`). Hint: to compute the square root of a number in Python, the function `sqrt` of the library math can be used – please remember to import it in the code. Some examples of execution:
7+
8+
* `is_prime(1)` will return `True`
9+
* `is_prime(2)` will return `True`
10+
* `is_prime(3)` will return `True`
11+
* `is_prime(4)` will return `False`
12+
* `is_prime(5)` will return `True`
13+
* `is_prime(6)` will return `False`
14+
15+
### Solution
16+
```python
17+
from math import sqrt
18+
19+
# Test case for the function
20+
def test_is_prime(n, expected):
21+
result = is_prime(n)
22+
if result is expected:
23+
return True
24+
else:
25+
return False
26+
27+
28+
# Code of the function
29+
def is_prime(n):
30+
d = 2
31+
32+
while d <= sqrt(n):
33+
if n % d == 0:
34+
return False
35+
d += 1
36+
37+
return True
38+
39+
40+
# Tests
41+
print(test_is_prime(1, True))
42+
print(test_is_prime(2, True))
43+
print(test_is_prime(3, True))
44+
print(test_is_prime(4, False))
45+
print(test_is_prime(5, True))
46+
print(test_is_prime(6, False))
47+
print(test_is_prime(17, True))
48+
print(test_is_prime(22, False))
49+
```
50+
51+
### Additional material
52+
The runnable [Python file](exercise_32.py) is available online.
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
## Development - Advanced, exercise 33
2+
3+
### Text
4+
The algorithm **globbing** is an algorithm for matching wildcards, which is useful when comparing text strings that may contain wildcard syntax. The most used wildcard is "*" and represent zero or more characters. Examples of use of wildcard search are listed as follows:
5+
6+
* `*foo*` matches any string containing `"foo"`, e.g. `"foo"`, `"fidafoo"`, `"farfoofi"`, `"footer"`;
7+
* `fee*` matches any string that begins with `"fee"`, e.g. `"fee"`, `"feedoo"`;
8+
* `*fae` matches any string that ends with `"fae"`, e.g. `"fae"`, `"fafae"`;
9+
* `doe` (i.e. no wildcards used) matches exactly with the string `"doe"`.
10+
11+
Write an algorithm in Python – `def glob(p, s)` – which takes a string `p` representing the pattern (that may includes zero or more wildcards "*") and another string `s` on which searching for the pattern, and returns `True` if `p` is matched in `s`, otherwise it returns `False`.
12+
13+
### Solution
14+
```python
15+
# Test case for the function
16+
def test_glob(p, s, expected):
17+
result = glob(p, s)
18+
if result is expected:
19+
return True
20+
else:
21+
return False
22+
23+
24+
# Code of the function
25+
def glob(p, s):
26+
matches = p.split("*")
27+
28+
for idx, pattern in enumerate(matches):
29+
if pattern in s:
30+
pos = s.index(pattern)
31+
32+
if idx == len(matches) - 1 and pattern == "":
33+
new_start = len(s)
34+
elif idx > 0:
35+
new_start = pos + len(pattern)
36+
elif pos == 0:
37+
new_start = len(pattern)
38+
else:
39+
return False
40+
41+
s = s[new_start:]
42+
else:
43+
return False
44+
45+
return s == ""
46+
47+
48+
49+
# Tests
50+
print(test_glob("*foo*", "foo", True))
51+
print(test_glob("*foo*", "fidafoo", True))
52+
print(test_glob("*foo*", "farfoofi", True))
53+
print(test_glob("*foo*", "footer", True))
54+
print(test_glob("*foo*", "fee", False))
55+
print(test_glob("fee*", "fee", True))
56+
print(test_glob("fee*", "feedoo", True))
57+
print(test_glob("fee*", "fooee", False))
58+
print(test_glob("*fae", "fae", True))
59+
print(test_glob("*fae", "fafae", True))
60+
print(test_glob("*fae", "fafaefa", False))
61+
print(test_glob("*fae", "fee", False))
62+
print(test_glob("doe", "doe", True))
63+
print(test_glob("doe", "john doe", False))
64+
print(test_glob("*pa*pe*", "paperino", True))
65+
print(test_glob("*pa*foo*", "paparfoo", True))
66+
print(test_glob("*pa*po*foo*", "papopaporfoo", True))
67+
print(test_glob("*pa*foo*", "paparfoo", True))
68+
```
69+
70+
### Additional material
71+
The runnable [Python file](exercise_33.py) is available online.
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# -*- coding: utf-8 -*-
2+
# Copyright (c) 2022, Silvio Peroni <essepuntato@gmail.com>
3+
#
4+
# Permission to use, copy, modify, and/or distribute this software for any purpose
5+
# with or without fee is hereby granted, provided that the above copyright notice
6+
# and this permission notice appear in all copies.
7+
#
8+
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
9+
# REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
10+
# FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT,
11+
# OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
12+
# DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
13+
# ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
14+
# SOFTWARE.
15+
16+
17+
from math import sqrt
18+
19+
# Test case for the function
20+
def test_is_prime(n, expected):
21+
result = is_prime(n)
22+
if result is expected:
23+
return True
24+
else:
25+
return False
26+
27+
28+
# Code of the function
29+
def is_prime(n):
30+
d = 2
31+
32+
while d <= sqrt(n):
33+
if n % d == 0:
34+
return False
35+
d += 1
36+
37+
return True
38+
39+
40+
# Tests
41+
print(test_is_prime(1, True))
42+
print(test_is_prime(2, True))
43+
print(test_is_prime(3, True))
44+
print(test_is_prime(4, False))
45+
print(test_is_prime(5, True))
46+
print(test_is_prime(6, False))
47+
print(test_is_prime(17, True))
48+
print(test_is_prime(22, False))
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
# -*- coding: utf-8 -*-
2+
# Copyright (c) 2022, Silvio Peroni <essepuntato@gmail.com>
3+
#
4+
# Permission to use, copy, modify, and/or distribute this software for any purpose
5+
# with or without fee is hereby granted, provided that the above copyright notice
6+
# and this permission notice appear in all copies.
7+
#
8+
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
9+
# REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
10+
# FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT,
11+
# OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
12+
# DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
13+
# ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
14+
# SOFTWARE.
15+
16+
17+
# Test case for the function
18+
def test_glob(p, s, expected):
19+
result = glob(p, s)
20+
if result is expected:
21+
return True
22+
else:
23+
return False
24+
25+
26+
# Code of the function
27+
def glob(p, s):
28+
matches = p.split("*")
29+
30+
for idx, pattern in enumerate(matches):
31+
if pattern in s:
32+
pos = s.index(pattern)
33+
34+
if idx == len(matches) - 1 and pattern == "":
35+
new_start = len(s)
36+
elif idx > 0:
37+
new_start = pos + len(pattern)
38+
elif pos == 0:
39+
new_start = len(pattern)
40+
else:
41+
return False
42+
43+
s = s[new_start:]
44+
else:
45+
return False
46+
47+
return s == ""
48+
49+
50+
51+
# Tests
52+
print(test_glob("*foo*", "foo", True))
53+
print(test_glob("*foo*", "fidafoo", True))
54+
print(test_glob("*foo*", "farfoofi", True))
55+
print(test_glob("*foo*", "footer", True))
56+
print(test_glob("*foo*", "fee", False))
57+
print(test_glob("fee*", "fee", True))
58+
print(test_glob("fee*", "feedoo", True))
59+
print(test_glob("fee*", "fooee", False))
60+
print(test_glob("*fae", "fae", True))
61+
print(test_glob("*fae", "fafae", True))
62+
print(test_glob("*fae", "fafaefa", False))
63+
print(test_glob("*fae", "fee", False))
64+
print(test_glob("doe", "doe", True))
65+
print(test_glob("doe", "john doe", False))
66+
print(test_glob("*pa*pe*", "paperino", True))
67+
print(test_glob("*pa*foo*", "paparfoo", True))
68+
print(test_glob("*pa*po*foo*", "papopaporfoo", True))
69+
print(test_glob("*pa*foo*", "paparfoo", True))

exercises/development/beginner/exercise-28.md

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,25 +6,27 @@ Write down a small function in Python that takes in input a string and a non-neg
66
### Solution
77
```python
88
# Test case for the function
9-
def test_f(i, expected):
10-
result = f(i)
9+
def test_f(s, i, expected):
10+
result = f(s, i)
1111
if expected == result:
1212
return True
1313
else:
1414
return False
1515

1616

1717
# Code of the function
18-
def f(i):
19-
return i % 2 == 0
18+
def f(s, i):
19+
if i < len(s):
20+
return s[i] in "aeiou"
21+
else:
22+
return False
2023

2124

2225
# Tests
23-
print(test_f(2, True))
24-
print(test_f(3, False))
25-
print(test_f(0, True))
26-
print(test_f(7, False))
27-
print(test_f(108, True))
26+
print(test_f("ciao", 2, True))
27+
print(test_f("ciao", 0, False))
28+
print(test_f("ciao", 1, True))
29+
print(test_f("ciao", 7, False))
2830
```
2931

3032
### Additional material
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
## Development - Beginner, exercise 29
2+
3+
### Text
4+
Write down a small function in Python that takes in input two strings and returns `True` if they are identical, `False` if they are not identical but contains the same number of characters, otherwise it returns the shorter one.
5+
6+
### Solution
7+
```python
8+
# Test case for the function
9+
def test_f(s, t, expected):
10+
result = f(s, t)
11+
if expected == result:
12+
return True
13+
else:
14+
return False
15+
16+
17+
# Code of the function
18+
def f(s, t):
19+
if s == t:
20+
return True
21+
elif len(s) == len(t):
22+
return False
23+
elif len(s) < len(t):
24+
return s
25+
else:
26+
return t
27+
28+
29+
# Tests
30+
print(test_f("ciao", "ciao", True))
31+
print(test_f("ciao", "oaic", False))
32+
print(test_f("ciao", "me", "me"))
33+
print(test_f("me", "ciao", "me"))
34+
```
35+
36+
### Additional material
37+
The runnable [Python file](exercise_29.py) is available online.
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
## Development - Beginner, exercise 30
2+
3+
### Text
4+
Write down a small function in Python that takes in input two integers and returns `0` if they are equal, `-1` if the first is divisible by the second (i.e. the rest of the division is `0`), `1` if the second is divisible by the first (i.e. the rest of the division is `0`), otherwise it returns `None`.
5+
6+
### Solution
7+
```python
8+
def test_f(x, y, expected):
9+
result = f(x, y)
10+
if expected == result:
11+
return True
12+
else:
13+
return False
14+
15+
16+
# Code of the function
17+
def f(x, y):
18+
if x == y:
19+
return 0
20+
elif x % y == 0:
21+
return -1
22+
elif y % x == 0:
23+
return 1
24+
else:
25+
return None
26+
27+
28+
# Tests
29+
print(test_f(5, 5, 0))
30+
print(test_f(5, 10, 1))
31+
print(test_f(10, 5, -1))
32+
print(test_f(5, 7, None))
33+
```
34+
35+
### Additional material
36+
The runnable [Python file](exercise_30.py) is available online.

0 commit comments

Comments
 (0)