forked from UnitTestBot/UTBotJava
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrecursion.py
More file actions
41 lines (32 loc) · 749 Bytes
/
recursion.py
File metadata and controls
41 lines (32 loc) · 749 Bytes
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
def factorial(n: int):
if n < 0:
raise ValueError()
if n == 0:
return 1
return n * factorial(n-1)
def fib(n: int):
if n < 0:
raise ValueError
if n == 0:
return 0
if n == 1:
return 1
return fib(n-1) + fib(n-2)
def summ(fst: int, snd: int):
def signum(x: int):
return 0 if x == 0 else 1 if x > 0 else -1
if snd == 0:
return fst
return summ(fst + signum(snd), snd - signum(snd))
def recursion_with_exception(n: int):
if n < 42:
recursion_with_exception(n+1)
if n > 42:
recursion_with_exception(n-1)
raise ValueError
def first(n: int):
def second(n: int):
first(n)
if n < 4:
return
second(n)