Created
March 12, 2012 23:32
-
-
Save yngwie74/2025419 to your computer and use it in GitHub Desktop.
LCD numbers kata and tests (python)
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/python | |
# -*- coding: utf-8 -*- | |
_NONE = ' ' | |
_LEFT = '| ' | |
_MIDL = ' - ' | |
_RGHT = ' |' | |
_BOTH = '| |' | |
_SEGMENTS_BY_DIGIT = { | |
0: [_MIDL, _BOTH, _NONE, _BOTH, _MIDL], | |
1: [_NONE, _RGHT, _NONE, _RGHT, _NONE], | |
2: [_MIDL, _RGHT, _MIDL, _LEFT, _MIDL], | |
3: [_MIDL, _RGHT, _MIDL, _RGHT, _MIDL], | |
4: [_NONE, _BOTH, _MIDL, _RGHT, _NONE], | |
5: [_MIDL, _LEFT, _MIDL, _RGHT, _MIDL], | |
6: [_MIDL, _RGHT, _MIDL, _BOTH, _MIDL], | |
7: [_MIDL, _RGHT, _NONE, _RGHT, _NONE], | |
8: [_MIDL, _BOTH, _MIDL, _BOTH, _MIDL], | |
9: [_MIDL, _BOTH, _MIDL, _RGHT, _MIDL], | |
} | |
def _digits_of(number): | |
return map(int, str(number)) | |
def _segments_for_each(digits): | |
return [_SEGMENTS_BY_DIGIT[digit] for digit in digits] | |
def _transverse_of(matrix): | |
return zip(*matrix) | |
def _as_text_lines(leds): | |
return '\n'.join(leds) | |
def _arrange_horizontally(segments): | |
return _as_text_lines(''.join(segs) for segs in _transverse_of(segments)) | |
def lcdOf(number): | |
return \ | |
_arrange_horizontally( | |
_segments_for_each( | |
_digits_of(number))) |
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/python | |
# -*- coding: utf-8 -*- | |
import unittest | |
from dojo_lcd import lcdOf | |
_ALL_TESTS = { | |
0: ' - \n' \ | |
'| |\n' \ | |
' \n' \ | |
'| |\n' \ | |
' - ', | |
1: ' \n' \ | |
' |\n' \ | |
' \n' \ | |
' |\n' \ | |
' ', | |
2: ' - \n' \ | |
' |\n' \ | |
' - \n' \ | |
'| \n' \ | |
' - ', | |
3: ' - \n' \ | |
' |\n' \ | |
' - \n' \ | |
' |\n' \ | |
' - ', | |
4: ' \n' \ | |
'| |\n' \ | |
' - \n' \ | |
' |\n' \ | |
' ', | |
5: ' - \n' \ | |
'| \n' \ | |
' - \n' \ | |
' |\n' \ | |
' - ', | |
6: ' - \n' \ | |
' |\n' \ | |
' - \n' \ | |
'| |\n' \ | |
' - ', | |
7: ' - \n' \ | |
' |\n' \ | |
' \n' \ | |
' |\n' \ | |
' ', | |
8: ' - \n' \ | |
'| |\n' \ | |
' - \n' \ | |
'| |\n' \ | |
' - ', | |
9: ' - \n' \ | |
'| |\n' \ | |
' - \n' \ | |
' |\n' \ | |
' - ', | |
10: ' - \n' \ | |
' || |\n' \ | |
' \n' \ | |
' || |\n' \ | |
' - ', | |
94871: | |
' - - - \n' \ | |
'| || || | | |\n' \ | |
' - - - \n' \ | |
' | || | | |\n' \ | |
' - - ', | |
} | |
class TestLcd(unittest.TestCase): | |
def _pretty_print(self, lines): | |
return '\n'.join(repr(line) for line in str(lines).split('\n')) | |
def _message_for(self, number, expected, actual): | |
return '''for number %r, the expected LCD rep was: | |
%s, but instead got: | |
%s''' % (number, self._pretty_print(expected), self._pretty_print(actual)) | |
def assertLcdOf(self, number, _is): | |
actual = lcdOf(number) | |
message = self._message_for(number, _is, actual) | |
self.assertEquals(_is, actual, message) | |
def _add_test(cls, number, expected): | |
name = 'test_lcd_of_%d' % number | |
def _new_test(self): | |
self.assertLcdOf(number, _is=expected) | |
_new_test.__doc__ = 'Test for lcdOf(%d)' % number | |
_new_test.__name__ = name | |
setattr(cls, name, _new_test) | |
for (number, expected) in _ALL_TESTS.iteritems(): | |
_add_test(TestLcd, number, expected) | |
if __name__ == '__main__': | |
unittest.main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment