Skip to content
Snippets Groups Projects
Commit 9bce19f5 authored by Cedric Hartz's avatar Cedric Hartz
Browse files

WEIGHTS hinzugefügt; Wahrscheinlichkeiten etc leicht angepasst

parent 1eeb551c
No related branches found
No related tags found
No related merge requests found
......@@ -4,7 +4,6 @@
"""
TODO:
- bitweise Operationen
- eventuell weights für die OPERATORS
- Erklärung:
- binary operators
......@@ -17,10 +16,12 @@ OPERATORS = ("**", "%", "*", "//", "/", "+", "-", ">=", "<=", ">", "<", "==", "!
EQUALITY_OPERATORS = ("<", ">", "<=", ">=", "==", "!=", "is", "is not") # these follow a different precedence rule (1<2<3 <=> 1<2 and 2<3)
PRECEDENCE = ({"**"}, {"%", "*", "//", "/"}, {"+", "-"}, EQUALITY_OPERATORS, {"not"}, {"and"}, {"or"}) # precedence order
### Probabilities:
WEIGHTS = (10,6,10,7,10,10,10,8,8,8,8,10,10,10,10,8,10,10) # not has own probability P_NOT
assert len(OPERATORS) == len(WEIGHTS)
P_OPEN_BRACKET = 0.2
P_NOT = 0.2
#P_LOGIC_NOT = 0.15
P_CLOSE_BRACKET = 0.25
P_CLOSE_BRACKET = 0.3
ERROR_LITERAL = "!err!"
QUOTATION_MARKS = "\""
......@@ -35,6 +36,8 @@ def get_question(length=4, operators=OPERATORS, brackets=True, conditions=False,
- length: num of literals that are connected via operators
"""
assert all([o in OPERATORS for o in operators]), "Es wurden Operatoren angegeben, die nicht erlaubt sind, vgl. ALLOWED_OPERATORS"
weights = tuple([WEIGHTS[i] for i in range(len(WEIGHTS)) if OPERATORS[i] in operators])
assert len(operators) == len(weights)
question_contains_errors = True
while question_contains_errors:
string = "" # that's how it all starts
......@@ -58,7 +61,7 @@ def get_question(length=4, operators=OPERATORS, brackets=True, conditions=False,
string += ")"
num_brackets -= 1
string += " " # between literal and operator
string += random.choice(operators)
string += random.choices(operators, weights)[0]
string += " " # between operator and literal (next loop)
string += random.choice(literals)() # last literal
string += ")"*num_brackets # close all brackets
......@@ -457,7 +460,7 @@ def main():
while 1:
print("\n----------------------\n") # seperator
# get a question
q = get_question(length=14)
q = get_question()
num_question += 1
# ask the question
print(f"Frage {num_question}: "+q)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment