-
Notifications
You must be signed in to change notification settings - Fork 0
/
275.py
64 lines (50 loc) · 1.39 KB
/
275.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
"""
Problem:
The "look and say" sequence is defined as follows: beginning with the term 1, each
subsequent term visually describes the digits appearing in the previous term. The first
few terms are as follows:
1
11
21
1211
111221
As an example, the fourth term is 1211, since the third term consists of one 2 and one
1.
Given an integer N, print the Nth term of this sequence.
"""
from functools import lru_cache
def generate_look_and_say_term(num: str) -> str:
result = ""
temp = ""
for char in num[::-1]:
if temp:
if char == temp[0]:
temp += char
else:
result = f"{len(temp)}{temp[0]}" + result
temp = char
else:
temp = char
result = f"{len(temp)}{temp[0]}" + result
return result
# cache is unnecessary for small sizes, but in case of large value of n, it drastically
# speeds up the process using memorization
@lru_cache(maxsize=5)
def get_look_and_say_term(n: int) -> str:
num = "1"
for _ in range(n - 1):
num = generate_look_and_say_term(num)
return num
if __name__ == "__main__":
for i in range(1, 6):
print(f"{i}th term = {get_look_and_say_term(i)}")
"""
SPECS:
[n = number of terms, m = longest look and say term]
TIME COMPLEXITY: O(n + m)
SPACE COMPLEXITY: O(n + m)
[with cache]
TIME COMPLEXITY: O(n * m)
SPACE COMPLEXITY: O(m)
[without cache]
"""