Re: [Tutor] self keyword in recursive function
On Fri, May 30, 2014 at 11:06 PM, Ritwik Raghav 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
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 wrote: > On Fri, May 30, 2014 at 10:16 PM, Ritwik Raghav > 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 >> 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 10:16 PM, Ritwik Raghav 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 > 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
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 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
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] Question about scraping
Thanks for the response Alan. I forgot to reply to tutor on my 2nd comment. Just incase someone might want to see it, here it is: "Okay I think learning how to scrap (library or framework) is not worth the trouble. Especially if some people consider it illegal. Thanks for the input." ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Question about scraping
On Fri, May 30, 2014 at 7:20 PM, Alan Gauld wrote: > > >> If a site offers an API that returns the data you need then use it, >> If not you have few alternatives to scraping (although scraping >> may be 'illegal' anyway due to the impact on other users). But scraping, >> whether a web page or a GUI or an old mainframe terminal >> is always a fragile and unsatisfactory solution. > >Okay I think learning how to scrap (library or framework) is not worth >the trouble. Especially if some people consider it illegal. Thanks for >the input. > > >As I say, sometimes you have no choice but to scrape. Its only 'illegal' if the site owner says so, in other words if their terms of use prohibit web scraping. If they have gone to the effort (and cost) of providing an API then it probably means scraping is prohibited. But many (most!) sites don't offer APIs and most smaller sites don't prohibit scraping, so it is still a valid technique. But before you try its always worth checking whether an API exists and whether scraping is permitted. And by 'illegal' I mean you are unlikely to be prosecuted in a court but you are likely to find your IP address and/or account closed. The systems generally monitor activity and if an account is navigating through pages too quickly to be a human they often close the account down. Alan g. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Library for .ppt to .txt conversion
On 30/05/14 10:41, Aaron Misquith wrote: Like pypdf is used to convert pdf to text; is there any library that is used in converting .ppt files to .txt? Even some sample programs will be helpful. Bearing in mind that Powerpoint is intended for graphical presentations the text elements are not necessarily going to be useful. Often Powerpoint text is actually part of a graphic anyway. If the Powerpoint is just a set of bullet points (shame on the presenter!) you probably don't want the text unless you can also get the notes. I don't know of any libraries that can do that. But one option is that Open/Libre office can import Powerpoint and apparently has a Python API which you could use to drive an export from there. Just a thought... -- 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] Question about scraping
On 30/05/14 18:25, Matthew Ngaha wrote: Hey all. I've been meaning to get into web scraping and was pointed to the directions of lxml (library) and scrapy (framework). Can I ask in terms of web scraping, what's the difference between a library and a framework? I don;t know of anything web specific. A framework tends to be a much bigger thing than a library. It dictates the architecture of the solution rather than just providing a few functions/classes. Surely everyone should use a framework Why? A framework is usually the fastest way to get started from zero but if you are integrating with an existing solution then a framework can add layers of unneeded complexity. As always the correct solution depends on the problem. I also have another question due to reading this: "[Tutor] HTML Parsing" . It seems some experienced coders don't find scraping as useful since web sites offer apis for their data. Is the idea/concept here the same as scraping? No, its completely different. Scraping means trying to decipher a public web page that is designed for display in a browser. Web pages are prone to frequent change and the data often moves around within the page meaning constant updates to your scraper. Also web pages are increasingly dynamically generated which makes scraping much harder. An API is relatively stable and returns just the data elements of the page. As such its usually easier to use, more secure, more stable, faster (lower bandwidth required) and has much less impact on the providers network/servers thus improving performance for everyone. And is there any use of scraping anymore when sites are now offering their data? If a site offers an API that returns the data you need then use it, If not you have few alternatives to scraping (although scraping may be 'illegal' anyway due to the impact on other users). But scraping, whether a web page or a GUI or an old mainframe terminal is always a fragile and unsatisfactory solution. An API will always be better in the long term if it exists. -- 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
[Tutor] Question about scraping
Hey all. I've been meaning to get into web scraping and was pointed to the directions of lxml (library) and scrapy (framework). Can I ask in terms of web scraping, what's the difference between a library and a framework? Surely everyone should use a framework but I get the idea more people use the available libraries like lxml over scrapy. Any advantages or disadvantages for each? And which do you use out of the 2? I also have another question due to reading this: "[Tutor] HTML Parsing" . It seems some experienced coders don't find scraping as useful since web sites offer apis for their data. Is the idea/concept here the same as scraping? And is there any use of scraping anymore when sites are now offering their data? ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Library for .ppt to .txt conversion
On Fri, May 30, 2014 at 2:41 AM, Aaron Misquith wrote: > Like pypdf is used to convert pdf to text; is there any library that is > used in converting .ppt files to .txt? Even some sample programs will be > helpful. > I suspect you'd need to use PowerPoint itself to do that cleanly; you can definitely drive PowerPoint from Python if you so desire, though: http://www.s-anand.net/blog/automating-powerpoint-with-python/ If anybody's written a package to brute-force the text out of a .ppt file without using PowerPoint, though, I'm unaware of it. That way lies madness, I suspect. (The new MS Office formats - .docx, .xlsx, .pptx - are XML files inside of a renamed ZIP container; it should be fairly easy to get the text out of a .pptx file using any one of Python's XML libraries. But the older format is proprietary and extremely scary.) ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] Library for .ppt to .txt conversion
Like pypdf is used to convert pdf to text; is there any library that is used in converting .ppt files to .txt? Even some sample programs will be helpful. ___ 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
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
[Tutor] self keyword in recursive function
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. -- Ritwik Raghav ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor