mirror of
https://github.com/TheAlgorithms/Python.git
synced 2024-11-24 05:21:09 +00:00
959507901a
* add final else-statement
* fix file_transfer
* fix quantum folder
* fix divide_and_conquer-folder
* Update build.yml
* updating DIRECTORY.md
* Update ripple_adder_classic.py
* Update .github/workflows/build.yml
* removed imports from typing
* removed conversion to string
* Revert "removed conversion to string"
This reverts commit 2f7c4731d1
.
* implemented suggested changes
* Update receive_file.py
Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com>
Co-authored-by: Christian Clauss <cclauss@me.com>
43 lines
1.4 KiB
Python
43 lines
1.4 KiB
Python
# 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()
|