-
Notifications
You must be signed in to change notification settings - Fork 0
/
recursive_factorial_and_palindrome.py
58 lines (43 loc) · 1.73 KB
/
recursive_factorial_and_palindrome.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
from UnitaryTest.test_tools import TestTools
# Takes as input a string, and returns a Boolean indicating if
# the input string is a palindrome.
def is_palindrome_recursive(s):
if len(s) < 2:
# Base Case: '' => True
return True
# Recursive Case: if first and last characters don't match => False
return s[0] == s[-1] and is_palindrome_recursive(s[1:-1])
def is_palindrome(s):
return s == s[::-1]
# Takes a natural number as its input, and
# returns the number of ways to arrange the input number of items.
def factorial(n):
if n == 0:
return 1
return n * factorial(n-1)
def main():
t = TestTools()
# Test factorial
t.new_test(func=factorial)
t.evaluate_result(factorial(0), expected=1)
t.new_test(func=factorial)
t.evaluate_result(factorial(5), expected=120)
t.new_test(func=factorial)
t.evaluate_result(factorial(10), expected=3628800)
# Test is_palindrome
t.new_test(func=is_palindrome)
t.evaluate_result(is_palindrome(''), expected=True)
t.new_test(func=is_palindrome)
t.evaluate_result(is_palindrome('abab'), expected=False)
t.new_test(func=is_palindrome)
t.evaluate_result(is_palindrome('abba'), expected=True)
t.new_test(func=is_palindrome_recursive)
t.evaluate_result(is_palindrome_recursive('abab'), expected=False)
t.new_test(func=is_palindrome_recursive)
t.evaluate_result(is_palindrome_recursive('abba'), expected=True)
t.new_test(func=is_palindrome_recursive)
t.evaluate_result(is_palindrome_recursive('acba'), expected=False)
t.new_test(func=is_palindrome_recursive)
t.evaluate_result(is_palindrome_recursive('dbba'), expected=False)
if __name__ == '__main__':
main()