Skip to content

Commit 52027d5

Browse files
committed
fixes and exercises
1 parent 5784390 commit 52027d5

11 files changed

Lines changed: 512 additions & 0 deletions

File tree

book/06.pdf

41.8 KB
Binary file not shown.
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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_check_issn(issn_string, expected):
19+
result = check_issn(issn_string)
20+
21+
if result == expected:
22+
return True
23+
else:
24+
return False
25+
26+
27+
# Code of the function
28+
def check_issn(issn_string):
29+
clean_string = issn_string.replace("-", "")
30+
31+
sum = 0
32+
for i, c in enumerate(clean_string[:7]):
33+
sum += int(c) * (8 - i)
34+
35+
rem = sum % 11
36+
37+
if rem == 0:
38+
return clean_string[7] == str(rem)
39+
else:
40+
rem = 11 - rem
41+
if rem == 10:
42+
return clean_string[7].upper() == "X"
43+
else:
44+
return clean_string[7] == str(rem)
45+
46+
47+
48+
# Tests
49+
print(test_check_issn("2055-7671", True))
50+
print(test_check_issn("2055-768X", True))
51+
print(test_check_issn("2632-3834", True))
52+
print(test_check_issn("1570-8268", True))
53+
print(test_check_issn("2641-3337", True))
54+
print(test_check_issn("2055-767X", False))
55+
print(test_check_issn("2055-7684", False))
56+
print(test_check_issn("2632-3838", False))
57+
print(test_check_issn("1570-8267", False))
58+
print(test_check_issn("2641-3331", False))
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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_jump_search(olist, s, b, expected):
19+
result = jump_search(olist, s, b)
20+
21+
if result == expected:
22+
return True
23+
else:
24+
return False
25+
26+
27+
# Code of the function
28+
def jump_search(olist, s, b):
29+
l_len = len(olist)
30+
31+
for start in range(0, l_len, b):
32+
stop = start + b - 1
33+
if stop >= l_len:
34+
stop = l_len - 1
35+
36+
if olist[stop] >= s:
37+
for idx in range(start, stop + 1):
38+
if olist[idx] == s:
39+
return idx
40+
41+
return None
42+
43+
return None
44+
45+
46+
# Tests
47+
print(test_jump_search([], 2, 3, None))
48+
print(test_jump_search([1], 2, 3, None))
49+
print(test_jump_search([2], 2, 3, 0))
50+
print(test_jump_search([1, 2], 2, 3, 1))
51+
print(test_jump_search([1, 2, 2, 4], 2, 3, 1))
52+
print(test_jump_search([1, 2, 2, 4, 5], 5, 3, 4))
53+
print(test_jump_search([1, 2, 2, 4, 5, 5], 5, 3, 4))
54+
print(test_jump_search([1, 2, 2, 4, 5, 6], 6, 3, 5))
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
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+
from networkx import Graph
17+
18+
# Test case for the function
19+
def test_greedy_colouring(g, expected):
20+
result = greedy_colouring(g)
21+
22+
if result == expected:
23+
return True
24+
else:
25+
return False
26+
27+
28+
# Code of the function
29+
def greedy_colouring(g):
30+
result = dict()
31+
32+
for node in g.nodes:
33+
colour = 0
34+
used = set()
35+
36+
for nei in g.neighbors(node):
37+
if nei in result:
38+
used.add(result[nei])
39+
40+
while colour in used:
41+
colour += 1
42+
43+
result[node] = colour
44+
45+
return result
46+
47+
48+
# Tests
49+
my_g = Graph()
50+
my_g.add_edge(1, 2)
51+
my_g.add_edge(2, 3)
52+
my_g.add_edge(3, 4)
53+
my_g.add_edge(4, 5)
54+
my_g.add_edge(5, 1)
55+
my_g.add_edge(1, 6)
56+
my_g.add_edge(2, 7)
57+
my_g.add_edge(3, 8)
58+
my_g.add_edge(4, 9)
59+
my_g.add_edge(5, 10)
60+
my_g.add_edge(6, 8)
61+
my_g.add_edge(6, 9)
62+
my_g.add_edge(7, 9)
63+
my_g.add_edge(7, 10)
64+
my_g.add_edge(8, 10)
65+
66+
result = {
67+
1: 0,
68+
2: 1,
69+
3: 0,
70+
4: 1,
71+
5: 2,
72+
6: 1,
73+
7: 0,
74+
8: 2,
75+
9: 2,
76+
10: 1
77+
}
78+
79+
print(test_greedy_colouring(my_g, result))
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
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+
# Test case for the function
17+
def test_odd_even(i_list, expected):
18+
result = odd_even(i_list)
19+
if result == expected:
20+
return True
21+
else:
22+
return False
23+
24+
25+
# Code of the function
26+
def odd_even(i_list):
27+
swapped = True
28+
29+
while swapped:
30+
swapped = False
31+
32+
for idx in range(1, len(i_list) - 1, 2):
33+
if i_list[idx] > i_list[idx + 1]:
34+
swap(i_list, idx, idx + 1)
35+
swapped = True
36+
37+
for idx in range(0, len(i_list) - 1, 2):
38+
if i_list[idx] > i_list[idx + 1]:
39+
swap(i_list, idx, idx + 1)
40+
swapped = True
41+
42+
return i_list
43+
44+
45+
def swap(i_list, i1, i2):
46+
tmp = i_list[i1]
47+
i_list[i1] = i_list[i2]
48+
i_list[i2] = tmp
49+
50+
51+
# Tests
52+
print(test_odd_even([5, 4, 1, 2, 7], [1, 2, 4, 5, 7]))
53+
print(test_odd_even([], []))
54+
print(test_odd_even([1], [1]))
55+
print(test_odd_even([5, 4, 1, 2, 7, 5, 3, 1, 7], [1, 1, 2, 3, 4, 5, 5, 7, 7]))
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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+
# Test case for the function
17+
def test_counting_sort(i_list, expected):
18+
result = counting_sort(i_list)
19+
if result == expected:
20+
return True
21+
else:
22+
return False
23+
24+
25+
# Code of the function
26+
def counting_sort(i_list):
27+
if len(i_list) > 0:
28+
tmp_list = []
29+
min_value = min(i_list)
30+
tmp_len = max(i_list) + 1 - min_value
31+
while tmp_len > 0:
32+
tmp_list.append(0)
33+
tmp_len -= 1
34+
35+
for item in i_list:
36+
tmp_list[item - min_value] += 1
37+
38+
ridx = 0
39+
for idx, item in enumerate(tmp_list):
40+
while item > 0:
41+
i_list[ridx] = idx + min_value
42+
ridx += 1
43+
item -= 1
44+
45+
return i_list
46+
47+
48+
# Tests
49+
print(test_counting_sort([5, 4, 1, 2, 7], [1, 2, 4, 5, 7]))
50+
print(test_counting_sort([], []))
51+
print(test_counting_sort([1], [1]))
52+
print(test_counting_sort([5, 4, 1, 2, 7, 5, 3, 1, 7], [1, 1, 2, 3, 4, 5, 5, 7, 7]))
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) 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 famnam(f_name):
20+
a = dict()
21+
b = dict()
22+
23+
for c in f_name:
24+
if c in "aeiou":
25+
if c not in a:
26+
a[c] = 0
27+
a[c] += 1
28+
else:
29+
if c not in b:
30+
b[c] = 0
31+
b[c] += 1
32+
33+
ma = 0
34+
mb = 0
35+
for c in f_name:
36+
if c in "aeiou":
37+
if a[c] > ma:
38+
ma = a[c]
39+
else:
40+
if b[c] > mb:
41+
mb = b[c]
42+
43+
r = ma * mb
44+
return r, f_name[r % len(f_name)]
45+
46+
47+
my_f_name = sub(" +", "", input("Please provide your family name: ")).lower()
48+
print("Result:", famnam(my_f_name))
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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 yep(gn):
20+
r = 0
21+
if len(gn) == 0:
22+
return r
23+
24+
done = False
25+
26+
for c in gn:
27+
if not done:
28+
if c in "aeiou":
29+
idx = gn.index(c)
30+
r = (-1 * r) + yep(gn[idx+1:])
31+
done = True
32+
else:
33+
r = r + 1
34+
35+
return r
36+
37+
38+
my_g_name = sub(" +", "", input("Please provide your given name: ")).lower()
39+
print("Result:", yep(my_g_name))

0 commit comments

Comments
 (0)