Guest User

Untitled

a guest
Nov 17th, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.66 KB | None | 0 0
  1. import sys
  2. import unittest
  3.  
  4. # 100000000 means 1 one and 0 of anything else, up to 9.
  5.  
  6. def is_self_counting(number, max):
  7. lookup = [1,2,3,4,5,6,7,8,9,0]
  8. print "Checking", number
  9. for i in range(0,max):
  10. count = number.count(lookup[i])
  11. if count <> number[i]:
  12. return False
  13. return True
  14.  
  15. def find_self_counting(max):
  16. print "from", 10**(max-1), "to", 10**max - 1, "using max", max
  17.  
  18. #for i in xrange(10**(max-1),10**max):
  19. for i in xrange(0,1000000000):
  20. l = to_string(i, max)
  21. #print i, l
  22. if sum(l) <= max:
  23. if is_self_counting(l, max):
  24. print "self counting", i
  25.  
  26.  
  27. def to_string(i, max):
  28. pos = 0
  29. l = [0 for t in range (0,max)]
  30. for c in str(i):
  31. l[pos] = int(c)
  32. pos = pos + 1
  33. return l
  34.  
  35.  
  36. class SelfCountingTests(unittest.TestCase):
  37. def test_that_known_ten_digit_number_is_self_counting_for_max_10(self):
  38. i = 2100010006
  39. max = 10
  40. l = to_string(i, max)
  41. self.assertTrue(is_self_counting(l, max))
  42.  
  43. def test_that_known_one_digit_number_is_self_counting_for_max_1(self):
  44. i = 1
  45. max = 1
  46. l = to_string(i, max)
  47. self.assertTrue(is_self_counting(l, max))
  48.  
  49. def test_that_2_is_not_self_counting_for_max_1(self):
  50. i = 2
  51. max = 1
  52. l = to_string(i, max)
  53. self.assertFalse(is_self_counting(l, max))
  54.  
  55. def test_that_known_nine_digit_number_is_self_counting_for_max_9(self):
  56. i = 100000000
  57. max = 9
  58. l = to_string(i, max)
  59. self.assertTrue(is_self_counting(l, max))
  60.  
  61. def test_that_123456789_is_not_self_counting_for_max_9(self):
  62. i = 123456789
  63. max = 9
  64. l = to_string(i, max)
  65. self.assertFalse(is_self_counting(l, max))
  66.  
  67.  
  68. if __name__ == "__main__":
  69. if len(sys.argv) > 1 and sys.argv[1] == "--test":
  70. unittest.main(argv = sys.argv[1:])
  71. else:
  72. find_self_counting(9)
Add Comment
Please, Sign In to add comment