Na gut, dann werde ich mal die Aufgabe lösen:
Code: Alles auswählen
import re
s = """...."""
s = "\n".join(s.splitlines()[6:])
s = re.sub(r'(?s)(["\'])(.*?)\1', lambda m:'%s%s%s' % (m.group(1), m.group(2).replace("\n", "\\n"), m.group(1)), s)
s = re.sub(r"(?s)/\*.*?\*/", "", s)
s = re.sub(r"\w+\s+(\w+)\(void\)\{", "def \\1():", s)
s = re.sub(r"int\s+(\w+)\(char\s+(\w+)\s*\[\]\s*\)\{", "def \\1(\\2):", s)
s = re.sub(r"while\(getchar\(\) != '\\n'\);", "pass", s)
s = re.sub(r"int (tipp)?", "", s)
s = re.sub(r"char \w+;", "", s)
s = re.sub(r"else if", "elif", s)
s = re.sub(r"else(\s*\{)?", "else:", s)
s = re.sub(r"(if\s*\(.*\))\n", "\\1:\n", s)
s = re.sub(r"\{", ":", s)
s = re.sub(r"\}", "", s)
s = re.sub(r'printf\((".*?"),', "print(\\1 % ", s)
s = re.sub(r'printf\(', "print(", s)
s = re.sub(r'scanf\("%c",\s*&(\w+)\)', "\\1 = input()", s)
s = re.sub(r"\(! ", "(not ", s)
exec(s)
main()
Einfach bei "..." den Originalquelltext einfügen, die Einrückung von dem einen "if" korrigieren und voilá, Zahlenraten in Python.
Stefan