I have an array with different numbers, and i have to find minimum, maximum and all the differences of those numbers. I have tried this:
# list of numbers from which we want to find differences.
list_of_nums = [1, 9, 7, 13, 56, 5]
def find_differences(list_of_nums):
list_of_nums = list(dict.fromkeys(list_of_nums)) # remove duplicates
differences = [] # list to keep differences
for i in list_of_nums:
for j in list_of_nums:
if i > j:
differences.append(i - j)
elif j > i:
differences.append(j - i)
else:
continue
differences = list(dict.fromkeys(differences)) # remove duplicates
return sorted(differences)
differences = find_differences(list_of_nums)
print("All differences: ", differences)
print("Maximum difference: ", max(differences))
print("Minimum difference: ", differences[0])
Everything here works, but lot of time is wasted for sorting, removing duplicates and looping through lists. So the bigger is the list, the slower program works. I tried to replace the build in sorted
function by a sorting algorithm, but it made worse. Here what i get:
All differences: [2, 4, 6, 8, 12, 43, 47, 49, 51, 55]
Maximum difference: 55
Minimum difference: 2
If anyone knows better way to solve this, i would be grateful!