def unserialize(cls, r): products = r['products'] typ = r['type'] reactives = r['reactives'] return Reaction( tuple(Expression.parse(product) for product in products), typ, tuple(Expression.parse(reactive) for reactive in reactives))
def part1(lines): sum = 0 for line in lines: expr = Expression(line) expr.parse() sum += expr.evaluate() return sum
def test_break_expands(self): n_combs = N // 10 for i in range(n_combs): self.pool.remove(Expression.parse('I')) self.pool.remove(Expression.parse('S')) self.pool.append(Expression.parse('IS')) self.assertEqual(len(self.pool), N - n_combs) for i in range(10 * N): t = self.pool.pop_reactive() if not self.pool.tape_break(t): self.pool.rollback(t) self.assertEqual(len(self.pool), N)
def test_break_conservation(self): for i in range(N // 10): self.pool.remove(Expression.parse('I')) self.pool.remove(Expression.parse('S')) self.pool.append(Expression.parse('IS')) total_size = self.pool.get_total_size() self.assertEqual(total_size, N) for i in range(10 * N): t = self.pool.pop_reactive() if not self.pool.tape_break(t): self.pool.rollback(t) total_size = self.pool.get_total_size() self.assertEqual(total_size, N)
def test_reduce_expands(self): n_combs = N // 10 for i in range(n_combs): self.pool.remove(Expression.parse('I')) self.pool.remove(Expression.parse('S')) self.pool.append(Expression.parse('IS')) self.assertEqual(len(self.pool), N - i - 1) c = 0 for i in range(10 * N): t = self.pool.pop_reactive() if self.pool.tape_reduce(t): c += 1 self.assertEqual(len(self.pool), N - n_combs + c) else: if len(t) == 2: print(t.is_reducible(self.pool)) self.pool.rollback(t) self.assertEqual(len(self.pool), N)
def main(): ap = argparse.ArgumentParser() ap.add_argument('expression') ap.add_argument('-n', type=int, default=10) ap.add_argument('-r', '--replace', action='append', default=[]) args = ap.parse_args() exp = Expression.parse(args.expression) exp = exp(exp) replacements = args.replace for i in range(args.n): if not exp.is_reducible(): break exp = exp.sreduce()[0] #dreduce()#sreduce()[0] print(with_replacements(exp, replacements)) print(str(exp).replace(args.expression, 'A'))
def test_parse_default_parenthesis_remove(self): t = Expression.parse("((SS)K)") self.assertEqual(t, e(S, S, K))
def test_parse_default_parenthesis(self): t = Expression.parse("SSK") self.assertEqual(t, e(S, S, K))
def test_parse_atom_par(self): t = Expression.parse('(K)') self.assertEqual(t, e(K))
def test_str_parsable_atomic(self): t = e(S) self.assertEqual(Expression.parse(str(t)), t)
def test_str_parsable(self): t = e(S, e(K, K), e(K, e(K, K), S, I)) self.assertEqual(Expression.parse(str(t)), t)
def test_parse_renormalize(self): t = Expression.parse('(SKK)S') self.assertEqual(t, e(S, K, K, S))
def test_parse_simplify(self): t = Expression.parse("(SK)") self.assertEqual(t, e(S, K))
def load_pool_expressions(self, expressions_strings): for expression_string in expressions_strings: self.append(Expression.parse(expression_string))
def test_parse_default_parenthesis_remove_embedded(self): t = Expression.parse("K(KKS)") self.assertEqual(t, e(K, e(K, K, S)))
def test_parse_spaces(self): t = Expression.parse("S ( S K )") self.assertEqual(t, e(S, e(S, K)))
def test_parse_complex(self): t = Expression.parse("(SK(SK))S") self.assertEqual(t, e(S, K, e(S, K), S))