-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprint_binary.py
More file actions
34 lines (27 loc) · 823 Bytes
/
print_binary.py
File metadata and controls
34 lines (27 loc) · 823 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
#from https://rb.gy/mdb28n
#https://rb.gy/ksjhzd
#Problem: Given a real number between 0 and 1, print the binary representation.
# If the length of the representation is > 32, return 'ERROR'.
class Bits(object):
MAX_BITS = 32
def print_binary(self, num):
if num is None or num >= 1 or num <= 0:
return 'ERROR'
result = ['0', '.']
fraction = 0.5
while num:
if num >= fraction:
result.append('1')
num -= fraction
else:
result.append('0')
if len(result) > self.MAX_BITS:
return 'ERROR'
fraction /= 2
return ''.join(result)
def main():
bits = Bits()
result = bits.print_binary(0.625)
print(result)
if __name__ == '__main__':
main()