AlgorithmQuiz/3Plus1Python으로 (이거 잘하면 one-liner로도 가능할듯~) --yong27, 2004-09-10

   1 import unittest
   2 
   3 def getCycleLength(n):
   4     nCycle = 1
   5     while n!=1:
   6         n=(n%2 and n*3+1) or n/2
   7         nCycle+=1
   8     return nCycle
   9 
  10 def getMaxCycleLength(i,j):
  11     result = []
  12     for n in range(i,j+1):
  13         result.append(getCycleLength(n))
  14     return max(result)
  15 
  16 def main():
  17     i,j = map(int,raw_input().split())
  18     print i, j, getMaxCycleLength(i,j)
  19 
  20 class SomeTest(unittest.TestCase):
  21     def testGetCycleLength(self):
  22         self.assertEquals(2, getCycleLength(2))
  23         self.assertEquals(8, getCycleLength(3))
  24         self.assertEquals(3, getCycleLength(4))
  25         self.assertEquals(16, getCycleLength(22))
  26     def testGetMaxCycleLength(self):
  27         self.assertEquals(8, getMaxCycleLength(2,4))
  28 
  29 if __name__=='__main__':
  30     unittest.main(argv=('','-v'))
  31     #main()

AlgorithmQuiz/3Plus1/yong27 (last edited 2013-08-19 14:53:11 by 61)

web biohackers.net