-
Notifications
You must be signed in to change notification settings - Fork 0
/
functional.py
59 lines (43 loc) · 1.21 KB
/
functional.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
"""
functional programming on Python example: check if string is a palendrome
dependencies:
pyfpm for pattern matching
python 3.7.1
author:
L1ttl3S1st3r
4/14/2019
"""
from pyfpm.matcher import Matcher
import typing
class IncorrectArgException(ValueError):
pass
# watch a descriptions in pyfmp docs
is_palendrome = Matcher()
# if arg is an empty string
@is_palendrome.handler("''")
def empty_string() -> bool:
return True
# if arg matchs to default string
@is_palendrome.handler('string: str')
def correct_string(string) -> bool:
if string[0] == string[-1]:
return is_palendrome(string[1:-1])
else:
return False
@is_palendrome.handler("_")
def random_arg() -> bool:
raise IncorrectArgException("incorrect input type")
def main() -> None:
tests = ["abc", "b", "",
"abcd", "abba", 1,
None, object]
for test in tests:
try:
if is_palendrome(test):
print(str(test) + " is a palendrome")
else:
print(str(test) + " is not a palendrome")
except IncorrectArgException:
print(str(test) + " has incorrect type:" + str(type(test)))
if __name__ == "__main__":
main()