Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Works with more examples

Supports all the 001.* calibrations
  • Loading branch information...
commit b1eeb5b91d90e307d06d404ce7cd717188aa82ea 1 parent f08726a
Eric Garside authored
5 converter.py
... ... @@ -1,6 +1,6 @@
1 1 #!/usr/bin/env python
2 2 """Zoidberg conversions"""
3   -from definitions import Operation
  3 +from definitions import Operation, Relation
4 4
5 5 # Utility
6 6 def members(cls):
@@ -21,3 +21,6 @@ def to_number(s):
21 21
22 22 def to_operation(s):
23 23 return convert(s, Operation)
  24 +
  25 +def to_relation(s):
  26 + return convert(s, Relation)
3  definitions.py
@@ -28,3 +28,6 @@ def do(op, t1, t2):
28 28
29 29 class Relation:
30 30 EQUIVALENCE="="
  31 +
  32 + class Defs:
  33 + EQUIVALENCE=["="]
4 inferences.py
@@ -25,3 +25,7 @@ def is_number(s):
25 25 def is_operation(s):
26 26 convert.to_operation(s)
27 27
  28 +@test_conversion
  29 +def is_relation(s):
  30 + convert.to_relation(s)
  31 +
40 interpreter.py
@@ -21,6 +21,7 @@ def __init__(self, problem):
21 21 # Current state
22 22 self.last_value = None
23 23 self.last_operation = None
  24 + self.last_relation = None
24 25
25 26 # Output
26 27 self.expression = None
@@ -49,15 +50,19 @@ def _analyze(self):
49 50 self._think("No statement created.")
50 51 if self.expression:
51 52 self._create_statement("of assumed equality.")
52   - self._add("expression")
53   - self.statement.relation = Relation.EQUIVALENCE
  53 + self.last_relation = Relation.EQUIVALENCE
  54 + self._handle_relation()
54 55
55 56 is_equivalence = self.statement.relation == Relation.EQUIVALENCE
56 57 if is_equivalence and len(self.statement.expressions) == 1:
  58 + if self.expression:
  59 + self._think("Adding current expression to statement.")
  60 + self._add("expression")
  61 + else:
57 62 self._create_expression("for hanging equality.")
58 63 self._set_variable()
59 64 self._add("expression")
60   - self._add("statement")
  65 + self._add("statement")
61 66
62 67 # Output
63 68 def _create_statement(self, reasoning):
@@ -74,9 +79,20 @@ def _create_variable(self):
74 79 def _create_expression(self, reasoning):
75 80 self._think("New expression {0}", reasoning)
76 81 self.expression = Expression()
  82 + self._handle_relation()
  83 +
  84 + # Handlers
  85 + def _handle_relation(self):
  86 + if self.last_relation:
  87 + if not self.statement:
  88 + self._create_statement("for relationship.")
  89 + self._add("expression")
  90 + self._add("last_relation")
77 91
78 92 # Expression building
79 93 def _add_term(self, term):
  94 + if self.build_expression and not self.expression:
  95 + self._create_expression("for new term")
80 96 self.expression.terms.append(term)
81 97
82 98 def _add(self, v):
@@ -90,6 +106,10 @@ def _add(self, v):
90 106 self.statements.append(val)
91 107 return
92 108
  109 + if v == "last_relation":
  110 + self.statement.relation = val
  111 + return
  112 +
93 113 if v == "last_value":
94 114 t = Term(Term.VALUE, val)
95 115 if v == "last_operation":
@@ -100,7 +120,7 @@ def _add(self, v):
100 120 # States
101 121 def _set_variable(self, name=None):
102 122 if name:
103   - self._think("'{0}' is a value (a variable).", v)
  123 + self._think("'{0}' is a value (a variable).", name)
104 124 v = Symbol(name)
105 125 else:
106 126 v = self._create_variable()
@@ -115,6 +135,14 @@ def _set_constant(self, v):
115 135 if self.build_expression:
116 136 self._add("last_value")
117 137
  138 + def _set_relation(self, v):
  139 + v = convert.to_relation(v)
  140 + self.last_relation = v
  141 + self._think("'{0}' is a relationship.", v)
  142 +
  143 + if self.expression:
  144 + self._handle_relation()
  145 +
118 146 def _set_operation(self, v):
119 147 v = convert.to_operation(v)
120 148 self.last_operation = v
@@ -136,8 +164,12 @@ def _set_operation(self, v):
136 164 def _interpret(self, word, tag):
137 165 if word.isalpha():
138 166 self._think("{0} is alpha", word)
  167 + if self.build_expression:
  168 + self._set_variable(word)
139 169 else:
140 170 if infer.is_number(word):
141 171 self._set_constant(word)
  172 + if infer.is_relation(word):
  173 + self._set_relation(word)
142 174 if infer.is_operation(word):
143 175 self._set_operation(word)
11 zoidberg.py
@@ -22,17 +22,6 @@ class Operation:
22 22 "-"
23 23 ]
24 24
25   - @staticmethod
26   - def do(op, t1, t2):
27   - if op == "+":
28   - result = t1 + t2
29   - if op == "-":
30   - result = t1 - t2
31   - if op == "*":
32   - result = t1 * t2
33   - if op == "/":
34   - result = t1 / t2
35   - return Term(Term.VARIABLE, result)
36 25
37 26 class AnswerType:
38 27 # Answer is likely a number

0 comments on commit b1eeb5b

Please sign in to comment.
Something went wrong with that request. Please try again.