Basically, the problem is given a number of candidates + voters, decide who has won an ``election''.
Input looks like this:
Code:
4
A B C D
5
A B D C
B A D C
A C B D
D C B A
C D B A
Following the format:
Code:
# number of candidates
<cand 1 name> <cand 2 name> <cand 3 name>
# number of voters
// each line following represents one voter
<voter-1 first choice> <voter-1 second choice> <voter-1 nth choice...>
<voter-nth first choice> <voter-nth second choice> <voter-nth nth choice...>
File should be named "in.txt" and should be in the same directory as the program.
This program will tell you who won:
python Code:
print reduce((lambda w: (lambda x: w(lambda *args: x(x)(*args)))(lambda x: w(lambda *args: x(x)(*args))))(lambda w: lambda z, y: ('No Winner' if (len(z[1]) == 1 or isinstance(z,str)) else w((z[0], z[1][1:]),(y[0], y[1][1:]))) if z[1][0] == y[1][0] else (('No Winner' if isinstance(z, str) else False) or (z[0], f[z[0]]) if z[1][0] > y[1][0] else (y[0], f[y[0]]))), list(globals().__setitem__('f', (lambda l: dict(zip(l[0], map(lambda x: map(lambda y: y[1], x), map(lambda ll: (lambda e: [e.__setitem__(x, e[x]+1) for x in ll] and sorted(e.items()))(dict(zip(range(len(l[0])),[0]*len(l[0])))), map(lambda (char, lst): map((lambda y: lambda x: x.find(y))(char), lst), zip(l[0], [l[1:] for x in xrange(len(l[0]))])))))))(map(lambda x: x.replace(' ', ''), filter(lambda x: x and len(x) > 1, [x.strip() for x in open('in.txt')])))) or f.iteritems()))
Output:
Code:
$ python ass5.py
('A', [2, 1, 0, 2])
Why? Because it was fun and I needed some relaxation. And also because my students will have to write the same, in Java, with approximate 500 times the amount of lines.