daily pastebin goal
48%
SHARE
TWEET

Untitled

a guest Oct 11th, 2018 69 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import itertools
  2. from collections import defaultdict
  3. from bisect import bisect_left
  4.  
  5. z_real = float(input("Enter real part of number: "))
  6. z_imag = float(input("Enter imaginary part of number: "))
  7. goal = complex(z_real, z_imag)
  8. print("Number: {}".format(goal))
  9. print("Now enter the set of complex numbers to choose from")
  10. choices = []
  11. while True:
  12.     c_real = 0
  13.  
  14.     try:
  15.         c_real = float(input("Enter the real part (or any non-number to continue): "))
  16.     except BaseException:
  17.         break
  18.  
  19.     c_imag = float(input("Enter imaginary part: "))
  20.  
  21.     choices.append(complex(c_real, c_imag))
  22.  
  23.  
  24. def findSums(z, choice):
  25.     choice.sort(key=lambda z_: z_.imag)
  26.     choice.sort(key=lambda z_: z_.real)
  27.     reals = map(lambda z_: z_.real, choice)
  28.     imags = map(lambda z_: z_.imag, choice)
  29.  
  30.     sums = defaultdict(list)
  31.  
  32.     for length in range(len(choices) + 1):
  33.         for subset in itertools.combinations(choices, length):
  34.             search = z - sum(subset)
  35.             pos = bisect_left(reals, search.real)
  36.             if pos < len(reals) and reals[pos] == search.real:
  37.                 loc = bisect_left(imags, search.imag)
  38.                 if loc < len(imags) and imags[loc] == search.imag:
  39.                     sums[length] += list(subset) + [search]
  40.     return sums
  41.  
  42.  
  43. print(findSums(goal, choices))
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top