0
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.

6
  • 2
    What are you expecting a.append[(str(count_2) + '2')] to do, exactly? Perhaps you should read e.g. docs.python.org/2/tutorial/datastructures.html#more-on-lists Commented Apr 17, 2015 at 13:52
  • We need to know what you're passing in to string, also return ''.join(list) should probably be outside the loop in f(), where is list coming from? There's a lot going on here... Commented Apr 17, 2015 at 13:52
  • Where is line 17 in the above code? Commented Apr 17, 2015 at 13:54
  • Here is line 17: a.append[(str(count_2) + '2')] Commented Apr 17, 2015 at 13:57
  • I think it is okay for me to use "a.append[(str(count_2) + '2')]", to add element for my list. I will join the members of the list after then to produce the string. Commented Apr 17, 2015 at 14:02

1 Answer 1

1

Well, it is clear, that line 17 should look like

a.append(str(count_2) + '2')

See documentation on lists

When you use square brackets (a.append[key]) Python will try to call __getitem__ method on a.append

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you. I overlooked.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.