Re: [Tutor] Tutor Digest, Vol 94, Issue 53
Please use a sensible subject line in future. Pleae trim any excess material from the post, specifically do NOT post the entire digest to the list. We've already seen it! On 15/12/11 02:15, rog capp wrote: On Wed, Dec 14, 2011 at 6:03 PM,tutor-requ...@python.org wrote: Send Tutor mailing list submissions to tutor@python.org To subscribe or unsubscribe via the World Wide Web, visit http://mail.python.org/mailman/listinfo/tutor or, via email, send a message with subject or body 'help' to tutor-requ...@python.org You can reach the person managing the list at tutor-ow...@python.org When replying, please edit your Subject line so it is more specific than Re: Contents of Tutor digest... Today's Topics: -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] ctype exceptions.ValueError for function call
Hi, Nice! Did not know there was a ctypes mailing list. Another thing: I found (but did not yet test) this program:http://www.nirsoft.net/utils/dll_export_viewer.html The screenshot looks promising. At least one is able to tell which functions in a dll are exported (and can therefore be called using ctypes). Cheers!! Albert-Jan ~~ All right, but apart from the sanitation, the medicine, education, wine, public order, irrigation, roads, a fresh water system, and public health, what have the Romans ever done for us? ~~ From: Alan Gauld alan.ga...@btinternet.com To: tutor@python.org Sent: Thursday, December 15, 2011 12:11 AM Subject: Re: [Tutor] ctype exceptions.ValueError for function call On 14/12/11 21:15, Santhirakumaran, Gokul wrote: Hi, I’m trying to use a SDK(dll file) with python ctypes to take measurement from a spectrometer. This list is for people learning the Python language. ctypes is a little advanced for most readers. You are more likely to get help on the ctypes mailing list. ctypes-us...@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/ctypes-users Or On the gmane news server under: gmane.comp.python.ctypes But you might get lucky and somebody here knows enough ctypes to answer you -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] how to print the match middle part out
Hi, For following: aaa model 0 bbb acb model 1 ccc How can I set regexp1 as model 0 and end model 1 so the final print out will be: model 0 bbb acb above is a very simple example, Thanks for any advice, Best, ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] timedelta, difference calculation
On Mon, Dec 12, 2011 at 4:14 PM, Lie Ryan lie.1...@gmail.com wrote: On 12/13/2011 06:46 AM, rail shafigulin wrote: i found something interesting during the timedate difference calculation import datetime import time def main(): mydatetime = datetime.datetime.now() time.sleep(1) mydatetime2 = datetime.datetime.now() diff = mydatetime - mydatetime2 print(diff) if __name__ == '__main__': main() if you run this code the result you get will be -1 day, 23:59:59 at least that is what i'm getting. i was expecting to get -1 second. diff object is of type timedelta. this kind of objects represent duration, at least that is what i understood from the documentation (http://docs.python.org/**release/3.1.3/library/** datetime.html#timedelta-**objectshttp://docs.python.org/release/3.1.3/library/datetime.html#timedelta-objects ). so, is this a bug in the timedelta implementation or is it me not understanding documentation properly? It's due to timedelta normalization; in timedelta object only the day part is ever negative, you never had negative hour, minute, or second. The `-1 day, 23:59:59` means to subtract 1 day, then add 23:59:59; therefore giving us -1 second. It is documented behavior, although perhaps hinted too subtly, from the docs: Note that normalization of negative values may be surprising at first. For example, from datetime import timedelta d = timedelta(microseconds=-1) (d.days, d.seconds, d.microseconds) (-1, 86399, 99) __**_ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/**mailman/listinfo/tutorhttp://mail.python.org/mailman/listinfo/tutor Thanks. You are right about the subtle documentation. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] how to print the match middle part out
Op 15-12-11 17:52, lina schreef: Hi, For following: aaa model 0 bbb acb model 1 ccc How can I set regexp1 as model 0 and end model 1 so the final print out will be: model 0 bbb acb Just iterate over the lines (file or string) and retrieve the lines from the wanted line, until the line you're not interested in anymore. something like this: data = aaa model 0 bbb acb model 1 ccc result = [] first_reached = False for line in data.split('\n'): if line == 'model 0': first_reached = True if line == 'model 1': break if first_reached: result.append(line) print result Cheers, Timo above is a very simple example, Thanks for any advice, Best, ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] how to print the match middle part out
lina wrote: Hi, For following: aaa model 0 bbb acb model 1 ccc How can I set regexp1 as model 0 and end model 1 In English, we have a saying When all you have is a hammer, everything looks like a nail. Don't make the mistake of thinking that regexes are your hammer. In my opinion, this example is best solved with a filter function, not a regex. Here is a simple example: def filter_lines(lines, start, end): lines = iter(lines) # Skip lines before matching start. for line in lines: if line == start: yield line break # Show lines after matching start, but before matching end. for line in lines: if line == end: break yield line text = aaa model 0 bbb acb model 1 ccc And the output: py for line in filter_lines(text.split('\n'), 'model 0', 'model 1'): ... print line ... model 0 bbb acb -- Steven ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] Localhost client-server simple ssl socket test program problems
Hello,everyone!! I am writing a simple ssl client-server test program on my personal laptop. And I encounter some problems with my simple programs. Please give me some helps. My server code: import socketimport sslbindsocket = socket.socket()bindsocket.bind(('127.0.0.1', 1234))bindsocket.listen(5)print 'server is waiting for connection...'newsocket, fromaddr = bindsocket.accept()print 'start ssl socket...'connstream = ssl.wrap_socket(newsocket, server_side=True, certfile=/etc/home/ckyang/PHA/testsslsocket/mypha.crt, keyfile=/etc/home/ckyang/PHA/testsslsocket/mypha.key, ssl_version=ssl.PROTOCOL_SSLv23)data = connstream.read()print 'connected from address', fromaddrprint 'received data as', repr(data)connstream.close() My client code: import socketimport ssls = socket.socket(socket.AF_INET, socket.SOCK_STREAM)ssl_sock = ssl.wrap_socket(s, ca_certs=/home/ckyang/PHA/testsslsocket/myCA.crt, cert_reqs=ssl.CERT_REQUIRED)ssl_sock.connect((127.0.0.1, 1234))ssl_sock.write(hello)ssl_sock.close() ---Server side error: File views.py, line 17, in moduleconnstream = ssl.wrap_socket(newsocket, server_side=True, certfile=/etc/home/ckyang/PHA/testsslsocket/mypha.crt, keyfile=/etc/home/ckyang/PHA/testsslsocket/mypha.key, ssl_version=ssl.PROTOCOL_SSLv23) File /usr/lib/python2.7/ssl.py, line 344, in wrap_socketciphers=ciphers) File /usr/lib/python2.7/ssl.py, line 119, in __init__ciphers)ssl.SSLError: [Errno 336265218] _ssl.c:347: error:140B0002:SSL routines:SSL_CTX_use_PrivateKey_file:system lib Client side error: File client.py, line 10, in modulessl_sock.connect((127.0.0.1, 1234)) File /usr/lib/python2.7/ssl.py, line 299, in connectself.do_handshake() File /usr/lib/python2.7/ssl.py, line 283, in do_handshake self._sslobj.do_handshake()socket.error: [Errno 104] Connection reset by peer So what is wrong with my code? The codes are so simple and so much like python official site sample demonstration, but I still cant get it work, so frustrating. Seems the problem happened on server side then cause client side cant connect well, is that right? My platform is ubuntu, with openssl 0.9.8 and python 2.7. All certificates and keys self-signed by openssl for test convenience. This is the site for referrence : http://andyjeffries.co.uk/articles/x509-encrypted-authenticated-socket-ruby-client Or should I need a real certificate issued by a real CA to let things work? Any tips or suggestions welcomed, thank you very much~ Good day. Kay ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Localhost client-server simple ssl socket test program problems
2011/12/15 Yang Chun-Kai waitmefore...@hotmail.com Hello,everyone!! I am writing a simple ssl client-server test program on my personal laptop. And I encounter some problems with my simple programs. Please give me some helps. My server code: import socket import ssl bindsocket = socket.socket() bindsocket.bind(('127.0.0.1', 1234)) bindsocket.listen(5) print 'server is waiting for connection...' newsocket, fromaddr = bindsocket.accept() print 'start ssl socket...' connstream = ssl.wrap_socket(newsocket, server_side=True, certfile=/etc/home/ckyang/PHA/testsslsocket/mypha.crt, keyfile=/etc/home/ckyang/PHA/testsslsocket/mypha.key, ssl_version=ssl.PROTOCOL_SSLv23) data = connstream.read() print 'connected from address', fromaddr print 'received data as', repr(data) connstream.close() My client code: import socket import ssl s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) ssl_sock = ssl.wrap_socket(s, ca_certs=/home/ckyang/PHA/testsslsocket/myCA.crt, cert_reqs=ssl.CERT_REQUIRED) ssl_sock.connect((127.0.0.1, 1234)) ssl_sock.write(hello) ssl_sock.close() --- Server side error: File views.py, line 17, in lt;module connstream = ssl.wrap_socket(newsocket, server_side=True, certfile=/etc/home/ckyang/PHA/testsslsocket/mypha.crt, keyfile=/etc/home/ckyang/PHA/testsslsocket/mypha.key, ssl_version=ssl.PROTOCOL_SSLv23) File /usr/lib/python2.7/ssl.py, line 344, in wrap_socket ciphers=ciphers) File /usr/lib/python2.7/ssl.py, line 119, in __init__ ciphers) ssl.SSLError: [Errno 336265218] _ssl.c:347: error:140B0002:SSL routines:SSL_CTX_use_PrivateKey_file:system lib Client side error: File client.py, line 10, in module ssl_sock.connect((127.0.0.1, 1234)) File /usr/lib/python2.7/ssl.py, line 299, in connect** self.do_handshake() File /usr/lib/python2.7/ssl.py, line 283, in do_handshake self._sslobj.do_handshake() socket.error: [Errno 104] Connection reset by peer So what is wrong with my code? The codes are so simple and so much like python official site sample demonstration, but I still cant get it work, so frustrating. Seems the problem happened on server side then cause client side cant connect well, is that right? ** My platform is ubuntu, with openssl 0.9.8 and python 2.7. All certificates and keys self-signed by openssl for test convenience. This is the site for referrence : http://andyjeffries.co.uk/articles/x509-encrypted-authenticated-socket-ruby-client Or should I need a real certificate issued by a real CA to let things work? Any tips or suggestions welcomed, thank you very much~ Good day. Kay ** ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor You're trying to connect to the same port on localhost as a client and a server? I don't know for certain but I don't think that should work. Two computers? -- Alexander 7D9C597B ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] timeit() help
So, it turns out that my ISP blocked Project Euler, so instead of working on my next problem, I polished Problem 4 a bit: A palindromic number reads the same both ways. The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 × 99. Find the largest palindrome made from the product of two 3-digit numbers. Those who don't want spoilers should look away. While it's perfectly fine to brute-force this (what I initially did) with two for-loops, I wanted to make a better version. Here's the code: First something to check whether a number is a palindrome: def is_palindrome(number): number = str(number) return number == number[::-1] Then the crunching part: def biggest(): big_x, big_y, max_seen = 0, 0, 0 for x in range(999, 99, -1): for y in range(x, 99, -1): #so we don't double count if x*y max_seen: continue #since we're decreasing if is_palindrome(x*y): big_x, big_y, max_seen = x, y, x*y return big_x, big_y, max_seen However, I got to thinking... If we assume that the palindrome starts with 9, it must end with 9 (I think that's a fair assumption, really -- but it could come back and bite me I suppose). That means that the only values for the third digit in each of the two factors would have to be 1, 3, 7 or 9 (1 * 9, 3 * 3, 7 * 7 or 9 * 1). If we check for this condition before checking whether a number is palindromic, we ought to cut down on the numbers checked by, oh, I don't know... half, at least? (it turns out that it's more: 405450 values, only 64980 have 1, 3, 7 or 9 in the end), so in order to avoid checking some 340,000 numbers, I wrote a third function: def check_value(number1, number2): number1, number2 = str(number1), str(number2) return number1[-1] in 1379 and number2[-1] in 1379 Putting this one inside the biggest() function, the final biggest() function looks like this: def biggest(): big_x, big_y, max_seen = 0, 0, 0 for x in range(999, 99, -1): for y in range(x, 99, -1): #so we don't double count if check_value(x, y): #we ignore all numbers that doesn't end in 1379 if x*y max_seen: continue #since we're decreasing if is_palindrome(x*y): big_x, big_y, max_seen = x, y, x*y return big_x, big_y, max_seen My biggest problem now is that I don't know how to measure any changes in efficiency. I know that the functions are working perfectly fine as-is, and I shouldn't really optimize without a need to, but I'm mostly curious as to whether the check_value() function is worth it or not. To this I thought I'd use timeit(), but I can't for the life of me work out how it works. At all. I've tried using it from the command prompt, from the interpreter and in the code itself and it just doesn't work. Or, it might very well work but it doesn't actually time anything for me. It's very frustrating, but I feel like I'm too stupid to read the official documentation for it (that is, I might understand the words in the documentation, but I can't get it to work). Please help? -- best regards, Robert S. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] [TUTOR]Code Deciphering
Hi! I was wondering, how do you use Python to decipher codes? It feels like it should be pretty simple, but I haven't found any tutorials about it yet. Have a nice day! // Calle ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] [TUTOR]Code Deciphering
I was wondering, how do you use Python to decipher codes? It feels like it should be pretty simple, but I haven't found any tutorials about it yet. What kind of codes? Or do you mean ciphers? Generally speaking, a code represent letters or numbers in transmitting a message. In other words, a code deals with phrases and sentences or whole words. Example steal the cabbage at dawn could mean kill the king on wednesday. A cipher deals with letters. It is a message written in letters in a predetermined code. This means that a cipher is a system of communication that uses letters instead of phrases. Examples being the standard Caesar cipher where APPLE might be written BQQMB (ie, shifted one letter to the right). -- best regards, Robert S. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] modify values for object derived from datetime.datetime
i writing some code to do device testing at my work. testing is related to date and time, so naturally i decided to create a class that inherits from datetime.datetime. main reason is that i need to add, subtract and compare datetime objects and datetime.datetime allows me to do that. here is the code: class LTime(datetime.datetime): TOLERANCE = 10 def __new__(self, year, month, day, *args): if year == 0: year = 2000 return super().__new__(self, year, month, day, *args) def modify(self): self = self.replace(2012, 12, 12) print(self) def main(): mytime = LTime.now() mytime.modify() print(mytime) if __name__ == '__main__': main() the problem that i see is that i'm not able to modify date and time because it seems that those attributes are immutable. another problem that i see is in the modify() routine. if you print mytime the date and time are still old. can anybody explain why this is happening and if it is even possible to achieve what i'm trying to achieve, which is to change self.date and self.time any help is appreciated. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] [TUTOR]Code Deciphering
-Ursprungligt meddelande- From: Robert Sjoblom Sent: Thursday, December 15, 2011 10:34 PM To: Calle Cc: tutor@python.org Subject: Re: [Tutor] [TUTOR]Code Deciphering I was wondering, how do you use Python to decipher codes? It feels like it should be pretty simple, but I haven't found any tutorials about it yet. What kind of codes? Or do you mean ciphers? Generally speaking, a code represent letters or numbers in transmitting a message. In other words, a code deals with phrases and sentences or whole words. Example steal the cabbage at dawn could mean kill the king on wednesday. A cipher deals with letters. It is a message written in letters in a predetermined code. This means that a cipher is a system of communication that uses letters instead of phrases. Examples being the standard Caesar cipher where APPLE might be written BQQMB (ie, shifted one letter to the right). -- best regards, Robert S. -- Hi! Sorry, I meant ciphers. How would a basic script for solving move-one-step-to-the-right ciphers look like? Have a nice day // Calle ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] String formating
Hey folks! What's the difference between these commands? print %02d % (12) print %d % (12) I know that d stands for decimal. What does 02 mean? Cheers! ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] [TUTOR]Code Deciphering
On 15 December 2011 23:37, Calle calle_pyt...@live.se wrote: -Ursprungligt meddelande- From: Robert Sjoblom Sent: Thursday, December 15, 2011 10:34 PM To: Calle Cc: tutor@python.org Subject: Re: [Tutor] [TUTOR]Code Deciphering I was wondering, how do you use Python to decipher codes? It feels like it should be pretty simple, but I haven't found any tutorials about it yet. What kind of codes? Or do you mean ciphers? Generally speaking, a code represent letters or numbers in transmitting a message. In other words, a code deals with phrases and sentences or whole words. Example steal the cabbage at dawn could mean kill the king on wednesday. A cipher deals with letters. It is a message written in letters in a predetermined code. This means that a cipher is a system of communication that uses letters instead of phrases. Examples being the standard Caesar cipher where APPLE might be written BQQMB (ie, shifted one letter to the right). Sorry, I meant ciphers. How would a basic script for solving move-one-step-to-the-right ciphers look like? Wll... There are different ways to solve that, but show us what you've come up with so far and we might be able to point you in the right direction. You won't learn anything by getting the answer posted and just copy-paste it for whatever (nefarious) use you need it; you'll learn a lot more if you work toward the solution yourself. I'll just point you in the direction of ASCII values for now. -- best regards, Robert S. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Localhost client-server simple ssl socket test program problems
Can you please be more careful to use plain text and not rich text or HTML when posting code? Because it destroys the necessary formatting: Yang Chun-Kai wrote: [...] My server code: import socketimport sslbindsocket = socket.socket()bindsocket.bind(('127.0.0.1', 1234))bindsocket.listen(5)print 'server is waiting for connection...'newsocket, fromaddr = bindsocket.accept()print 'start ssl socket...'connstream = ssl.wrap_socket(newsocket, server_side=True, certfile=/etc/home/ckyang/PHA/testsslsocket/mypha.crt, keyfile=/etc/home/ckyang/PHA/testsslsocket/mypha.key, ssl_version=ssl.PROTOCOL_SSLv23)data = connstream.read()print 'connected from address', fromaddrprint 'received data as', repr(data)connstream.close() Thank you. -- Steven ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] [TUTOR]Code Deciphering
Calle wrote: Hi! I was wondering, how do you use Python to decipher codes? It feels like it should be pretty simple, but I haven't found any tutorials about it yet. This is not a tutorial, but you might find it useful: http://pypi.python.org/pypi/obfuscate/ -- Steven ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] String formating
Stayvoid wrote: Hey folks! What's the difference between these commands? print %02d % (12) print %d % (12) I know that d stands for decimal. What does 02 mean? 0 means to use leading zeroes; 2 means to use 2 digits. Here's a better example: py %05d % 12 '00012' More information here: http://docs.python.org/library/stdtypes.html#string-formatting-operations -- Steven ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] Test - please ignore
___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] [TUTOR]Code Deciphering
On 15/12/11 23:53, Robert Sjoblom wrote: you'll learn a lot more if you work toward the solution yourself. I'll just point you in the direction of ASCII values for now. You might find the ord() function useful too... -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] where I am going wrong?
Suggestions for potentially simpler and more efficient code. Create a ruple of the 5th powers of the 10 digits and look them up (should be faster than recomputing the 5th power each time) (0, 1, 16, ... ) Instead of trying all permutations of digits, use combinations. 12345 will yield the same sum-of-5th-powers as 23154. Compute the sum then see if it is composed of the source digits. -- Bob Gailer 919-636-4239 Chapel Hill NC ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Localhost client-server simple ssl socket test program problems
You're trying to connect to the same port on localhost as a client and a server? I don't know for certain but I don't think that should work. Two computers? -- Alexander it should work. I did short tutorial on this like a month ago and it worked. The only difference is that in mine it didn't have the part below which is the source of the problem. Also, in mine, I used send and recv though I doubt this makes any difference. ssl_sock = ssl.wrap_socket(s, ca_certs=/home/ckyang/PHA/testsslsocket/ myCA.crt, cert_reqs=ssl.CERT_REQUIRED) ssl_sock.connect((127.0.0.1, 1234)) ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Localhost client-server simple ssl socket test program problems
welcomed, thank you very much~ Good day. Kay ** ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor You're trying to connect to the same port on localhost as a client and a server? I don't know for certain but I don't think that should work. Two computers? -- Alexander 7D9C597B -- next part -- An HTML attachment was scrubbed... URL: http://mail.python.org/pipermail/tutor/attachments/20111215/236b2679/attachment-0001.html -- Message: 2 Date: Thu, 15 Dec 2011 20:59:29 +0100 From: Robert Sjoblom robert.sjob...@gmail.com To: Tutor - python List tutor@python.org Subject: [Tutor] timeit() help Message-ID: cajku7g2w8rjp83rnnc+5pzqh5wbqtzpaaiqnows-fuapwjm...@mail.gmail.com Content-Type: text/plain; charset=ISO-8859-1 So, it turns out that my ISP blocked Project Euler, so instead of working on my next problem, I polished Problem 4 a bit: A palindromic number reads the same both ways. The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 ? 99. Find the largest palindrome made from the product of two 3-digit numbers. Those who don't want spoilers should look away. While it's perfectly fine to brute-force this (what I initially did) with two for-loops, I wanted to make a better version. Here's the code: First something to check whether a number is a palindrome: def is_palindrome(number): number = str(number) return number == number[::-1] Then the crunching part: def biggest(): big_x, big_y, max_seen = 0, 0, 0 for x in range(999, 99, -1): for y in range(x, 99, -1): #so we don't double count if x*y max_seen: continue #since we're decreasing if is_palindrome(x*y): big_x, big_y, max_seen = x, y, x*y return big_x, big_y, max_seen However, I got to thinking... If we assume that the palindrome starts with 9, it must end with 9 (I think that's a fair assumption, really -- but it could come back and bite me I suppose). That means that the only values for the third digit in each of the two factors would have to be 1, 3, 7 or 9 (1 * 9, 3 * 3, 7 * 7 or 9 * 1). If we check for this condition before checking whether a number is palindromic, we ought to cut down on the numbers checked by, oh, I don't know... half, at least? (it turns out that it's more: 405450 values, only 64980 have 1, 3, 7 or 9 in the end), so in order to avoid checking some 340,000 numbers, I wrote a third function: def check_value(number1, number2): number1, number2 = str(number1), str(number2) return number1[-1] in 1379 and number2[-1] in 1379 Putting this one inside the biggest() function, the final biggest() function looks like this: def biggest(): big_x, big_y, max_seen = 0, 0, 0 for x in range(999, 99, -1): for y in range(x, 99, -1): #so we don't double count if check_value(x, y): #we ignore all numbers that doesn't end in 1379 if x*y max_seen: continue #since we're decreasing if is_palindrome(x*y): big_x, big_y, max_seen = x, y, x*y return big_x, big_y, max_seen My biggest problem now is that I don't know how to measure any changes in efficiency. I know that the functions are working perfectly fine as-is, and I shouldn't really optimize without a need to, but I'm mostly curious as to whether the check_value() function is worth it or not. To this I thought I'd use timeit(), but I can't for the life of me work out how it works. At all. I've tried using it from the command prompt, from the interpreter and in the code itself and it just doesn't work. Or, it might very well work but it doesn't actually time anything for me. It's very frustrating, but I feel like I'm too stupid to read the official documentation for it (that is, I might understand the words in the documentation, but I can't get it to work). Please help? -- best regards, Robert S. -- Message: 3 Date: Thu, 15 Dec 2011 22:09:20 +0100 From: Calle calle_pyt...@live.se To: tutor@python.org Subject: [Tutor] [TUTOR]Code Deciphering Message-ID: dub109-ds323696802b29051022aa06f8...@phx.gbl Content-Type: text/plain; format=flowed; charset=iso-8859-1; reply-type=original Hi! I was wondering, how do you use Python to decipher codes? It feels like it should be pretty simple, but I haven't found any tutorials about it yet. Have a nice day! // Calle -- Message: 4 Date: Thu, 15 Dec 2011 22:34:41 +0100 From: Robert Sjoblom robert.sjob...@gmail.com To: Calle calle_pyt...@live.se Cc: tutor@python.org Subject: Re: [Tutor] [TUTOR]Code Deciphering Message-ID: CAJKU7g1fq=sledo0gncjkzobe30ozj9v2z4opnfx3kwbfzg...@mail.gmail.com Content-Type