-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcalc.py
More file actions
75 lines (70 loc) · 1.62 KB
/
calc.py
File metadata and controls
75 lines (70 loc) · 1.62 KB
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#!/bin/env python
#def calc(seq):
# maximum = 0
# max_item = []
# for i in seq:
# product = (i[0]*100 + i[1]*10 +i[2]) * (i[3]*10 + i[4])
# if product > maximum:
# maximum = product
# max_item = i
# elif product == maximum:
# max_item += ','+i
# return max_item, maximum
#
#seq = [[5, 6, 7, 8, 9], [5, 6, 7, 9, 8]]
#max_item, maximum = calc(seq)
#print "Maximum at", max_item, ", product", maximum
def calc(seq, where):
maximum, max_item = 0, []
for i in seq:
product = int(i[:where]) * int(i[where:])
if product > maximum:
maximum, max_item = product, i
elif product == maximum:
max_item += ',' + i
print "Maximum at", max_item, ",product", maximum
def permute(seq):
result = []
for a in seq:
for b in seq:
for c in seq:
for d in seq:
for e in seq:
if a!=b and a!=c and a!=d and a!=e and \
b!=c and b!=d and b!=e and \
c!=d and c!=e and d!=e:
result.append(''.join([a,b,c,d,e]))
return result
def permute1(seq):
l = len(seq)
if l == 1:
return [seq]
else:
res = []
for i in range(len(seq)):
rest = seq[:i] + seq[i+1:]
for x in permute1(rest):
res.append(seq[i:i+1] + x)
return res
def permute2(seq):
l = len(seq)
if l <= 2:
if l == 2:
return [ seq, [seq[1], seq[0]] ]
else:
return [seq]
return [seq]
else:
res = []
for i in range(len(seq)):
rest = seq[:i] + seq[i+1:]
for x in permute1(rest):
res.append(seq[i:i+1] + x)
return res
if __name__ == '__main__':
import sys
seq = list(sys.argv[1])
where = int(sys.argv[2])
thelist = [ ''.join(x) for x in permute2(seq) ]
print 'Got', len(thelist), 'items.'
calc(thelist, where)