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 **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
+
deftest_is_prime(n, expected):
21
+
result = is_prime(n)
22
+
if result is expected:
23
+
returnTrue
24
+
else:
25
+
returnFalse
26
+
27
+
28
+
# Code of the function
29
+
defis_prime(n):
30
+
d =2
31
+
32
+
while d <= sqrt(n):
33
+
if n % d ==0:
34
+
returnFalse
35
+
d +=1
36
+
37
+
returnTrue
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.
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`.
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
+
deftest_f(s, t, expected):
10
+
result = f(s, t)
11
+
if expected == result:
12
+
returnTrue
13
+
else:
14
+
returnFalse
15
+
16
+
17
+
# Code of the function
18
+
deff(s, t):
19
+
if s == t:
20
+
returnTrue
21
+
eliflen(s) ==len(t):
22
+
returnFalse
23
+
eliflen(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.
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
+
deftest_f(x, y, expected):
9
+
result = f(x, y)
10
+
if expected == result:
11
+
returnTrue
12
+
else:
13
+
returnFalse
14
+
15
+
16
+
# Code of the function
17
+
deff(x, y):
18
+
if x == y:
19
+
return0
20
+
elif x % y ==0:
21
+
return-1
22
+
elif y % x ==0:
23
+
return1
24
+
else:
25
+
returnNone
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