Implement Three New Algorithms (#948) · zinating/algorithms-python@1c9d995 · GitHub
Skip to content

Commit 1c9d995

Browse files
PatOnTheBackAnupKumarPanwar
authored andcommitted
Implement Three New Algorithms (TheAlgorithms#948)
* Create average_median.py I created a program to calculate the median of a list of numbers. * Changed Odd to Even in String * Create decimal_to_binary.py - Added 'conversions' folder. - Created a decimal to binary converter. * Implemented Decimal to Octal Algorithm - I created a decimal to octal converter based on the converter in the TheAlgorithms/Python project. - I added two newlines to make the output of decimal_to_binary.py better.
1 parent 217615a commit 1c9d995

3 files changed

Lines changed: 104 additions & 0 deletions

File tree

conversions/decimal_to_binary.py

Lines changed: 25 additions & 0 deletions

conversions/decimal_to_octal.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
"""Convert a Decimal Number to an Octal Number."""
2+
3+
import math
4+
5+
# Modified from:
6+
# https://github.com/TheAlgorithms/Javascript/blob/master/Conversions/DecimalToOctal.js
7+
8+
9+
def decimal_to_octal(num):
10+
"""Convert a Decimal Number to an Octal Number."""
11+
octal = 0
12+
counter = 0
13+
while num > 0:
14+
remainder = num % 8
15+
octal = octal + (remainder * math.pow(10, counter))
16+
counter += 1
17+
num = math.floor(num / 8) # basically /= 8 without remainder if any
18+
# This formatting removes trailing '.0' from `octal`.
19+
return'{0:g}'.format(float(octal))
20+
21+
22+
def main():
23+
"""Print octal equivelents of decimal numbers."""
24+
print("\n2 in octal is:")
25+
print(decimal_to_octal(2)) # = 2
26+
print("\n8 in octal is:")
27+
print(decimal_to_octal(8)) # = 10
28+
print("\n65 in octal is:")
29+
print(decimal_to_octal(65)) # = 101
30+
print("\n216 in octal is:")
31+
print(decimal_to_octal(216)) # = 330
32+
print("\n512 in octal is:")
33+
print(decimal_to_octal(512)) # = 1000
34+
print("\n")
35+
36+
37+
if __name__ == '__main__':
38+
main()

maths/average_median.py

Lines changed: 41 additions & 0 deletions

0 commit comments

Comments
 (0)