Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 42 additions & 17 deletions electronics/ohms_law.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,48 @@

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}
Apply Ohm's Law to calculate the missing electrical value.

Ohm's Law: V = I × R, where V=voltage, I=current, R=resistance

Args:
voltage: Electrical potential difference in volts (0 if unknown)
current: Electrical current in amperes (0 if unknown)
resistance: Electrical resistance in ohms (0 if unknown)

Returns:
Dictionary with the calculated value: {'voltage': V}, {'current': I}, or {'resistance': R}

Raises:
ValueError: If not exactly one value is 0, or if resistance is negative

Examples:
>>> ohms_law(voltage=10, resistance=5, current=0)
{'current': 2.0}
>>> ohms_law(voltage=0, current=2, resistance=5)
{'voltage': 10.0}
>>> ohms_law(voltage=10, current=2, resistance=0)
{'resistance': 5.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}
>>> ohms_law(voltage=12, current=3, resistance=0)
{'resistance': 4.0}
>>> ohms_law(voltage=-9, current=3, resistance=0)
{'resistance': -3.0}
>>> ohms_law(voltage=0, current=2.5, resistance=4)
{'voltage': 10.0}
>>> ohms_law(voltage=7.5, current=0, resistance=3)
{'current': 2.5}
"""
if (voltage, current, resistance).count(0) != 1:
raise ValueError("One and only one argument must be 0")
Expand Down
25 changes: 21 additions & 4 deletions web_programming/current_stock_price.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,27 @@

def stock_price(symbol: str = "AAPL") -> str:
"""
>>> stock_price("EEEE")
'No <fin-streamer> tag with the specified data-testid attribute found.'
>>> isinstance(float(stock_price("GOOG")),float)
True
Get current stock price from Yahoo Finance.

Args:
symbol: Stock ticker symbol (e.g., 'AAPL', 'GOOG')

Returns:
Current stock price as string, or error message if not found

Examples:
>>> stock_price("EEEE") # Invalid symbol
'No <fin-streamer> tag with the specified data-testid attribute found.'
>>> isinstance(float(stock_price("GOOG")), float) # Valid symbol
True
>>> stock_price("") # Empty symbol
'No <fin-streamer> tag with the specified data-testid attribute found.'
>>> stock_price("INVALID_SYMBOL_123") # Another invalid symbol
'No <fin-streamer> tag with the specified data-testid attribute found.'
>>> # Test output format for valid symbols
>>> result = stock_price("AAPL")
>>> result.replace(".", "").replace(",", "").isdigit() or "No <fin-streamer>" in result
True
"""
url = f"https://finance.yahoo.com/quote/{symbol}?p={symbol}"
yahoo_finance_source = httpx.get(
Expand Down