mirror of
https://github.com/TheAlgorithms/Python.git
synced 2025-02-25 18:38:39 +00:00
Update prime_factors.py
This commit is contained in:
parent
ed509591e4
commit
9cc02c8ba7
@ -1,13 +1,38 @@
|
|||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
from math import ceil, sqrt
|
from math import ceil, sqrt
|
||||||
import time
|
import time
|
||||||
|
from typing import Any, List, Tuple
|
||||||
|
|
||||||
|
|
||||||
def timer(func):
|
def timer(func: callable) -> callable:
|
||||||
def wrapper(*args, **kwargs):
|
"""
|
||||||
|
Decorator that measures the execution time of a function.
|
||||||
|
|
||||||
|
:param func: The function to be timed.
|
||||||
|
:return: The wrapped function.
|
||||||
|
|
||||||
|
>>> @timer
|
||||||
|
... def test_function():
|
||||||
|
... time.sleep(1)
|
||||||
|
... return "Test completed."
|
||||||
|
...
|
||||||
|
>>> result = test_function()
|
||||||
|
Test completed.
|
||||||
|
test_function took 1.000272 seconds to execute.
|
||||||
|
>>> result
|
||||||
|
'Test completed.'
|
||||||
|
"""
|
||||||
|
|
||||||
|
def wrapper(*args: Any, **kwargs: Any) -> Any:
|
||||||
|
"""
|
||||||
|
Wraps the given function and measures its execution time.
|
||||||
|
|
||||||
|
:param args: Positional arguments for the function.
|
||||||
|
:param kwargs: Keyword arguments for the function.
|
||||||
|
:return: The result of the wrapped function.
|
||||||
|
"""
|
||||||
start = time.time()
|
start = time.time()
|
||||||
result = func(*args, **kwargs)
|
result = func(*args, **kwargs)
|
||||||
print(result)
|
|
||||||
end = time.time()
|
end = time.time()
|
||||||
print(f"{func.__name__} took {end - start:.6f} seconds to execute.")
|
print(f"{func.__name__} took {end - start:.6f} seconds to execute.")
|
||||||
return result
|
return result
|
||||||
@ -16,8 +41,9 @@ def timer(func):
|
|||||||
|
|
||||||
|
|
||||||
@timer
|
@timer
|
||||||
def prime_factors(n: int) -> list[int]:
|
def prime_factors(number: int) -> List[int]:
|
||||||
"""
|
"""
|
||||||
|
Returns prime factors of a given number as a list.
|
||||||
Returns prime factors of n as a list.
|
Returns prime factors of n as a list.
|
||||||
|
|
||||||
>>> prime_factors(0)
|
>>> prime_factors(0)
|
||||||
@ -59,8 +85,10 @@ def prime_factors(n: int) -> list[int]:
|
|||||||
|
|
||||||
|
|
||||||
@timer
|
@timer
|
||||||
def primeproduct(num: int) -> list[int]:
|
def primeproduct(num: int) -> List[int]:
|
||||||
"""
|
"""
|
||||||
|
Returns prime factors of a positive integer num as a list.
|
||||||
|
|
||||||
>>> primeproduct(868)
|
>>> primeproduct(868)
|
||||||
[2, 2, 7, 31]
|
[2, 2, 7, 31]
|
||||||
>>> primeproduct(9039423423423743)
|
>>> primeproduct(9039423423423743)
|
||||||
@ -70,8 +98,8 @@ def primeproduct(num: int) -> list[int]:
|
|||||||
ValueError: invalid literal for int() with base 10: '0.02'
|
ValueError: invalid literal for int() with base 10: '0.02'
|
||||||
>>> primeproduct(-2342)
|
>>> primeproduct(-2342)
|
||||||
[]
|
[]
|
||||||
"""
|
|
||||||
|
|
||||||
|
"""
|
||||||
if num <= 1:
|
if num <= 1:
|
||||||
return []
|
return []
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user