class Solution:
# @param n, an integer
# @return a string
def countAndSay(self, n):
def f(string):
a = []
count_1 = 1
count_2 = 1
s = string[1:] + 'e' #calculate i from string[1](this is to disregard the case of string[0]), meanwhile add 'e' to notify the end of the string
for i, j in enumerate(s):
if j == '1':
if string[i] == '1': #check whether the last member in string is the same as j
count_1 += 1 #if so, count_1 add 1, save for further list appending
else:
count_1 = 1 #reset the count if last character does not match with j
a.append[(str(count_2) + '2')]
if j == '2':
if string[i] == '2':
count_2 += 1
else:
count_2 = 1
a.append[(str(count_1) + '1')]
if j == 'e': #j=='e' if the iteration reach the end, and it is the right time for adding the final member of the expected list
if string[i] == '1':
a.append[(str(count_1) + '1')]
if string[i] == '2':
a.append[(str(count_2) + '2')]
return ''.join(list)
def seq(n):
if n == 1:
return '1'
if n == 2:
return '11'
if n == 3:
return '21'
else:
return f(seq(n-1))
return seq(n)
This is a solution of a problem from leetcode: https://leetcode.com/problems/count-and-say/
It notifies me that:
Runtime Error Message: Line 17: TypeError: 'builtin_function_or_method' object has no attribute 'getitem'
Last executed input: 4
Line 17 comes as follows:
a.append[(str(count_2) + '2')]
Please, I am a completely novice and don't know how to solve this problem. Also, I would like you to improve my code as well. Thank you so much.
a.append[(str(count_2) + '2')]to do, exactly? Perhaps you should read e.g. docs.python.org/2/tutorial/datastructures.html#more-on-listsstring, alsoreturn ''.join(list)should probably be outside the loop inf(), where islistcoming from? There's a lot going on here...