From 9b86958fc042e585b274f44fa55c457965761fec Mon Sep 17 00:00:00 2001 From: shellhub Date: Mon, 5 Oct 2020 01:01:54 +0800 Subject: [PATCH 1/5] add mode --- maths/mode.py | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 maths/mode.py diff --git a/maths/mode.py b/maths/mode.py new file mode 100644 index 0000000..bac9fb9 --- /dev/null +++ b/maths/mode.py @@ -0,0 +1,38 @@ +""" +https://en.wikipedia.org/wiki/Mode_(statistics) +""" + + +def mode(numbers): + """ + Calculate mode of a list numbers. + :param numbers: the numbers + :return: mode number of the numbers. + + >>> import statistics + >>> mode([1, 2, 2, 3, 4, 7, 9]) == statistics.mode([1, 2, 2, 3, 4, 7, 9]) + True + >>> import random + >>> numbers = random.sample(range(-50, 50), 100) + >>> mode(numbers) == statistics.mode(numbers) + True + """ + max_count = 1 + mode_number = numbers[0] + + for number in numbers: + count = 0 + for temp in numbers: + if temp == number: + count += 1 + if count > max_count: + max_count = count + mode_number = number + + return mode_number + + +if __name__ == "__main__": + from doctest import testmod + + testmod() From 028c8dc623b660a4a4443059361b56188f669386 Mon Sep 17 00:00:00 2001 From: shellhub Date: Thu, 22 Oct 2020 20:07:24 +0800 Subject: [PATCH 2/5] add variable --- basics/variable.py | 63 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 basics/variable.py diff --git a/basics/variable.py b/basics/variable.py new file mode 100644 index 0000000..10fc208 --- /dev/null +++ b/basics/variable.py @@ -0,0 +1,63 @@ +def main(): + """ + >>> a = 3 + >>> b = 4 + >>> a + b + 7 + >>> a - b + -1 + >>> a * b + 12 + >>> a / b + 0.75 + >>> a // b + 0 + >>> a % b + 3 + >>> 3 ** 4 + 81 + + >>> type(3) + + >>> type(3.14) + + >>> type('a') + + >>> type("abc") + + >>> type(True) + + >>> type(None) + + >>> type(3 + 4j) + + + >>> int(3.14) + 3 + >>> int(-3) + -3 + >>> float("3.14") + 3.14 + >>> str(3.14) + '3.14' + >>> str(3 + 4j) + '(3+4j)' + >>> chr(65) + 'A' + >>> chr(97) + 'a' + >>> ord("a") + 97 + >>> ord("A") + 65 + >>> chr(ord('a') - 32) + 'A' + >>> chr(ord('A') + 32) + 'a' + """ + + +if __name__ == "__main__": + from doctest import testmod + + testmod() From c831056a024f3c3ad46b1773e72c19cb9762adb2 Mon Sep 17 00:00:00 2001 From: shellhub Date: Thu, 22 Oct 2020 20:21:04 +0800 Subject: [PATCH 3/5] fixed build error --- maths/mode.py | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/maths/mode.py b/maths/mode.py index bac9fb9..f4df405 100644 --- a/maths/mode.py +++ b/maths/mode.py @@ -9,13 +9,8 @@ def mode(numbers): :param numbers: the numbers :return: mode number of the numbers. - >>> import statistics - >>> mode([1, 2, 2, 3, 4, 7, 9]) == statistics.mode([1, 2, 2, 3, 4, 7, 9]) - True - >>> import random - >>> numbers = random.sample(range(-50, 50), 100) - >>> mode(numbers) == statistics.mode(numbers) - True + >>> mode([1, 2, 2, 3, 4, 7, 9]) + 2 """ max_count = 1 mode_number = numbers[0] From 321275035bea379c273acac71638d0118e5a326b Mon Sep 17 00:00:00 2001 From: shellhub Date: Thu, 22 Oct 2020 20:31:13 +0800 Subject: [PATCH 4/5] fahrenheit to celsius --- conversions/__init__.py | 0 conversions/fahrenheit_to_celsius.py | 19 +++++++++++++++++++ 2 files changed, 19 insertions(+) create mode 100644 conversions/__init__.py create mode 100644 conversions/fahrenheit_to_celsius.py diff --git a/conversions/__init__.py b/conversions/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/conversions/fahrenheit_to_celsius.py b/conversions/fahrenheit_to_celsius.py new file mode 100644 index 0000000..e22c598 --- /dev/null +++ b/conversions/fahrenheit_to_celsius.py @@ -0,0 +1,19 @@ +""" +https://en.wikipedia.org/wiki/Fahrenheit +""" + + +def fahrenheit_to_celsius(temperature: float) -> float: + """ + >>> fahrenheit_to_celsius(32) + 0.0 + >>> fahrenheit_to_celsius(39) + 3.888888888888889 + """ + return 5 * (temperature - 32) / 9 + + +if __name__ == '__main__': + from doctest import testmod + + testmod() From 99a73ac043146653a585332353b2c0a04471e695 Mon Sep 17 00:00:00 2001 From: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Date: Thu, 22 Oct 2020 12:32:08 +0000 Subject: [PATCH 5/5] Formatted with psf/black --- conversions/fahrenheit_to_celsius.py | 2 +- maths/median.py | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/conversions/fahrenheit_to_celsius.py b/conversions/fahrenheit_to_celsius.py index e22c598..ea342d3 100644 --- a/conversions/fahrenheit_to_celsius.py +++ b/conversions/fahrenheit_to_celsius.py @@ -13,7 +13,7 @@ def fahrenheit_to_celsius(temperature: float) -> float: return 5 * (temperature - 32) / 9 -if __name__ == '__main__': +if __name__ == "__main__": from doctest import testmod testmod() diff --git a/maths/median.py b/maths/median.py index d34144f..f4681e0 100644 --- a/maths/median.py +++ b/maths/median.py @@ -22,7 +22,8 @@ def median(numbers): numbers = sorted(numbers) mid_index = len(numbers) // 2 return ( - (numbers[mid_index] + numbers[mid_index - 1]) / 2 if mid_index % 2 == 0 + (numbers[mid_index] + numbers[mid_index - 1]) / 2 + if mid_index % 2 == 0 else numbers[mid_index] )