Created
December 20, 2014 12:49
-
-
Save darth30joker/5b704326bfd0affa07b8 to your computer and use it in GitHub Desktop.
homework
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import re | |
import math | |
class CalculationException(Exception): | |
pass | |
class Calculation: | |
def __init__(self, string): | |
self.string = self._parse(string) | |
def _parse(self, string): | |
return ''.join([i for i in string if i != ' ']) | |
def calculate(self): | |
string = self.string | |
while True: | |
if string.isnumeric(): | |
return string | |
string = self._calculate(string) | |
def _calculate(self, string): | |
""" | |
Priorities: | |
1. sqrt | |
2. a * b or a / b | |
4. () | |
5. a + b or a - b | |
""" | |
m = re.search('sqrt\((\d+)\)', string) | |
if m: | |
return string.replace( 'sqrt(%s)' % m.groups()[0], '%s' % int(math.sqrt(int(m.groups()[0]))) ) | |
m = re.search('(\d+)\*(\d+)', string) | |
if m: | |
l, r = m.groups()[0], m.groups()[1] | |
return string.replace( '%s*%s' % (l, r), '%s' % int( (int(l) * int(r) ) ) ) | |
m = re.search('(\d+)\/(\d+)', string) | |
if m: | |
l, r = m.groups()[0], m.groups()[1] | |
return string.replace( '%s/%s' % (l, r), '%s' % int( int(l) / int(r) ) ) | |
m = re.search('\((\d+)\)', string) | |
if m: | |
return string.replace('(%s)' % (m.groups()[0]), '%s' % (m.groups()[0])) | |
m = re.search('\((\d+)\+(\d+)\)', string) | |
if m: | |
l, r = m.groups()[0], m.groups()[1] | |
return string.replace('%s+%s' % (l, r), '%s' % (int(l) + int(r))) | |
m = re.search('\((\d+)\-(\d+)\)', string) | |
if m: | |
l, r = m.groups()[0], m.groups()[1] | |
return string.replace('%s-%s' % (l, r), '%s' % (int(l) - int(r))) | |
m = re.search('(\d+)\+(\d+)', string) | |
if m: | |
l, r = m.groups()[0], m.groups()[1] | |
return string.replace('%s+%s' % (l, r), '%s' % (int(l) + int(r))) | |
m = re.search('(\d+)\-(\d+)', string) | |
if m: | |
l, r = m.groups()[0], m.groups()[1] | |
return string.replace('%s-%s' % (l, r), '%s' % (int(l) - int(r))) | |
raise CalculationException('Wrong formula!') | |
if __name__ == '__main__': | |
calc = Calculation('( 2 + ( ( 4 + 6 ) * sqrt(9)) / 2 )') | |
print(calc.calculate()) | |
calc = Calculation('( 2 + ( ( 4 + 6 ) * (9 * 2) - ( 5 - 1)))') | |
print(calc.calculate()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import unittest | |
from calc import Calculation | |
from calc import CalculationException | |
class CalculationTests(unittest.TestCase): | |
def test_plus(self): | |
calc = Calculation('5 + 3') | |
self.assertEqual(calc.calculate(), '8') | |
def test_minus(self): | |
calc = Calculation('6 - 4') | |
self.assertEqual(calc.calculate(), '2') | |
def test_times(self): | |
calc = Calculation('2 * 4') | |
self.assertEqual(calc.calculate(), '8') | |
def test_divide(self): | |
calc = Calculation('6 / 3') | |
self.assertEqual(calc.calculate(), '2') | |
def test_brackets(self): | |
calc = Calculation('5 * (2 + 3)') | |
self.assertEqual(calc.calculate(), '25') | |
def test_together(self): | |
calc = Calculation('( 2 + ( ( 4 + 6 ) * sqrt(9)) / 2 )') | |
self.assertEqual(calc.calculate(), '17') | |
def test_nagative(self): | |
with self.assertRaises(CalculationException): | |
calc = Calculation('( 2 + ( ( 4 + 6 ) * st(9)) / 2 )') | |
calc.calculate() | |
if __name__ == '__main__': | |
unittest.main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment