program for chameneous-redux from http://shootout.alioth.debian.org

 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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# The Computer Language Benchmarks Game
# http://shootout.alioth.debian.org/
# contributed by Daniel Nanz 2008-04-10
# modified by Vsevolod Solovyov 2008-10-29
import sys
import itertools
# colors and matching
creature_colors = ['blue', 'red', 'yellow']
def complement(c1, c2):
if c1 == c2: return c1
if c1 == 'blue':
if c2 == 'red': return 'yellow'
return 'red'
if c1 == 'red':
if c2 == 'blue': return 'yellow'
return 'blue'
if c2 == 'blue': return 'red'
return 'blue'
compl_dict = dict(((c1, c2), complement(c1, c2))
for c1 in creature_colors
for c2 in creature_colors)
# this is for faster unpacking while meeting
compl_dict2 = dict(((c1, c2), (complement(c1, c2),)*2)
for c1 in creature_colors
for c2 in creature_colors)
def check_complement(colors=creature_colors, compl=compl_dict):
for c1 in colors:
for c2 in colors:
print '%s + %s -> %s' % (c1, c2, compl[c1, c2])
print ''
# reporting
def spellout(n):
numbers = ['zero', 'one', 'two', 'three', 'four',
'five', 'six', 'seven', 'eight', 'nine']
return ' ' + ' '.join(numbers[int(c)] for c in str(n))
def report(input_zoo, met, self_met):
print ' ' + ' '.join(input_zoo)
for m, sm in zip(met, self_met):
print str(m) + spellout(sm)
print spellout(sum(met)) + '\n'
def chain_merge(iterables):
for it in itertools.cycle(iterables):
yield it.next()
def let_them_meet(meetings_left, input_zoo, compl=compl_dict2):
# prepare
c_no = len(input_zoo)
met = [0] * c_no
self_met = [0] * c_no
colors = input_zoo[:]
# make generator-based coroutines
creatures = chain_merge(itertools.repeat(ci) for ci in xrange(c_no))
id1 = creatures.next()
while meetings_left > 0:
id2 = creatures.next()
if id1 != id2:
colors[id1], colors[id2] = compl[(colors[id1], colors[id2])]
met[id1] += 1
met[id2] += 1
else:
self_met[id1] += 1
met[id1] += 1
meetings_left -= 1
id1 = id2
else:
report(input_zoo, met, self_met)
return
def chameneosiate(n):
check_complement()
let_them_meet(n, ['blue', 'red', 'yellow'])
let_them_meet(n, ['blue', 'red', 'yellow', 'red', 'yellow',
'blue', 'red', 'yellow', 'red', 'blue'])
chameneosiate(int(sys.argv[1]))