Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import itertools
- from collections import defaultdict
- from bisect import bisect_left
- z_real = float(input("Enter real part of number: "))
- z_imag = float(input("Enter imaginary part of number: "))
- goal = complex(z_real, z_imag)
- print("Number: {}".format(goal))
- print("Now enter the set of complex numbers to choose from")
- choices = []
- while True:
- c_real = 0
- try:
- c_real = float(input("Enter the real part (or any non-number to continue): "))
- except BaseException:
- break
- c_imag = float(input("Enter imaginary part: "))
- choices.append(complex(c_real, c_imag))
- def findSums(z, choice):
- choice.sort(key=lambda z_: z_.imag)
- choice.sort(key=lambda z_: z_.real)
- reals = map(lambda z_: z_.real, choice)
- imags = map(lambda z_: z_.imag, choice)
- sums = defaultdict(list)
- for length in range(len(choices) + 1):
- for subset in itertools.combinations(choices, length):
- search = z - sum(subset)
- pos = bisect_left(reals, search.real)
- if pos < len(reals) and reals[pos] == search.real:
- loc = bisect_left(imags, search.imag)
- if loc < len(imags) and imags[loc] == search.imag:
- sums[length] += list(subset) + [search]
- return sums
- print(findSums(goal, choices))
Add Comment
Please, Sign In to add comment