Guest User

Untitled

a guest
Jan 18th, 2018
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.90 KB | None | 0 0
  1. for i, item in enumerate(numbers):
  2. for j in range(i+1, len(numbers)):
  3. total_of_two_items = numbers[i] + numbers[j]
  4. if(total_of_two_items == total_number):
  5. print '{first_item} {second_item}'.format(first_item=i+1, second_item=j+1)
  6. print 'n'
  7.  
  8. n = 181
  9. n2 = n//2
  10. numbers = [80, 98, 83, 92, 1, 38, 37, 54, 58, 89]
  11. goodnums = {n-x for x in numbers if x<=n2} & {x for x in numbers if x>n2}
  12. pairs = {(n-x, x) for x in goodnums}
  13.  
  14. if not n%2 and (n2, n2) in pairs and numbers.count(n2) == 1:
  15. pairs.remove((n2, n2))
  16.  
  17. def mergeSort(A):
  18.  
  19. if len(A) > 1:
  20. mid = len(A)//2
  21. lefthalf = A[:mid]
  22. righthalf = A[mid:]
  23.  
  24. mergeSort(lefthalf)
  25. mergeSort(righthalf)
  26.  
  27. # Merge the halves
  28. i,j,k=0,0,0
  29.  
  30. while i<len(lefthalf) and j<len(righthalf):
  31. if lefthalf[i] < righthalf[j]:
  32. A[k] = lefthalf[i]
  33. i = i + 1
  34. else:
  35. A[k] = righthalf[j]
  36. j = j + 1
  37. k = k + 1
  38.  
  39. while i < len(lefthalf):
  40. A[k] = lefthalf[i]
  41. k = k +1
  42. i = i + 1
  43.  
  44. while j < len(righthalf):
  45. A[k] = righthalf[j]
  46. k = k + 1
  47. j = j + 1
  48.  
  49.  
  50. def find_pairs(alist, item):
  51. # We take two flags left and right to point to the ends of the sorted list
  52. left = 0
  53. right = len(alist) - 1
  54. pairs = []
  55. while(left<right):
  56. # We find the sum of the numbers in at these two points.
  57. # If the sum is equal to our number for which we are finding pairs, we consider this pair and add it to our results
  58. # If the sum is greater than expected then we move the right pointer one step back to a smaller number and then compute sum again
  59. # If the sum is smaller than expected then we move the left pointer a step ahead and check the sum with a greater number
  60. sum = alist[left] + alist[right]
  61. if sum == item:
  62. pairs += [(alist[left],alist[right])]
  63. # Move the pointers to next elements in the list and find more pairs
  64. right -= 1
  65. left += 1
  66. elif sum > item:
  67. right -= 1
  68. else:
  69. left += 1
  70. return pairs
  71.  
  72.  
  73. l1 = [80, 98, 83, 92, 1, 38, 37, 54, 58, 89]
  74. mergeSort(l1)
  75. print l1
  76. print find_pairs(l1,181)
  77.  
  78. l2 = [-5,-2, -23, 34,21,90,1,0,65,8,-10]
  79. mergeSort(l2)
  80. print l2
  81. print find_pairs(l2,-2)
  82.  
  83. [1, 37, 38, 54, 58, 80, 83, 89, 92, 98]
  84. [(83, 98), (89, 92)]
  85.  
  86.  
  87. [-23, -10, -5, -2, 0, 1, 8, 21, 34, 65, 90]
  88. [(-23, 21), (-10, 8), (-2, 0)]
  89.  
  90. def sum_of_pairs_matches(K, arr):
  91. uniques = {i: True for i in arr}
  92. pairs = set()
  93. for val in arr:
  94. k = -val + K if val<K else -K - val
  95. if(uniques.get(k, False)):
  96. pairs.add(tuple(sorted([k,val])))
  97. return pairs
  98.  
  99. sum_of_pairs_matches(5, [-5, -4, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
  100.  
  101. {(-5, 10), (-4, 9), (-1, 6), (0, 5), (1, 4), (2, 3)}
Add Comment
Please, Sign In to add comment