You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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
+
<imgsrc="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
+
deftest_rolling_hash(s, a, expected):
17
+
result = rolling_hash(s, a)
18
+
if result == expected:
19
+
returnTrue
20
+
else:
21
+
returnFalse
22
+
23
+
24
+
# Code of the function
25
+
defrolling_hash(s, a):
26
+
r =0
27
+
n =len(s)
28
+
29
+
for i, c inenumerate(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.
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)`.
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.
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.
0 commit comments