-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlibrary.py
76 lines (60 loc) · 2.29 KB
/
library.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
## Module of Python functions utilized across various solutions.
def digits(integer):
return [ int(x) for x in str(integer) ]
def digits_to_number(t):
"""Converts a tuple of digits to an integer, e.g. (4, 7, 1) -> 471."""
return int(str().join(map(str, t)))
def is_prime(n):
if n % 2 == 0:
return False
for i in range(3, int(n**0.5)+2, 2):
if n % i == 0:
break
else:
return True
return False
def primes_up_to(n):
# Use the Sieve of Eratosthenes
# https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes
#
# Create a boolean array "prime[0..n]" and initialize
# all entries it as true. A value in prime[i] will
# finally be false if i is not a prime, else true.
prime = [ True for i in range(n + 1) ]
prime[0]= False
prime[1]= False
p = 2
while (p * p <= n):
# If prime[p] is not changed, then it is a prime
if prime[p]:
# Update all multiples of p
for i in range(p * 2, n + 1, p):
prime[i] = False
p += 1
return [ p for p in range(n+1) if prime[p] ]
import signal
import time
from functools import wraps
def timed(timeout_seconds=None):
def decorator(func):
@wraps(func)
def wrapper(*args, **kwargs):
if timeout_seconds is not None: # If timeout is specified
def timeout_handler(signum, frame):
raise TimeoutError("Function execution exceeded the time limit.")
# Set the timeout signal handler
signal.signal(signal.SIGALRM, timeout_handler)
signal.alarm(timeout_seconds) # Set the timeout duration
try:
start_time = time.time() # Start timing
result = func(*args, **kwargs) # Execute the function
elapsed_time = time.time() - start_time # Calculate elapsed time
return result, elapsed_time
except TimeoutError as e:
## print(f"Timeout occurred: {e}")
return None, None
finally:
if timeout_seconds is not None:
signal.alarm(0) # Cancel the alarm
return wrapper
return decorator