forked from Srinivas11789/AlgorithmNuggets
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcount.py
More file actions
119 lines (104 loc) · 3.05 KB
/
count.py
File metadata and controls
119 lines (104 loc) · 3.05 KB
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# Two Pointer Technique - 100 pass
class Solution(object):
def countAndSay(self, n):
"""
:type n: int
:rtype:str
"""
# 1 11 21
total = 1
start = ["1"]
while total < n:
prev = start[-1]
init = 0
move = 1
ans = ""
while init < len(prev):
current_count = 1
while move < len(prev) and prev[init] == prev[move]:
current_count += 1
move += 1
current_count = str(current_count)
ans += current_count
ans += prev[init]
init = move
move += 1
start.append(ans)
total += 1
print start
return start[-1]
"""
i = 1
memo = [["1"]]
while i < n:
current = memo[-1]
j = 0
memo.append([])
count = 1
# Logic for counting the sequence
while j < len(current):
print i,count
if j == len(current)-1:
if current[j] == current[j-1]:
count += 1
memo[-1].append(str(count)+current[j])
elif current[j] == current[j+1]:
count += 1
elif current[j] != current[j+1]:
memo[-1].append(str(count)+current[j])
count = 1
j += 1
i += 1
print memo
return "".join(memo[-1])
"""
"""
class Solution(object):
nums = ["1"]
def countAndSay(self, n):
"""
:type n: int
:rtype: str
"""
nums = ["0","1"]
for i in range(2,n+1):
prev = nums[i-1]
j = 0
result = []
while j < len(prev):
count = 1
k = 0
while j+1 < len(prev) and prev[j] == prev[j+1]:
count += 1
k += 1
if j == len(prev)-1:
if prev[j] == prev[j-1]:
count += 1
result.append(str(count)+prev[j-1])
if k > 0:
j += k
else:
j += 1
nums.append("".join(result))
print nums
return nums[n]
"""
# Recursion logic
global nums
if n == 0:
return nums[0]
elif nums[n]:
return nums[n]
else:
result = ""
prev = countAndSay(n-1)
while i < len(prev):
count = 0
while prev[i] == prev[i+1]:
count += 1
i += 1
result.append(str(count)+prev[i-1])
nums.append(result)
return result
"""
"""