The code is giving the first output correctly but it does not work when there is the same element more than once.
I am expecting this output:
Group 1 : 60, 120
Group 2 : 30, 150
Ungrouped : 30 60
But I keep getting:
Group 1: 60, 120
Group 2: 150, 30
Group 3: 150, 30
Group 4: 60, 120
Ungrouped: [ 30 120 30 30 30 0]
Till now I did this
def findGroups(money, fare):
count=0
ungroup = np.zeros(len(money), dtype = int)
for i in range(0,len(money)):
for j in range(i+1, len(money)):
if money[i]+money[j] == fare:
count += 1
print(f'Group {count}: {money[i]}, {money[j]}')
else:
ungroup[i] = money[j]
print(f"Ungrouped: {ungroup} ")
money = np.array( [60, 150, 60, 30, 120, 30])
fare = 180
print(f'Task 3:')
findGroups(money, fare) # This should print
# Group 1 : 60, 120
# Group 2 : 30, 150
# Ungrouped : 30 60