Skip to content

Commit e095e34

Browse files
committed
new exercises
1 parent 1b3a18d commit e095e34

6 files changed

Lines changed: 194 additions & 2 deletions

File tree

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
## Development - Advanced, exercise 40
2+
3+
### Text
4+
A **rolling hash** function is a hash function able to convert an input string into an integer representing that string. It is based on multiplications and additions that depend on the number of characters in the input string. In particular, given a string *s* containing *n* characters, the hash value is computed by summing the multiplication involving the integer representation of each character and a coefficient *a* raised to the power of *n* minus the position number next to the current character, i.e.:
5+
6+
<img src="img/hash.png" alt="Hash function for strings" style="max-height:35px;" />
7+
8+
where *n* is the length of the input string *s*, *a* is a given constant value, *c<sub>0</sub>* is the integer representation of the character in position 0 of the input string *s*, *c<sub>1</sub>* is the integer representation of the character in position 1 of the input string *s*, *c<sub>2</sub>* is the integer representation of the character in position 2 of the input string *s*, etc.
9+
10+
Write an algorithm in Python – `def rolling_hash(s, a)` – which takes in input a string `s` representing a text and an integer value `a` representing a coefficient used in the formula above, and returns the hash value of that string computed as shown above. Python makes available the builtin function `ord` that takes in input a string of one character and returns the integer representation of that character – e.g. executing `ord("a")` will return the value `97`.
11+
12+
13+
### Solution
14+
```python
15+
# Test case for the function
16+
def test_rolling_hash(s, a, expected):
17+
result = rolling_hash(s, a)
18+
if result == expected:
19+
return True
20+
else:
21+
return False
22+
23+
24+
# Code of the function
25+
def rolling_hash(s, a):
26+
r = 0
27+
n = len(s)
28+
29+
for i, c in enumerate(s):
30+
r += ord(c) * a**(n-(i+1))
31+
32+
return r
33+
34+
35+
# Tests
36+
print(test_rolling_hash("ciao", 1, 412))
37+
print(test_rolling_hash("ciao", 2, 1517))
38+
print(test_rolling_hash("ciao", 3, 4020))
39+
```
40+
41+
### Additional material
42+
The runnable [Python file](exercise_40.py) is available online.
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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_rolling_hash(s, a, expected):
19+
result = rolling_hash(s, a)
20+
if result == expected:
21+
return True
22+
else:
23+
return False
24+
25+
26+
# Code of the function
27+
def rolling_hash(s, a):
28+
r = 0
29+
n = len(s)
30+
31+
for i, c in enumerate(s):
32+
r += ord(c) * a**(n-(i+1))
33+
34+
return r
35+
36+
37+
# Tests
38+
print(test_rolling_hash("ciao", 1, 412))
39+
print(test_rolling_hash("ciao", 2, 1517))
40+
print(test_rolling_hash("ciao", 3, 4020))
11.4 KB
Loading
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
## Understanding - Advanced, exercise 40
2+
3+
### Text
4+
The variables `my_given_name` and `my_family_name` contain, respectively, the strings of a given name and a family name in lowercase. What is the value returned by calling the function `ann` as shown as follows: `ann(my_given_name, my_family_name)`.
5+
6+
7+
```python
8+
def ann(given_name, family_name):
9+
if len(given_name) + len(family_name) > 0:
10+
d = {}
11+
for c in given_name:
12+
if c not in d:
13+
d[c] = 1
14+
else:
15+
d[c] = d[c] + 1
16+
17+
for c in family_name:
18+
if c in d:
19+
d[c] = d[c] - 1
20+
21+
idx = -1000
22+
for c in d:
23+
if d[c] > idx:
24+
idx = d[c]
25+
26+
p_char = set()
27+
for c in d:
28+
if d[c] == idx:
29+
p_char.add(c)
30+
31+
n_given_name = []
32+
for c in given_name:
33+
if c not in p_char:
34+
n_given_name.append(c)
35+
36+
n_family_name = []
37+
for c in family_name:
38+
if c not in p_char:
39+
n_family_name.append(c)
40+
print(idx)
41+
return idx + ann("".join(n_given_name), "".join(n_family_name))
42+
else:
43+
return 0
44+
```
45+
46+
### Hints
47+
The input of the function `ann` should be structured in a very specific way to see a reasonable result...
48+
49+
### Additional material
50+
The runnable [Python file](exercise_40.py) is available online. You can run it executing the command `python exercise_40.py` in a shell, and then following the instructions on screen to specify the intended input.
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
# -*- coding: utf-8 -*-
2+
# Copyright (c) 2023, 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+
from re import sub
17+
18+
19+
def ann(given_name, family_name):
20+
if len(given_name) + len(family_name) > 0:
21+
d = {}
22+
for c in given_name:
23+
if c not in d:
24+
d[c] = 1
25+
else:
26+
d[c] = d[c] + 1
27+
28+
for c in family_name:
29+
if c in d:
30+
d[c] = d[c] - 1
31+
32+
idx = -1000
33+
for c in d:
34+
if d[c] > idx:
35+
idx = d[c]
36+
37+
p_char = set()
38+
for c in d:
39+
if d[c] == idx:
40+
p_char.add(c)
41+
42+
n_given_name = []
43+
for c in given_name:
44+
if c not in p_char:
45+
n_given_name.append(c)
46+
47+
n_family_name = []
48+
for c in family_name:
49+
if c not in p_char:
50+
n_family_name.append(c)
51+
print(idx)
52+
return idx + ann("".join(n_given_name), "".join(n_family_name))
53+
else:
54+
return 0
55+
56+
57+
58+
my_given_name = sub(" +", "", input("Please provide your given name: ")).lower()
59+
my_family_name = sub(" +", "", input("Please provide your family name: ")).lower()
60+
print("Result:", ann(my_given_name, my_family_name))

index.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,14 +113,14 @@ The exercises of this section focus on figuring out the execution of a particula
113113

114114
* Beginner: [1](exercises/understanding/beginner/exercise-1), [2](exercises/understanding/beginner/exercise-2), [3](exercises/understanding/beginner/exercise-3), [4](exercises/understanding/beginner/exercise-4), [5](exercises/understanding/beginner/exercise-5), [6](exercises/understanding/beginner/exercise-6), [7](exercises/understanding/beginner/exercise-7), [8](exercises/understanding/beginner/exercise-8), [9](exercises/understanding/beginner/exercise-9), [10](exercises/understanding/beginner/exercise-10), [11](exercises/understanding/beginner/exercise-11), [12](exercises/understanding/beginner/exercise-12), [13](exercises/understanding/beginner/exercise-13), [14](exercises/understanding/beginner/exercise-14), [15](exercises/understanding/beginner/exercise-15), [16](exercises/understanding/beginner/exercise-16), [17](exercises/understanding/beginner/exercise-17), [18](exercises/understanding/beginner/exercise-18), [19](exercises/understanding/beginner/exercise-19)
115115
* Intermediate: [1](exercises/understanding/intermediate/exercise-1), [2](exercises/understanding/intermediate/exercise-2), [3](exercises/understanding/intermediate/exercise-3), [4](exercises/understanding/intermediate/exercise-4), [5](exercises/understanding/intermediate/exercise-5), [6](exercises/understanding/intermediate/exercise-6), [7](exercises/understanding/intermediate/exercise-7)
116-
* Advanced: [1](exercises/understanding/advanced/exercise-1), [2](exercises/understanding/advanced/exercise-2), [3](exercises/understanding/advanced/exercise-3), [4](exercises/understanding/advanced/exercise-4), [5](exercises/understanding/advanced/exercise-5), [6](exercises/understanding/advanced/exercise-6), [7](exercises/understanding/advanced/exercise-7), [8](exercises/understanding/advanced/exercise-8), [9](exercises/understanding/advanced/exercise-9), [10](exercises/understanding/advanced/exercise-10), [11](exercises/understanding/advanced/exercise-11), [12](exercises/understanding/advanced/exercise-12), [13](exercises/understanding/advanced/exercise-13), [14](exercises/understanding/advanced/exercise-14), [15](exercises/understanding/advanced/exercise-15), [16](exercises/understanding/advanced/exercise-16), [17](exercises/understanding/advanced/exercise-17), [18](exercises/understanding/advanced/exercise-18), [19](exercises/understanding/advanced/exercise-19), [20](exercises/understanding/advanced/exercise-20), [21](exercises/understanding/advanced/exercise-21), [22](exercises/understanding/advanced/exercise-22), [23](exercises/understanding/advanced/exercise-23), [24](exercises/understanding/advanced/exercise-24), [25](exercises/understanding/advanced/exercise-25), [26](exercises/understanding/advanced/exercise-26), [27](exercises/understanding/advanced/exercise-27), [28](exercises/understanding/advanced/exercise-28), [29](exercises/understanding/advanced/exercise-29), [30](exercises/understanding/advanced/exercise-30), [31](exercises/understanding/advanced/exercise-31), [32](exercises/understanding/advanced/exercise-32), [33](exercises/understanding/advanced/exercise-33), [34](exercises/understanding/advanced/exercise-34), [35](exercises/understanding/advanced/exercise-35), [36](exercises/understanding/advanced/exercise-36), [37](exercises/understanding/advanced/exercise-37), [38](exercises/understanding/advanced/exercise-38), [39](exercises/understanding/advanced/exercise-39)
116+
* Advanced: [1](exercises/understanding/advanced/exercise-1), [2](exercises/understanding/advanced/exercise-2), [3](exercises/understanding/advanced/exercise-3), [4](exercises/understanding/advanced/exercise-4), [5](exercises/understanding/advanced/exercise-5), [6](exercises/understanding/advanced/exercise-6), [7](exercises/understanding/advanced/exercise-7), [8](exercises/understanding/advanced/exercise-8), [9](exercises/understanding/advanced/exercise-9), [10](exercises/understanding/advanced/exercise-10), [11](exercises/understanding/advanced/exercise-11), [12](exercises/understanding/advanced/exercise-12), [13](exercises/understanding/advanced/exercise-13), [14](exercises/understanding/advanced/exercise-14), [15](exercises/understanding/advanced/exercise-15), [16](exercises/understanding/advanced/exercise-16), [17](exercises/understanding/advanced/exercise-17), [18](exercises/understanding/advanced/exercise-18), [19](exercises/understanding/advanced/exercise-19), [20](exercises/understanding/advanced/exercise-20), [21](exercises/understanding/advanced/exercise-21), [22](exercises/understanding/advanced/exercise-22), [23](exercises/understanding/advanced/exercise-23), [24](exercises/understanding/advanced/exercise-24), [25](exercises/understanding/advanced/exercise-25), [26](exercises/understanding/advanced/exercise-26), [27](exercises/understanding/advanced/exercise-27), [28](exercises/understanding/advanced/exercise-28), [29](exercises/understanding/advanced/exercise-29), [30](exercises/understanding/advanced/exercise-30), [31](exercises/understanding/advanced/exercise-31), [32](exercises/understanding/advanced/exercise-32), [33](exercises/understanding/advanced/exercise-33), [34](exercises/understanding/advanced/exercise-34), [35](exercises/understanding/advanced/exercise-35), [36](exercises/understanding/advanced/exercise-36), [37](exercises/understanding/advanced/exercise-37), [38](exercises/understanding/advanced/exercise-38), [39](exercises/understanding/advanced/exercise-39), [40](exercises/understanding/advanced/exercise-40)
117117

118118
### Development
119119
The exercises of this section focus on creating a Python function according to a natural language description of its input, output, and behaviour. The goal is to test a user to write a program that can be interpreted by an electronic computer. All the exercises of this section are accompanied by the related executable code, which includes also the tests created as part of the test-driven development methodology introduced in [chapter "Programming Language"](book/04.pdf). This chapter also introduces a possible methodology to follow for the development of an algorithm as a Python function, that can be used as basis to solve the exercises in this section.
120120

121121
* Beginner: [1](exercises/development/beginner/exercise-1), [2](exercises/development/beginner/exercise-2), [3](exercises/development/beginner/exercise-3), [4](exercises/development/beginner/exercise-4), [5](exercises/development/beginner/exercise-5), [6](exercises/development/beginner/exercise-6), [7](exercises/development/beginner/exercise-7), [8](exercises/development/beginner/exercise-8), [9](exercises/development/beginner/exercise-9), [10](exercises/development/beginner/exercise-10), [11](exercises/development/beginner/exercise-11), [12](exercises/development/beginner/exercise-12), [13](exercises/development/beginner/exercise-13), [14](exercises/development/beginner/exercise-14), [15](exercises/development/beginner/exercise-15), [16](exercises/development/beginner/exercise-16), [17](exercises/development/beginner/exercise-17), [18](exercises/development/beginner/exercise-18), [19](exercises/development/beginner/exercise-19), [20](exercises/development/beginner/exercise-20), [21](exercises/development/beginner/exercise-21), [22](exercises/development/beginner/exercise-22), [23](exercises/development/beginner/exercise-23), [24](exercises/development/beginner/exercise-24), [25](exercises/development/beginner/exercise-25), [26](exercises/development/beginner/exercise-26), [27](exercises/development/beginner/exercise-27), [28](exercises/development/beginner/exercise-28), [29](exercises/development/beginner/exercise-29), [30](exercises/development/beginner/exercise-30)
122122
* Intermediate: [1](exercises/development/intermediate/exercise-1), [2](exercises/development/intermediate/exercise-2), [3](exercises/development/intermediate/exercise-3), [4](exercises/development/intermediate/exercise-4), [5](exercises/development/intermediate/exercise-5), [6](exercises/development/intermediate/exercise-6), [7](exercises/development/intermediate/exercise-7), [8](exercises/development/intermediate/exercise-8)
123-
* Advanced: [1](exercises/development/advanced/exercise-1), [2](exercises/development/advanced/exercise-2), [3](exercises/development/advanced/exercise-3), [4](exercises/development/advanced/exercise-4), [5](exercises/development/advanced/exercise-5), [6](exercises/development/advanced/exercise-6), [7](exercises/development/advanced/exercise-7), [8](exercises/development/advanced/exercise-8), [9](exercises/development/advanced/exercise-9), [10](exercises/development/advanced/exercise-10), [11](exercises/development/advanced/exercise-11), [12](exercises/development/advanced/exercise-12), [13](exercises/development/advanced/exercise-13), [14](exercises/development/advanced/exercise-14), [15](exercises/development/advanced/exercise-15), [16](exercises/development/advanced/exercise-16), [17](exercises/development/advanced/exercise-17), [18](exercises/development/advanced/exercise-18), [19](exercises/development/advanced/exercise-19), [20](exercises/development/advanced/exercise-20), [21](exercises/development/advanced/exercise-21), [22](exercises/development/advanced/exercise-22), [23](exercises/development/advanced/exercise-23), [24](exercises/development/advanced/exercise-24), [25](exercises/development/advanced/exercise-25), [26](exercises/development/advanced/exercise-26), [27](exercises/development/advanced/exercise-27), [28](exercises/development/advanced/exercise-28), [29](exercises/development/advanced/exercise-29), [30](exercises/development/advanced/exercise-30), [31](exercises/development/advanced/exercise-31), [32](exercises/development/advanced/exercise-32), [33](exercises/development/advanced/exercise-33), [34](exercises/development/advanced/exercise-34), [35](exercises/development/advanced/exercise-35), [36](exercises/development/advanced/exercise-36), [37](exercises/development/advanced/exercise-37), [38](exercises/development/advanced/exercise-38), [39](exercises/development/advanced/exercise-39)
123+
* Advanced: [1](exercises/development/advanced/exercise-1), [2](exercises/development/advanced/exercise-2), [3](exercises/development/advanced/exercise-3), [4](exercises/development/advanced/exercise-4), [5](exercises/development/advanced/exercise-5), [6](exercises/development/advanced/exercise-6), [7](exercises/development/advanced/exercise-7), [8](exercises/development/advanced/exercise-8), [9](exercises/development/advanced/exercise-9), [10](exercises/development/advanced/exercise-10), [11](exercises/development/advanced/exercise-11), [12](exercises/development/advanced/exercise-12), [13](exercises/development/advanced/exercise-13), [14](exercises/development/advanced/exercise-14), [15](exercises/development/advanced/exercise-15), [16](exercises/development/advanced/exercise-16), [17](exercises/development/advanced/exercise-17), [18](exercises/development/advanced/exercise-18), [19](exercises/development/advanced/exercise-19), [20](exercises/development/advanced/exercise-20), [21](exercises/development/advanced/exercise-21), [22](exercises/development/advanced/exercise-22), [23](exercises/development/advanced/exercise-23), [24](exercises/development/advanced/exercise-24), [25](exercises/development/advanced/exercise-25), [26](exercises/development/advanced/exercise-26), [27](exercises/development/advanced/exercise-27), [28](exercises/development/advanced/exercise-28), [29](exercises/development/advanced/exercise-29), [30](exercises/development/advanced/exercise-30), [31](exercises/development/advanced/exercise-31), [32](exercises/development/advanced/exercise-32), [33](exercises/development/advanced/exercise-33), [34](exercises/development/advanced/exercise-34), [35](exercises/development/advanced/exercise-35), [36](exercises/development/advanced/exercise-36), [37](exercises/development/advanced/exercise-37), [38](exercises/development/advanced/exercise-38), [39](exercises/development/advanced/exercise-39), [40](exercises/development/advanced/exercise-40)
124124

125125

126126
## Additional material

0 commit comments

Comments
 (0)