Commit 964eba71 authored by Taddeus Kroes's avatar Taddeus Kroes

Applied pep8.

parent 3b35d584
...@@ -164,7 +164,7 @@ class Parser(BisonParser): ...@@ -164,7 +164,7 @@ class Parser(BisonParser):
+ '|([\x00-\x09\x0b-\x19a-z0-9])\s*(\()' # a( -> a * ( + '|([\x00-\x09\x0b-\x19a-z0-9])\s*(\()' # a( -> a * (
+ '|(\))\s*([\x00-\x09\x0b-\x19a-z0-9])' # )a -> ) * a + '|(\))\s*([\x00-\x09\x0b-\x19a-z0-9])' # )a -> ) * a
+ '|([\x00-\x09\x0b-\x19a-z])\s*' + '|([\x00-\x09\x0b-\x19a-z])\s*'
+'([\x00-\x09\x0b-\x19a-z]+)' # ab -> a * b + '([\x00-\x09\x0b-\x19a-z]+)' # ab -> a * b
+ '|([0-9])\s*([\x00-\x09\x0b-\x19a-z])' # 4a -> 4 * a + '|([0-9])\s*([\x00-\x09\x0b-\x19a-z])' # 4a -> 4 * a
+ '|([\x00-\x09\x0b-\x19a-z])\s*([0-9])' # a4 -> a ^ 4 + '|([\x00-\x09\x0b-\x19a-z])\s*([0-9])' # a4 -> a ^ 4
+ '|([0-9])\s+([0-9]))' # 4 4 -> 4 * 4 + '|([0-9])\s+([0-9]))' # 4 4 -> 4 * 4
......
...@@ -25,109 +25,125 @@ class TestLeidenOefenopgave(TestCase): ...@@ -25,109 +25,125 @@ class TestLeidenOefenopgave(TestCase):
self.assertEqual(str(rewrite(exp)), solution) self.assertEqual(str(rewrite(exp)), solution)
def test_1_2(self): def test_1_2(self):
self.assertRewrite(['(x+1)^3', '(x + 1)(x + 1) ^ 2', self.assertRewrite([
'(x + 1)(x + 1)(x + 1)', '(x+1)^3', '(x + 1)(x + 1) ^ 2',
'(xx + x * 1 + 1x + 1 * 1)(x + 1)', '(x + 1)(x + 1)(x + 1)',
'(x ^ (1 + 1) + x * 1 + 1x + 1 * 1)(x + 1)', '(xx + x * 1 + 1x + 1 * 1)(x + 1)',
'(x ^ 2 + x * 1 + 1x +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+ 1 * 1', 'x ^ 3 + x ^ 2 * 2 + 1x ^ 2 + 1 * 2x + 1x + 1 * 1',
'x ^ 3 + (2 + 1)x ^ 2 + 1 * 2x + 1x + 1 * 1', 'x ^ 3 + x ^ 2 * 2 + x ^ 2 + 1 * 2x + 1x + 1 * 1',
'x ^ 3 + 3x ^ 2 + 1 * 2x + 1x + 1 * 1', 'x ^ 3 + (2 + 1)x ^ 2 + 1 * 2x + 1x + 1 * 1',
'x ^ 3 + 3x ^ 2 + 2x + 1x + 1 * 1', 'x ^ 3 + 3x ^ 2 + 1 * 2x + 1x + 1 * 1',
'x ^ 3 + 3x ^ 2 + 2x + x + 1 * 1', 'x ^ 3 + 3x ^ 2 + 2x + 1x + 1 * 1',
'x ^ 3 + 3x ^ 2 + (2 + 1)x + 1 * 1', 'x ^ 3 + 3x ^ 2 + 2x + x + 1 * 1',
'x ^ 3 + 3x ^ 2 + 3x + 1 * 1', 'x ^ 3 + 3x ^ 2 + (2 + 1)x + 1 * 1',
'x ^ 3 + 3x ^ 2 + 3x + 1', 'x ^ 3 + 3x ^ 2 + 3x + 1 * 1',
]) 'x ^ 3 + 3x ^ 2 + 3x + 1',
])
def test_1_3(self): def test_1_3(self):
# (x+1)^2 -> x^2 + 2x + 1 # (x+1)^2 -> x^2 + 2x + 1
self.assertRewrite(['(x+1)^2', '(x + 1)(x + 1)', self.assertRewrite([
'xx + x * 1 + 1x + 1 * 1', '(x+1)^2', '(x + 1)(x + 1)',
'x ^ (1 + 1) + x * 1 + 1x + 1 * 1', 'xx + x * 1 + 1x + 1 * 1',
'x ^ 2 + x * 1 + 1x + 1 * 1', 'x ^ (1 + 1) + x * 1 + 1x + 1 * 1',
'x ^ 2 + x + 1x + 1 * 1', 'x ^ 2 + x * 1 + 1x + 1 * 1',
'x ^ 2 + x + x + 1 * 1', 'x ^ 2 + x + 1x + 1 * 1',
'x ^ 2 + (1 + 1)x + 1 * 1', 'x ^ 2 + x + x + 1 * 1',
'x ^ 2 + 2x + 1 * 1', 'x ^ 2 + (1 + 1)x + 1 * 1',
'x ^ 2 + 2x + 1', 'x ^ 2 + 2x + 1 * 1',
]) 'x ^ 2 + 2x + 1',
])
def test_1_4(self): def test_1_4(self):
# (x-1)^2 -> x^2 - 2x + 1 # (x-1)^2 -> x^2 - 2x + 1
self.assertRewrite(['(x-1)^2', '(x - 1)(x - 1)', self.assertRewrite([
'xx + x * -1 - 1x - 1 * -1', '(x-1)^2', '(x - 1)(x - 1)',
'x ^ (1 + 1) + x * -1 - 1x - 1 * -1', 'xx + x * -1 - 1x - 1 * -1',
'x ^ 2 + x * -1 - 1x - 1 * -1', 'x ^ (1 + 1) + x * -1 - 1x - 1 * -1',
'x ^ 2 - x * 1 - 1x - 1 * -1', 'x ^ 2 + x * -1 - 1x - 1 * -1',
'x ^ 2 - x - 1x - 1 * -1', 'x ^ 2 - x * 1 - 1x - 1 * -1',
'x ^ 2 - x - x - 1 * -1', 'x ^ 2 - x - 1x - 1 * -1',
'x ^ 2 + (1 + 1) * -x - 1 * -1', 'x ^ 2 - x - x - 1 * -1',
'x ^ 2 + 2 * -x - 1 * -1', 'x ^ 2 + (1 + 1) * -x - 1 * -1',
'x ^ 2 - 2x - 1 * -1', 'x ^ 2 + 2 * -x - 1 * -1',
'x ^ 2 - 2x - -1', 'x ^ 2 - 2x - 1 * -1',
'x ^ 2 - 2x + 1', 'x ^ 2 - 2x - -1',
]) 'x ^ 2 - 2x + 1',
])
def test_1_4_1(self): def test_1_4_1(self):
self.assertRewrite(['x * -1 + 1x', self.assertRewrite([
'-x * 1 + 1x', 'x * -1 + 1x',
'-x + 1x', '-x * 1 + 1x',
'-x + x', '-x + 1x',
'(-1 + 1)x', '-x + x',
'0x', '(-1 + 1)x',
'0']) '0x',
'0',
])
def test_1_4_2(self): def test_1_4_2(self):
self.assertRewrite(['x * -1 - 1x', self.assertRewrite([
'-x * 1 - 1x', 'x * -1 - 1x',
'-x - 1x', '-x * 1 - 1x',
'-x - x', '-x - 1x',
'(1 + 1) * -x', '-x - x',
'2 * -x', '(1 + 1) * -x',
'-2x']) '2 * -x',
'-2x',
])
def test_1_4_3(self): def test_1_4_3(self):
self.assertRewrite(['x * -1 + x * -1', self.assertRewrite([
'-x * 1 + x * -1', 'x * -1 + x * -1',
'-x + x * -1', '-x * 1 + x * -1',
'-x - x * 1', '-x + x * -1',
'-x - x', '-x - x * 1',
'(1 + 1) * -x', '-x - x',
'2 * -x', '(1 + 1) * -x',
'-2x']) '2 * -x',
'-2x',
])
def test_1_5(self): def test_1_5(self):
self.assertRewrite(['(2x + x)x', '(2 + 1)xx', '3xx', self.assertRewrite([
'3x ^ (1 + 1)', '3x ^ 2']) '(2x + x)x',
'(2 + 1)xx',
'3xx',
'3x ^ (1 + 1)',
'3x ^ 2',
])
def test_1_7(self): def test_1_7(self):
self.assertRewrite(['(4x + 5) * -(5 - 4x)', self.assertRewrite([
'(4x + 5)(-5 - -4x)', '(4x + 5) * -(5 - 4x)',
'(4x + 5)(-5 + 4x)', '(4x + 5)(-5 - -4x)',
'4x * -5 + 4x * 4x + 5 * -5 + 5 * 4x', '(4x + 5)(-5 + 4x)',
'-20x + 4x * 4x + 5 * -5 + 5 * 4x', '4x * -5 + 4x * 4x + 5 * -5 + 5 * 4x',
'-20x + 16xx + 5 * -5 + 5 * 4x', '-20x + 4x * 4x + 5 * -5 + 5 * 4x',
'-20x + 16x ^ (1 + 1) + 5 * -5 + 5 * 4x', '-20x + 16xx + 5 * -5 + 5 * 4x',
'-20x + 16x ^ 2 + 5 * -5 + 5 * 4x', '-20x + 16x ^ (1 + 1) + 5 * -5 + 5 * 4x',
'-20x + 16x ^ 2 - 25 + 5 * 4x', '-20x + 16x ^ 2 + 5 * -5 + 5 * 4x',
'-20x + 16x ^ 2 - 25 + 20x', '-20x + 16x ^ 2 - 25 + 5 * 4x',
'(-20 + 20)x + 16x ^ 2 - 25', '-20x + 16x ^ 2 - 25 + 20x',
'0x + 16x ^ 2 - 25', '(-20 + 20)x + 16x ^ 2 - 25',
'0 + 16x ^ 2 - 25', '0x + 16x ^ 2 - 25',
'16x ^ 2 - 25']) '0 + 16x ^ 2 - 25',
'16x ^ 2 - 25',
])
def test_2(self): def test_2(self):
pass pass
...@@ -136,25 +152,39 @@ class TestLeidenOefenopgave(TestCase): ...@@ -136,25 +152,39 @@ class TestLeidenOefenopgave(TestCase):
pass pass
def test_4_1(self): def test_4_1(self):
self.assertRewrite(['2/15 + 1/4', '8 / 60 + 15 / 60', '(8 + 15) / 60', self.assertRewrite([
'23 / 60']) '2/15 + 1/4',
'8 / 60 + 15 / 60',
'(8 + 15) / 60',
'23 / 60',
])
def test_4_2(self): def test_4_2(self):
self.assertRewrite(['2/7 - 4/11', '22 / 77 - 28 / 77', self.assertRewrite([
'(22 - 28) / 77', '-6 / 77']) '2/7 - 4/11',
'22 / 77 - 28 / 77',
'(22 - 28) / 77',
'-6 / 77',
])
def test_4_3(self): def test_4_3(self):
self.assertRewrite(['(7/3)(3/5)', self.assertRewrite([
'7 * 3 / (3 * 5)', '(7/3)(3/5)',
'21 / (3 * 5)', '7 * 3 / (3 * 5)',
'21 / 15', '21 / (3 * 5)',
'7 / 5']) '21 / 15',
'7 / 5',
])
#def test_4_4(self): #def test_4_4(self):
# self.assertRewrite(['(3/4) / (5/6)', '9 / 10']) # self.assertRewrite(['(3/4) / (5/6)', '9 / 10'])
def test_4_5(self): def test_4_5(self):
self.assertRewrite(['1/4 * 1/x', '1 / 4 / x', '1 / (4x)']) self.assertRewrite([
'1/4 * 1/x',
'1 / 4 / x',
'1 / (4x)',
])
#def test_4_6(self): #def test_4_6(self):
# self.assertRewrite(['(3/x^2) / (x/7)', '21 / x^3']) # self.assertRewrite(['(3/x^2) / (x/7)', '21 / x^3'])
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment