-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrecursionInterview.py
More file actions
52 lines (38 loc) · 923 Bytes
/
Copy pathrecursionInterview.py
File metadata and controls
52 lines (38 loc) · 923 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
41
42
43
44
45
46
47
48
49
50
51
# Created by Roshan Jayswal on 4/01/20.
# Copyright © 2021. All rights reserved.
# Question 1
def sumofDigits(n):
assert n>=0 and int(n) == n , 'The number has to be a postive integer only!'
if n == 0:
return 0
else:
return int(n%10) + sumofDigits(int(n/10))
print(sumofDigits(11111))
#Question 2
def power(base,exp):
if exp == 0:
return 1
if(exp==1):
return(base)
if(exp!=1):
return (base*power(base,exp-1))
print(power(4,2))
# Question 3
def gcd(a, b):
assert int(a) == a and int(b) == b, 'The numbers must be integer only!'
if a < 0:
a = -1 * a
if b < 0:
b = -1 * b
if b == 0:
return a
else:
return gcd(b, a%b)
print(gcd(12,1.2))
# Question 4
def decimalToBinary(n):
if n == 0:
return 0
else:
return n%2 + 10*decimalToBinary(int(n/2))
print(decimalToBinary(1))