Skip to content

Instantly share code, notes, and snippets.

@ZechCodes
Created January 12, 2021 03:29
Show Gist options
  • Save ZechCodes/cd83a63b87690574945a693a3bf7ee1c to your computer and use it in GitHub Desktop.
Save ZechCodes/cd83a63b87690574945a693a3bf7ee1c to your computer and use it in GitHub Desktop.
Challenge 163 - Calculate the Missing Value with Ohm's Law

Challenge 163 - Calculate the Missing Value with Ohm's Law

Create a function that calculates the missing value of 3 inputs using Ohm's law. The inputs are v, r or i (aka: voltage, resistance and current).

Ohm's law:

V = R * I

Return the missing value rounded to two decimal places.

Examples

ohms_law(v=12, r=220) ➞ 0.05

ohms_law(v=230, i=2) ➞ 115

ohms_law(r=220, i=0.02) ➞ 4.4

ohms_law(i=10) ➞ "Invalid"

ohms_law(v=500, r=50, i=10) ➞ "Invalid"

Notes

  • Missing values will be None
  • If there is more than one missing value, or no missing value, return "Invalid"
  • Only numbers will be given.
from typing import Optional, Union
import unittest
Number = Union[float, int]
Value = Optional[Number]
def ohms_law(v: Value = None, r: Value = None, i: Value = None) -> Union[Number, str]:
return 0 # Put your code here!!!
class Test(unittest.TestCase):
def test_1(self):
self.assertEqual(ohms_law(v=12, r=220), 0.05)
def test_2(self):
self.assertEqual(ohms_law(v=230, i=2), 115)
def test_3(self):
self.assertEqual(ohms_law(r=220, i=0.02), 4.4)
def test_4(self):
self.assertEqual(ohms_law(i=10), "Invalid")
def test_5(self):
self.assertEqual(ohms_law(v=500, r=50, i=10), "Invalid")
if __name__ == "__main__":
unittest.main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment