[ create a new paste ] login | about

Link: http://codepad.org/tN4MUVq3    [ raw code | output | fork ]

Scaevolus - Python, pasted on Sep 7:
import collections
from operator import itemgetter as ind

data = ((1, 'Size', 'Small'),
        (2, 'Size', 'Large'),
        (3, 'Color', 'Red'),
        (4, 'Color', 'Green'),
        (5, 'Color', 'Blue'),
        (6, 'Shape', 'Cube'),
        (7, 'Shape', 'Sphere'),
        (8, 'Shape', 'Pyramid'))

attributes = collections.defaultdict(lambda: [])

for id, attribute, value in data:
    attributes[attribute] += [(id, value)]

attributes = sorted(attributes.iteritems())

def permute(undone):
    if not undone:
        yield []
        return
    attribute = undone[0][0]
    for id, value in undone[0][1]:
        for permutation in permute(undone[1:]):
            yield [(id, attribute, value)] + permutation
            
permutations = sorted(map(sorted, permute(attributes)))

for p in permutations:
    print ','.join(map(str, map(ind(0), p))),
    print '        (%s)' % ', '.join(map(ind(2), p))


Output:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
1,3,6         (Small, Red, Cube)
1,3,7         (Small, Red, Sphere)
1,3,8         (Small, Red, Pyramid)
1,4,6         (Small, Green, Cube)
1,4,7         (Small, Green, Sphere)
1,4,8         (Small, Green, Pyramid)
1,5,6         (Small, Blue, Cube)
1,5,7         (Small, Blue, Sphere)
1,5,8         (Small, Blue, Pyramid)
2,3,6         (Large, Red, Cube)
2,3,7         (Large, Red, Sphere)
2,3,8         (Large, Red, Pyramid)
2,4,6         (Large, Green, Cube)
2,4,7         (Large, Green, Sphere)
2,4,8         (Large, Green, Pyramid)
2,5,6         (Large, Blue, Cube)
2,5,7         (Large, Blue, Sphere)
2,5,8         (Large, Blue, Pyramid)


Create a new paste based on this one


Comments: