From 3986d64af1a20ec9244331439b520c894528af4c Mon Sep 17 00:00:00 2001 From: Erdum Date: Sun, 22 Nov 2020 21:41:27 +0500 Subject: [PATCH 01/16] New algorithm added --- Electronics/ohms_law.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 Electronics/ohms_law.py diff --git a/Electronics/ohms_law.py b/Electronics/ohms_law.py new file mode 100644 index 000000000000..8109d61cd76a --- /dev/null +++ b/Electronics/ohms_law.py @@ -0,0 +1,13 @@ +# Defining function +def ohms_law(v: float = 0, i: float = 0, r: float = 0) -> float: + if v == 0: + result = i * r + return result + elif i == 0: + result = v / r + return result + elif r == 0: + result = v / i + return result + else: + return 0 From fa26af697bd9f321b0f0f37b78b4c3edabe30706 Mon Sep 17 00:00:00 2001 From: Erdum Date: Sun, 22 Nov 2020 22:50:28 +0500 Subject: [PATCH 02/16] Errors resolvedc --- Electronics/ohms_law.py | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/Electronics/ohms_law.py b/Electronics/ohms_law.py index 8109d61cd76a..b21708984337 100644 --- a/Electronics/ohms_law.py +++ b/Electronics/ohms_law.py @@ -1,7 +1,22 @@ +# Importing doctest for testing our function +from doctest import testmod + # Defining function + + def ohms_law(v: float = 0, i: float = 0, r: float = 0) -> float: + + """ + This function calculates the any one + of the three fundemental value, of + electronics. + >>> ohms_law(v=10, r=5) + 2.0 + >>> ohms_law(i=1, r=10) + 10.0 + """ if v == 0: - result = i * r + result = float(i * r) return result elif i == 0: result = v / r @@ -11,3 +26,7 @@ def ohms_law(v: float = 0, i: float = 0, r: float = 0) -> float: return result else: return 0 + + +if __name__ == "__main__": + testmod(name="ohms_law", verbose=True) From dd10b84fbcb6a316f0333be6055ecc37bc772511 Mon Sep 17 00:00:00 2001 From: Erdum Date: Sun, 22 Nov 2020 22:59:01 +0500 Subject: [PATCH 03/16] New Algorithm --- Electronics/ohms_law.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Electronics/ohms_law.py b/Electronics/ohms_law.py index b21708984337..ab666160a427 100644 --- a/Electronics/ohms_law.py +++ b/Electronics/ohms_law.py @@ -8,7 +8,7 @@ def ohms_law(v: float = 0, i: float = 0, r: float = 0) -> float: """ This function calculates the any one - of the three fundemental value, of + of the three fundemental values (v, i, r), of electronics. >>> ohms_law(v=10, r=5) 2.0 From 01cf2d3fbbc3043dff02cb040b8d65e6414c1c9f Mon Sep 17 00:00:00 2001 From: Erdum Date: Sun, 22 Nov 2020 23:07:45 +0500 Subject: [PATCH 04/16] New algorithm added --- Electronics/ohms_law.py | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/Electronics/ohms_law.py b/Electronics/ohms_law.py index ab666160a427..95152b45e56c 100644 --- a/Electronics/ohms_law.py +++ b/Electronics/ohms_law.py @@ -4,25 +4,25 @@ # Defining function -def ohms_law(v: float = 0, i: float = 0, r: float = 0) -> float: +def ohms_law(voltage: float, current: float, resistance: float) -> float: """ This function calculates the any one - of the three fundemental values (v, i, r), of - electronics. - >>> ohms_law(v=10, r=5) + of the three fundemental values voltage, + current, resistance, of electronics. + >>> ohms_law(voltage=10, resistance=5,current=0 ) 2.0 - >>> ohms_law(i=1, r=10) + >>> ohms_law(current=1, resistance=10, voltage=0) 10.0 """ - if v == 0: - result = float(i * r) + if voltage == 0: + result = float(current * resistance) return result - elif i == 0: - result = v / r + elif current == 0: + result = voltage / resistance return result - elif r == 0: - result = v / i + elif resistance == 0: + result = voltage / current return result else: return 0 From 79cc57cbb31ba5530de450de1dd8a026ba3d992d Mon Sep 17 00:00:00 2001 From: Erdum Date: Sun, 22 Nov 2020 23:15:33 +0500 Subject: [PATCH 05/16] Added new algorithm --- Electronics/ohms_law.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Electronics/ohms_law.py b/Electronics/ohms_law.py index 95152b45e56c..3b315e562b8c 100644 --- a/Electronics/ohms_law.py +++ b/Electronics/ohms_law.py @@ -8,7 +8,7 @@ def ohms_law(voltage: float, current: float, resistance: float) -> float: """ This function calculates the any one - of the three fundemental values voltage, + of the three fundemental ohms_law values voltage, current, resistance, of electronics. >>> ohms_law(voltage=10, resistance=5,current=0 ) 2.0 From 75c0eeaf67c0de67eb827b0d5bc34d1338f7fc02 Mon Sep 17 00:00:00 2001 From: Erdum Date: Sun, 22 Nov 2020 23:27:21 +0500 Subject: [PATCH 06/16] work --- Electronics/ohms_law.py | 32 -------------------------------- 1 file changed, 32 deletions(-) delete mode 100644 Electronics/ohms_law.py diff --git a/Electronics/ohms_law.py b/Electronics/ohms_law.py deleted file mode 100644 index 3b315e562b8c..000000000000 --- a/Electronics/ohms_law.py +++ /dev/null @@ -1,32 +0,0 @@ -# Importing doctest for testing our function -from doctest import testmod - -# Defining function - - -def ohms_law(voltage: float, current: float, resistance: float) -> float: - - """ - This function calculates the any one - of the three fundemental ohms_law values voltage, - current, resistance, of electronics. - >>> ohms_law(voltage=10, resistance=5,current=0 ) - 2.0 - >>> ohms_law(current=1, resistance=10, voltage=0) - 10.0 - """ - if voltage == 0: - result = float(current * resistance) - return result - elif current == 0: - result = voltage / resistance - return result - elif resistance == 0: - result = voltage / current - return result - else: - return 0 - - -if __name__ == "__main__": - testmod(name="ohms_law", verbose=True) From 71fa55004089b41523645ea00bb19fd60e6357db Mon Sep 17 00:00:00 2001 From: Erdum Date: Sun, 22 Nov 2020 23:29:34 +0500 Subject: [PATCH 07/16] New algorithm added --- electronics/ohms_law.py | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 electronics/ohms_law.py diff --git a/electronics/ohms_law.py b/electronics/ohms_law.py new file mode 100644 index 000000000000..3b315e562b8c --- /dev/null +++ b/electronics/ohms_law.py @@ -0,0 +1,32 @@ +# Importing doctest for testing our function +from doctest import testmod + +# Defining function + + +def ohms_law(voltage: float, current: float, resistance: float) -> float: + + """ + This function calculates the any one + of the three fundemental ohms_law values voltage, + current, resistance, of electronics. + >>> ohms_law(voltage=10, resistance=5,current=0 ) + 2.0 + >>> ohms_law(current=1, resistance=10, voltage=0) + 10.0 + """ + if voltage == 0: + result = float(current * resistance) + return result + elif current == 0: + result = voltage / resistance + return result + elif resistance == 0: + result = voltage / current + return result + else: + return 0 + + +if __name__ == "__main__": + testmod(name="ohms_law", verbose=True) From 873b6ebead5651c558c793733195289a28ef3df9 Mon Sep 17 00:00:00 2001 From: Erdum Date: Sun, 22 Nov 2020 23:42:10 +0500 Subject: [PATCH 08/16] Hope this is final --- electronics/ohms_law.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/electronics/ohms_law.py b/electronics/ohms_law.py index 3b315e562b8c..2d14acf66c4c 100644 --- a/electronics/ohms_law.py +++ b/electronics/ohms_law.py @@ -8,7 +8,7 @@ def ohms_law(voltage: float, current: float, resistance: float) -> float: """ This function calculates the any one - of the three fundemental ohms_law values voltage, + of the three ohms_law values voltage, current, resistance, of electronics. >>> ohms_law(voltage=10, resistance=5,current=0 ) 2.0 From 136887e86d66e7fb698a8feb8db94d9d719fef4d Mon Sep 17 00:00:00 2001 From: Erdum Date: Mon, 23 Nov 2020 17:18:44 +0500 Subject: [PATCH 09/16] Update electronics/ohms_law.py Co-authored-by: xcodz-dot <71920621+xcodz-dot@users.noreply.github.com> --- electronics/ohms_law.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/electronics/ohms_law.py b/electronics/ohms_law.py index 2d14acf66c4c..4107a7021889 100644 --- a/electronics/ohms_law.py +++ b/electronics/ohms_law.py @@ -4,7 +4,7 @@ # Defining function -def ohms_law(voltage: float, current: float, resistance: float) -> float: +def ohms_law(voltage: float = 0, current: float = 0, resistance: float = 0) -> float: """ This function calculates the any one From a120c71c345a4afb4d4c3f0c3f2c07655fc488fc Mon Sep 17 00:00:00 2001 From: Erdum Date: Tue, 24 Nov 2020 22:25:52 +0500 Subject: [PATCH 10/16] update decimal value & negative value test --- electronics/ohms_law.py | 27 +++++++++++++++++++++------ 1 file changed, 21 insertions(+), 6 deletions(-) diff --git a/electronics/ohms_law.py b/electronics/ohms_law.py index 4107a7021889..88f9572de166 100644 --- a/electronics/ohms_law.py +++ b/electronics/ohms_law.py @@ -10,17 +10,32 @@ def ohms_law(voltage: float = 0, current: float = 0, resistance: float = 0) -> f This function calculates the any one of the three ohms_law values voltage, current, resistance, of electronics. - >>> ohms_law(voltage=10, resistance=5,current=0 ) + Note: "resistance cannot be negative" + >>> ohms_law(voltage=10, resistance=5) 2.0 - >>> ohms_law(current=1, resistance=10, voltage=0) + >>> ohms_law(current=1, resistance=10) 10.0 + >>> ohms_law(voltage=1.5, resistance=10) + 0.15 + >>> ohms_law(voltage=-15, resistance=10) + -1.5 + >>> ohms_law(current=-1, resistance=2) + -2.0 + >>> ohms_law(voltage=-15, resistance=-10) + 0 """ if voltage == 0: - result = float(current * resistance) - return result + if resistance > 0: + result = float(current * resistance) + return result + else: + return 0 elif current == 0: - result = voltage / resistance - return result + if resistance > 0: + result = voltage / resistance + return result + else: + return 0 elif resistance == 0: result = voltage / current return result From b3d0f38cd5c749648d157dfa9009cb576d703509 Mon Sep 17 00:00:00 2001 From: Erdum Date: Tue, 24 Nov 2020 22:33:53 +0500 Subject: [PATCH 11/16] update as cclauss suggest --- electronics/ohms_law.py | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/electronics/ohms_law.py b/electronics/ohms_law.py index 88f9572de166..b69b0a46d017 100644 --- a/electronics/ohms_law.py +++ b/electronics/ohms_law.py @@ -4,24 +4,24 @@ # Defining function -def ohms_law(voltage: float = 0, current: float = 0, resistance: float = 0) -> float: +def ohms_law(voltage: float, current: float, resistance: float) -> float: """ This function calculates the any one of the three ohms_law values voltage, current, resistance, of electronics. Note: "resistance cannot be negative" - >>> ohms_law(voltage=10, resistance=5) + >>> ohms_law(voltage=10, resistance=5, current=0) 2.0 - >>> ohms_law(current=1, resistance=10) + >>> ohms_law(current=1, resistance=10, voltage=0) 10.0 - >>> ohms_law(voltage=1.5, resistance=10) + >>> ohms_law(voltage=1.5, resistance=10, current=0) 0.15 - >>> ohms_law(voltage=-15, resistance=10) + >>> ohms_law(voltage=-15, resistance=10, current=0) -1.5 - >>> ohms_law(current=-1, resistance=2) + >>> ohms_law(current=-1, resistance=2, voltage=0) -2.0 - >>> ohms_law(voltage=-15, resistance=-10) + >>> ohms_law(voltage=-15, resistance=-10, current=0) 0 """ if voltage == 0: @@ -29,7 +29,7 @@ def ohms_law(voltage: float = 0, current: float = 0, resistance: float = 0) -> f result = float(current * resistance) return result else: - return 0 + return 0 elif current == 0: if resistance > 0: result = voltage / resistance From d36b7a00cf5f1223a000d1c05a372820d8455916 Mon Sep 17 00:00:00 2001 From: Erdum Date: Tue, 24 Nov 2020 22:35:22 +0500 Subject: [PATCH 12/16] Update electronics/ohms_law.py Co-authored-by: Christian Clauss --- electronics/ohms_law.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/electronics/ohms_law.py b/electronics/ohms_law.py index b69b0a46d017..e96240fb1ba7 100644 --- a/electronics/ohms_law.py +++ b/electronics/ohms_law.py @@ -7,9 +7,8 @@ def ohms_law(voltage: float, current: float, resistance: float) -> float: """ - This function calculates the any one - of the three ohms_law values voltage, - current, resistance, of electronics. + For an electrical system, calculate the missing ohms_law values: voltage, current, + or resistance. Note: "resistance cannot be negative" >>> ohms_law(voltage=10, resistance=5, current=0) 2.0 From 0fd3f58ee394af71eee2da8f0ba91a991006fee8 Mon Sep 17 00:00:00 2001 From: Erdum Date: Wed, 25 Nov 2020 00:08:55 +0500 Subject: [PATCH 13/16] updated as suggested by cclauss --- electronics/ohms_law.py | 64 +++++++++++++++++++---------------------- 1 file changed, 30 insertions(+), 34 deletions(-) diff --git a/electronics/ohms_law.py b/electronics/ohms_law.py index b69b0a46d017..dcb23b7f21b7 100644 --- a/electronics/ohms_law.py +++ b/electronics/ohms_law.py @@ -1,47 +1,43 @@ -# Importing doctest for testing our function -from doctest import testmod - -# Defining function - - def ohms_law(voltage: float, current: float, resistance: float) -> float: - """ - This function calculates the any one - of the three ohms_law values voltage, - current, resistance, of electronics. - Note: "resistance cannot be negative" + This function apply ohm's law, on any two given electrical values, + which can be voltage current resistance, + and then return name and value pair in the form of dictionary >>> ohms_law(voltage=10, resistance=5, current=0) - 2.0 - >>> ohms_law(current=1, resistance=10, voltage=0) - 10.0 - >>> ohms_law(voltage=1.5, resistance=10, current=0) - 0.15 - >>> ohms_law(voltage=-15, resistance=10, current=0) - -1.5 - >>> ohms_law(current=-1, resistance=2, voltage=0) - -2.0 - >>> ohms_law(voltage=-15, resistance=-10, current=0) - 0 + {'current': 2.0} + >>> ohms_law(voltage=0, current=0, resistance=10) + Traceback (most recent call last): + File "", line 14, in + ValueError: Only one argument can be 0 at the time + >>> ohms_law(resistance=0, voltage=-10, current=1) + {'resistance': -10.0} """ if voltage == 0: - if resistance > 0: - result = float(current * resistance) - return result + if current == 0 or resistance == 0: + raise ValueError("Only one argument can be 0 at the time") + elif resistance <= 0: + raise ValueError("Resistance can't be 0 or in negative") else: - return 0 - elif current == 0: - if resistance > 0: - result = voltage / resistance + result = {"voltage": float(current * resistance)} return result + elif current == 0: + if voltage == 0 or resistance == 0: + raise ValueError("Only one argument can be 0 at the time") + elif resistance <= 0: + raise ValueError("Resistance can't be 0 or in negative") else: - return 0 + result = {"current": voltage / resistance} + return result elif resistance == 0: - result = voltage / current - return result - else: - return 0 + if voltage == 0 or current == 0: + raise ValueError("Only one argument can be 0 at the time") + else: + result = {"resistance": voltage / current} + return result if __name__ == "__main__": + # Importing doctest to test our function + from doctest import testmod + # Tesmod function is called to run test testmod(name="ohms_law", verbose=True) From 0af817cc88cda6c0e0f7bb31ac0d65514a9a1f58 Mon Sep 17 00:00:00 2001 From: Erdum Date: Wed, 25 Nov 2020 00:18:01 +0500 Subject: [PATCH 14/16] update as suggested by cclauss --- electronics/ohms_law.py | 1 + 1 file changed, 1 insertion(+) diff --git a/electronics/ohms_law.py b/electronics/ohms_law.py index dcb23b7f21b7..3745e1c907b1 100644 --- a/electronics/ohms_law.py +++ b/electronics/ohms_law.py @@ -39,5 +39,6 @@ def ohms_law(voltage: float, current: float, resistance: float) -> float: if __name__ == "__main__": # Importing doctest to test our function from doctest import testmod + # Tesmod function is called to run test testmod(name="ohms_law", verbose=True) From f8bf2b477f7ede719e90e732cbaa213f95965b78 Mon Sep 17 00:00:00 2001 From: Erdum Date: Wed, 25 Nov 2020 04:50:00 +0500 Subject: [PATCH 15/16] Update as suggested by cclauss --- electronics/ohms_law.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/electronics/ohms_law.py b/electronics/ohms_law.py index 3745e1c907b1..5e270c8ffba3 100644 --- a/electronics/ohms_law.py +++ b/electronics/ohms_law.py @@ -11,6 +11,8 @@ def ohms_law(voltage: float, current: float, resistance: float) -> float: ValueError: Only one argument can be 0 at the time >>> ohms_law(resistance=0, voltage=-10, current=1) {'resistance': -10.0} + >>> ohms_law(voltage=0, current=-1.5, resistance=2) + {'voltage': -3.0} """ if voltage == 0: if current == 0 or resistance == 0: From e7327d301257f57e59b7f06938a42e63433d4a1f Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Wed, 25 Nov 2020 12:00:22 +0100 Subject: [PATCH 16/16] Update ohms_law.py --- electronics/ohms_law.py | 49 ++++++++++++++++++----------------------- 1 file changed, 21 insertions(+), 28 deletions(-) diff --git a/electronics/ohms_law.py b/electronics/ohms_law.py index 5e270c8ffba3..a7b37b635397 100644 --- a/electronics/ohms_law.py +++ b/electronics/ohms_law.py @@ -1,46 +1,39 @@ +# https://en.wikipedia.org/wiki/Ohm%27s_law + + def ohms_law(voltage: float, current: float, resistance: float) -> float: """ - This function apply ohm's law, on any two given electrical values, - which can be voltage current resistance, - and then return name and value pair in the form of dictionary + Apply Ohm's Law, on any two given electrical values, which can be voltage, current, + and resistance, and then in a Python dict return name/value pair of the zero value. + >>> ohms_law(voltage=10, resistance=5, current=0) {'current': 2.0} >>> ohms_law(voltage=0, current=0, resistance=10) Traceback (most recent call last): - File "", line 14, in - ValueError: Only one argument can be 0 at the time + ... + ValueError: One and only one argument must be 0 + >>> ohms_law(voltage=0, current=1, resistance=-2) + Traceback (most recent call last): + ... + ValueError: Resistance cannot be negative >>> ohms_law(resistance=0, voltage=-10, current=1) {'resistance': -10.0} >>> ohms_law(voltage=0, current=-1.5, resistance=2) {'voltage': -3.0} """ + if (voltage, current, resistance).count(0) != 1: + raise ValueError("One and only one argument must be 0") + if resistance < 0: + raise ValueError("Resistance cannot be negative") if voltage == 0: - if current == 0 or resistance == 0: - raise ValueError("Only one argument can be 0 at the time") - elif resistance <= 0: - raise ValueError("Resistance can't be 0 or in negative") - else: - result = {"voltage": float(current * resistance)} - return result + return {"voltage": float(current * resistance)} elif current == 0: - if voltage == 0 or resistance == 0: - raise ValueError("Only one argument can be 0 at the time") - elif resistance <= 0: - raise ValueError("Resistance can't be 0 or in negative") - else: - result = {"current": voltage / resistance} - return result + return {"current": voltage / resistance} elif resistance == 0: - if voltage == 0 or current == 0: - raise ValueError("Only one argument can be 0 at the time") - else: - result = {"resistance": voltage / current} - return result + return {"resistance": voltage / current} if __name__ == "__main__": - # Importing doctest to test our function - from doctest import testmod + import doctest - # Tesmod function is called to run test - testmod(name="ohms_law", verbose=True) + doctest.testmod()