Hi,
I have a question, unrelated to the whole program. The following program can work, but if I replace s_out += str(n-i) + s[n-1]
with s_out += str(j+1-i) + s[j]
, it won’t work. Why not? After a loop, j and (n-1) should be equivalent. I got an error “local variable ‘j’ referenced before assignment”.
class Solution38:
def countAndSay(self, n: int) -> str:
if n == 1:
return '1'
def rec_say(s): # recursive say
n = len(s)
i = 0
s_out = ''
for j in range(1,n):
if s[j] != s[j-1]:
s_out += str(j-i) + s[i]
i = j
# s_out += str(j+1-i) + s[j] # j is actually (n-1) Why not work?
s_out += str(n-i) + s[n-1]
return s_out
s = '1'
for i in range(n-1):
s = rec_say(s)
return s
sol38 = Solution38()
sol38.countAndSay(4)
I thought it was because j was not predefined before range(n), but the following program does work:
for jx in range(3):
print('1')
y1 = '1'
y1 += str(jx)
print(y1)
PS: I hate the usage of the class here, which does nothing. It is how Lxxxcode uses it, and I just copy it.