AlgorithmQuiz/LcdDisplay

destineC++ 코드보다는 정말이지 짧고, 간결한 코드가 아닐 수 없다. 역시 Python... 난 이 문제를 풀면서, FastFeedback의 중요성을 다시 느꼈다. 머리속에 정확히 정리가 안되도, 그때그때 눈으로 중간결과를 확인하면서 하니까, 더 쉽게 문제해결에 도달하더라. --yong27, 2004-12-29

   1 import unittest
   2 
   3 numDict={ # 0   1   2   3   4   5   6
   4     '0' : ('-','|','|',' ','|','|','-'),
   5     '1' : (' ',' ','|',' ',' ','|',' '),
   6     '2' : ('-',' ','|','-','|',' ','-'),
   7     '3' : ('-',' ','|','-',' ','|','-'),
   8     '4' : (' ','|','|','-',' ','|',' '),
   9     '5' : ('-','|',' ','-',' ','|','-'),
  10     '6' : ('-','|',' ','-','|','|','-'),
  11     '7' : ('-',' ','|',' ',' ','|',' '),
  12     '8' : ('-','|','|','-','|','|','-'),
  13     '9' : ('-','|','|','-',' ','|','-'),
  14     }
  15 
  16 def drawHorizon(num, pos, size):
  17     r=list()
  18     for each in num:
  19         tick = numDict[each]
  20         r.append(' '+tick[pos]*size+' ')
  21     return ' '.join(r)
  22 
  23 def drawVertical(num, pos, size):
  24     r=list()
  25     for i in range(size):
  26         subr = list()
  27         for each in num:
  28             tick = numDict[each]
  29             subr.append(tick[pos]+' '*size+tick[pos+1])
  30         r.append(' '.join(subr))
  31     return '\n'.join(r)
  32 
  33 def printToLcd(num, s=1):
  34     r = list()
  35     r.append(drawHorizon(num, 0, s))
  36     r.append(drawVertical(num, 1, s))
  37     r.append(drawHorizon(num, 3, s))
  38     r.append(drawVertical(num, 4, s))
  39     r.append(drawHorizon(num, 6, s))
  40     return '\n'.join(r)
  41 
  42 def main():
  43     inputs = list()
  44     while True:
  45         rawInputs = raw_input()
  46         if rawInputs == str(0):
  47             break
  48         inputs.append(rawInputs.split())
  49     for s, num in inputs:
  50         print printToLcd(num,int(s))
  51 
  52 class LcdTest(unittest.TestCase):
  53     def testOne(self):
  54         expected = '''\
  55  - 
  56 | |
  57  - 
  58 | |
  59  - '''
  60         self.assertEquals(expected, printToLcd('8'))
  61 
  62     def testTwo(self):
  63         expected = '''\
  64  -   - 
  65 | | | |
  66  -   - 
  67 | | | |
  68  -   - '''
  69         self.assertEquals(expected, printToLcd('88'))
  70 
  71     def testSize(self):
  72         expected = '''\
  73  --   -- 
  74 |  | |  |
  75 |  | |  |
  76  --   -- 
  77 |  | |  |
  78 |  | |  |
  79  --   -- '''
  80         self.assertEquals(expected, printToLcd('88',2))
  81 
  82 
  83 if __name__=='__main__':
  84     unittest.main(argv=('','-v'))
  85     #main()

AlgorithmQuiz/LcdDisplay/yong27 (last edited 2012-08-14 14:22:39 by 182)

web biohackers.net