From a56525855cf0030de0ac2374146ebfb67df778cf Mon Sep 17 00:00:00 2001 From: avivfaraj <73610201+avivfaraj@users.noreply.github.com> Date: Fri, 3 Sep 2021 19:47:55 -0400 Subject: [PATCH 01/27] Add files via upload --- physics/gamma_function.py | 83 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 83 insertions(+) create mode 100644 physics/gamma_function.py diff --git a/physics/gamma_function.py b/physics/gamma_function.py new file mode 100644 index 000000000000..395968aeb99b --- /dev/null +++ b/physics/gamma_function.py @@ -0,0 +1,83 @@ +""" +Gamma function is a very useful tool in physics. +It helps calculating complex integral in a convenient way. +for more info: https://en.wikipedia.org/wiki/Gamma_function +""" + +# Importing packages +from math import sqrt, pi +from re import match +from typing import Union + + +def gamma(num : Union[int, float]) -> Union[int, float]: + """ + Calculates the value of Gamma function of num + where num is either an integer (1,2,3..) or a half-integer (0.5,1.5,2.5...). + Implemented using recursion + Examples: + >>> Gamma of: 0.5 + √π + >>> Gamma of: 2 + 1 + >>> Gamma of: 3.5 + 1.875√π + """ + if num == 1: + return 1 + elif num == 0.5: + return sqrt(pi) + elif num > 1: + return (num - 1) * gamma(num - 1) + else: + # Error + return -2 + + +def test_gamma() -> None: + """ + >>> test_gamma() + """ + assert sqrt(pi) == gamma(0.5) + assert 1 == gamma(1) + assert 1 == gamma(2) + + +if __name__ == "__main__": + # Initialize boolean + number = True + # Get input from user + input_ = input("Gamma of: ") + # Ensure valid input + try: + # Ensure input matches half-integer (float) pattern + if match(r'^[0-9]*\.5$', input_): + # Convert string to float + num = float(input_) + # Ensure input matches an integer pattern + elif match(r'^[1-9][0-9]*$', input_): + # Convert string to int + num = int(input_) + # Input is not a valid number + else: + # raise an error + raise ValueError + # Ensure print an error message + except ValueError: + print("Invalid input: Must be an integer or an half-integer!") + number = False + finally: + # Ensure input is a valid number + if number: + # Ensure input is an integer + if (isinstance(gamma(num), int)): + # Print result + print(gamma(num)) + # Otherwise print results with √π (gamma of 0.5 is √π) + # Therefore all results will be a number times √π + else: + results = "{result:.4f}".format(result=gamma(num) / sqrt(pi)) + results = results.rstrip('0').rstrip('.') + if results == "1": + results = "" + print(results + "\u221A\u03c0") From 4951686999339c82012f6f9f6004c8ad7b918482 Mon Sep 17 00:00:00 2001 From: avivfaraj <73610201+avivfaraj@users.noreply.github.com> Date: Sat, 4 Sep 2021 10:19:15 -0400 Subject: [PATCH 02/27] Changed print to f-string Also printed out results in a math notation --- physics/gamma_function.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/physics/gamma_function.py b/physics/gamma_function.py index 395968aeb99b..bbef623408e7 100644 --- a/physics/gamma_function.py +++ b/physics/gamma_function.py @@ -64,11 +64,12 @@ def test_gamma() -> None: raise ValueError # Ensure print an error message except ValueError: - print("Invalid input: Must be an integer or an half-integer!") + print("Error: Input must be an integer or an half-integer!") number = False finally: # Ensure input is a valid number if number: + print(f"\u0393({num}) = ", end="") # Ensure input is an integer if (isinstance(gamma(num), int)): # Print result @@ -76,7 +77,7 @@ def test_gamma() -> None: # Otherwise print results with √π (gamma of 0.5 is √π) # Therefore all results will be a number times √π else: - results = "{result:.4f}".format(result=gamma(num) / sqrt(pi)) + results = f"{gamma(num) / sqrt(pi):.4f}" results = results.rstrip('0').rstrip('.') if results == "1": results = "" From ff2a162bddfb46e75a4020d5212efef684dd5850 Mon Sep 17 00:00:00 2001 From: avivfaraj <73610201+avivfaraj@users.noreply.github.com> Date: Sat, 4 Sep 2021 12:41:26 -0400 Subject: [PATCH 03/27] Add files via upload --- physics/horizontal_projectile_motion.py | 131 ++++++++++++++++++++++++ 1 file changed, 131 insertions(+) create mode 100644 physics/horizontal_projectile_motion.py diff --git a/physics/horizontal_projectile_motion.py b/physics/horizontal_projectile_motion.py new file mode 100644 index 000000000000..a5b2cff28fc1 --- /dev/null +++ b/physics/horizontal_projectile_motion.py @@ -0,0 +1,131 @@ +""" +Horizontal Projectile Motion problem in physics. +This algorithm solves a specific problem in which +the motion starts from the ground as can be seen below: + (v = 0) + ** + * * + * * + * * + * * + * * +GROUND GROUND + +For more info: https://en.wikipedia.org/wiki/Projectile_motion +""" + +# Importing packages +from math import pi, sin + +# Acceleration Constant on hearth (unit m/s^2) +g = 9.80665 + + +def angle_to_radians(angle): + """ + Convert an angle from degrees to randians + """ + return angle * pi / 180 + + +def horizontal_distance(init_velocity: float, angle: float) -> float: + """ + Returns the horizontal distance that the object cover + + Formula: + v_0^2 * sin(2 * alpha) + --------------------- + g + + v_0 - initial velocity + alpha - angle + + >>> horizontal_distance(30, 45) + 91.77 + >>> horizontal_distance(100, 78) + 414.76 + """ + radians = angle_to_radians(2 * angle) + return round((init_velocity ** 2) * sin(radians) / g, 2) + + +def max_height(init_velocity: float, angle: float) -> float: + """ + Returns the maximum height that the object reach + + Formula: + v_0^2 * sin^2(alpha) + -------------------- + 2g + + v_0 - initial velocity + alpha - angle + + >>> max_height(30, 45) + 22.94 + >>> max_height(100, 78) + 487.82 + """ + + radians = angle_to_radians(angle) + return round(((init_velocity ** 2) * (sin(radians)) ** 2) / (2 * g), 2) + + +def total_time(init_velocity: float, angle: float) -> float: + """ + Returns total time of the motion + + Formula: + 2 * v_0 * sin(alpha) + -------------------- + g + + v_0 - initial velocity + alpha - angle + + >>> total_time(30, 45) + 4.33 + >>> total_time(100, 78) + 19.95 + """ + + radians = angle_to_radians(angle) + return round((2 * init_velocity) * (sin(radians)) / g, 2) + + +def test_motion() -> None: + """ + >>> test_motion() + """ + v0, angle = 25, 20 + assert 40.97 == horizontal_distance(v0, angle) + assert 3.73 == max_height(v0, angle) + assert 1.74 == total_time(v0, angle) + + +if __name__ == "__main__": + + # Get input from user + init_vel = float(input("Initial Velocity: ")) + + # Get input from user + angle = float(input("angle: ")) + + # Ensure valid angle + if angle > 90 or angle <= 0: + print("Error: Invalid angle. Range is 1-90 degrees.") + + # Ensure valid velocity + elif init_vel < 0: + print("Error: Invalid velocity. Should be a positive number.") + + # Print results + else: + print() + h_dis = str(horizontal_distance(init_vel, angle)) + v_dis = str(max_height(init_vel, angle)) + t_time = str(total_time(init_vel, angle)) + print("Results: ") + print("Horizontal Distance: " + h_dis + " [m]") + print("Maximum Height: " + v_dis + " [m]") + print("Total Time: " + t_time + " [s]") From 6550183d999e2a4a149790e40de6d536c2d509dd Mon Sep 17 00:00:00 2001 From: avivfaraj <73610201+avivfaraj@users.noreply.github.com> Date: Sat, 4 Sep 2021 12:53:35 -0400 Subject: [PATCH 04/27] Fixes: #4710 provided return type --- physics/horizontal_projectile_motion.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/physics/horizontal_projectile_motion.py b/physics/horizontal_projectile_motion.py index a5b2cff28fc1..09a0d07489e8 100644 --- a/physics/horizontal_projectile_motion.py +++ b/physics/horizontal_projectile_motion.py @@ -21,7 +21,7 @@ g = 9.80665 -def angle_to_radians(angle): +def angle_to_radians(angle : float) -> float: """ Convert an angle from degrees to randians """ From 552546f2c1e07587366b88b36b71b912c730f3a3 Mon Sep 17 00:00:00 2001 From: avivfaraj <73610201+avivfaraj@users.noreply.github.com> Date: Sat, 4 Sep 2021 12:55:13 -0400 Subject: [PATCH 05/27] File exists in another pull request --- physics/gamma_function.py | 84 --------------------------------------- 1 file changed, 84 deletions(-) delete mode 100644 physics/gamma_function.py diff --git a/physics/gamma_function.py b/physics/gamma_function.py deleted file mode 100644 index bbef623408e7..000000000000 --- a/physics/gamma_function.py +++ /dev/null @@ -1,84 +0,0 @@ -""" -Gamma function is a very useful tool in physics. -It helps calculating complex integral in a convenient way. -for more info: https://en.wikipedia.org/wiki/Gamma_function -""" - -# Importing packages -from math import sqrt, pi -from re import match -from typing import Union - - -def gamma(num : Union[int, float]) -> Union[int, float]: - """ - Calculates the value of Gamma function of num - where num is either an integer (1,2,3..) or a half-integer (0.5,1.5,2.5...). - Implemented using recursion - Examples: - >>> Gamma of: 0.5 - √π - >>> Gamma of: 2 - 1 - >>> Gamma of: 3.5 - 1.875√π - """ - if num == 1: - return 1 - elif num == 0.5: - return sqrt(pi) - elif num > 1: - return (num - 1) * gamma(num - 1) - else: - # Error - return -2 - - -def test_gamma() -> None: - """ - >>> test_gamma() - """ - assert sqrt(pi) == gamma(0.5) - assert 1 == gamma(1) - assert 1 == gamma(2) - - -if __name__ == "__main__": - # Initialize boolean - number = True - # Get input from user - input_ = input("Gamma of: ") - # Ensure valid input - try: - # Ensure input matches half-integer (float) pattern - if match(r'^[0-9]*\.5$', input_): - # Convert string to float - num = float(input_) - # Ensure input matches an integer pattern - elif match(r'^[1-9][0-9]*$', input_): - # Convert string to int - num = int(input_) - # Input is not a valid number - else: - # raise an error - raise ValueError - # Ensure print an error message - except ValueError: - print("Error: Input must be an integer or an half-integer!") - number = False - finally: - # Ensure input is a valid number - if number: - print(f"\u0393({num}) = ", end="") - # Ensure input is an integer - if (isinstance(gamma(num), int)): - # Print result - print(gamma(num)) - # Otherwise print results with √π (gamma of 0.5 is √π) - # Therefore all results will be a number times √π - else: - results = f"{gamma(num) / sqrt(pi):.4f}" - results = results.rstrip('0').rstrip('.') - if results == "1": - results = "" - print(results + "\u221A\u03c0") From bc31e3fa7160f11b98d4dcca50c7e89faf61480b Mon Sep 17 00:00:00 2001 From: avivfaraj <73610201+avivfaraj@users.noreply.github.com> Date: Sun, 5 Sep 2021 12:56:31 -0400 Subject: [PATCH 06/27] imported radians from math --- physics/horizontal_projectile_motion.py | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) diff --git a/physics/horizontal_projectile_motion.py b/physics/horizontal_projectile_motion.py index 09a0d07489e8..89d6f635f6e5 100644 --- a/physics/horizontal_projectile_motion.py +++ b/physics/horizontal_projectile_motion.py @@ -15,19 +15,12 @@ """ # Importing packages -from math import pi, sin +from math import pi, sin, radians as angle_to_radians # Acceleration Constant on hearth (unit m/s^2) g = 9.80665 -def angle_to_radians(angle : float) -> float: - """ - Convert an angle from degrees to randians - """ - return angle * pi / 180 - - def horizontal_distance(init_velocity: float, angle: float) -> float: """ Returns the horizontal distance that the object cover From 974e0a6e9561b145631fcbabb6b036be4485966e Mon Sep 17 00:00:00 2001 From: Aviv Faraj <73610201+avivfaraj@users.noreply.github.com> Date: Mon, 6 Sep 2021 13:08:33 -0400 Subject: [PATCH 07/27] Updated file according to pre-commit test --- physics/horizontal_projectile_motion.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/physics/horizontal_projectile_motion.py b/physics/horizontal_projectile_motion.py index 89d6f635f6e5..7147da1949a5 100644 --- a/physics/horizontal_projectile_motion.py +++ b/physics/horizontal_projectile_motion.py @@ -15,7 +15,9 @@ """ # Importing packages -from math import pi, sin, radians as angle_to_radians +from math import pi +from math import radians as angle_to_radians +from math import sin # Acceleration Constant on hearth (unit m/s^2) g = 9.80665 From 1d4fc814fac69ecd821559b0a540f4eea588b922 Mon Sep 17 00:00:00 2001 From: Username Date: Mon, 6 Sep 2021 13:16:58 -0400 Subject: [PATCH 08/27] Updated file --- maths/gamma_recursive.py | 83 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 83 insertions(+) create mode 100644 maths/gamma_recursive.py diff --git a/maths/gamma_recursive.py b/maths/gamma_recursive.py new file mode 100644 index 000000000000..11c14c2cb2a3 --- /dev/null +++ b/maths/gamma_recursive.py @@ -0,0 +1,83 @@ +""" +Gamma function is a very useful tool in physics. +It helps calculating complex integral in a convenient way. +for more info: https://en.wikipedia.org/wiki/Gamma_function +""" + +# Importing packages +from math import sqrt, pi +from re import match +from typing import Union + + +def gamma(num : Union[int, float]) -> Union[int, float]: + """ + Calculates the value of Gamma function of num + where num is either an integer (1,2,3..) or a half-integer (0.5,1.5,2.5...). + Implemented using recursion + Examples: + >>> Gamma of: 0.5 + √π + >>> Gamma of: 2 + 1 + >>> Gamma of: 3.5 + 1.875√π + """ + if num == 1: + return 1 + elif num == 0.5: + return sqrt(pi) + elif num > 1: + return (num - 1) * gamma(num - 1) + # Error + return -2 + + +def test_gamma() -> None: + """ + >>> test_gamma() + """ + assert sqrt(pi) == gamma(0.5) + assert 1 == gamma(1) + assert 1 == gamma(2) + + +if __name__ == "__main__": + # Initialize boolean + number = True + # Get input from user + input_ = input("Gamma of: ") + # Ensure valid input + try: + # Ensure input matches half-integer (float) pattern + if match(r"^[0-9]*\.5$", input_): + # Convert string to float + num = float(input_) + # Ensure input matches an integer pattern + elif match(r"^[1-9][0-9]*$", input_): + # Convert string to int + num = int(input_) + # Input is not a valid number + else: + # raise an error + raise ValueError + # Ensure print an error message + except ValueError: + print("Error: Input must be an integer or an half-integer!") + number = False + finally: + # Ensure input is a valid number + if number: + print(f"\u0393({num}) = ", end="") + # Ensure input is an integer + if isinstance(gamma(num), int): + # Print result + print(gamma(num)) + # Otherwise print results with √π (gamma of 0.5 is √π) + # Therefore all results will be a number times √π + else: + results = f"{gamma(num) / sqrt(pi):.4f}" + results = results.rstrip("0").rstrip(".") + if results == "1": + results = "" + print(results + "\u221A\u03c0") \ No newline at end of file From 7bd84b4eee7aa118edff8aba79bf7130b099bd8b Mon Sep 17 00:00:00 2001 From: Username Date: Mon, 6 Sep 2021 13:55:20 -0400 Subject: [PATCH 09/27] Updated gamma --- maths/gamma_recursive.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/maths/gamma_recursive.py b/maths/gamma_recursive.py index 11c14c2cb2a3..e7e2c70c7ebe 100644 --- a/maths/gamma_recursive.py +++ b/maths/gamma_recursive.py @@ -5,12 +5,12 @@ """ # Importing packages -from math import sqrt, pi +from math import pi, sqrt from re import match from typing import Union -def gamma(num : Union[int, float]) -> Union[int, float]: +def gamma(num: Union[int, float]) -> Union[int, float]: """ Calculates the value of Gamma function of num where num is either an integer (1,2,3..) or a half-integer (0.5,1.5,2.5...). @@ -80,4 +80,4 @@ def test_gamma() -> None: results = results.rstrip("0").rstrip(".") if results == "1": results = "" - print(results + "\u221A\u03c0") \ No newline at end of file + print(results + "\u221A\u03c0") From 2841c11da1cac9d5e94dd72f6cd301561baad012 Mon Sep 17 00:00:00 2001 From: Aviv Faraj <73610201+avivfaraj@users.noreply.github.com> Date: Mon, 6 Sep 2021 13:58:46 -0400 Subject: [PATCH 10/27] Deleted duplicate file --- maths/gamma_recursive.py | 83 ---------------------------------------- 1 file changed, 83 deletions(-) delete mode 100644 maths/gamma_recursive.py diff --git a/maths/gamma_recursive.py b/maths/gamma_recursive.py deleted file mode 100644 index e7e2c70c7ebe..000000000000 --- a/maths/gamma_recursive.py +++ /dev/null @@ -1,83 +0,0 @@ -""" -Gamma function is a very useful tool in physics. -It helps calculating complex integral in a convenient way. -for more info: https://en.wikipedia.org/wiki/Gamma_function -""" - -# Importing packages -from math import pi, sqrt -from re import match -from typing import Union - - -def gamma(num: Union[int, float]) -> Union[int, float]: - """ - Calculates the value of Gamma function of num - where num is either an integer (1,2,3..) or a half-integer (0.5,1.5,2.5...). - Implemented using recursion - Examples: - >>> Gamma of: 0.5 - √π - >>> Gamma of: 2 - 1 - >>> Gamma of: 3.5 - 1.875√π - """ - if num == 1: - return 1 - elif num == 0.5: - return sqrt(pi) - elif num > 1: - return (num - 1) * gamma(num - 1) - # Error - return -2 - - -def test_gamma() -> None: - """ - >>> test_gamma() - """ - assert sqrt(pi) == gamma(0.5) - assert 1 == gamma(1) - assert 1 == gamma(2) - - -if __name__ == "__main__": - # Initialize boolean - number = True - # Get input from user - input_ = input("Gamma of: ") - # Ensure valid input - try: - # Ensure input matches half-integer (float) pattern - if match(r"^[0-9]*\.5$", input_): - # Convert string to float - num = float(input_) - # Ensure input matches an integer pattern - elif match(r"^[1-9][0-9]*$", input_): - # Convert string to int - num = int(input_) - # Input is not a valid number - else: - # raise an error - raise ValueError - # Ensure print an error message - except ValueError: - print("Error: Input must be an integer or an half-integer!") - number = False - finally: - # Ensure input is a valid number - if number: - print(f"\u0393({num}) = ", end="") - # Ensure input is an integer - if isinstance(gamma(num), int): - # Print result - print(gamma(num)) - # Otherwise print results with √π (gamma of 0.5 is √π) - # Therefore all results will be a number times √π - else: - results = f"{gamma(num) / sqrt(pi):.4f}" - results = results.rstrip("0").rstrip(".") - if results == "1": - results = "" - print(results + "\u221A\u03c0") From cea2deb0c8348f91cd566f327a3468fcfd98beec Mon Sep 17 00:00:00 2001 From: Aviv Faraj <73610201+avivfaraj@users.noreply.github.com> Date: Mon, 6 Sep 2021 13:59:46 -0400 Subject: [PATCH 11/27] removed pi --- physics/horizontal_projectile_motion.py | 1 - 1 file changed, 1 deletion(-) diff --git a/physics/horizontal_projectile_motion.py b/physics/horizontal_projectile_motion.py index 7147da1949a5..7ec370579810 100644 --- a/physics/horizontal_projectile_motion.py +++ b/physics/horizontal_projectile_motion.py @@ -15,7 +15,6 @@ """ # Importing packages -from math import pi from math import radians as angle_to_radians from math import sin From db7db3b3709ccb0b9c5a34e1b381b4b2693906f5 Mon Sep 17 00:00:00 2001 From: Aviv Faraj <73610201+avivfaraj@users.noreply.github.com> Date: Tue, 7 Sep 2021 09:31:36 -0400 Subject: [PATCH 12/27] reversed tests --- physics/horizontal_projectile_motion.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/physics/horizontal_projectile_motion.py b/physics/horizontal_projectile_motion.py index 7ec370579810..551c4089cd95 100644 --- a/physics/horizontal_projectile_motion.py +++ b/physics/horizontal_projectile_motion.py @@ -92,9 +92,9 @@ def test_motion() -> None: >>> test_motion() """ v0, angle = 25, 20 - assert 40.97 == horizontal_distance(v0, angle) - assert 3.73 == max_height(v0, angle) - assert 1.74 == total_time(v0, angle) + assert horizontal_distance(v0, angle) == 40.97 + assert max_height(v0, angle) == 3.73 + assert total_time(v0, angle) == 1.74 if __name__ == "__main__": From 132e4952ee8f20aebcd1b02232f38c3103c4906d Mon Sep 17 00:00:00 2001 From: Aviv Faraj <73610201+avivfaraj@users.noreply.github.com> Date: Tue, 7 Sep 2021 09:32:55 -0400 Subject: [PATCH 13/27] Fixed angle condition --- physics/horizontal_projectile_motion.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/physics/horizontal_projectile_motion.py b/physics/horizontal_projectile_motion.py index 551c4089cd95..ea90b5603771 100644 --- a/physics/horizontal_projectile_motion.py +++ b/physics/horizontal_projectile_motion.py @@ -106,7 +106,7 @@ def test_motion() -> None: angle = float(input("angle: ")) # Ensure valid angle - if angle > 90 or angle <= 0: + if angle > 90 or angle < 1: print("Error: Invalid angle. Range is 1-90 degrees.") # Ensure valid velocity From c0e5071e797e0621ce0034111d48514f4d3b929b Mon Sep 17 00:00:00 2001 From: Aviv Faraj <73610201+avivfaraj@users.noreply.github.com> Date: Tue, 7 Sep 2021 09:35:30 -0400 Subject: [PATCH 14/27] Modified prints to f-string --- physics/horizontal_projectile_motion.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/physics/horizontal_projectile_motion.py b/physics/horizontal_projectile_motion.py index ea90b5603771..96afc92b2241 100644 --- a/physics/horizontal_projectile_motion.py +++ b/physics/horizontal_projectile_motion.py @@ -120,6 +120,6 @@ def test_motion() -> None: v_dis = str(max_height(init_vel, angle)) t_time = str(total_time(init_vel, angle)) print("Results: ") - print("Horizontal Distance: " + h_dis + " [m]") - print("Maximum Height: " + v_dis + " [m]") - print("Total Time: " + t_time + " [s]") + print(f"Horizontal Distance: {h_dis} [m]") + print(f"Maximum Height: {v_dis} [m]") + print(f"Total Time: {t_time} [s]") From 894fa7f0098efc67592b4eb0d7aac068790f1657 Mon Sep 17 00:00:00 2001 From: Aviv Faraj <73610201+avivfaraj@users.noreply.github.com> Date: Tue, 7 Sep 2021 09:37:33 -0400 Subject: [PATCH 15/27] Update horizontal_projectile_motion.py --- physics/horizontal_projectile_motion.py | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/physics/horizontal_projectile_motion.py b/physics/horizontal_projectile_motion.py index 96afc92b2241..0ae6348ea5e5 100644 --- a/physics/horizontal_projectile_motion.py +++ b/physics/horizontal_projectile_motion.py @@ -116,10 +116,7 @@ def test_motion() -> None: # Print results else: print() - h_dis = str(horizontal_distance(init_vel, angle)) - v_dis = str(max_height(init_vel, angle)) - t_time = str(total_time(init_vel, angle)) print("Results: ") - print(f"Horizontal Distance: {h_dis} [m]") - print(f"Maximum Height: {v_dis} [m]") - print(f"Total Time: {t_time} [s]") + print(f"Horizontal Distance: {str(horizontal_distance(init_vel, angle))} [m]") + print(f"Maximum Height: {str(max_height(init_vel, angle))} [m]") + print(f"Total Time: {str(total_time(init_vel, angle))} [s]") From 07646ac92b6510e595c20211f374af8fc7e97df2 Mon Sep 17 00:00:00 2001 From: Aviv Faraj <73610201+avivfaraj@users.noreply.github.com> Date: Tue, 7 Sep 2021 14:41:47 -0400 Subject: [PATCH 16/27] Update horizontal_projectile_motion.py --- physics/horizontal_projectile_motion.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/physics/horizontal_projectile_motion.py b/physics/horizontal_projectile_motion.py index 0ae6348ea5e5..1fb02f42cc2a 100644 --- a/physics/horizontal_projectile_motion.py +++ b/physics/horizontal_projectile_motion.py @@ -21,7 +21,6 @@ # Acceleration Constant on hearth (unit m/s^2) g = 9.80665 - def horizontal_distance(init_velocity: float, angle: float) -> float: """ Returns the horizontal distance that the object cover @@ -40,7 +39,7 @@ def horizontal_distance(init_velocity: float, angle: float) -> float: 414.76 """ radians = angle_to_radians(2 * angle) - return round((init_velocity ** 2) * sin(radians) / g, 2) + return round(init_velocity ** 2 * sin(radians) / g, 2) def max_height(init_velocity: float, angle: float) -> float: @@ -62,7 +61,7 @@ def max_height(init_velocity: float, angle: float) -> float: """ radians = angle_to_radians(angle) - return round(((init_velocity ** 2) * (sin(radians)) ** 2) / (2 * g), 2) + return round((init_velocity ** 2 * sin(radians) ** 2) / (2 * g), 2) def total_time(init_velocity: float, angle: float) -> float: @@ -84,7 +83,7 @@ def total_time(init_velocity: float, angle: float) -> float: """ radians = angle_to_radians(angle) - return round((2 * init_velocity) * (sin(radians)) / g, 2) + return round(2 * init_velocity * sin(radians) / g, 2) def test_motion() -> None: From 4e2fcaf6fbbf41b77c3538c60dd44a908982b27c Mon Sep 17 00:00:00 2001 From: Username Date: Tue, 7 Sep 2021 15:15:08 -0400 Subject: [PATCH 17/27] Fixes #4710 added exceptions and tests --- physics/horizontal_projectile_motion.py | 79 +++++++++++++++++++------ 1 file changed, 60 insertions(+), 19 deletions(-) diff --git a/physics/horizontal_projectile_motion.py b/physics/horizontal_projectile_motion.py index 1fb02f42cc2a..9abb7ede4cfe 100644 --- a/physics/horizontal_projectile_motion.py +++ b/physics/horizontal_projectile_motion.py @@ -21,6 +21,28 @@ # Acceleration Constant on hearth (unit m/s^2) g = 9.80665 + +def check_args(init_velocity: float, angle: float) -> None: + """ + Check that the arguments are valid + """ + + # Ensure valid instance + if not isinstance(init_velocity, (int, float)): + raise TypeError("Invalid velocity. Should be a positive number.") + + if not isinstance(angle, (int, float)): + raise TypeError("Invalid angle. Range is 1-90 degrees.") + + # Ensure valid angle + if angle > 90 or angle < 1: + raise ValueError("Invalid angle. Range is 1-90 degrees.") + + # Ensure valid velocity + if init_velocity < 0: + raise ValueError("Invalid velocity. Should be a positive number.") + + def horizontal_distance(init_velocity: float, angle: float) -> float: """ Returns the horizontal distance that the object cover @@ -37,7 +59,16 @@ def horizontal_distance(init_velocity: float, angle: float) -> float: 91.77 >>> horizontal_distance(100, 78) 414.76 + >>> horizontal_distance(-1, 20) + Traceback (most recent call last): + ... + ValueError: Invalid velocity. Should be a positive number. + >>> horizontal_distance(30, -20) + Traceback (most recent call last): + ... + ValueError: Invalid angle. Range is 1-90 degrees. """ + check_args(init_velocity, angle) radians = angle_to_radians(2 * angle) return round(init_velocity ** 2 * sin(radians) / g, 2) @@ -58,10 +89,18 @@ def max_height(init_velocity: float, angle: float) -> float: 22.94 >>> max_height(100, 78) 487.82 + >>> max_height("a", 20) + Traceback (most recent call last): + ... + TypeError: Invalid velocity. Should be a positive number. + >>> horizontal_distance(30, "b") + Traceback (most recent call last): + ... + TypeError: Invalid angle. Range is 1-90 degrees. """ - + check_args(init_velocity, angle) radians = angle_to_radians(angle) - return round((init_velocity ** 2 * sin(radians) ** 2) / (2 * g), 2) + return round(init_velocity ** 2 * sin(radians) ** 2 / (2 * g), 2) def total_time(init_velocity: float, angle: float) -> float: @@ -80,8 +119,16 @@ def total_time(init_velocity: float, angle: float) -> float: 4.33 >>> total_time(100, 78) 19.95 + >>> total_time(-10, 40) + Traceback (most recent call last): + ... + ValueError: Invalid velocity. Should be a positive number. + >>> total_time(30, "b") + Traceback (most recent call last): + ... + TypeError: Invalid angle. Range is 1-90 degrees. """ - + check_args(init_velocity, angle) radians = angle_to_radians(angle) return round(2 * init_velocity * sin(radians) / g, 2) @@ -97,25 +144,19 @@ def test_motion() -> None: if __name__ == "__main__": + from doctest import testmod - # Get input from user - init_vel = float(input("Initial Velocity: ")) + testmod() # Get input from user - angle = float(input("angle: ")) + init_vel = float(input("Initial Velocity: ").strip()) - # Ensure valid angle - if angle > 90 or angle < 1: - print("Error: Invalid angle. Range is 1-90 degrees.") - - # Ensure valid velocity - elif init_vel < 0: - print("Error: Invalid velocity. Should be a positive number.") + # Get input from user + angle = float(input("angle: ").strip()) # Print results - else: - print() - print("Results: ") - print(f"Horizontal Distance: {str(horizontal_distance(init_vel, angle))} [m]") - print(f"Maximum Height: {str(max_height(init_vel, angle))} [m]") - print(f"Total Time: {str(total_time(init_vel, angle))} [s]") + print() + print("Results: ") + print(f"Horizontal Distance: {str(horizontal_distance(init_vel, angle))} [m]") + print(f"Maximum Height: {str(max_height(init_vel, angle))} [m]") + print(f"Total Time: {str(total_time(init_vel, angle))} [s]") From dcacc95b9d5c7fe70951b9655551e6708130a212 Mon Sep 17 00:00:00 2001 From: Username Date: Tue, 7 Sep 2021 15:18:18 -0400 Subject: [PATCH 18/27] Added float tests --- physics/horizontal_projectile_motion.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/physics/horizontal_projectile_motion.py b/physics/horizontal_projectile_motion.py index 9abb7ede4cfe..404164f1882c 100644 --- a/physics/horizontal_projectile_motion.py +++ b/physics/horizontal_projectile_motion.py @@ -59,6 +59,8 @@ def horizontal_distance(init_velocity: float, angle: float) -> float: 91.77 >>> horizontal_distance(100, 78) 414.76 + >>> horizontal_distance(4.5, 5.5) + 0.39 >>> horizontal_distance(-1, 20) Traceback (most recent call last): ... @@ -89,6 +91,8 @@ def max_height(init_velocity: float, angle: float) -> float: 22.94 >>> max_height(100, 78) 487.82 + >>> max_height(4.5, 5.5) + 0.01 >>> max_height("a", 20) Traceback (most recent call last): ... @@ -119,6 +123,8 @@ def total_time(init_velocity: float, angle: float) -> float: 4.33 >>> total_time(100, 78) 19.95 + >>> total_time(4.5, 5.5) + 0.09 >>> total_time(-10, 40) Traceback (most recent call last): ... From 3f2b238c34cd926b335d1f6f750e009f08e8f270 Mon Sep 17 00:00:00 2001 From: Username Date: Tue, 14 Sep 2021 15:30:42 -0400 Subject: [PATCH 19/27] Fixed type annotations --- ciphers/enigma_machine2.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/ciphers/enigma_machine2.py b/ciphers/enigma_machine2.py index f4ce5a075f46..b2b2953f4bc1 100644 --- a/ciphers/enigma_machine2.py +++ b/ciphers/enigma_machine2.py @@ -14,9 +14,9 @@ Created by TrapinchO """ - -RotorPositionT = tuple[int, int, int] -RotorSelectionT = tuple[str, str, str] +from typing import Tuple, Dict +RotorPositionT = Tuple[int, int, int] +RotorSelectionT = Tuple[str, str, str] # used alphabet -------------------------- @@ -69,7 +69,7 @@ def _validator( rotpos: RotorPositionT, rotsel: RotorSelectionT, pb: str -) -> tuple[RotorPositionT, RotorSelectionT, dict[str, str]]: +) -> tuple[RotorPositionT, RotorSelectionT, Dict[str, str]]: """ Checks if the values can be used for the 'enigma' function @@ -110,7 +110,7 @@ def _validator( return rotpos, rotsel, pbdict -def _plugboard(pbstring: str) -> dict[str, str]: +def _plugboard(pbstring: str) -> Dict[str, str]: """ https://en.wikipedia.org/wiki/Enigma_machine#Plugboard From e3678fdeadd23f1bfca27015ab524efa184f6c79 Mon Sep 17 00:00:00 2001 From: Username Date: Tue, 14 Sep 2021 15:35:51 -0400 Subject: [PATCH 20/27] Fixed last annotation --- ciphers/enigma_machine2.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ciphers/enigma_machine2.py b/ciphers/enigma_machine2.py index b2b2953f4bc1..3ed94c59de60 100644 --- a/ciphers/enigma_machine2.py +++ b/ciphers/enigma_machine2.py @@ -69,7 +69,7 @@ def _validator( rotpos: RotorPositionT, rotsel: RotorSelectionT, pb: str -) -> tuple[RotorPositionT, RotorSelectionT, Dict[str, str]]: +) -> Tuple[RotorPositionT, RotorSelectionT, Dict[str, str]]: """ Checks if the values can be used for the 'enigma' function From c37bb9540834cb77e37822eb376a5896cda34778 Mon Sep 17 00:00:00 2001 From: Username Date: Tue, 14 Sep 2021 15:42:02 -0400 Subject: [PATCH 21/27] Fixed annotations --- ciphers/a1z26.py | 7 +++++-- ciphers/trafid_cipher.py | 11 +++++++---- ciphers/xor_cipher.py | 7 +++++-- 3 files changed, 17 insertions(+), 8 deletions(-) diff --git a/ciphers/a1z26.py b/ciphers/a1z26.py index e6684fb1e6fc..8f038a4026d2 100644 --- a/ciphers/a1z26.py +++ b/ciphers/a1z26.py @@ -7,7 +7,10 @@ """ -def encode(plain: str) -> list[int]: +from typing import List + + +def encode(plain: str) -> List[int]: """ >>> encode("myname") [13, 25, 14, 1, 13, 5] @@ -15,7 +18,7 @@ def encode(plain: str) -> list[int]: return [ord(elem) - 96 for elem in plain] -def decode(encoded: list[int]) -> str: +def decode(encoded: List[int]) -> str: """ >>> decode([13, 25, 14, 1, 13, 5]) 'myname' diff --git a/ciphers/trafid_cipher.py b/ciphers/trafid_cipher.py index 1c8ea3024d33..f4bf45b3dab7 100644 --- a/ciphers/trafid_cipher.py +++ b/ciphers/trafid_cipher.py @@ -1,7 +1,10 @@ # https://en.wikipedia.org/wiki/Trifid_cipher -def __encryptPart(messagePart: str, character2Number: dict[str, str]) -> str: +from typing import Tuple, Dict + + +def __encryptPart(messagePart: str, character2Number: Dict[str, str]) -> str: one, two, three = "", "", "" tmp = [] @@ -17,8 +20,8 @@ def __encryptPart(messagePart: str, character2Number: dict[str, str]) -> str: def __decryptPart( - messagePart: str, character2Number: dict[str, str] -) -> tuple[str, str, str]: + messagePart: str, character2Number: Dict[str, str] +) -> Tuple[str, str, str]: tmp, thisPart = "", "" result = [] @@ -36,7 +39,7 @@ def __decryptPart( def __prepare( message: str, alphabet: str -) -> tuple[str, str, dict[str, str], dict[str, str]]: +) -> Tuple[str, str, Dict[str, str], Dict[str, str]]: # Validate message and alphabet, set to upper and remove spaces alphabet = alphabet.replace(" ", "").upper() message = message.replace(" ", "").upper() diff --git a/ciphers/xor_cipher.py b/ciphers/xor_cipher.py index 12d580e720bc..6bd22ea24169 100644 --- a/ciphers/xor_cipher.py +++ b/ciphers/xor_cipher.py @@ -18,6 +18,9 @@ """ +from typing import List + + class XORCipher: def __init__(self, key: int = 0): """ @@ -28,7 +31,7 @@ def __init__(self, key: int = 0): # private field self.__key = key - def encrypt(self, content: str, key: int) -> list[str]: + def encrypt(self, content: str, key: int) -> List[str]: """ input: 'content' of type string and 'key' of type int output: encrypted string 'content' as a list of chars @@ -53,7 +56,7 @@ def encrypt(self, content: str, key: int) -> list[str]: return ans - def decrypt(self, content: str, key: int) -> list[str]: + def decrypt(self, content: str, key: int) -> List[str]: """ input: 'content' of type list and 'key' of type int output: decrypted string 'content' as a list of chars From 5b0249ac0a0f9c36c3cfbab8423eb72925a73ffb Mon Sep 17 00:00:00 2001 From: Username Date: Tue, 14 Sep 2021 15:44:10 -0400 Subject: [PATCH 22/27] fixed format --- ciphers/enigma_machine2.py | 3 ++- ciphers/trafid_cipher.py | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/ciphers/enigma_machine2.py b/ciphers/enigma_machine2.py index 3ed94c59de60..771c4db0da5b 100644 --- a/ciphers/enigma_machine2.py +++ b/ciphers/enigma_machine2.py @@ -14,7 +14,8 @@ Created by TrapinchO """ -from typing import Tuple, Dict +from typing import Dict, Tuple + RotorPositionT = Tuple[int, int, int] RotorSelectionT = Tuple[str, str, str] diff --git a/ciphers/trafid_cipher.py b/ciphers/trafid_cipher.py index f4bf45b3dab7..b0800b0f2bff 100644 --- a/ciphers/trafid_cipher.py +++ b/ciphers/trafid_cipher.py @@ -1,7 +1,7 @@ # https://en.wikipedia.org/wiki/Trifid_cipher -from typing import Tuple, Dict +from typing import Dict, Tuple def __encryptPart(messagePart: str, character2Number: Dict[str, str]) -> str: From 06e1eb7d55319ed8552b357c677cf92fd099a309 Mon Sep 17 00:00:00 2001 From: Aviv Faraj <73610201+avivfaraj@users.noreply.github.com> Date: Tue, 14 Sep 2021 16:55:11 -0400 Subject: [PATCH 23/27] Add files via upload --- ciphers/a1z26.py | 7 ++++--- ciphers/enigma_machine2.py | 8 +++++--- ciphers/trafid_cipher.py | 10 +++++----- ciphers/xor_cipher.py | 6 +++--- 4 files changed, 17 insertions(+), 14 deletions(-) diff --git a/ciphers/a1z26.py b/ciphers/a1z26.py index 8f038a4026d2..cd88e0243695 100644 --- a/ciphers/a1z26.py +++ b/ciphers/a1z26.py @@ -7,10 +7,11 @@ """ -from typing import List +# from typing import List +from __future__ import annotations -def encode(plain: str) -> List[int]: +def encode(plain: str) -> list[int]: """ >>> encode("myname") [13, 25, 14, 1, 13, 5] @@ -18,7 +19,7 @@ def encode(plain: str) -> List[int]: return [ord(elem) - 96 for elem in plain] -def decode(encoded: List[int]) -> str: +def decode(encoded: list[int]) -> str: """ >>> decode([13, 25, 14, 1, 13, 5]) 'myname' diff --git a/ciphers/enigma_machine2.py b/ciphers/enigma_machine2.py index 771c4db0da5b..09ab679726a4 100644 --- a/ciphers/enigma_machine2.py +++ b/ciphers/enigma_machine2.py @@ -14,7 +14,9 @@ Created by TrapinchO """ -from typing import Dict, Tuple +from __future__ import annotations + +from typing import Tuple RotorPositionT = Tuple[int, int, int] RotorSelectionT = Tuple[str, str, str] @@ -70,7 +72,7 @@ def _validator( rotpos: RotorPositionT, rotsel: RotorSelectionT, pb: str -) -> Tuple[RotorPositionT, RotorSelectionT, Dict[str, str]]: +) -> tuple[RotorPositionT, RotorSelectionT, dict[str, str]]: """ Checks if the values can be used for the 'enigma' function @@ -111,7 +113,7 @@ def _validator( return rotpos, rotsel, pbdict -def _plugboard(pbstring: str) -> Dict[str, str]: +def _plugboard(pbstring: str) -> dict[str, str]: """ https://en.wikipedia.org/wiki/Enigma_machine#Plugboard diff --git a/ciphers/trafid_cipher.py b/ciphers/trafid_cipher.py index b0800b0f2bff..497e8ca4286f 100644 --- a/ciphers/trafid_cipher.py +++ b/ciphers/trafid_cipher.py @@ -1,10 +1,10 @@ # https://en.wikipedia.org/wiki/Trifid_cipher -from typing import Dict, Tuple +from __future__ import annotations -def __encryptPart(messagePart: str, character2Number: Dict[str, str]) -> str: +def __encryptPart(messagePart: str, character2Number: dict[str, str]) -> str: one, two, three = "", "", "" tmp = [] @@ -20,8 +20,8 @@ def __encryptPart(messagePart: str, character2Number: Dict[str, str]) -> str: def __decryptPart( - messagePart: str, character2Number: Dict[str, str] -) -> Tuple[str, str, str]: + messagePart: str, character2Number: dict[str, str] +) -> tuple[str, str, str]: tmp, thisPart = "", "" result = [] @@ -39,7 +39,7 @@ def __decryptPart( def __prepare( message: str, alphabet: str -) -> Tuple[str, str, Dict[str, str], Dict[str, str]]: +) -> tuple[str, str, dict[str, str], dict[str, str]]: # Validate message and alphabet, set to upper and remove spaces alphabet = alphabet.replace(" ", "").upper() message = message.replace(" ", "").upper() diff --git a/ciphers/xor_cipher.py b/ciphers/xor_cipher.py index 6bd22ea24169..7b4e55193ec1 100644 --- a/ciphers/xor_cipher.py +++ b/ciphers/xor_cipher.py @@ -18,7 +18,7 @@ """ -from typing import List +from __future__ import annotations class XORCipher: @@ -31,7 +31,7 @@ def __init__(self, key: int = 0): # private field self.__key = key - def encrypt(self, content: str, key: int) -> List[str]: + def encrypt(self, content: str, key: int) -> list[str]: """ input: 'content' of type string and 'key' of type int output: encrypted string 'content' as a list of chars @@ -56,7 +56,7 @@ def encrypt(self, content: str, key: int) -> List[str]: return ans - def decrypt(self, content: str, key: int) -> List[str]: + def decrypt(self, content: str, key: int) -> list[str]: """ input: 'content' of type list and 'key' of type int output: decrypted string 'content' as a list of chars From 77365dc951e0d6a0fae6282b931c2620d382131e Mon Sep 17 00:00:00 2001 From: Aviv Faraj <73610201+avivfaraj@users.noreply.github.com> Date: Tue, 14 Sep 2021 16:55:34 -0400 Subject: [PATCH 24/27] Update a1z26.py --- ciphers/a1z26.py | 1 - 1 file changed, 1 deletion(-) diff --git a/ciphers/a1z26.py b/ciphers/a1z26.py index cd88e0243695..06f14075de6f 100644 --- a/ciphers/a1z26.py +++ b/ciphers/a1z26.py @@ -7,7 +7,6 @@ """ -# from typing import List from __future__ import annotations From 2c9d1268d9dac0924fe047b9e1f7b7f25ba745e3 Mon Sep 17 00:00:00 2001 From: Aviv Faraj <73610201+avivfaraj@users.noreply.github.com> Date: Tue, 14 Sep 2021 17:00:31 -0400 Subject: [PATCH 25/27] Update enigma_machine2.py --- ciphers/enigma_machine2.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ciphers/enigma_machine2.py b/ciphers/enigma_machine2.py index 09ab679726a4..441a829c65ca 100644 --- a/ciphers/enigma_machine2.py +++ b/ciphers/enigma_machine2.py @@ -15,9 +15,9 @@ Created by TrapinchO """ from __future__ import annotations - from typing import Tuple + RotorPositionT = Tuple[int, int, int] RotorSelectionT = Tuple[str, str, str] From 85b233ddcae8ff65df7b7c39e7c951036fbdac55 Mon Sep 17 00:00:00 2001 From: Aviv Faraj <73610201+avivfaraj@users.noreply.github.com> Date: Tue, 14 Sep 2021 17:06:39 -0400 Subject: [PATCH 26/27] Delete horizontal_projectile_motion.py --- physics/horizontal_projectile_motion.py | 168 ------------------------ 1 file changed, 168 deletions(-) delete mode 100644 physics/horizontal_projectile_motion.py diff --git a/physics/horizontal_projectile_motion.py b/physics/horizontal_projectile_motion.py deleted file mode 100644 index 404164f1882c..000000000000 --- a/physics/horizontal_projectile_motion.py +++ /dev/null @@ -1,168 +0,0 @@ -""" -Horizontal Projectile Motion problem in physics. -This algorithm solves a specific problem in which -the motion starts from the ground as can be seen below: - (v = 0) - ** - * * - * * - * * - * * - * * -GROUND GROUND - -For more info: https://en.wikipedia.org/wiki/Projectile_motion -""" - -# Importing packages -from math import radians as angle_to_radians -from math import sin - -# Acceleration Constant on hearth (unit m/s^2) -g = 9.80665 - - -def check_args(init_velocity: float, angle: float) -> None: - """ - Check that the arguments are valid - """ - - # Ensure valid instance - if not isinstance(init_velocity, (int, float)): - raise TypeError("Invalid velocity. Should be a positive number.") - - if not isinstance(angle, (int, float)): - raise TypeError("Invalid angle. Range is 1-90 degrees.") - - # Ensure valid angle - if angle > 90 or angle < 1: - raise ValueError("Invalid angle. Range is 1-90 degrees.") - - # Ensure valid velocity - if init_velocity < 0: - raise ValueError("Invalid velocity. Should be a positive number.") - - -def horizontal_distance(init_velocity: float, angle: float) -> float: - """ - Returns the horizontal distance that the object cover - - Formula: - v_0^2 * sin(2 * alpha) - --------------------- - g - - v_0 - initial velocity - alpha - angle - - >>> horizontal_distance(30, 45) - 91.77 - >>> horizontal_distance(100, 78) - 414.76 - >>> horizontal_distance(4.5, 5.5) - 0.39 - >>> horizontal_distance(-1, 20) - Traceback (most recent call last): - ... - ValueError: Invalid velocity. Should be a positive number. - >>> horizontal_distance(30, -20) - Traceback (most recent call last): - ... - ValueError: Invalid angle. Range is 1-90 degrees. - """ - check_args(init_velocity, angle) - radians = angle_to_radians(2 * angle) - return round(init_velocity ** 2 * sin(radians) / g, 2) - - -def max_height(init_velocity: float, angle: float) -> float: - """ - Returns the maximum height that the object reach - - Formula: - v_0^2 * sin^2(alpha) - -------------------- - 2g - - v_0 - initial velocity - alpha - angle - - >>> max_height(30, 45) - 22.94 - >>> max_height(100, 78) - 487.82 - >>> max_height(4.5, 5.5) - 0.01 - >>> max_height("a", 20) - Traceback (most recent call last): - ... - TypeError: Invalid velocity. Should be a positive number. - >>> horizontal_distance(30, "b") - Traceback (most recent call last): - ... - TypeError: Invalid angle. Range is 1-90 degrees. - """ - check_args(init_velocity, angle) - radians = angle_to_radians(angle) - return round(init_velocity ** 2 * sin(radians) ** 2 / (2 * g), 2) - - -def total_time(init_velocity: float, angle: float) -> float: - """ - Returns total time of the motion - - Formula: - 2 * v_0 * sin(alpha) - -------------------- - g - - v_0 - initial velocity - alpha - angle - - >>> total_time(30, 45) - 4.33 - >>> total_time(100, 78) - 19.95 - >>> total_time(4.5, 5.5) - 0.09 - >>> total_time(-10, 40) - Traceback (most recent call last): - ... - ValueError: Invalid velocity. Should be a positive number. - >>> total_time(30, "b") - Traceback (most recent call last): - ... - TypeError: Invalid angle. Range is 1-90 degrees. - """ - check_args(init_velocity, angle) - radians = angle_to_radians(angle) - return round(2 * init_velocity * sin(radians) / g, 2) - - -def test_motion() -> None: - """ - >>> test_motion() - """ - v0, angle = 25, 20 - assert horizontal_distance(v0, angle) == 40.97 - assert max_height(v0, angle) == 3.73 - assert total_time(v0, angle) == 1.74 - - -if __name__ == "__main__": - from doctest import testmod - - testmod() - - # Get input from user - init_vel = float(input("Initial Velocity: ").strip()) - - # Get input from user - angle = float(input("angle: ").strip()) - - # Print results - print() - print("Results: ") - print(f"Horizontal Distance: {str(horizontal_distance(init_vel, angle))} [m]") - print(f"Maximum Height: {str(max_height(init_vel, angle))} [m]") - print(f"Total Time: {str(total_time(init_vel, angle))} [s]") From 14410dff7a49b4359bb1f494ffc959cf8f01ac4e Mon Sep 17 00:00:00 2001 From: Aviv Faraj <73610201+avivfaraj@users.noreply.github.com> Date: Tue, 14 Sep 2021 17:35:57 -0400 Subject: [PATCH 27/27] Update enigma_machine2.py --- ciphers/enigma_machine2.py | 1 + 1 file changed, 1 insertion(+) diff --git a/ciphers/enigma_machine2.py b/ciphers/enigma_machine2.py index 441a829c65ca..dfc3d0c42cf4 100644 --- a/ciphers/enigma_machine2.py +++ b/ciphers/enigma_machine2.py @@ -15,6 +15,7 @@ Created by TrapinchO """ from __future__ import annotations + from typing import Tuple