Last active
July 29, 2021 06:30
-
-
Save Komet-Kazi/2cd581ee048f694795c29ce12d88b679 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # https://en.m.wikipedia.org/wiki/Electric_power | |
| from collections import namedtuple | |
| from typing import Tuple | |
| def electric_power(voltage: float, current: float, power: float) -> Tuple: | |
| """ | |
| This function can calculate any one of the three (voltage, current, power), | |
| fundamental value of electrical system. | |
| examples are below: | |
| >>> electric_power(voltage=0, current=2, power=5) | |
| result(name='voltage', value=2.5) | |
| >>> electric_power(voltage=2, current=2, power=0) | |
| result(name='power', value=4.0) | |
| >>> electric_power(voltage=-2, current=3, power=0) | |
| result(name='power', value=6.0) | |
| >>> electric_power(voltage=2, current=4, power=2) | |
| Traceback (most recent call last): | |
| File "<stdin>", line 15, in <module> | |
| ValueError: Only one argument must be 0 | |
| >>> electric_power(voltage=0, current=0, power=2) | |
| Traceback (most recent call last): | |
| File "<stdin>", line 19, in <module> | |
| ValueError: Only one argument must be 0 | |
| >>> electric_power(voltage=0, current=2, power=-4) | |
| Traceback (most recent call last): | |
| File "<stdin>", line 23, in <modulei | |
| ValueError: Power cannot be negative in any electrical/electronics system | |
| >>> electric_power(voltage=2.2, current=2.2, power=0) | |
| result(name='power', value=4.84) | |
| """ | |
| result = namedtuple("result", "name value") | |
| if (voltage, current, power).count(0) != 1: | |
| raise ValueError("Only one argument must be 0") | |
| elif power < 0: | |
| raise ValueError( | |
| "Power cannot be negative in any electrical/electronics system" | |
| ) | |
| elif voltage == 0: | |
| return result("voltage", power / current) | |
| elif current == 0: | |
| return result("current", power / voltage) | |
| elif power == 0: | |
| return result("power", float(round(abs(voltage * current), 2))) | |
| else: | |
| raise ValueError("Exactly one argument must be 0") | |
| if __name__ == "__main__": | |
| import doctest | |
| doctest.testmod() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # https://en.wikipedia.org/wiki/Ohm%27s_law | |
| from typing import Dict | |
| def ohms_law(voltage: float, current: float, resistance: float) -> Dict[str, float]: | |
| """ | |
| 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): | |
| ... | |
| 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: | |
| return {"voltage": float(current * resistance)} | |
| elif current == 0: | |
| return {"current": voltage / resistance} | |
| elif resistance == 0: | |
| return {"resistance": voltage / current} | |
| else: | |
| raise ValueError("Exactly one argument must be 0") | |
| if __name__ == "__main__": | |
| import doctest | |
| doctest.testmod() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment