mirror of
https://github.com/TheAlgorithms/Python.git
synced 2024-10-05 21:29:29 +00:00
Compare commits
7 Commits
c9e1546870
...
ff9fc0f8bf
Author | SHA1 | Date | |
---|---|---|---|
|
ff9fc0f8bf | ||
|
ad6395d340 | ||
|
50aca04c67 | ||
|
5a8655d306 | ||
|
451ac39ef8 | ||
|
d09163121d | ||
|
7e6e6420b8 |
|
@ -96,7 +96,7 @@ We want your work to be readable by others; therefore, we encourage you to note
|
|||
|
||||
```bash
|
||||
python3 -m pip install ruff # only required the first time
|
||||
ruff .
|
||||
ruff check
|
||||
```
|
||||
|
||||
- Original code submission require docstrings or comments to describe your work.
|
||||
|
|
38
data_structures/stacks/lexicographical_numbers.py
Normal file
38
data_structures/stacks/lexicographical_numbers.py
Normal file
|
@ -0,0 +1,38 @@
|
|||
from collections.abc import Iterator
|
||||
|
||||
|
||||
def lexical_order(max_number: int) -> Iterator[int]:
|
||||
"""
|
||||
Generate numbers in lexical order from 1 to max_number.
|
||||
|
||||
>>> " ".join(map(str, lexical_order(13)))
|
||||
'1 10 11 12 13 2 3 4 5 6 7 8 9'
|
||||
>>> list(lexical_order(1))
|
||||
[1]
|
||||
>>> " ".join(map(str, lexical_order(20)))
|
||||
'1 10 11 12 13 14 15 16 17 18 19 2 20 3 4 5 6 7 8 9'
|
||||
>>> " ".join(map(str, lexical_order(25)))
|
||||
'1 10 11 12 13 14 15 16 17 18 19 2 20 21 22 23 24 25 3 4 5 6 7 8 9'
|
||||
>>> list(lexical_order(12))
|
||||
[1, 10, 11, 12, 2, 3, 4, 5, 6, 7, 8, 9]
|
||||
"""
|
||||
|
||||
stack = [1]
|
||||
|
||||
while stack:
|
||||
num = stack.pop()
|
||||
if num > max_number:
|
||||
continue
|
||||
|
||||
yield num
|
||||
if (num % 10) != 9:
|
||||
stack.append(num + 1)
|
||||
|
||||
stack.append(num * 10)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
from doctest import testmod
|
||||
|
||||
testmod()
|
||||
print(f"Numbers from 1 to 25 in lexical order: {list(lexical_order(26))}")
|
|
@ -28,6 +28,24 @@ def longest_common_subsequence(x: str, y: str):
|
|||
(2, 'ph')
|
||||
>>> longest_common_subsequence("computer", "food")
|
||||
(1, 'o')
|
||||
>>> longest_common_subsequence("", "abc") # One string is empty
|
||||
(0, '')
|
||||
>>> longest_common_subsequence("abc", "") # Other string is empty
|
||||
(0, '')
|
||||
>>> longest_common_subsequence("", "") # Both strings are empty
|
||||
(0, '')
|
||||
>>> longest_common_subsequence("abc", "def") # No common subsequence
|
||||
(0, '')
|
||||
>>> longest_common_subsequence("abc", "abc") # Identical strings
|
||||
(3, 'abc')
|
||||
>>> longest_common_subsequence("a", "a") # Single character match
|
||||
(1, 'a')
|
||||
>>> longest_common_subsequence("a", "b") # Single character no match
|
||||
(0, '')
|
||||
>>> longest_common_subsequence("abcdef", "ace") # Interleaved subsequence
|
||||
(3, 'ace')
|
||||
>>> longest_common_subsequence("ABCD", "ACBD") # No repeated characters
|
||||
(3, 'ABD')
|
||||
"""
|
||||
# find the length of strings
|
||||
|
||||
|
|
90
electronics/power_factor.py
Normal file
90
electronics/power_factor.py
Normal file
|
@ -0,0 +1,90 @@
|
|||
import math
|
||||
|
||||
|
||||
def calculate_apparent_power(voltage, current):
|
||||
"""Calculate the apparent power (S) in volt-amperes (VA)."""
|
||||
try:
|
||||
return voltage * current
|
||||
except TypeError:
|
||||
error_msg = "Invalid input types for voltage or current. Both must be numbers."
|
||||
raise ValueError(error_msg)
|
||||
|
||||
|
||||
def calculate_power_factor(real_power, apparent_power):
|
||||
"""Calculate the power factor (PF)."""
|
||||
try:
|
||||
if apparent_power == 0:
|
||||
raise ValueError("Apparent power cannot be zero.")
|
||||
return real_power / apparent_power
|
||||
except TypeError:
|
||||
error_msg = (
|
||||
"Invalid input types for real power or apparent power. "
|
||||
"Both must be numbers."
|
||||
)
|
||||
raise ValueError(error_msg)
|
||||
|
||||
|
||||
def calculate_reactive_power(real_power, apparent_power):
|
||||
"""Calculate the reactive power (Q) in volt-amperes reactive (VAR)."""
|
||||
try:
|
||||
if apparent_power < real_power:
|
||||
raise ValueError(
|
||||
"Apparent power must be greater than or equal to real power."
|
||||
)
|
||||
return math.sqrt(apparent_power**2 - real_power**2)
|
||||
except TypeError:
|
||||
error_msg = (
|
||||
"Invalid input types for real power or apparent power. "
|
||||
"Both must be numbers."
|
||||
)
|
||||
raise ValueError(error_msg)
|
||||
except ValueError as ve:
|
||||
error_msg = f"Calculation error: {ve}"
|
||||
raise ValueError(error_msg)
|
||||
|
||||
|
||||
def calculate_correction_capacitance(reactive_power, voltage, frequency=60):
|
||||
"""Calculate the size of the correction capacitor in microfarads (µF)."""
|
||||
try:
|
||||
if voltage == 0:
|
||||
raise ValueError("Voltage cannot be zero.")
|
||||
capacitance = (reactive_power * 1_000_000) / (
|
||||
2 * math.pi * frequency * voltage**2
|
||||
)
|
||||
return capacitance
|
||||
except TypeError:
|
||||
error_msg = (
|
||||
"Invalid input types for reactive power, voltage, or "
|
||||
"frequency. They must be numbers."
|
||||
)
|
||||
raise ValueError(error_msg)
|
||||
except ValueError as ve:
|
||||
error_msg = f"Calculation error: {ve}"
|
||||
raise ValueError(error_msg)
|
||||
|
||||
|
||||
def main():
|
||||
try:
|
||||
real_power = float(input("Enter real power in watts: "))
|
||||
current = float(input("Enter current in amps: "))
|
||||
voltage = float(input("Enter voltage in volts: "))
|
||||
|
||||
apparent_power = calculate_apparent_power(voltage, current)
|
||||
power_factor = calculate_power_factor(real_power, apparent_power)
|
||||
reactive_power = calculate_reactive_power(real_power, apparent_power)
|
||||
correction_capacitance = calculate_correction_capacitance(
|
||||
reactive_power, voltage
|
||||
)
|
||||
|
||||
print("\nResults:")
|
||||
print(f"Power Factor: {power_factor:.4f}")
|
||||
print(f"Apparent Power: {apparent_power:.0f} VA")
|
||||
print(f"Reactive Power: {reactive_power:.0f} VAR")
|
||||
print(f"Correction Capacitance: {correction_capacitance:.3f} µF")
|
||||
|
||||
except ValueError as e:
|
||||
print(f"Error: {e}")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
Loading…
Reference in New Issue
Block a user