-
Notifications
You must be signed in to change notification settings - Fork 320
/
ch-2.py
executable file
·45 lines (32 loc) · 1.05 KB
/
ch-2.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#!/usr/bin/env python3
import sys
def get_degree(nums):
'''Return the highest frequency of a set of numbers'''
count = {}
for i in nums:
if i in count:
count[i] += 1
else:
count[i] = 1
return max(count.values())
def main(array):
# Turn the list into an list of integers
nums = [int(s) for s in array]
degree = get_degree(nums)
# Store the best thing found so far
solution = None
total = None
for start in range(len(nums)-degree+1):
for end in range(start+degree-1, len(nums)+1):
value = nums[start:end]
# Check if value has the same degree and is lower than the current
# value (or the current value is not)
if get_degree(value) == degree \
and (total is None or sum(value) < total):
# We have a new best solution
solution = value
total = sum(value)
# Display the result
print(*solution, sep=', ')
if __name__ == '__main__':
main(sys.argv[1:])