잊지 않겠습니다.

'Code도장'에 해당되는 글 1건

  1. 2009.01.07 0001. Three plus one

Python

import sys
import unittest

class ThreePlusOne:
    def __init__(self):
        pass
    
    def getArray(self, number):
        return self.__calculateValues(number)

    def __calculateValues(self, number):
        current_value = number
        calculated_values = []
        calculated_values.append(current_value)
        
        while current_value != 1:
            if (current_value % 2) == 0:
                current_value = current_value / 2
            else:
                current_value = current_value * 3 + 1
            calculated_values.append(current_value)

        return calculated_values

    def getLength(self, number):
        return len(self.__calculateValues(number))
    
    def getMaxLengthWithRange(self, start_number, end_number):
        max_length = 0
        for current_number in range(start_number, end_number):
            length = self.getLength(current_number)
            if length > max_length:
                max_length = length
        return max_length
        
    def displayResult(self, start_number, end_number):
        max_length = self.getMaxLengthWithRange(start_number, end_number)
        print '%d %d %d' % (start_number, end_number, max_length)

class ThreePlusOneTest(unittest.TestCase):
    def setUp(self):
        pass

    def testCheckValue(self):
        correct_values = [22, 11, 34, 17, 52, 26, 13, 40, 20, 10, 5, 16, 8, 4, 2, 1]
        t = ThreePlusOne()
        make_values = t.getArray(22)

        self.assertEqual(make_values, correct_values)
        
    def testCheckSize(self):
        correct_value = 16
        t = ThreePlusOne()
        make_value = t.getLength(22)
        self.assertEqual(make_value, correct_value)
    
    def testCheckRange(self):
        t = ThreePlusOne()
        
        result = t.getMaxLengthWithRange(1, 10)
        self.assertEqual(result, 20)
        
        result = t.getMaxLengthWithRange(100, 200)
        self.assertEqual(result, 125)

        result = t.getMaxLengthWithRange(201, 210)
        self.assertEqual(result, 89)

        result = t.getMaxLengthWithRange(900, 1000)
        self.assertEqual(result, 174)
    
    def testDisplay(self):
        t = ThreePlusOne()
        t.displayResult(1, 10)
        t.displayResult(100, 200)
        t.displayResult(201, 210)
        t.displayResult(900, 1000)
    
if __name__ == '__main__':
    unittest.main()

Ruby

require 'test/unit'

class ThreePlusOne
    attr_reader :recent_results, :recent_number
    attr_writer :recent_results, :recent_number
    
    def getArray(number)
        self.calculate(number)
        return self.recent_results
    end
    
    def getLength(number)
        self.calculate(number)
        return  self.recent_results.length
    end
    
    def getMaxLengthWithRange(start_number, end_number)
        max_length = 0
        for i in start_number..end_number
            current_length = self.getLength(i)
            max_length = [max_length, current_length].max
        end
        return max_length
    end
    
    def calculate(number)
        @recent_number = number
        @recent_results = []
        current_value = number
        @recent_results.push(current_value)
        while current_value != 1
            if current_value % 2 == 0 : current_value = current_value / 2
            else current_value = current_value * 3 + 1
            end
            @recent_results.push(current_value)
        end
        return @recent_results
    end        
end

class TestThreePlusOne < Test::Unit::TestCase
    def test_case
        t = ThreePlusOne.new()
        assert_equal(t.getArray(22), [22, 11, 34, 17, 52, 26, 13, 40, 20, 10, 5, 16, 8, 4, 2, 1])
        assert_equal(t.getLength(22), 16)
        assert_equal(t.getMaxLengthWithRange(1, 10), 20)
        assert_equal(t.getMaxLengthWithRange(100, 200), 125)
        assert_equal(t.getMaxLengthWithRange(201, 210), 89)
        assert_equal(t.getMaxLengthWithRange(900, 1000), 174)
    end
end

  

  

신고
Posted by xyzlast Y2K


티스토리 툴바