-
Notifications
You must be signed in to change notification settings - Fork 13
/
recursionInterview.py
50 lines (37 loc) · 10.8 KB
/
recursionInterview.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
# 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 binaryToDemical(n):
if n == 0:
return 1
else:
return n%2 + 10*binaryToDemical(int(n/2))
print(binaryToDemical(1))
{"threads":[{"position":0,"start":0,"end":467,"connection":"open"},{"position":468,"start":468,"end":934,"connection":"idle"}],"url":"https://att-b.udemycdn.com/2020-04-09_04-56-00-0cd783d9d2ea58be0197b80c6d0f1c99/original.py?secure=QH1NRyv63E9N4rWlBcw5-A%3D%3D%2C1603028171&filename=recursionInterview.py","method":"GET","port":443,"downloadSize":934,"headers":{"date":"Sun, 18 Oct 2020 09:09:41 GMT","content-type":"text/x-python-script","content-length":"934","connection":"close","x-amz-id-2":"T2TaDJC08atSKKzgzoeG5kJ8lNGf+9uIvXT+tivqtD7r3FP7DhoLxtpMdVR9s3I1kADc7wh5wC0=","x-amz-request-id":"F809825EE8D3B891","last-modified":"Thu, 09 Apr 2020 04:56:01 GMT","etag":"\"82d247bc8e75c819226e22373c690edd\"","x-amz-meta-qqfilename":"recursionInterview.py","x-amz-version-id":"7NuRn2Kk7H5T30kxCcTHy7nHtzw4S1uK","x-77-nzt":"Ark73AFqrJDeMd4TALm0Dqco9abvAAAAAA==","x-edge-ip":"185.180.14.167","x-edge-pop":"pragueCZ","x-cache":"HIT","x-age":"0","server":"CDN77-Turbo","cache-control":"max-age=31536000","content-disposition":"attachment; filename=\"recursionInterview.py\"","x-lb-ip":"185.59.220.1","x-lb-pop":"frankfurtDE","x-cache-lb":"REVALIDATED","x-age-lb":"1302065","accept-ranges":"bytes"}}