Skip to content

Commit 305d1f9

Browse files
committed
Added binary to decimal, decimal to binary, hex to decimal and decimal to hex implementations
1 parent ecd5976 commit 305d1f9

File tree

1 file changed

+102
-0
lines changed

1 file changed

+102
-0
lines changed

pygorithm/math/conversion.py

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
"""
2+
Author: OMKAR PATHAK
3+
Created On: 23 August 2017
4+
"""
5+
6+
def decimal_to_binary(number):
7+
'''
8+
This function calculates the binary of the given decimal number
9+
:param number: decimal number in string or integer format
10+
:return : string of the equivalent binary number
11+
12+
Algo:
13+
1. Divide the decimal number by 2. Treat the division as an integer division.
14+
2. Write down the remainder (in binary).
15+
3. Divide the result again by 2. Treat the division as an integer division.
16+
4. Repeat step 2 and 3 until result is 0.
17+
5. The binary value is the digit sequence of the remainders from the last to first.
18+
'''
19+
if isinstance(number, str):
20+
number = int(number)
21+
binary = []
22+
while number >= 1:
23+
remainder = number % 2
24+
binary.append(remainder)
25+
number = number // 2
26+
27+
return ''.join(map(str, binary[::-1]))
28+
29+
def binary_to_decimal(number):
30+
'''
31+
This function calculates the decimal of the given binary number
32+
:param number: decimal number in string or integer format
33+
:return : integer of the equivalent decimal number
34+
35+
Algo:
36+
1. Get the last digit of the binary number.
37+
2. Multiply the current digit with (2^power), store the result.
38+
3. Increment power by 1.
39+
4. Repeat from step 2 until all digits have been multiplied.
40+
5. Sum the result of step 2 to get the answer number.
41+
'''
42+
decimal = []
43+
number = list(str(number)[::-1])
44+
for i in range(len(number)):
45+
decimal.append(int(number[i]) * (2 ** i))
46+
47+
return sum(decimal)
48+
49+
def decimal_to_hex(number):
50+
'''
51+
This function calculates the hex of the given decimal number
52+
:param number: decimal number in string or integer format
53+
:return : string of the equivalent hex number
54+
55+
Algo:
56+
1. Divide the decimal number by 16. Treat the division as an integer division.
57+
2. Write down the remainder (in hexadecimal).
58+
3. Divide the result again by 16. Treat the division as an integer division.
59+
4. Repeat step 2 and 3 until result is 0.
60+
5. The hex value is the digit sequence of the remainders from the last to first.
61+
'''
62+
if isinstance(number, str):
63+
number = int(number)
64+
hexadec = []
65+
hex_equivalents = {10:'A', 11:'B', 12:'C', 13:'D', 14:'E', 15:'F'}
66+
while number >= 1:
67+
remainder = number % 16
68+
if remainder < 10:
69+
hexadec.append(remainder)
70+
elif remainder >= 10:
71+
hexadec.append(hex_equivalents[remainder])
72+
73+
number = number // 16
74+
75+
return ''.join(map(str, hexadec[::-1]))
76+
77+
def hex_to_decimal(number):
78+
'''
79+
This function calculates the decimal of the given hex number
80+
:param number: hex number in string or integer format
81+
:return : integer of the equivalent decimal number
82+
83+
Algo:
84+
1. Get the last digit of the hex number.
85+
2. Multiply the current digit with (16^power), store the result.
86+
3. Increment power by 1.
87+
4. Repeat from step 2 until all digits have been multiplied.
88+
5. Sum the result of step 2 to get the answer number.
89+
'''
90+
decimal = []
91+
decimal_equivalents = {'A':10, 'B':11, 'C':12, 'D':13, 'E':14, 'F':15}
92+
number = list(str(number)[::-1])
93+
for i in range(len(number)):
94+
try:
95+
if int(number[i]) < 10:
96+
decimal.append(int(number[i]) * (16 ** i))
97+
except ValueError:
98+
decimal.append(decimal_equivalents[number[i]] * (16 ** i))
99+
100+
return sum(decimal)
101+
102+
print(hex_to_decimal('7DE'))

0 commit comments

Comments
 (0)