-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlight2.py
46 lines (45 loc) · 930 Bytes
/
light2.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
def toBinary(n):
if isinstance(n,str):
if n.isdigit():
n=int(n)
else:
return "ERR: Arg isn't a digit"
elif not isinstance(n,int):
return "ERR: Arg can only be string either int"
b=[1]
p=1
while b[-1]*2<=n:
b.append(2**p)
p=p+1
i=1
r=""
while i<=len(b):
if b[-i]<=n:
n=n-b[-i]
r="1"+r
else:
r="0"+r
i=i+1
return int(r[::-1])
def fromBinary(n):
if isinstance(n,int):
n=str(n)
elif isinstance(n,str):
if not n.isdigit:
return "ERR: Arg isn't a digit"
else:
return "ERR: Arg needs to be int either string(digits only)"
i=len(n)
b=[1]
t=1
while t<i:
b.append(2**t)
t+=1
t=0
r=0
while t<i:
if n[t]=="1":
d=t+1
r=r+b[-d]
t=t+1
return r