#let’s say Speak What You See
#Python3
Use a function (let’s say SpeakWhat YouSee) which takes 2 positive integers as arguments and generate a series and returns last term as output of initial n terms from the starting.¶
Each term in the speak-what-you-see series (except for the first term) is formed using the trailing term using below given process.
For example consider a number 222423322, starting with a term in the series
Separate the term into sub-sequences of consecutive repeating digits:
222 4 2 33 22¶
Count the number of digits in each subsequence
Three 2 one 4 one 2 two 3 two 2¶
Turn everything into digits
32 14 122322¶
Now combate everything into one number
3214122322¶
So 3214122322 is the next term in the sequence after 222423322¶
In [56]:
class Solution:
def countAndSay(self, n):
def getNextNumber(s):
result = []
i = 0
while i < len(s):
count = 1
while i < len(s) -1 and s[i] == s[i+1]:
count += 1
i += 1
result.append(str(count) + s[i])
i += 1
return ''.join(result)
ans = "1"
for _ in range(n-1):
ans = getNextNumber(ans)
return ans
#TEST CASES
n, n=map(int,input().split())
Object = Solution()
Object.countAndSay(n)
1 7
Out[56]:
'13112221'
In [ ]: