UglyNumbers

~cpp 
import unittest

class UglyNumbers:
    def uglyNumber(self, index):
        numberList = [pow(2,i)*pow(3,j)*pow(5,k) for k in xrange(30) \
             for j in xrange(30) for i in xrange(30)]
        numberList.sort()
        return numberList[index-1]

    def printUglyNum(self, num):
        print "The 1500'th ugly number is <" + str(num) + ">."

class UglyNumbersTestCase(unittest.TestCase):
    def setUp(self):
        self.u = UglyNumbers()
    
    def testFindUglyNumber(self):        
        self.assertEquals(859963392, self.u.uglyNumber(1500))
        self.assertEquals(2, self.u.uglyNumber(2))
        self.assertEquals(1, self.u.uglyNumber(1))
        self.assertEquals(12, self.u.uglyNumber(10))
        self.assertEquals(15, self.u.uglyNumber(11))


def main():
    u = UglyNumbers()
    n = u.uglyNumber(1500)
    u.printUglyNum(n)
    
if __name__ == '__main__':
    #unittest.main()
    main()

쓰레드

  • 완전 야매... 너무 비효율적이다. -- 재선


Retrieved from http://wiki.zeropage.org/wiki.php/UglyNumbers/황재선
last modified 2021-02-07 05:28:20