Skip to content

Commit 1c9d995

Browse files
PatOnTheBackAnupKumarPanwar
authored andcommitted
Implement Three New Algorithms (#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

File tree

3 files changed

+104
-0
lines changed

3 files changed

+104
-0
lines changed

conversions/decimal_to_binary.py

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
"""Convert a Decimal Number to a Binary Number."""
2+
3+
4+
def decimal_to_binary(num):
5+
"""Convert a Decimal Number to a Binary Number."""
6+
binary = []
7+
while num > 0:
8+
binary.insert(0, num % 2)
9+
num >>= 1
10+
return "".join(str(e) for e in binary)
11+
12+
13+
def main():
14+
"""Print binary equivelents of decimal numbers."""
15+
print("\n2 in binary is:")
16+
print(decimal_to_binary(2)) # = 10
17+
print("\n7 in binary is:")
18+
print(decimal_to_binary(7)) # = 111
19+
print("\n35 in binary is:")
20+
print(decimal_to_binary(35)) # = 100011
21+
print("\n")
22+
23+
24+
if __name__ == '__main__':
25+
main()

conversions/decimal_to_octal.py

+38
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

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
"""
2+
Find median of a list of numbers.
3+
4+
Read more about medians:
5+
https://en.wikipedia.org/wiki/Median
6+
"""
7+
8+
9+
def median(nums):
10+
"""Find median of a list of numbers."""
11+
# Sort list
12+
sorted_list = sorted(nums)
13+
print("List of numbers:")
14+
print(sorted_list)
15+
16+
# Is number of items in list even?
17+
if len(sorted_list) % 2 == 0:
18+
# Find index for first middle value.
19+
mid_index_1 = len(sorted_list) / 2
20+
# Find index for second middle value.
21+
mid_index_2 = -(len(sorted_list) / 2) - 1
22+
# Divide middle values by 2 to get average (mean).
23+
med = (sorted_list[mid_index_1] + sorted_list[mid_index_2]) / float(2)
24+
return med # Return makes `else:` unnecessary.
25+
# Number of items is odd.
26+
mid_index = (len(sorted_list) - 1) / 2
27+
# Middle index is median.
28+
med = sorted_list[mid_index]
29+
return med
30+
31+
32+
def main():
33+
"""Call average module to find median of a specific list of numbers."""
34+
print("Odd number of numbers:")
35+
print(median([2, 4, 6, 8, 20, 50, 70]))
36+
print("Even number of numbers:")
37+
print(median([2, 4, 6, 8, 20, 50]))
38+
39+
40+
if __name__ == '__main__':
41+
main()

0 commit comments

Comments
 (0)