Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #lab 1.3
- # combo toolkit allows easy coding combinations, found after spending hours trying to
- # hard code if statements, this is way easier
- from itertools import combinations
- # itertools uses the combinations to iterate through and find matches ([index], choose 3)
- input_int = list(combinations([1, 3, 6, 2, -1, 2, 8, -2, 9], 3))
- # variables, and index for parsing through the input_int
- triplets = []
- ind = 0
- length = len(input_int)
- # for loop iterates through index, sum(input_int[ind]) == 0 stops when it finds a
- # three integer combonation with a total sum of zero
- for i in range(length):
- if sum(input_int[ind]) == 0:
- triplets.append(input_int[ind])
- ind += 1
- print("The following triplets have sum zero: ", triplets)
Add Comment
Please, Sign In to add comment