Last active
May 31, 2018 18:47
-
-
Save cbefus/da35a83ae5d958baa43368022db6d67a to your computer and use it in GitHub Desktop.
A series of files demonstrating TDD ping pong in python creating a reverse polish calculator
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
class ReversePolishCalculatorTest(unittest.TestCase): | |
def test_can_instantiate_a_calculator(self): | |
ReversePolishCalculator() | |
# Ran 1 test in 0.002s | |
# FAILED (errors=1) | |
# Error | |
# Traceback (most recent call last): | |
# File "/usr/lib/python3.5/unittest/case.py", line 58, in testPartExecutor | |
# yield | |
# File "/usr/lib/python3.5/unittest/case.py", line 600, in run | |
# testMethod() | |
# File "/home/cbefus/indeed/interview/python/reverse_polish.py", line 7, in test_can_instantiate_a_calculator | |
# ReversePolishCalculator() | |
# NameError: name 'ReversePolishCalculator' is not defined |
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
import unittest | |
class ReversePolishCalculator: | |
def eval(self, input): | |
return 3 if input else None | |
class EvaluationException(Exception): | |
pass | |
class ReversePolishCalculatorTest(unittest.TestCase): | |
def test_can_instantiate_a_calculator(self): | |
ReversePolishCalculator() | |
def test_can_eval_a_single_number(self): | |
calculator = ReversePolishCalculator() | |
self.assertEqual(3, calculator.eval("3")) | |
def test_evals_empty_as_none(self): | |
calculator = ReversePolishCalculator() | |
self.assertEqual(None, calculator.eval("")) | |
def test_evals_missing_operator(self): | |
calculator = ReversePolishCalculator() | |
with self.assertRaises(EvaluationException): | |
calculator.eval("2 5") | |
# Testing started at 6:48 PM ... | |
# /home/cbefus/indeed/interview/python/venv/bin/python /home/cbefus/.local/share/JetBrains/Toolbox/apps/PyCharm-P/ch-0/181.4892.64/helpers/pycharm/_jb_unittest_runner.py --target reverse_polish.ReversePolishCalculatorTest | |
# Launching unittests with arguments python -m unittest reverse_polish.ReversePolishCalculatorTest in /home/cbefus/indeed/interview/python | |
# Failure | |
# Traceback (most recent call last): | |
# File "/usr/lib/python3.5/unittest/case.py", line 58, in testPartExecutor | |
# yield | |
# File "/usr/lib/python3.5/unittest/case.py", line 600, in run | |
# testMethod() | |
# File "/home/cbefus/indeed/interview/python/reverse_polish.py", line 29, in test_evals_missing_operator | |
# calculator.eval("2 5") | |
# File "/usr/lib/python3.5/unittest/case.py", line 198, in __exit__ | |
# self._raiseFailure("{} not raised".format(exc_name)) | |
# File "/usr/lib/python3.5/unittest/case.py", line 134, in _raiseFailure | |
# raise self.test_case.failureException(msg) | |
# AssertionError: EvaluationException not raised | |
# Ran 4 tests in 0.003s | |
# FAILED (failures=1) | |
# Process finished with exit code 1 |
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
import unittest | |
class ReversePolishCalculator: | |
def eval(self, input): | |
if len(input) == 1: | |
return 3 | |
elif len(input) == 0: | |
return None | |
else: | |
raise EvaluationException() | |
class EvaluationException(Exception): | |
pass | |
class ReversePolishCalculatorTest(unittest.TestCase): | |
def test_can_instantiate_a_calculator(self): | |
ReversePolishCalculator() | |
def test_can_eval_a_single_number(self): | |
calculator = ReversePolishCalculator() | |
self.assertEqual(3, calculator.eval("3")) | |
def test_evals_empty_as_none(self): | |
calculator = ReversePolishCalculator() | |
self.assertEqual(None, calculator.eval("")) | |
def test_evals_missing_operator(self): | |
calculator = ReversePolishCalculator() | |
with self.assertRaises(EvaluationException): | |
calculator.eval("2 5") | |
# Testing started at 6:51 PM ... | |
# /home/cbefus/indeed/interview/python/venv/bin/python /home/cbefus/.local/share/JetBrains/Toolbox/apps/PyCharm-P/ch-0/181.4892.64/helpers/pycharm/_jb_unittest_runner.py --target reverse_polish.ReversePolishCalculatorTest | |
# Ran 4 tests in 0.001s | |
# OK | |
# Launching unittests with arguments python -m unittest reverse_polish.ReversePolishCalculatorTest in /home/cbefus/indeed/interview/python | |
# Process finished with exit code 0 |
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
import unittest | |
class ReversePolishCalculator: | |
def eval(self, input): | |
if len(input) == 1: | |
return 3 | |
elif len(input) == 0: | |
return None | |
else: | |
raise EvaluationException() | |
class EvaluationException(Exception): | |
pass | |
class ReversePolishCalculatorTest(unittest.TestCase): | |
def test_can_instantiate_a_calculator(self): | |
ReversePolishCalculator() | |
def test_can_eval_a_single_number(self): | |
calculator = ReversePolishCalculator() | |
self.assertEqual(3, calculator.eval("3")) | |
def test_evals_empty_as_none(self): | |
calculator = ReversePolishCalculator() | |
self.assertEqual(None, calculator.eval("")) | |
def test_evals_missing_operator(self): | |
calculator = ReversePolishCalculator() | |
with self.assertRaises(EvaluationException): | |
calculator.eval("2 5") | |
def test_evals_simple_addition(self): | |
calculator = ReversePolishCalculator() | |
self.assertEqual(9, calculator.eval("6 3 +")) | |
# Testing started at 6:52 PM ... | |
# /home/cbefus/indeed/interview/python/venv/bin/python /home/cbefus/.local/share/JetBrains/Toolbox/apps/PyCharm-P/ch-0/181.4892.64/helpers/pycharm/_jb_unittest_runner.py --target reverse_polish.ReversePolishCalculatorTest | |
# Launching unittests with arguments python -m unittest reverse_polish.ReversePolishCalculatorTest in /home/cbefus/indeed/interview/python | |
# Error | |
# Traceback (most recent call last): | |
# File "/usr/lib/python3.5/unittest/case.py", line 58, in testPartExecutor | |
# yield | |
# File "/usr/lib/python3.5/unittest/case.py", line 600, in run | |
# testMethod() | |
# File "/home/cbefus/indeed/interview/python/reverse_polish.py", line 38, in test_evals_simple_addition | |
# self.assertEqual(9, calculator.eval("6 3 +")) | |
# File "/home/cbefus/indeed/interview/python/reverse_polish.py", line 12, in eval | |
# raise EvaluationException() | |
# reverse_polish.EvaluationException | |
# Ran 5 tests in 0.003s | |
# FAILED (errors=1) | |
# Process finished with exit code 1 |
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
import unittest | |
class ReversePolishCalculator: | |
def eval(self, input): | |
tokens = input.split() | |
if not tokens: | |
return None | |
stack = [] | |
for token in tokens: | |
if token.isdigit(): | |
stack.append(int(token)) | |
else: | |
operand1 = stack.pop() | |
operand2 = stack.pop() | |
return operand1 + operand2 | |
if len(stack) != 1: | |
raise EvaluationException() | |
return stack.pop() | |
class EvaluationException(Exception): | |
pass | |
class ReversePolishCalculatorTest(unittest.TestCase): | |
def test_can_instantiate_a_calculator(self): | |
ReversePolishCalculator() | |
def test_can_eval_a_single_number(self): | |
calculator = ReversePolishCalculator() | |
self.assertEqual(3, calculator.eval("3")) | |
def test_evals_empty_as_none(self): | |
calculator = ReversePolishCalculator() | |
self.assertEqual(None, calculator.eval("")) | |
def test_evals_missing_operator(self): | |
calculator = ReversePolishCalculator() | |
with self.assertRaises(EvaluationException): | |
calculator.eval("2 5") | |
def test_evals_simple_addition(self): | |
calculator = ReversePolishCalculator() | |
self.assertEqual(9, calculator.eval("6 3 +")) | |
# Testing started at 7:03 PM ... | |
# /home/cbefus/indeed/interview/python/venv/bin/python /home/cbefus/.local/share/JetBrains/Toolbox/apps/PyCharm-P/ch-0/181.4892.64/helpers/pycharm/_jb_unittest_runner.py --target reverse_polish.ReversePolishCalculatorTest | |
# Ran 5 tests in 0.001s | |
# OK | |
# Launching unittests with arguments python -m unittest reverse_polish.ReversePolishCalculatorTest in /home/cbefus/indeed/interview/python | |
# Process finished with exit code 0 |
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
import unittest | |
class ReversePolishCalculator: | |
def eval(self, input): | |
tokens = input.split() | |
if not tokens: | |
return None | |
stack = [] | |
for token in tokens: | |
if token.isdigit(): | |
stack.append(int(token)) | |
else: | |
operand1 = stack.pop() | |
operand2 = stack.pop() | |
return operand1 + operand2 | |
if len(stack) != 1: | |
raise EvaluationException() | |
return stack.pop() | |
class EvaluationException(Exception): | |
pass | |
class ReversePolishCalculatorTest(unittest.TestCase): | |
def test_can_instantiate_a_calculator(self): | |
ReversePolishCalculator() | |
def test_can_eval_a_single_number(self): | |
calculator = ReversePolishCalculator() | |
self.assertEqual(3, calculator.eval("3")) | |
def test_evals_empty_as_none(self): | |
calculator = ReversePolishCalculator() | |
self.assertEqual(None, calculator.eval("")) | |
def test_evals_missing_operator(self): | |
calculator = ReversePolishCalculator() | |
with self.assertRaises(EvaluationException): | |
calculator.eval("2 5") | |
def test_evals_simple_addition(self): | |
calculator = ReversePolishCalculator() | |
self.assertEqual(9, calculator.eval("6 3 +")) | |
def test_eval_operand_missing_raises(self): | |
calculator = ReversePolishCalculator() | |
with self.assertRaises(EvaluationException): | |
calculator.eval("+") | |
# Testing started at 7:05 PM ... | |
# /home/cbefus/indeed/interview/python/venv/bin/python /home/cbefus/.local/share/JetBrains/Toolbox/apps/PyCharm-P/ch-0/181.4892.64/helpers/pycharm/_jb_unittest_runner.py --target reverse_polish.ReversePolishCalculatorTest | |
# Launching unittests with arguments python -m unittest reverse_polish.ReversePolishCalculatorTest in /home/cbefus/indeed/interview/python | |
# Error | |
# Traceback (most recent call last): | |
# File "/usr/lib/python3.5/unittest/case.py", line 58, in testPartExecutor | |
# yield | |
# File "/usr/lib/python3.5/unittest/case.py", line 600, in run | |
# testMethod() | |
# File "/home/cbefus/indeed/interview/python/reverse_polish.py", line 56, in test_eval_operand_missing_raises | |
# calculator.eval("+") | |
# File "/home/cbefus/indeed/interview/python/reverse_polish.py", line 17, in eval | |
# operand1 = stack.pop() | |
# IndexError: pop from empty list | |
# Ran 6 tests in 0.002s | |
# FAILED (errors=1) | |
# Process finished with exit code 1 |
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
import unittest | |
class ReversePolishCalculator: | |
def eval(self, input): | |
tokens = input.split() | |
if not tokens: | |
return None | |
stack = [] | |
for token in tokens: | |
if token.isdigit(): | |
stack.append(int(token)) | |
else: | |
try: | |
operand1 = stack.pop() | |
operand2 = stack.pop() | |
except IndexError: | |
raise EvaluationException() | |
return operand1 + operand2 | |
if len(stack) != 1: | |
raise EvaluationException() | |
return stack.pop() | |
class EvaluationException(Exception): | |
pass | |
class ReversePolishCalculatorTest(unittest.TestCase): | |
def test_can_instantiate_a_calculator(self): | |
ReversePolishCalculator() | |
def test_can_eval_a_single_number(self): | |
calculator = ReversePolishCalculator() | |
self.assertEqual(3, calculator.eval("3")) | |
def test_evals_empty_as_none(self): | |
calculator = ReversePolishCalculator() | |
self.assertEqual(None, calculator.eval("")) | |
def test_evals_missing_operator(self): | |
calculator = ReversePolishCalculator() | |
with self.assertRaises(EvaluationException): | |
calculator.eval("2 5") | |
def test_evals_simple_addition(self): | |
calculator = ReversePolishCalculator() | |
self.assertEqual(9, calculator.eval("6 3 +")) | |
def test_eval_operand_missing_raises(self): | |
calculator = ReversePolishCalculator() | |
with self.assertRaises(EvaluationException): | |
calculator.eval("+") | |
# Testing started at 7:08 PM ... | |
# /home/cbefus/indeed/interview/python/venv/bin/python /home/cbefus/.local/share/JetBrains/Toolbox/apps/PyCharm-P/ch-0/181.4892.64/helpers/pycharm/_jb_unittest_runner.py --target reverse_polish.ReversePolishCalculatorTest | |
# Ran 6 tests in 0.002s | |
# OK | |
# Launching unittests with arguments python -m unittest reverse_polish.ReversePolishCalculatorTest in /home/cbefus/indeed/interview/python | |
# Process finished with exit code 0 | |
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
import unittest | |
class ReversePolishCalculator: | |
def eval(self, input): | |
tokens = input.split() | |
if not tokens: | |
return None | |
stack = [] | |
for token in tokens: | |
if token.isdigit(): | |
stack.append(int(token)) | |
else: | |
try: | |
operand1 = stack.pop() | |
operand2 = stack.pop() | |
except IndexError: | |
raise EvaluationException() | |
return operand1 + operand2 | |
if len(stack) != 1: | |
raise EvaluationException() | |
return stack.pop() | |
class EvaluationException(Exception): | |
pass | |
class ReversePolishCalculatorTest(unittest.TestCase): | |
def test_can_instantiate_a_calculator(self): | |
ReversePolishCalculator() | |
def test_can_eval_a_single_number(self): | |
calculator = ReversePolishCalculator() | |
self.assertEqual(3, calculator.eval("3")) | |
def test_evals_empty_as_none(self): | |
calculator = ReversePolishCalculator() | |
self.assertEqual(None, calculator.eval("")) | |
def test_evals_missing_operator(self): | |
calculator = ReversePolishCalculator() | |
with self.assertRaises(EvaluationException): | |
calculator.eval("2 5") | |
def test_evals_simple_addition(self): | |
calculator = ReversePolishCalculator() | |
self.assertEqual(9, calculator.eval("6 3 +")) | |
def test_eval_operand_missing_raises(self): | |
calculator = ReversePolishCalculator() | |
with self.assertRaises(EvaluationException): | |
calculator.eval("+") | |
def test_eval_multiple_operators(self): | |
calculator = ReversePolishCalculator() | |
self.assertEqual(200, calculator.eval("100 20 80 + +")) | |
# Testing started at 7:19 PM ... | |
# /home/cbefus/indeed/interview/python/venv/bin/python /home/cbefus/.local/share/JetBrains/Toolbox/apps/PyCharm-P/ch-0/181.4892.64/helpers/pycharm/_jb_unittest_runner.py --target reverse_polish.ReversePolishCalculatorTest | |
# Launching unittests with arguments python -m unittest reverse_polish.ReversePolishCalculatorTest in /home/cbefus/indeed/interview/python | |
# Ran 7 tests in 0.003s | |
# FAILED (failures=1) | |
# 100 != 200 | |
# Expected :200 | |
# Actual :100 | |
# <Click to see difference> | |
# Traceback (most recent call last): | |
# File "/home/cbefus/.local/share/JetBrains/Toolbox/apps/PyCharm-P/ch-0/181.4892.64/helpers/pycharm/teamcity/diff_tools.py", line 32, in _patched_equals | |
# old(self, first, second, msg) | |
# File "/usr/lib/python3.5/unittest/case.py", line 820, in assertEqual | |
# assertion_func(first, second, msg=msg) | |
# File "/usr/lib/python3.5/unittest/case.py", line 813, in _baseAssertEqual | |
# raise self.failureException(msg) | |
# AssertionError: 200 != 100 | |
# During handling of the above exception, another exception occurred: | |
# Traceback (most recent call last): | |
# File "/usr/lib/python3.5/unittest/case.py", line 58, in testPartExecutor | |
# yield | |
# File "/usr/lib/python3.5/unittest/case.py", line 600, in run | |
# testMethod() | |
# File "/home/cbefus/indeed/interview/python/reverse_polish.py", line 63, in test_eval_multiple_operators | |
# self.assertEqual(200, calculator.eval("100 20 80 + +")) | |
# Process finished with exit code 1 | |
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
import unittest | |
class ReversePolishCalculator: | |
def eval(self, input): | |
tokens = input.split() | |
if not tokens: | |
return None | |
stack = [] | |
for token in tokens: | |
if token.isdigit(): | |
stack.append(int(token)) | |
else: | |
try: | |
operand1 = stack.pop() | |
operand2 = stack.pop() | |
except IndexError: | |
raise EvaluationException() | |
stack.append(operand1 + operand2) | |
if len(stack) != 1: | |
raise EvaluationException() | |
return stack.pop() | |
class EvaluationException(Exception): | |
pass | |
class ReversePolishCalculatorTest(unittest.TestCase): | |
def test_can_instantiate_a_calculator(self): | |
ReversePolishCalculator() | |
def test_can_eval_a_single_number(self): | |
calculator = ReversePolishCalculator() | |
self.assertEqual(3, calculator.eval("3")) | |
def test_evals_empty_as_none(self): | |
calculator = ReversePolishCalculator() | |
self.assertEqual(None, calculator.eval("")) | |
def test_evals_missing_operator(self): | |
calculator = ReversePolishCalculator() | |
with self.assertRaises(EvaluationException): | |
calculator.eval("2 5") | |
def test_evals_simple_addition(self): | |
calculator = ReversePolishCalculator() | |
self.assertEqual(9, calculator.eval("6 3 +")) | |
def test_eval_operand_missing_raises(self): | |
calculator = ReversePolishCalculator() | |
with self.assertRaises(EvaluationException): | |
calculator.eval("+") | |
def test_eval_multiple_operators(self): | |
calculator = ReversePolishCalculator() | |
self.assertEqual(200, calculator.eval("100 20 80 + +")) | |
# Testing started at 7:20 PM ... | |
# /home/cbefus/indeed/interview/python/venv/bin/python /home/cbefus/.local/share/JetBrains/Toolbox/apps/PyCharm-P/ch-0/181.4892.64/helpers/pycharm/_jb_unittest_runner.py --target reverse_polish.ReversePolishCalculatorTest | |
# Launching unittests with arguments python -m unittest reverse_polish.ReversePolishCalculatorTest in /home/cbefus/indeed/interview/python | |
# Ran 7 tests in 0.003s | |
# OK | |
# Process finished with exit code 0 |
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
import unittest | |
class ReversePolishCalculator: | |
def eval(self, input): | |
tokens = input.split() | |
if not tokens: | |
return None | |
stack = [] | |
for token in tokens: | |
if token.isdigit(): | |
stack.append(int(token)) | |
else: | |
try: | |
operand1 = stack.pop() | |
operand2 = stack.pop() | |
except IndexError: | |
raise EvaluationException() | |
stack.append(operand1 + operand2) | |
if len(stack) != 1: | |
raise EvaluationException() | |
return stack.pop() | |
class EvaluationException(Exception): | |
pass | |
class ReversePolishCalculatorTest(unittest.TestCase): | |
def test_can_instantiate_a_calculator(self): | |
ReversePolishCalculator() | |
def test_can_eval_a_single_number(self): | |
calculator = ReversePolishCalculator() | |
self.assertEqual(3, calculator.eval("3")) | |
def test_evals_empty_as_none(self): | |
calculator = ReversePolishCalculator() | |
self.assertEqual(None, calculator.eval("")) | |
def test_evals_missing_operator(self): | |
calculator = ReversePolishCalculator() | |
with self.assertRaises(EvaluationException): | |
calculator.eval("2 5") | |
def test_evals_simple_addition(self): | |
calculator = ReversePolishCalculator() | |
self.assertEqual(9, calculator.eval("6 3 +")) | |
def test_eval_operand_missing_raises(self): | |
calculator = ReversePolishCalculator() | |
with self.assertRaises(EvaluationException): | |
calculator.eval("+") | |
def test_eval_multiple_operators(self): | |
calculator = ReversePolishCalculator() | |
self.assertEqual(200, calculator.eval("100 20 80 + +")) | |
def test_eval_with_multiply(self): | |
calculator = ReversePolishCalculator() | |
self.assertEqual(200, calculator.eval("100 2 *")) | |
# Testing started at 7:22 PM ... | |
# /home/cbefus/indeed/interview/python/venv/bin/python /home/cbefus/.local/share/JetBrains/Toolbox/apps/PyCharm-P/ch-0/181.4892.64/helpers/pycharm/_jb_unittest_runner.py --target reverse_polish.ReversePolishCalculatorTest | |
# Launching unittests with arguments python -m unittest reverse_polish.ReversePolishCalculatorTest in /home/cbefus/indeed/interview/python | |
# 102 != 200 | |
# Expected :200 | |
# Actual :102 | |
# <Click to see difference> | |
# Traceback (most recent call last): | |
# File "/home/cbefus/.local/share/JetBrains/Toolbox/apps/PyCharm-P/ch-0/181.4892.64/helpers/pycharm/teamcity/diff_tools.py", line 32, in _patched_equals | |
# old(self, first, second, msg) | |
# File "/usr/lib/python3.5/unittest/case.py", line 820, in assertEqual | |
# assertion_func(first, second, msg=msg) | |
# File "/usr/lib/python3.5/unittest/case.py", line 813, in _baseAssertEqual | |
# raise self.failureException(msg) | |
# AssertionError: 200 != 102 | |
# During handling of the above exception, another exception occurred: | |
# Traceback (most recent call last): | |
# File "/usr/lib/python3.5/unittest/case.py", line 58, in testPartExecutor | |
# yield | |
# File "/usr/lib/python3.5/unittest/case.py", line 600, in run | |
# testMethod() | |
# File "/home/cbefus/indeed/interview/python/reverse_polish.py", line 67, in test_eval_with_multiply | |
# self.assertEqual(200, calculator.eval("100 2 *")) | |
# Ran 8 tests in 0.003s | |
# FAILED (failures=1) | |
# Process finished with exit code 1 |
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
import unittest | |
import operator | |
class ReversePolishCalculator: | |
OPERATORS = { | |
"+": operator.add, | |
"*": operator.mul | |
} | |
def eval(self, input): | |
tokens = input.split() | |
if not tokens: | |
return None | |
stack = [] | |
for token in tokens: | |
if token.isdigit(): | |
stack.append(int(token)) | |
else: | |
try: | |
operand1 = stack.pop() | |
operand2 = stack.pop() | |
except IndexError: | |
raise EvaluationException() | |
stack.append(self.OPERATORS[token](operand1, operand2)) | |
if len(stack) != 1: | |
raise EvaluationException() | |
return stack.pop() | |
class EvaluationException(Exception): | |
pass | |
class ReversePolishCalculatorTest(unittest.TestCase): | |
def test_can_instantiate_a_calculator(self): | |
ReversePolishCalculator() | |
def test_can_eval_a_single_number(self): | |
calculator = ReversePolishCalculator() | |
self.assertEqual(3, calculator.eval("3")) | |
def test_evals_empty_as_none(self): | |
calculator = ReversePolishCalculator() | |
self.assertEqual(None, calculator.eval("")) | |
def test_evals_missing_operator(self): | |
calculator = ReversePolishCalculator() | |
with self.assertRaises(EvaluationException): | |
calculator.eval("2 5") | |
def test_evals_simple_addition(self): | |
calculator = ReversePolishCalculator() | |
self.assertEqual(9, calculator.eval("6 3 +")) | |
def test_eval_operand_missing_raises(self): | |
calculator = ReversePolishCalculator() | |
with self.assertRaises(EvaluationException): | |
calculator.eval("+") | |
def test_eval_multiple_operators(self): | |
calculator = ReversePolishCalculator() | |
self.assertEqual(200, calculator.eval("100 20 80 + +")) | |
def test_eval_with_multiply(self): | |
calculator = ReversePolishCalculator() | |
self.assertEqual(200, calculator.eval("100 2 *")) | |
# Testing started at 7:26 PM ... | |
# /home/cbefus/indeed/interview/python/venv/bin/python /home/cbefus/.local/share/JetBrains/Toolbox/apps/PyCharm-P/ch-0/181.4892.64/helpers/pycharm/_jb_unittest_runner.py --target reverse_polish.ReversePolishCalculatorTest | |
# Launching unittests with arguments python -m unittest reverse_polish.ReversePolishCalculatorTest in /home/cbefus/indeed/interview/python | |
# Ran 8 tests in 0.002s | |
# OK | |
# Process finished with exit code 0 |
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
import unittest | |
class ReversePolishCalculator: | |
pass | |
class ReversePolishCalculatorTest(unittest.TestCase): | |
def test_can_instantiate_a_calculator(self): | |
ReversePolishCalculator() | |
# Ran 1 test in 0.001s | |
# OK | |
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
import unittest | |
import operator | |
class ReversePolishCalculator: | |
OPERATORS = { | |
"+": operator.add, | |
"*": operator.mul | |
} | |
def eval(self, input): | |
tokens = input.split() | |
if not tokens: | |
return None | |
stack = [] | |
for token in tokens: | |
if token.isdigit(): | |
stack.append(int(token)) | |
else: | |
try: | |
operand1 = stack.pop() | |
operand2 = stack.pop() | |
except IndexError: | |
raise EvaluationException() | |
stack.append(self.OPERATORS[token](operand1, operand2)) | |
if len(stack) != 1: | |
raise EvaluationException() | |
return stack.pop() | |
class EvaluationException(Exception): | |
pass | |
class ReversePolishCalculatorTest(unittest.TestCase): | |
def test_can_instantiate_a_calculator(self): | |
ReversePolishCalculator() | |
def test_can_eval_a_single_number(self): | |
calculator = ReversePolishCalculator() | |
self.assertEqual(3, calculator.eval("3")) | |
def test_evals_empty_as_none(self): | |
calculator = ReversePolishCalculator() | |
self.assertEqual(None, calculator.eval("")) | |
def test_evals_missing_operator(self): | |
calculator = ReversePolishCalculator() | |
with self.assertRaises(EvaluationException): | |
calculator.eval("2 5") | |
def test_evals_simple_addition(self): | |
calculator = ReversePolishCalculator() | |
self.assertEqual(9, calculator.eval("6 3 +")) | |
def test_eval_operand_missing_raises(self): | |
calculator = ReversePolishCalculator() | |
with self.assertRaises(EvaluationException): | |
calculator.eval("+") | |
def test_eval_multiple_operators(self): | |
calculator = ReversePolishCalculator() | |
self.assertEqual(200, calculator.eval("100 20 80 + +")) | |
def test_eval_with_multiply(self): | |
calculator = ReversePolishCalculator() | |
self.assertEqual(200, calculator.eval("100 2 *")) | |
def test_eval_mix_of_operators(self): | |
calculator = ReversePolishCalculator() | |
self.assertEqual(3970, calculator.eval("1000 990 1 2 + * +")) | |
# Testing started at 7:28 PM ... | |
# /home/cbefus/indeed/interview/python/venv/bin/python /home/cbefus/.local/share/JetBrains/Toolbox/apps/PyCharm-P/ch-0/181.4892.64/helpers/pycharm/_jb_unittest_runner.py --target reverse_polish.ReversePolishCalculatorTest | |
# Launching unittests with arguments python -m unittest reverse_polish.ReversePolishCalculatorTest in /home/cbefus/indeed/interview/python | |
# Ran 9 tests in 0.003s | |
# OK | |
# Process finished with exit code 0 |
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
import unittest | |
class ReversePolishCalculator: | |
pass | |
class ReversePolishCalculatorTest(unittest.TestCase): | |
def test_can_instantiate_a_calculator(self): | |
ReversePolishCalculator() | |
def test_can_eval_a_single_number(self): | |
calculator = ReversePolishCalculator() | |
self.assertEqual(3, calculator.eval("3")) | |
Testing started at 6:33 PM ... | |
/home/cbefus/indeed/interview/python/venv/bin/python /home/cbefus/.local/share/JetBrains/Toolbox/apps/PyCharm-P/ch-0/181.4668.75/helpers/pycharm/_jb_unittest_runner.py --target reverse_polish.ReversePolishCalculatorTest | |
# Ran 2 tests in 0.001s | |
# FAILED (errors=1) | |
# Launching unittests with arguments python -m unittest reverse_polish.ReversePolishCalculatorTest in /home/cbefus/indeed/interview/python | |
# Error | |
# Traceback (most recent call last): | |
# File "/usr/lib/python3.5/unittest/case.py", line 58, in testPartExecutor | |
# yield | |
# File "/usr/lib/python3.5/unittest/case.py", line 600, in run | |
# testMethod() | |
# File "/home/cbefus/indeed/interview/python/reverse_polish.py", line 15, in test_can_eval_a_single_number | |
# self.assertEqual(3, calculator.eval("3")) | |
# AttributeError: 'ReversePolishCalculator' object has no attribute 'eval' | |
# Process finished with exit code 1 |
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
import unittest | |
class ReversePolishCalculator: | |
def eval(self): | |
pass | |
class ReversePolishCalculatorTest(unittest.TestCase): | |
def test_can_instantiate_a_calculator(self): | |
ReversePolishCalculator() | |
def test_can_eval_a_single_number(self): | |
calculator = ReversePolishCalculator() | |
self.assertEqual(3, calculator.eval("3")) | |
# Testing started at 6:34 PM ... | |
# /home/cbefus/indeed/interview/python/venv/bin/python /home/cbefus/.local/share/JetBrains/Toolbox/apps/PyCharm-P/ch-0/181.4668.75/helpers/pycharm/_jb_unittest_runner.py --target reverse_polish.ReversePolishCalculatorTest | |
# Launching unittests with arguments python -m unittest reverse_polish.ReversePolishCalculatorTest in /home/cbefus/indeed/interview/python | |
# Error | |
# Traceback (most recent call last): | |
# File "/usr/lib/python3.5/unittest/case.py", line 58, in testPartExecutor | |
# yield | |
# File "/usr/lib/python3.5/unittest/case.py", line 600, in run | |
# testMethod() | |
# File "/home/cbefus/indeed/interview/python/reverse_polish.py", line 17, in test_can_eval_a_single_number | |
# self.assertEqual(3, calculator.eval("3")) | |
# TypeError: eval() takes 1 positional argument but 2 were given | |
# Ran 2 tests in 0.001s | |
# FAILED (errors=1) | |
# Process finished with exit code 1 |
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
import unittest | |
class ReversePolishCalculator: | |
def eval(self, input): | |
pass | |
class ReversePolishCalculatorTest(unittest.TestCase): | |
def test_can_instantiate_a_calculator(self): | |
ReversePolishCalculator() | |
def test_can_eval_a_single_number(self): | |
calculator = ReversePolishCalculator() | |
self.assertEqual(3, calculator.eval("3")) | |
# Testing started at 6:35 PM ... | |
# /home/cbefus/indeed/interview/python/venv/bin/python /home/cbefus/.local/share/JetBrains/Toolbox/apps/PyCharm-P/ch-0/181.4668.75/helpers/pycharm/_jb_unittest_runner.py --target reverse_polish.ReversePolishCalculatorTest | |
# Launching unittests with arguments python -m unittest reverse_polish.ReversePolishCalculatorTest in /home/cbefus/indeed/interview/python | |
# None != 3 | |
# Expected :3 | |
# Actual :None | |
# <Click to see difference> | |
# Traceback (most recent call last): | |
# File "/home/cbefus/.local/share/JetBrains/Toolbox/apps/PyCharm-P/ch-0/181.4668.75/helpers/pycharm/teamcity/diff_tools.py", line 32, in _patched_equals | |
# old(self, first, second, msg) | |
# File "/usr/lib/python3.5/unittest/case.py", line 820, in assertEqual | |
# assertion_func(first, second, msg=msg) | |
# File "/usr/lib/python3.5/unittest/case.py", line 813, in _baseAssertEqual | |
# raise self.failureException(msg) | |
# AssertionError: 3 != None | |
# During handling of the above exception, another exception occurred: | |
# Traceback (most recent call last): | |
# File "/usr/lib/python3.5/unittest/case.py", line 58, in testPartExecutor | |
# yield | |
# File "/usr/lib/python3.5/unittest/case.py", line 600, in run | |
# testMethod() | |
# File "/home/cbefus/indeed/interview/python/reverse_polish.py", line 17, in test_can_eval_a_single_number | |
# self.assertEqual(3, calculator.eval("3")) | |
# Ran 2 tests in 0.002s | |
# FAILED (failures=1) | |
# Process finished with exit code 1 |
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
import unittest | |
class ReversePolishCalculator: | |
def eval(self, input): | |
return 3 | |
class ReversePolishCalculatorTest(unittest.TestCase): | |
def test_can_instantiate_a_calculator(self): | |
ReversePolishCalculator() | |
def test_can_eval_a_single_number(self): | |
calculator = ReversePolishCalculator() | |
self.assertEqual(3, calculator.eval("3")) | |
# Testing started at 6:36 PM ... | |
# /home/cbefus/indeed/interview/python/venv/bin/python /home/cbefus/.local/share/JetBrains/Toolbox/apps/PyCharm-P/ch-0/181.4668.75/helpers/pycharm/_jb_unittest_runner.py --target reverse_polish.ReversePolishCalculatorTest | |
# Launching unittests with arguments python -m unittest reverse_polish.ReversePolishCalculatorTest in /home/cbefus/indeed/interview/python | |
# Ran 2 tests in 0.001s | |
# OK | |
# Process finished with exit code 0 |
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
import unittest | |
class ReversePolishCalculator: | |
def eval(self, input): | |
return 3 | |
class ReversePolishCalculatorTest(unittest.TestCase): | |
def test_can_instantiate_a_calculator(self): | |
ReversePolishCalculator() | |
def test_can_eval_a_single_number(self): | |
calculator = ReversePolishCalculator() | |
self.assertEqual(3, calculator.eval("3")) | |
def test_evals_empty_as_none(self): | |
calculator = ReversePolishCalculator() | |
self.assertEqual(None, calculator.eval("")) | |
# Testing started at 6:38 PM ... | |
# /home/cbefus/indeed/interview/python/venv/bin/python /home/cbefus/.local/share/JetBrains/Toolbox/apps/PyCharm-P/ch-0/181.4668.75/helpers/pycharm/_jb_unittest_runner.py --target reverse_polish.ReversePolishCalculatorTest | |
# Launching unittests with arguments python -m unittest reverse_polish.ReversePolishCalculatorTest in /home/cbefus/indeed/interview/python | |
# Ran 3 tests in 0.002s | |
# FAILED (failures=1) | |
# 3 != None | |
# Expected :None | |
# Actual :3 | |
# <Click to see difference> | |
# Traceback (most recent call last): | |
# File "/home/cbefus/.local/share/JetBrains/Toolbox/apps/PyCharm-P/ch-0/181.4668.75/helpers/pycharm/teamcity/diff_tools.py", line 32, in _patched_equals | |
# old(self, first, second, msg) | |
# File "/usr/lib/python3.5/unittest/case.py", line 820, in assertEqual | |
# assertion_func(first, second, msg=msg) | |
# File "/usr/lib/python3.5/unittest/case.py", line 813, in _baseAssertEqual | |
# raise self.failureException(msg) | |
# AssertionError: None != 3 | |
# During handling of the above exception, another exception occurred: | |
# Traceback (most recent call last): | |
# File "/usr/lib/python3.5/unittest/case.py", line 58, in testPartExecutor | |
# yield | |
# File "/usr/lib/python3.5/unittest/case.py", line 600, in run | |
# testMethod() | |
# File "/home/cbefus/indeed/interview/python/reverse_polish.py", line 21, in test_evals_empty_as_none | |
# self.assertEqual(None, calculator.eval("")) | |
# Process finished with exit code 1 |
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
import unittest | |
class ReversePolishCalculator: | |
def eval(self, input): | |
return 3 if input else None | |
class ReversePolishCalculatorTest(unittest.TestCase): | |
def test_can_instantiate_a_calculator(self): | |
ReversePolishCalculator() | |
def test_can_eval_a_single_number(self): | |
calculator = ReversePolishCalculator() | |
self.assertEqual(3, calculator.eval("3")) | |
def test_evals_empty_as_none(self): | |
calculator = ReversePolishCalculator() | |
self.assertEqual(None, calculator.eval("")) | |
# Testing started at 6:44 PM ... | |
# /home/cbefus/indeed/interview/python/venv/bin/python /home/cbefus/.local/share/JetBrains/Toolbox/apps/PyCharm-P/ch-0/181.4892.64/helpers/pycharm/_jb_unittest_runner.py --target reverse_polish.ReversePolishCalculatorTest | |
# Ran 3 tests in 0.001s | |
# OK | |
# Launching unittests with arguments python -m unittest reverse_polish.ReversePolishCalculatorTest in /home/cbefus/indeed/interview/python | |
# Process finished with exit code 0 |
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
import unittest | |
class ReversePolishCalculator: | |
def eval(self, input): | |
return 3 if input else None | |
class ReversePolishCalculatorTest(unittest.TestCase): | |
def test_can_instantiate_a_calculator(self): | |
ReversePolishCalculator() | |
def test_can_eval_a_single_number(self): | |
calculator = ReversePolishCalculator() | |
self.assertEqual(3, calculator.eval("3")) | |
def test_evals_empty_as_none(self): | |
calculator = ReversePolishCalculator() | |
self.assertEqual(None, calculator.eval("")) | |
def test_evals_missing_operator(self): | |
calculator = ReversePolishCalculator() | |
with self.assertRaises(EvaluationException): | |
calculator.eval("2 5") | |
# Testing started at 6:47 PM ... | |
# /home/cbefus/indeed/interview/python/venv/bin/python /home/cbefus/.local/share/JetBrains/Toolbox/apps/PyCharm-P/ch-0/181.4892.64/helpers/pycharm/_jb_unittest_runner.py --target reverse_polish.ReversePolishCalculatorTest | |
# Launching unittests with arguments python -m unittest reverse_polish.ReversePolishCalculatorTest in /home/cbefus/indeed/interview/python | |
# Error | |
# Traceback (most recent call last): | |
# File "/usr/lib/python3.5/unittest/case.py", line 58, in testPartExecutor | |
# yield | |
# File "/usr/lib/python3.5/unittest/case.py", line 600, in run | |
# testMethod() | |
# File "/home/cbefus/indeed/interview/python/reverse_polish.py", line 25, in test_evals_missing_operator | |
# with self.assertRaises(EvaluationException): | |
# NameError: name 'EvaluationException' is not defined |
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
import unittest | |
import operator | |
OPERATORS = { | |
"+": operator.add, | |
"*": operator.mul | |
} | |
class ReversePolishCalculator: | |
def eval_tree(self, input): | |
reverse_stack = input.split()[::-1] | |
if len(reverse_stack) == 0: | |
return None | |
root = Node(reverse_stack[0]).grow(reverse_stack[1:]) | |
if root.length() != len(reverse_stack): | |
raise EvaluationException() | |
return root.calculate() | |
class Node: | |
def __init__(self, token): | |
self._token = token | |
self._left_child_node = None | |
self._right_child_node = None | |
def calculate(self): | |
if self.is_leaf(): | |
return int(self._token) | |
return OPERATORS[self._token](self._left_child_node.calculate(), self._right_child_node.calculate()) | |
def grow(self, reverse_stack): | |
if self.is_full(): | |
return self | |
if len(reverse_stack) < 2: | |
raise EvaluationException() | |
self._left_child_node = Node(reverse_stack[0]).grow(reverse_stack[1:-1]) | |
self._right_child_node = Node(reverse_stack[-1]) | |
return self | |
def length(self): | |
if self.is_leaf(): | |
return 1 | |
else: | |
return self._left_child_node.length() + self._right_child_node.length() + 1 | |
def is_leaf(self): | |
return self._token.isdigit() | |
def is_full(self): | |
return self.is_leaf() or (self._left_child_node is not None and self._right_child_node is not None) | |
class EvaluationException(Exception): | |
pass | |
class ReversePolishCalculatorTest(unittest.TestCase): | |
def test_can_instantiate_a_calculator(self): | |
ReversePolishCalculator() | |
def test_can_eval_a_single_number(self): | |
calculator = ReversePolishCalculator() | |
self.assertEqual(3, calculator.eval_tree("3")) | |
def test_evals_empty_as_none(self): | |
calculator = ReversePolishCalculator() | |
self.assertEqual(None, calculator.eval_tree("")) | |
def test_evals_missing_operator(self): | |
calculator = ReversePolishCalculator() | |
with self.assertRaises(EvaluationException): | |
calculator.eval_tree("2 5") | |
def test_evals_simple_addition(self): | |
calculator = ReversePolishCalculator() | |
self.assertEqual(9, calculator.eval_tree("6 3 +")) | |
def test_eval_operand_missing_raises(self): | |
calculator = ReversePolishCalculator() | |
with self.assertRaises(EvaluationException): | |
calculator.eval_tree("+") | |
def test_eval_multiple_operators(self): | |
calculator = ReversePolishCalculator() | |
self.assertEqual(200, calculator.eval_tree("100 20 80 + +")) | |
def test_eval_with_multiply(self): | |
calculator = ReversePolishCalculator() | |
self.assertEqual(200, calculator.eval_tree("100 2 *")) | |
def test_eval_tree(self): | |
calculator = ReversePolishCalculator() | |
self.assertEqual(3970, calculator.eval_tree("1000 990 1 2 + * +")) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment