Skip to content

Instantly share code, notes, and snippets.

@YoniTsafir
Created April 14, 2011 10:33
Show Gist options
  • Save YoniTsafir/919241 to your computer and use it in GitHub Desktop.
Save YoniTsafir/919241 to your computer and use it in GitHub Desktop.
LCD Kata
'''
Created on Apr 14, 2011
'''
class LCDCalculator(object):
def calc(self, num1, num2):
return reduce(lambda x, y: x * y, self.smart_union(self.factorize(num1), self.factorize(num2)))
def factorize(self, num):
res = []
for i in xrange(2, num + 1):
while num % i == 0:
res.append(i)
num /= i
return res
def smart_union(self, list1, list2):
result = []
count_dict1 = self.get_count_dict(list1)
count_dict2 = self.get_count_dict(list2)
for item in count_dict1.items():
times = max(item[1], count_dict2.get(item[0], 0))
result += ([item[0]] * times)
for item in count_dict2.items():
if not count_dict1.has_key(item[0]):
result += ([item[0]] * item[1])
result.sort()
return result
def get_count_dict(self, list):
return dict([(x, list.count(x)) for x in list])
'''
Created on Apr 14, 2011
'''
import unittest
from lcd_calc import LCDCalculator
class LCDTest(unittest.TestCase):
def setUp(self):
self.lcd_calc = LCDCalculator()
def test_two_and_two_returns_two(self):
self.assertEquals(2, self.lcd_calc.calc(2, 2))
def test_three_and_three_returns_three(self):
self.assertEquals(3, self.lcd_calc.calc(3, 3))
def test_two_and_three_returns_six(self):
self.assertEquals(6, self.lcd_calc.calc(2, 3))
def test_two_and_four_returns_four(self):
self.assertEquals(4, self.lcd_calc.calc(2, 4))
def test_four_and_two_returns_four(self):
self.assertEquals(4, self.lcd_calc.calc(4, 2))
def test_four_and_six_returns_twelve(self):
self.assertEquals(12, self.lcd_calc.calc(4, 6))
def test_prime_factorization_two(self):
self.assertEquals([2], self.lcd_calc.factorize(2))
def test_prime_factorization_three(self):
self.assertEquals([3], self.lcd_calc.factorize(3))
def test_prime_factorization_four(self):
self.assertEquals([2, 2], self.lcd_calc.factorize(4))
def test_prime_factorization_nine(self):
self.assertEquals([3, 3], self.lcd_calc.factorize(9))
def test_prime_factorization_eight(self):
self.assertEquals([2, 2, 2], self.lcd_calc.factorize(8))
def test_prime_factorization_eleven(self):
self.assertEquals([11], self.lcd_calc.factorize(11))
def test_smart_union_singleton_two_twice(self):
self.assertEquals([2], self.lcd_calc.smart_union([2], [2]))
def test_smart_union_singleton_two_singleton_three(self):
self.assertEquals([2, 3], self.lcd_calc.smart_union([2], [3]))
def test_smart_union_two_two_twice(self):
self.assertEquals([2, 2], self.lcd_calc.smart_union([2, 2], [2, 2]))
def test_smart_union_two_two_three_and_two_two_five(self):
self.assertEquals([2, 2, 3, 5], self.lcd_calc.smart_union([2, 2, 3], [2, 2, 5]))
def test_smart_union_two_two_five_and_two_two_three(self):
self.assertEquals([2, 2, 3, 5], self.lcd_calc.smart_union([2, 2, 5], [2, 2, 3]))
def test_get_count_dict_two(self):
self.assertEquals({2: 1}, self.lcd_calc.get_count_dict([2]))
def test_get_count_dict_two_three(self):
self.assertEquals({2: 1, 3: 1}, self.lcd_calc.get_count_dict([2, 3]))
def test_get_count_dict_two_two(self):
self.assertEquals({2: 2}, self.lcd_calc.get_count_dict([2, 2]))
def test_get_count_dict_compex(self):
self.assertEquals({2: 3, 5: 2, 7: 1, 11: 1, 17: 4},
self.lcd_calc.get_count_dict([2, 2, 2, 5, 5, 7, 11, 17, 17, 17, 17]))
def test_smart_union_two_two_five_and_two_two_two_three(self):
self.assertEquals([2, 2, 2, 3, 5], self.lcd_calc.smart_union([2, 2, 5], [2, 2, 2, 3]))
def test_four_million_and_six_million_returns_twelve_million(self):
self.assertEquals(12 * 10**6, self.lcd_calc.calc(4 * 10**6, 6 * 10**6))
if __name__ == "__main__":
unittest.main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment