import unittest
import os
import math
segment_enables = [
[True, True, True, False, True, True, True],
[False, False, True, False, False, True, False],
[True, False, True, True, True, False, True],
[True, False, True, True, False, True, True],
[False, True, True, True, False, True, False],
[True, True, False, True, False, True, True],
[False, True, False, True, True, True, True],
[True, True, True, False, False, True, True],
[True, True, True, True, True, True, True],
[True, True, True, True, False, True, True]
]
class SevenSegmentNumber:
def __init__(self, number, size):
self.size = size
self.number = number
self.max_line = size * 2 + 3
def printHorizontalLine(self, print_enable):
lines = ' '
for i in range(self.size):
if print_enable:
lines = lines + '-'
else:
lines = lines + ' '
lines = lines + ' '
return lines
def printVerticalLine(self, print_first, print_last):
lines = ''
vert_size = self.size + 2
if print_first:
lines = lines + '|'
else:
lines = lines + ' '
for i in range(vert_size - 2):
lines = lines + ' '
if print_last:
lines = lines + '|'
else:
lines = lines + ' '
return lines
def getLine(self, line):
total_line = self.size * 2 + 3
line_segment = (total_line - 3) / 2 + 1
line_end = total_line - 1
out_line = ''
if line == 0:
out_line = self.printHorizontalLine(segment_enables[self.number][0])
elif 0 < line and line < line_segment:
out_line = self.printVerticalLine(segment_enables[self.number][1], segment_enables[self.number][2])
elif line == line_segment:
out_line = self.printHorizontalLine(segment_enables[self.number][3])
elif line_segment < line and line < line_end:
out_line = self.printVerticalLine(segment_enables[self.number][4], segment_enables[self.number][5])
elif line == line_end:
out_line = self.printHorizontalLine(segment_enables[self.number][6])
return out_line
class Numbers:
def __init__(self):
self.numbers = []
self.seven_segments = []
def displayNumber(self, number, size):
decimal = int(math.log(number) / math.log(10)) + 1
self.numbers = []
self.seven_segments = []
for i in range(decimal):
self.numbers.append(int(str(number)[i]))
segment = SevenSegmentNumber(self.numbers[i], size)
self.seven_segments.append(segment)
max_line = self.seven_segments[0].max_line
for i in range(max_line):
line = ''
for s in range(decimal):
line = line + self.seven_segments[s].getLine(i) + ' '
print line
class SevenSegmentNumberTest(unittest.TestCase):
"""
def testDisplay(self):
print 'Start'
zero = SevenSegmentNumber(8, 3)
for i in range(zero.max_line):
print zero.getLine(i),
print ""
"""
def testNumber1(self):
number = SevenSegmentNumber(8, 2)
#print number.getLine(0)
#self.assertEqual(number.max_line, 3 + 2 * 2)
#self.assertEqual(' ', number.getLine(0))
#self.assertEqual(' |', number.getLine(1))
#for i in range(0, number.max_line):
# print number.getLine(i)
def testOutputNumber(self):
for i in range(10):
number = SevenSegmentNumber(i, 2)
for j in range(number.max_line):
print number.getLine(j)
if __name__ == '__main__':
#unittest.main()
n = Numbers()
n.displayNumber(1238, 3)
require 'test/unit'
class SevenSegment
attr_reader :number, :size, :height, :width
attr_writer :number, :size
@@segment_enables = [
[true, true, true, false, true, true, true],
[false, false, true, false, false, true, false],
[true, false, true, true, true, false, true],
[true, false, true, true, false, true, true],
[false, true, true, true, false, true, false],
[true, true, false, true, false, true, true],
[false, true, false, true, true, true, true],
[true, true, true, false, false, true, false],
[true, true, true, true, true, true, true],
[true, true, true, true, false, true, true] ]
def initialize(number, size)
@number = number
@size = size
@height = size * 2 + 3
@width = size + 2
end
def get_vertical_line(mark_first, mark_second)
line = ''
length = @size + 2
line = line + '|' if mark_first
line = line + ' ' if !mark_first
@size.times { line = line + ' ' }
line = line + '|' if mark_second
line = line + ' ' if !mark_second
line
end
def get_horizontal_line(enable)
mark = ''
mark = '-' if enable
mark = ' ' if !enable
length = self.size + 2
line = ' '
@size.times { line = line + mark }
line = line + ' '
line
end
def get_line(line_count)
if line_count == 0 then self.get_horizontal_line(@@segment_enables[@number][0])
elsif line_count > 0 and line_count < (@size+1) then self.get_vertical_line(@@segment_enables[@number][1], @@segment_enables[@number][2])
elsif line_count == (@size+1) then self.get_horizontal_line(@@segment_enables[@number][3])
elsif line_count > (@size+1) and line_count < (@size * 2 + 2) then self.get_vertical_line(@@segment_enables[@number][4], @@segment_enables[@number][5])
elsif line_count == (@size * 2 + 2) then self.get_horizontal_line(@@segment_enables[@number][6])
end
end
end
class SevenSegments
attr_reader :numbers, :size
attr_writer :numbers, :size
def initialize(size, number)
@size = size
self.set_number(number)
end
def set_number(number)
@numbers = []
string_numbers = number.to_s()
string_numbers.length.times { |index| @numbers << string_numbers[index] - 48 }
end
def display_number
puts ' '
seven_segments = []
@numbers.each { |n| seven_segments << SevenSegment.new(n, @size) }
line = @size * 2 + 3
line.times do |line_count|
line = ''
seven_segments.each do |seven_segment|
line = line + seven_segment.get_line(line_count) + ' '
end
puts line
end
end
end
class SevenSegmentsTest < Test::Unit::TestCase
def test_numbers
s = SevenSegments.new(3, 987654321)
print s.numbers[0]
s.display_number
assert_equal(9, s.numbers[0])
end
end
class SevenSegmentTest < Test::Unit::TestCase
def test_number0
s = SevenSegment.new(0,2)
assert_equal(" -- ", s.get_line(0))
assert_equal("| |", s.get_line(1))
assert_equal("| |", s.get_line(2))
assert_equal(" ", s.get_line(3))
assert_equal("| |", s.get_line(4))
assert_equal("| |", s.get_line(5))
assert_equal(" -- ", s.get_line(6))
end
def test_number1
s = SevenSegment.new(1,2)
assert_equal(" ", s.get_line(0))
assert_equal(" |", s.get_line(1))
assert_equal(" |", s.get_line(2))
assert_equal(" ", s.get_line(3))
assert_equal(" |", s.get_line(4))
assert_equal(" |", s.get_line(5))
assert_equal(" ", s.get_line(6))
end
def test_number8
s = SevenSegment.new(8,2)
assert_equal(" -- ", s.get_line(0))
assert_equal("| |", s.get_line(1))
assert_equal("| |", s.get_line(2))
assert_equal(" -- ", s.get_line(3))
assert_equal("| |", s.get_line(4))
assert_equal("| |", s.get_line(5))
assert_equal(" -- ", s.get_line(6))
end
end