Differences
This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision | ||
lehrkraefte:blc:informatik:ffprg1-2022:wordle:start [2022/04/27 20:59] Ivo Blöchliger [Richtig Platzierte] |
lehrkraefte:blc:informatik:ffprg1-2022:wordle:start [2022/06/02 12:19] (current) Ivo Blöchliger [Bryhton] |
||
---|---|---|---|
Line 48: | Line 48: | ||
</ | </ | ||
- | ==== Falsch platzierte | + | ==== Vollständige Bewertungsfunktion |
- | Wie oben, schreiben Sie eine Funktion '' | + | <WRAP todo> |
- | Schreiben sie ebenfalls Tests dafür. Testen Sie auch Wortpaare wie " | + | Wie oben, schreiben Sie eine Funktion '' |
+ | Es gibt zwei Videos, die den Algorithmus erklären. Versuchen Sie erst, die Aufgabe ohne die Videos zu lösen. Wenn das nicht klappt, schauen Sie sich nur den ersten Video an und versuchen Sie dann, die Methode in Python umzusetzen. Wenn das auch nicht klappt, schauen Sie sich das zweite Video auch noch an, schreiben Sie den Code aber nicht aus dem Video ab, sondern versuchen Sie, diesen Code danach selbst zu schreiben. | ||
+ | </ | ||
+ | Die Funktion sollte folgende Tests erfüllen (erfinden Sie gerne eigen, weitere Tests!) | ||
+ | <code python> | ||
+ | def tests(): | ||
+ | cases = [[" | ||
+ | | ||
+ | | ||
+ | | ||
+ | ] | ||
+ | for test in cases: | ||
+ | res = bewerten(test[0], | ||
+ | if res!=test[2]: | ||
+ | raise ValueError(" | ||
+ | print(" | ||
+ | |||
+ | </ | ||
+ | |||
+ | **Erklärvideos**: | ||
+ | * Funktion des Algorithmus (ohne Code): [[https:// | ||
+ | * Erklärung des entsprechen Python-Codes: | ||
+ | |||
+ | |||
+ | ==== Farben im Terminal ausgeben ==== | ||
+ | <code python> | ||
+ | print(" | ||
+ | print(" | ||
+ | </ | ||
+ | ==== Wortliste ==== | ||
+ | * Variante «easy»: Wortliste aus dem JavaScript-Code eines online Wordle kopieren. | ||
+ | * Variante «suchen und finden»: Wortliste auf dem Web suchen. | ||
+ | * Variante «selber bauen»: Wikipedia-Artikel herunterladen, | ||
+ | |||
+ | === Wikipedia «downloaden» === | ||
+ | Folgender Code funktioniert mehr schlecht als recht. Besser wäre es wohl, eine Library oder externes Programm wie ' | ||
+ | |||
+ | Der Vorteil dieses Programm ist, dass man die Wörter gleich mit der Häufigkeit hat. So kann man nur die häufigsten Wörter als zu erratende Wörter zulassen, aber alle als gültige Eingabewörter. | ||
+ | |||
+ | <hidden Code> | ||
+ | <code python crawler.py> | ||
+ | import socket | ||
+ | import ssl | ||
+ | import urllib | ||
+ | import re | ||
+ | import os | ||
+ | import sys | ||
+ | |||
+ | # Or simply set this to True in TigerJython | ||
+ | tigerJython = (sys.executable.find(" | ||
+ | |||
+ | # Download einer Wikipedia-Seite | ||
+ | # URL ist z.B. / | ||
+ | def download(url): | ||
+ | host = " | ||
+ | port = 443 | ||
+ | s = socket.socket(socket.AF_INET, | ||
+ | s.settimeout(0.5) | ||
+ | s.connect((host , port)) | ||
+ | s = ssl.wrap_socket(s) | ||
+ | request = "GET " + url + " HTTP/ | ||
+ | if (tigerJython): | ||
+ | s.sendall(request) | ||
+ | else: | ||
+ | s.sendall(bytes(request, | ||
+ | try: | ||
+ | reply = s.recv(4096) | ||
+ | except socket.timeout: | ||
+ | s.close() | ||
+ | return "" | ||
+ | start = reply.decode(' | ||
+ | if (start==-1): | ||
+ | start = reply.decode(' | ||
+ | if (start> | ||
+ | end = reply.decode(' | ||
+ | #while reply[end]> | ||
+ | # end+=1 | ||
+ | # print(" | ||
+ | numbytes = int(reply[(start+16): | ||
+ | print(" | ||
+ | while reply.decode(' | ||
+ | # print(" | ||
+ | more = s.recv(4096) | ||
+ | # print(" | ||
+ | reply += more | ||
+ | reply = reply[(reply.decode(' | ||
+ | # print(" | ||
+ | while len(reply)< | ||
+ | # print(" | ||
+ | more = s.recv(4096) | ||
+ | reply += more | ||
+ | # print(" | ||
+ | |||
+ | else: | ||
+ | # print(" | ||
+ | # print(reply) | ||
+ | try: | ||
+ | while True: | ||
+ | # print(" | ||
+ | more = s.recv(4096) | ||
+ | reply += more | ||
+ | except socket.timeout: | ||
+ | pass | ||
+ | s.close() | ||
+ | return reply.decode(' | ||
+ | |||
+ | |||
+ | todo = ["/ | ||
+ | words = {} | ||
+ | linksdone = {} | ||
+ | # Load existing data, if any | ||
+ | for l in range(4, | ||
+ | datafile = " | ||
+ | if os.path.isfile(datafile): | ||
+ | with open(datafile, | ||
+ | for line in file.readlines(): | ||
+ | entries = line.split(" | ||
+ | words[entries[0]] = int(entries[1]) | ||
+ | |||
+ | |||
+ | |||
+ | for i in range(200): | ||
+ | url = todo.pop(0) | ||
+ | linksdone[url] = True | ||
+ | print(" | ||
+ | html = download(url) | ||
+ | links = re.findall(r'< | ||
+ | links = [l for l in links if re.search(' | ||
+ | for l in links: | ||
+ | if not l in linksdone: | ||
+ | linksdone[l]=True | ||
+ | #print(l) | ||
+ | todo.append(l) | ||
+ | | ||
+ | text = re.sub('< | ||
+ | text = re.sub('< | ||
+ | ww = [w.upper() for w in text.split() if re.match(' | ||
+ | # print(" | ||
+ | newwords = 0 | ||
+ | for w in ww: | ||
+ | if w in words: | ||
+ | words[w]+=1 | ||
+ | else: | ||
+ | newwords+=1 | ||
+ | words[w] = 1 | ||
+ | | ||
+ | print(" | ||
+ | |||
+ | print(" | ||
+ | print(todo[0: | ||
+ | |||
+ | alle = words.keys() | ||
+ | # print(" | ||
+ | for l in range(4, | ||
+ | wl = [w for w in alle if len(w)==l] | ||
+ | wl.sort() | ||
+ | # print(" | ||
+ | with open(" | ||
+ | for w in wl: | ||
+ | file.write(" | ||
+ | | ||
+ | |||
+ | </ | ||
+ | </ | ||
+ | |||
+ | Ein mögliches Resultat dieses Codes ist {{kurse: | ||
+ | |||
+ | Eine riesige Liste (1.2 Mio) deutscher Wörter gibt es hier: https:// | ||
+ | Diese habe ich gefiltert und folgende Liste extrahiert: {{kurse: | ||
+ | |||
+ | ===== Brython ===== | ||
+ | Brython erlaubt es, Python auf Webseiten einzusetzen. Hier sind die nötigen Funktionen einmal programmiert. Was noch fehlt, ist die Spiel-Logik: | ||
+ | |||
+ | https:// | ||
+ | |||
+ | Hinweis: Öffnen Sie die Developper-Tools im Browser (F12). Im Konsolen-Tab sehen Sie die print-Ausgaben. Das kann bei der Fehlersuche helfen. | ||
+ | ===== JavaScript ===== | ||
+ | Rudimentäre Vorlage: https:// |