Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python-Challenge Level 3 [closed]

Tags:

python

The questions is: One small letter, surrounded by EXACTLY three big bodyguards on each of its sides. I wrote this code and get an answer. I thougt it would be correct, but it doesn't work. Can anybody help me? My answer: KWGtIDC

import urllib, sys, string
from string import maketrans

bbb = 0

f = urllib.urlopen("http://www.pythonchallenge.com/pc/def/equality.html")
while 1:
    buf = f.read(200000)
    if not len(buf):
        break
    for x in range(len(buf)):
        if buf[x] in string.ascii_lowercase:
           if buf[x+1] in string.ascii_uppercase:
               if buf[x-1] in string.ascii_uppercase:
                   if buf[x+2] in string.ascii_uppercase:
                       if buf[x-2] in string.ascii_uppercase:
                           if buf[x+3] in string.ascii_uppercase:
                               if buf[x-3] in string.ascii_uppercase:
                                   if buf[x+4] in string.ascii_lowercase:
                                       if buf[x-4] in string.ascii_lowercase:
                                           bbb = x
    sys.stdout.write(buf)
    print(buf[bbb-3:bbb+4])
like image 240
kame Avatar asked May 15 '26 12:05

kame


2 Answers

A couple of points:

  • You need to operate on the comment block in the source of the html page, not the entire page itself. I'm not sure if the rest of the page makes a difference, but still. I'd copy the comment block to another file locally, and go from there.

  • The title of the page is "re". Does that ring any bells?

  • There may be more than one occurrence of the pattern that fits your requirement, and your program overwrites it each time bbb = x. You need all of them, IIRC.

like image 83
sykora Avatar answered May 18 '26 02:05

sykora


In your code

if buf[x+4] in string.ascii_lowercase:

will only work if there is a fouth (lowercase) character, but did you consider the case that there is no fourth character, such as the end of sring (for instanceL: "ABCdEFG")?

Without ruining the puzzle, have you try creating a regular expression? A regex avoids the nested loops and requires a lot less lines.

like image 32
catchmeifyoutry Avatar answered May 18 '26 02:05

catchmeifyoutry



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!