Guest User

Untitled

a guest
Sep 23rd, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.50 KB | None | 0 0
  1. #!/usr/bin/env python
  2.  
  3. from collections import Counter
  4.  
  5. def decompose_sum(s):
  6. return [(a,s-a) for a in range(1,int(s/2+1))]
  7.  
  8. def isprime(n):
  9. '''check if integer n is a prime'''
  10.  
  11. # make sure n is a positive integer
  12. n = abs(int(n))
  13.  
  14. # 0 and 1 are not primes
  15. if n < 2:
  16. return False
  17.  
  18. # 2 is the only even prime number
  19. if n == 2:
  20. return True
  21.  
  22. # all other even numbers are not primes
  23. if not n & 1:
  24. return False
  25.  
  26. # range starts with 3 and only needs to go up
  27. # the square root of n for all odd numbers
  28. for x in range(3, int(n**0.5) + 1, 2):
  29. if n % x == 0:
  30. return False
  31.  
  32. return True
  33.  
  34. ################################################################################
  35. ################################################################################
  36. ## Template file for problem 1. You have to fill in the function findNumbers ##
  37. ## defined below. The function takes in an input number and return the list ##
  38. ## of numbers that satisfy the problem statement. Please ensure you return a ##
  39. ## list as the submission will be auto evaluated. We have provided a little ##
  40. ## helper to ensure that the return value is correct. ##
  41. ## ##
  42. ## You can run this template file to see the output of your function. ##
  43. ## First replace the TEST_NUMBER with correct number. ##
  44. ## Then simply run: `python problem1_template.py` ##
  45. ## You should see the output printed once your program runs. ##
  46. ## ##
  47. ## DO NOT CHANGE THE NAME OF THE FUNCTION BELOW. ONLY FILL IN THE LOGIC. ##
  48. ## DONT FORGET TO RETURN THE VALUES AS A LIST ##
  49. ## IF YOU MAKE ANY IMPORTS PUT THEM IN THE BODY OF THE FUNCTION ##
  50. ## ##
  51. ## You are free to write additional helper functions but ensure they are all ##
  52. ## in this file else your submission wont work. ##
  53. ## ##
  54. ## Good Luck! ##
  55. ################################################################################
  56. ################################################################################
  57.  
  58. TEST_NUMBER = 100
  59.  
  60. def findNumbers(inputNumber):
  61. ##################################
  62. ## FILL ME IN ##
  63. ##################################
  64. # Generate all possible pairs
  65. all_pairs = set((a,b) for a in range(1,inputNumber+1) for b in range(a,inputNumber+1) if a+b <= 2*inputNumber)
  66. # Fact 1 --> Select pairs for which all sum decompositions have non-unique product
  67. product_counts = Counter(c*d for c,d in all_pairs)
  68. unique_products = set((a,b) for a,b in all_pairs if product_counts[a*b]==1)
  69. s_pairs = [(a,b) for a,b in all_pairs if
  70. all((x,y) not in unique_products for (x,y) in decompose_sum(a+b)) ]
  71.  
  72. # Fact 2 --> Select pairs for which the product is unique
  73. product_counts = Counter(c*d for c,d in s_pairs)
  74. p_pairs = [(a,b) for a,b in s_pairs if product_counts[a*b]==1]
  75.  
  76. # Fact 3 --> Select pairs for which the sum is unique
  77. sum_counts = Counter(c+d for c,d in p_pairs)
  78. final_pairs = [(a,b) for a,b in p_pairs if sum_counts[a+b]==1 ]
  79.  
  80. if len(final_pairs) == 1:
  81. a,b = final_pairs[0]
  82. return [a,b]
  83. if(len(final_pairs)==0):
  84. return []
  85.  
  86. # for pair in final_pairs:
  87. # print(pair)
  88.  
  89. dict_map = {}
  90. for pair in final_pairs:
  91. a,b=pair
  92. if b-a in dict_map:
  93. dict_map[b-a].append([a,b])
  94. else:
  95. dict_map[b-a] = [[a,b]]
  96.  
  97.  
  98. dup_list = []
  99.  
  100. for key in dict_map:
  101. if len(dict_map[key]) > 1:
  102. dup_list.append(dict_map[key])
  103.  
  104. for item in dup_list:
  105. print(item,end='\n')
  106.  
  107. pos = -1
  108. ele = 0
  109. flag = False
  110. for i,lists in enumerate(dup_list):
  111. dicter = {}
  112.  
  113. for j,item in enumerate(lists):
  114. a,b = item
  115. if (a in dicter and dicter[a] == 1) or (b in dicter and dicter[b] == 1):
  116. pos = i
  117. if a in dicter:
  118. ele = a
  119. else:
  120. ele = b
  121. flag = True
  122. break
  123. dicter[a]=dicter.get(a,0)+1
  124. dicter[b]=dicter.get(b,0)+1
  125.  
  126. ans = []
  127.  
  128. print(ele)
  129.  
  130. print(pos)
  131. if pos==-1:
  132. return []
  133. elif pos !=-1 :
  134. for i,item in enumerate(dup_list[pos]):
  135. a,b = item
  136. if a is ele or b is ele:
  137. continue
  138. else:
  139. ans = [a,b]
  140.  
  141.  
  142. for item in dup_list:
  143. print(item)
  144.  
  145. return ans
  146.  
  147. def ensureNumbers(returnList):
  148. for num in returnList:
  149. if type(num) is int:
  150. continue
  151. else:
  152. print(num, ' is not an integer.')
  153. raise TypeError('The return value is not a list of integers')
  154. return returnList
  155.  
  156.  
  157. def ensureListOfNumbers(returnList):
  158. if type(returnList) is list:
  159. return ensureNumbers(returnList)
  160. else:
  161. print('Return value is not a list. Please ensure you return a list.')
  162. raise TypeError('The return value is not a list')
  163.  
  164.  
  165.  
  166. if __name__ == "__main__":
  167. print(ensureListOfNumbers(findNumbers(TEST_NUMBER)))
Add Comment
Please, Sign In to add comment