From 0acaf0a5ac9473c49c80c50228e8bf07cac1a943 Mon Sep 17 00:00:00 2001 From: PatOnTheBack <51241310+PatOnTheBack@users.noreply.github.com> Date: Wed, 3 Jul 2019 09:27:29 -0400 Subject: [PATCH 1/4] Create average_median.py I created a program to calculate the median of a list of numbers. --- maths/average_median.py | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 maths/average_median.py diff --git a/maths/average_median.py b/maths/average_median.py new file mode 100644 index 000000000000..afbaca0ad224 --- /dev/null +++ b/maths/average_median.py @@ -0,0 +1,41 @@ +""" +Find median of a list of numbers. + +Read more about medians: + https://en.wikipedia.org/wiki/Median +""" + + +def median(nums): + """Find median of a list of numbers.""" + # Sort list + sorted_list = sorted(nums) + print("List of numbers:") + print(sorted_list) + + # Is number of items in list even? + if len(sorted_list) % 2 == 0: + # Find index for first middle value. + mid_index_1 = len(sorted_list) / 2 + # Find index for second middle value. + mid_index_2 = -(len(sorted_list) / 2) - 1 + # Divide middle values by 2 to get average (mean). + med = (sorted_list[mid_index_1] + sorted_list[mid_index_2]) / float(2) + return med # Return makes `else:` unnecessary. + # Number of items is odd. + mid_index = (len(sorted_list) - 1) / 2 + # Middle index is median. + med = sorted_list[mid_index] + return med + + +def main(): + """Call average module to find median of a specific list of numbers.""" + print("Odd number of numbers:") + print(median([2, 4, 6, 8, 20, 50, 70])) + print("Odd number of numbers:") + print(median([2, 4, 6, 8, 20, 50])) + + +if __name__ == '__main__': + main() From ea380fc95372845c9a1d7925d8d124a298d82260 Mon Sep 17 00:00:00 2001 From: PatOnTheBack <51241310+PatOnTheBack@users.noreply.github.com> Date: Wed, 3 Jul 2019 09:57:10 -0400 Subject: [PATCH 2/4] Changed Odd to Even in String --- maths/average_median.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/maths/average_median.py b/maths/average_median.py index afbaca0ad224..565bb4afd112 100644 --- a/maths/average_median.py +++ b/maths/average_median.py @@ -33,7 +33,7 @@ def main(): """Call average module to find median of a specific list of numbers.""" print("Odd number of numbers:") print(median([2, 4, 6, 8, 20, 50, 70])) - print("Odd number of numbers:") + print("Even number of numbers:") print(median([2, 4, 6, 8, 20, 50])) From ab91e6983a3d1368b6f74dea1c41f22623c8ef9f Mon Sep 17 00:00:00 2001 From: PatOnTheBack <51241310+PatOnTheBack@users.noreply.github.com> Date: Wed, 3 Jul 2019 11:43:24 -0400 Subject: [PATCH 3/4] Create decimal_to_binary.py - Added 'conversions' folder. - Created a decimal to binary converter. --- conversions/decimal_to_binary.py | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 conversions/decimal_to_binary.py diff --git a/conversions/decimal_to_binary.py b/conversions/decimal_to_binary.py new file mode 100644 index 000000000000..864f20c8a833 --- /dev/null +++ b/conversions/decimal_to_binary.py @@ -0,0 +1,24 @@ +"""Convert a Decimal Number to a Binary Number.""" + + +def decimal_to_binary(num): + """Convert a Decimal Number to a Binary Number.""" + binary = [] + while num > 0: + binary.insert(0, num % 2) + num >>= 1 + return "".join(str(e) for e in binary) + + +def main(): + """Print binary equivelents of decimal numbers.""" + print("2 in binary is:") + print(decimal_to_binary(2)) # = 10 + print("\n7 in binary is:") + print(decimal_to_binary(7)) # = 111 + print("\n35 in binary is:") + print(decimal_to_binary(35)) # = 100011 + + +if __name__ == '__main__': + main() From de89ed40351418104a2b3e86cdff21d14893a41d Mon Sep 17 00:00:00 2001 From: PatOnTheBack <51241310+PatOnTheBack@users.noreply.github.com> Date: Wed, 3 Jul 2019 17:02:46 -0400 Subject: [PATCH 4/4] 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. --- conversions/decimal_to_binary.py | 3 ++- conversions/decimal_to_octal.py | 38 ++++++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+), 1 deletion(-) create mode 100644 conversions/decimal_to_octal.py diff --git a/conversions/decimal_to_binary.py b/conversions/decimal_to_binary.py index 864f20c8a833..43ceee61a388 100644 --- a/conversions/decimal_to_binary.py +++ b/conversions/decimal_to_binary.py @@ -12,12 +12,13 @@ def decimal_to_binary(num): def main(): """Print binary equivelents of decimal numbers.""" - print("2 in binary is:") + print("\n2 in binary is:") print(decimal_to_binary(2)) # = 10 print("\n7 in binary is:") print(decimal_to_binary(7)) # = 111 print("\n35 in binary is:") print(decimal_to_binary(35)) # = 100011 + print("\n") if __name__ == '__main__': diff --git a/conversions/decimal_to_octal.py b/conversions/decimal_to_octal.py new file mode 100644 index 000000000000..187a0300e33a --- /dev/null +++ b/conversions/decimal_to_octal.py @@ -0,0 +1,38 @@ +"""Convert a Decimal Number to an Octal Number.""" + +import math + +# Modified from: +# https://github.com/TheAlgorithms/Javascript/blob/master/Conversions/DecimalToOctal.js + + +def decimal_to_octal(num): + """Convert a Decimal Number to an Octal Number.""" + octal = 0 + counter = 0 + while num > 0: + remainder = num % 8 + octal = octal + (remainder * math.pow(10, counter)) + counter += 1 + num = math.floor(num / 8) # basically /= 8 without remainder if any + # This formatting removes trailing '.0' from `octal`. + return'{0:g}'.format(float(octal)) + + +def main(): + """Print octal equivelents of decimal numbers.""" + print("\n2 in octal is:") + print(decimal_to_octal(2)) # = 2 + print("\n8 in octal is:") + print(decimal_to_octal(8)) # = 10 + print("\n65 in octal is:") + print(decimal_to_octal(65)) # = 101 + print("\n216 in octal is:") + print(decimal_to_octal(216)) # = 330 + print("\n512 in octal is:") + print(decimal_to_octal(512)) # = 1000 + print("\n") + + +if __name__ == '__main__': + main()