dave2

Feb 9th, 2021
462
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. from itertools import combinations
2. from itertools import product
3.
4.
5. def compute(a,b):
6.     return abs(a-b)
7.
8. A = [0,3,3,7,5,3,11,1]
9.
10. dict = {}
11. min = A[0]
12. max = A[0]
13.
14. for i in range(len(A)):
15.     if A[i] in dict:
16.         dict[A[i]].append(i)
17.     else:
18.         dict[A[i]] = [i]
19.
20.     if A[i] < min:
21.         min = A[i]
22.     elif A[i] > max:
23.         max = A[i]
24.
25.
26. prev = dict[min]
27. combs = []
28.
29.
30.
31. for i in range(min+1,max+1,1):
32.     if i in dict:
33.         prev.extend(dict[i])
34.         prev.sort()
35.         comb = list(combinations(prev, 2))
36.         combs.extend(comb)
37.         prev = dict[i]
38.
39.
40. min = compute(A[combs[0][0]], A[combs[0][1]])
41.
42. for couple in combs:
43.     value = compute(A[couple[0]], A[couple[1]])
44.     if value < min:
45.         min = value
46.
47. print(min)
48.
RAW Paste Data