Seite 1 von 1
Python Challenge Aufgabe 3
Verfasst: Freitag 20. April 2012, 18:47
von anymagical
Servus,
bin gerade dabei mich durch die Challenge zu arbeiten. 0 und 1 gingen ja noch

In der 3 Aufgabe heißt es:
One small letter, surrounded by EXACTLY three big bodyguards on each of its sides
Mit:
Code: Alles auswählen
for char in string.ascii_lowercase:
y=x.find(char)
if ord(x[y-3])<=90 and ord(x[y-2])<=90 and ord(x[y-1])<=90 and ord(x[y+3])<=90 and ord(x[y+2])<=90 and ord(x[y+1])<=90:
print x[y]
zeigt er mir keinen einzigen kleinen Buchstaben an. Wo ist denn da der Wurm drinnen, abgesehen vom 6*"and"
Denke ich in die falsche Richtung? FGHkAJH so stelle ich es mir vor.
MfG
Anymagical
Re: Python Challenge Aufgabe 3
Verfasst: Freitag 20. April 2012, 19:00
von EyDu
Ich nehme mal an, dass ein Text vorliegt, den du in x gespeichert hast? Dann suchst du mittels finde nur nach dem ersten Kleinbuchstaben und ignorierst alle späteren. Viel einfacher ginge das übrigen mit regulären Ausdrücken.
Re: Python Challenge Aufgabe 3
Verfasst: Freitag 20. April 2012, 19:46
von anymagical
Genau in x sind cirka 100000 byte
Habe mir jetzt das modul re angeschaut. Nun habe ich
Code: Alles auswählen
f=r"[A-Z]{3}[a-z]{1}[A-Z]{3}"
if re.search(f,x) != None:
print re.match
gibt mir eine Adresse im Speicher(?) aus.
Re: Python Challenge Aufgabe 3
Verfasst: Freitag 20. April 2012, 19:52
von EyDu
Kein Wunder, wenn du nicht das Ergebnis von re.search speicherst, sondern auf die match-Methode im re-Modul zugreifst

Re: Python Challenge Aufgabe 3
Verfasst: Freitag 20. April 2012, 20:07
von anymagical
m=re.search
print m.group(0)
Vielen Dank für die Hilfe und den Tip mit dem Modul!
Re: Python Challenge Aufgabe 3
Verfasst: Freitag 20. April 2012, 20:18
von BlackJack
@anymagical: Wobei man da auch ganz gut mit `re.findall()` arbeiten kann.
Re: Python Challenge Aufgabe 3
Verfasst: Samstag 21. April 2012, 07:02
von karolus
Hallo
Python_Challenge_Beschreibung hat geschrieben:One small letter, surrounded by EXACTLY three big bodyguards on each of its sides
Es gibt einen Grund dafür das 'EXACTLY' gross geschrieben wurde...
Karo
Re: Python Challenge Aufgabe 3
Verfasst: Samstag 21. April 2012, 09:31
von BlackJack
@karolus: Ich denke mal ja.
Re: Python Challenge Aufgabe 3
Verfasst: Samstag 21. April 2012, 10:20
von /me
karolus hat geschrieben:Es gibt einen Grund dafür das 'EXACTLY' gross geschrieben wurde...
Dann muss man den regulären Ausdruck wohl noch mit negative lookahead und negative lookbehind anreichern.
Re: Python Challenge Aufgabe 3
Verfasst: Sonntag 22. April 2012, 01:53
von karolus
Hallo
BlackJack hat geschrieben:@karolus: Ich denke mal ja.
@BlackJack:
Ich meinte das als Hinweis, nicht als Frage.
Karo
Re: Python Challenge Aufgabe 3
Verfasst: Montag 23. April 2012, 13:06
von mkesper
Ja, obwohl ich den Umgang mit dem re-Modul nicht unbedingt als typisch pythonisch bezeichnen würde, zielen zumindest einige der Aufgaben stark darauf ab.