Re: [Tutor] self keyword in recursive function
That's all the code I'm writing. The complete problem statement is: http://pastebin.com/E970qYXk On Sat, May 31, 2014 at 11:25 AM, Marc Tompkins marc.tompk...@gmail.com wrote: On Fri, May 30, 2014 at 10:16 PM, Ritwik Raghav ritwikragha...@gmail.com wrote: It has again given some error I do not understand. This time my code is: count = 0 def getPersistence(self,n): product = 1 if len(str(n)) == 1: return self.count else: a = str(n) for i in a: product *= int(i) self.count += 1 return self.getPersistence(product) and the error is: Correct Return Value: No Answer check result: Result must be not null. Execution Time: 0.017s Peak memory used: 24.551MB abnormal termination (exit 1) Standard Output: Standard Error: Traceback (most recent call last): File Wrapper.py, line 182, in module AttributeError: 'module' object has no attribute 'PersistentNumber' I do not understand what it is trying to tell me? I tried to test it for 99. The error is in some part of your code that you _haven't_ posted here - please post your entire script (as an attachment, if that's more convenient) and we'll be better able to help. -- Ritwik Raghav ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] self keyword in recursive function
On Fri, May 30, 2014 at 11:06 PM, Ritwik Raghav ritwikragha...@gmail.com wrote: That's all the code I'm writing. That can't be true - the 11 lines of code you posted doesn't include anything that would give you Correct Return Value: No, let alone any reference to PersistentNumber. From the error message, it would appear that you've written (at least) 182 lines, and that the problem is on line 182 - but that's not what you're showing us. The complete problem statement is: http://pastebin.com/E970qYXk Yes, but that's not _your_ code, so it tells us nothing about your error. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] self keyword in recursive function
On Fri, May 30, 2014 at 10:16 PM, Ritwik Raghav ritwikragha...@gmail.com mailto:ritwikragha...@gmail.com wrote: and the error is: Correct Return Value: No Answer check result: Result must be not null. Execution Time: 0.017s Peak memory used: 24.551MB abnormal termination (exit 1) Standard Output: Standard Error: Traceback (most recent call last): File Wrapper.py, line 182, in module AttributeError: 'module' object has no attribute 'PersistentNumber' This is not standard Python error output so I assume it has something to do with how you submit code to the site you are using. It looks like it has some kkind of testing framework that your code must comply with. You probably need to read their guidance on code submission closely. One possible bug I did notice in your code is here: count = 0 You define count outside the method but not as part of self. def getPersistence(self,n): product = 1 if len(str(n)) == 1: return self.count then you return self.count. That's a different value, which may be defaulting to None in your test environment? If you insist on using recursion I'd bring the count back as an argument defaulted to zero as you did in your original code. else: a = str(n) for i in a: product *= int(i) self.count += 1 return self.getPersistence(product) But without knowing how toploader tests your code its hard to say for sure what's going wrong. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.flickr.com/photos/alangauldphotos ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] self keyword in recursive function
That's all the code I'm writing. As for the reference to line 182, I have no idea since my code doesn't have line 182. Also, as Peter wrote: The topcoder site is probably infested with the world view of Java where every function is a method inside a class. Topcoder seems too much in love with Java style of coding and Python seems much better. I'm switching to some other platform. Thank You all for the quick response. On Sat, May 31, 2014 at 11:53 AM, Marc Tompkins marc.tompk...@gmail.com wrote: On Fri, May 30, 2014 at 11:06 PM, Ritwik Raghav ritwikragha...@gmail.com wrote: That's all the code I'm writing. That can't be true - the 11 lines of code you posted doesn't include anything that would give you Correct Return Value: No, let alone any reference to PersistentNumber. From the error message, it would appear that you've written (at least) 182 lines, and that the problem is on line 182 - but that's not what you're showing us. The complete problem statement is: http://pastebin.com/E970qYXk Yes, but that's not _your_ code, so it tells us nothing about your error. -- Ritwik Raghav ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] self keyword in recursive function
On 30/05/14 14:14, Ritwik Raghav wrote: I joined the topcoder community tomorrow and tried solving the PersistentNumber problem: Time travel! I love it already... :-) 8*1 = 8. Thus, the persistence of 99 is 2. You will be given n, and you must return its persistence. It asks to define a function def getPersistence(self, n). I solved the problem in IDLE. My code is: You seem to have solved the problem. Your code could be cleaned up a little but it seems to work. The fact that the exercise asks for a self argument suggests that it is supposed to be part of a class definition. Is there a class definition anywhere that you are supposed to extend? |Some comments on the code below: def getPersistence(n,count = 0) Since you never get passed count as an argument you could just make it a variable. You only need it as an argument if you use recursion but the problem didn't ask for that... product = 1 if len(str(n)) == 1: return count else: a = str(n) for i in range(len(a)): product *= int(a[i]) This is not good Python style. Its better to use for c in a: product += int(c) count += 1 return getPersistence(product,count) Rather than using recursion you could have used a while loop (untested code!): if n 10: return 0 product = 1 while True: count += 1 a = str(n) for c in a: product *= int(c) if product 10: break return count Now plz help me to convert the above code in specified format. Or help me understand how to recreate the function as specified. You have created a function that does what is needed, it just doesn't have a self parameter. self is only used when the function is part of a class definition. Without sight of the class that it should be part of we can't offer much more help. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.flickr.com/photos/alangauldphotos ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] self keyword in recursive function
Ritwik Raghav wrote: I joined the topcoder community tomorrow and tried solving the PersistentNumber problem: Given a number x, we can define p(x) as the product of the digits of x. We can then form a sequence x, p(x), p(p(x))... The persistence of x is then defined as the index (0-based) of the first single digit number in the sequence. For example, using 99, we get the sequence 99, 9*9 = 81, 8*1 = 8. Thus, the persistence of 99 is 2. You will be given n, and you must return its persistence. It asks to define a function def getPersistence(self, n). I solved the problem in IDLE. My code is: def getPersistence(n,count = 0): product = 1 if len(str(n)) == 1: return count else: a = str(n) for i in range(len(a)): product *= int(a[i]) count += 1 return getPersistence(product,count) Now plz help me to convert the above code in specified format. Or help me understand how to recreate the function as specified. The topcoder site is probably infested with the world view of Java where every function is a method inside a class. You may have to wrap your code into something like class PersistentNumber: def getPersistence(self, n, count=0): ... # your code with a minor change (*) (*) Inside the method the recursive call becomes self.getPersistence(product, count) instead of just getPersistence(product, count) ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] self keyword in recursive function
Alan Gauld wrote: On 30/05/14 14:14, Ritwik Raghav wrote: I joined the topcoder community tomorrow and tried solving the PersistentNumber problem: Time travel! I love it already... :-) 8*1 = 8. Thus, the persistence of 99 is 2. You will be given n, and you must return its persistence. It asks to define a function def getPersistence(self, n). I solved the problem in IDLE. My code is: You seem to have solved the problem. Your code could be cleaned up a little but it seems to work. The fact that the exercise asks for a self argument suggests that it is supposed to be part of a class definition. That much I figured out. But I have never worked with classes in Python. Neither have I read about them. Please suggest one book I should read to understand class and objects in Python. Is there a class definition anywhere that you are supposed to extend? I have read that Topcoder implements the code inside a class. So, yes this code is supposed to be the part of that class and public. |Some comments on the code below: def getPersistence(n,count = 0) Since you never get passed count as an argument you could just make it a variable. You only need it as an argument if you use recursion but the problem didn't ask for that... product = 1 if len(str(n)) == 1: return count else: a = str(n) for i in range(len(a)): product *= int(a[i]) This is not good Python style. Its better to use for c in a: product += int(c) Thanks, I will implement so. count += 1 return getPersistence(product,count) Rather than using recursion you could have used a while loop (untested code!): if n 10: return 0 product = 1 while True: count += 1 a = str(n) for c in a: product *= int(c) if product 10: break return count I thought recursion would be better. Now plz help me to convert the above code in specified format. Or help me understand how to recreate the function as specified. You have created a function that does what is needed, it just doesn't have a self parameter. self is only used when the function is part of a class definition. Without sight of the class that it should be part of we can't offer much more help. Thanks for your help Alan. -- Ritwik Raghav ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] self keyword in recursive function
Peter Otten wrote: Ritwik Raghav wrote: I joined the topcoder community tomorrow and tried solving the PersistentNumber problem: Given a number x, we can define p(x) as the product of the digits of x. We can then form a sequence x, p(x), p(p(x))... The persistence of x is then defined as the index (0-based) of the first single digit number in the sequence. For example, using 99, we get the sequence 99, 9*9 = 81, 8*1 = 8. Thus, the persistence of 99 is 2. You will be given n, and you must return its persistence. It asks to define a function def getPersistence(self, n). I solved the problem in IDLE. My code is: def getPersistence(n,count = 0): product = 1 if len(str(n)) == 1: return count else: a = str(n) for i in range(len(a)): product *= int(a[i]) count += 1 return getPersistence(product,count) Now plz help me to convert the above code in specified format. Or help me understand how to recreate the function as specified. The topcoder site is probably infested with the world view of Java where every function is a method inside a class. You may have to wrap your code into something like class PersistentNumber: def getPersistence(self, n, count=0): ... # your code with a minor change (*) (*) Inside the method the recursive call becomes self.getPersistence(product, count) instead of just getPersistence(product, count) It has again given some error I do not understand. This time my code is: count = 0 def getPersistence(self,n): product = 1 if len(str(n)) == 1: return self.count else: a = str(n) for i in a: product *= int(i) self.count += 1 return self.getPersistence(product) and the error is: Correct Return Value: No Answer check result: Result must be not null. Execution Time: 0.017s Peak memory used: 24.551MB abnormal termination (exit 1) Standard Output: Standard Error: Traceback (most recent call last): File Wrapper.py, line 182, in module AttributeError: 'module' object has no attribute 'PersistentNumber' I do not understand what it is trying to tell me? I tried to test it for 99. -- Ritwik Raghav ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] self keyword in recursive function
On Fri, May 30, 2014 at 10:16 PM, Ritwik Raghav ritwikragha...@gmail.com wrote: It has again given some error I do not understand. This time my code is: count = 0 def getPersistence(self,n): product = 1 if len(str(n)) == 1: return self.count else: a = str(n) for i in a: product *= int(i) self.count += 1 return self.getPersistence(product) and the error is: Correct Return Value: No Answer check result: Result must be not null. Execution Time: 0.017s Peak memory used: 24.551MB abnormal termination (exit 1) Standard Output: Standard Error: Traceback (most recent call last): File Wrapper.py, line 182, in module AttributeError: 'module' object has no attribute 'PersistentNumber' I do not understand what it is trying to tell me? I tried to test it for 99. The error is in some part of your code that you _haven't_ posted here - please post your entire script (as an attachment, if that's more convenient) and we'll be better able to help. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor