Results 1 to 14 of 14

Thread: One-Liner for elections

  1. #1
    Join Date
    Feb 2006
    Location
    Amsterdam
    Posts
    13,691
    Mentioned
    146 Post(s)
    Quoted
    130 Post(s)

    Default One-Liner for elections

    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.
    Last edited by Wizzup?; 11-12-2011 at 09:11 PM.



    The best way to contact me is by email, which you can find on my website: http://wizzup.org
    I also get email notifications of private messages, though.

    Simba (on Twitter | Group on Villavu | Website | Stable/Unstable releases
    Documentation | Source | Simba Bug Tracker on Github and Villavu )


    My (Blog | Website)

  2. #2
    Join Date
    Jan 2008
    Location
    Ontario, Canada
    Posts
    7,805
    Mentioned
    5 Post(s)
    Quoted
    3 Post(s)

    Default

    You're an asshole.

    Big ol' mean TA.
    Writing an SRL Member Application | [Updated] Pascal Scripting Statements
    My GitHub

    Progress Report:
    13:46 <@BenLand100> <SourceCode> @BenLand100: what you have just said shows you 
                        have serious physchological problems
    13:46 <@BenLand100> HE GETS IT!
    13:46 <@BenLand100> HE FINALLY GETS IT!!!!1

  3. #3
    Join Date
    Sep 2008
    Location
    Not here.
    Posts
    5,422
    Mentioned
    13 Post(s)
    Quoted
    242 Post(s)

    Default

    why do they have to use 500+ lines?

  4. #4
    Join Date
    Mar 2006
    Location
    Behind you
    Posts
    3,193
    Mentioned
    61 Post(s)
    Quoted
    63 Post(s)

    Default

    We should write out a whole bunch of problems for Pascal that people need to solve. This would give people some programming logic experience to help them learn scripting. I've had the Idea bumping around for awhile but haven't put it into a thread yet. I really need to stop being lazy with my learning Ideas for others.

    "Sometimes User's don't need the Answer spelled out with Code. Sometimes all they need is guidance and explanation of the logic to get where they are going."

  5. #5
    Join Date
    May 2006
    Location
    Amsterdam
    Posts
    3,620
    Mentioned
    5 Post(s)
    Quoted
    0 Post(s)

    Default

    Quote Originally Posted by BraK View Post
    We should write out a whole bunch of problems for Pascal that people need to solve. This would give people some programming logic experience to help them learn scripting. I've had the Idea bumping around for awhile but haven't put it into a thread yet. I really need to stop being lazy with my learning Ideas for others.
    http://villavu.com/forum/showthread.php?t=50949
    Verrekte Koekwous

  6. #6
    Join Date
    Apr 2007
    Location
    Lithuania
    Posts
    384
    Mentioned
    0 Post(s)
    Quoted
    15 Post(s)

    Default

    Quote Originally Posted by BraK View Post
    We should write out a whole bunch of problems for Pascal that people need to solve. This would give people some programming logic experience to help them learn scripting. I've had the Idea bumping around for awhile but haven't put it into a thread yet. I really need to stop being lazy with my learning Ideas for others.
    Project Euler, check it out.

    Also, I saw Ben Land was doing that too.

  7. #7
    Join Date
    Feb 2006
    Location
    Amsterdam
    Posts
    13,691
    Mentioned
    146 Post(s)
    Quoted
    130 Post(s)

    Default

    Quote Originally Posted by mormonman View Post
    why do they have to use 500+ lines?
    Just the approximate size of the Java program, and then a bit exaggerated because it is Java. Hopefully they'll do it in less.



    The best way to contact me is by email, which you can find on my website: http://wizzup.org
    I also get email notifications of private messages, though.

    Simba (on Twitter | Group on Villavu | Website | Stable/Unstable releases
    Documentation | Source | Simba Bug Tracker on Github and Villavu )


    My (Blog | Website)

  8. #8
    Join Date
    Nov 2011
    Posts
    130
    Mentioned
    0 Post(s)
    Quoted
    11 Post(s)

    Default

    Hm, intresting concept.

  9. #9
    Join Date
    Jan 2008
    Location
    NC, USA.
    Posts
    4,429
    Mentioned
    0 Post(s)
    Quoted
    4 Post(s)

    Default

    Should have used J.
    Quote Originally Posted by irc
    [00:55:29] < Guest3097> I lol at how BenLand100 has become noidea
    [01:07:40] <@BenLand100> i'm not noidea i'm
    [01:07:44] -!- BenLand100 is now known as BenLand42-
    [01:07:46] <@BenLand42-> shit
    [01:07:49] -!- BenLand42- is now known as BenLand420
    [01:07:50] <@BenLand420> YEA

  10. #10
    Join Date
    Feb 2006
    Location
    Amsterdam
    Posts
    13,691
    Mentioned
    146 Post(s)
    Quoted
    130 Post(s)

    Default

    Quote Originally Posted by noidea View Post
    Should have used J.
    I liked the concept of using reduce() to reduce the list of winners to only one candidate, and I just worked from that.

    Reading the file is pretty simple. Just iterate over the lines, removing spaces. numbers and empty lines. Then use .find() to determinate the position of each letter. Use that letter as an index in an array (or in my case, dictionary) to ``count'' the amount of each position/rank. This was one hard part to do in one line. This does it:

    python Code:
    (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]))))

    Then you simply turn that dictionary into a list of tuples. 'A' : 4 -> ('A', 4) and pass this to the ``reduce function''.

    My first one worked like this:

    python Code:
    # Collapse until the winner is the only item
    def reducer(x, y):
        if isinstance(x, str):
            return x
        if x[1][0] == y[1][0]:
            if len(x[1]) == 1:
                return 'No Winner'
            return reducer((x[0], x[1][1:]),(y[0], y[1][1:]))
        elif x[1][0] > y[1][0]:
            return (x[0], f[x[0]])
        else:
            return (y[0], f[y[0]])

    On one line, using lambda calculus. (Y-Combinator)

    python Code:
    (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]])))

    Then just call reduce on the list of tuples with the above function as ``reducer'.. It'll pick two items from the list, return the winner and compare to the next candidate until only one candidate is left. So you really collapse the list until only the winner is left.



    The best way to contact me is by email, which you can find on my website: http://wizzup.org
    I also get email notifications of private messages, though.

    Simba (on Twitter | Group on Villavu | Website | Stable/Unstable releases
    Documentation | Source | Simba Bug Tracker on Github and Villavu )


    My (Blog | Website)

  11. #11
    Join Date
    May 2006
    Location
    Amsterdam
    Posts
    3,620
    Mentioned
    5 Post(s)
    Quoted
    0 Post(s)

    Default

    Well, not to be a bitch, but your one-liners are unreadable.
    Verrekte Koekwous

  12. #12
    Join Date
    Feb 2006
    Location
    Amsterdam
    Posts
    13,691
    Mentioned
    146 Post(s)
    Quoted
    130 Post(s)

    Default

    Quote Originally Posted by mastaraymond View Post
    Well, not to be a bitch, but your one-liners are unreadable.
    Well, that was the point.
    It's not a hard problem to solve.

    You could probably make it more readable, but then I doubt you'd have it on one line.



    The best way to contact me is by email, which you can find on my website: http://wizzup.org
    I also get email notifications of private messages, though.

    Simba (on Twitter | Group on Villavu | Website | Stable/Unstable releases
    Documentation | Source | Simba Bug Tracker on Github and Villavu )


    My (Blog | Website)

  13. #13
    Join Date
    May 2006
    Location
    Amsterdam
    Posts
    3,620
    Mentioned
    5 Post(s)
    Quoted
    0 Post(s)

    Default

    Quote Originally Posted by Wizzup? View Post
    Well, that was the point.
    It's not a hard problem to solve.

    You could probably make it more readable, but then I doubt you'd have it on one line.
    Or you could just run an obfuscater over readable code, one-liners for free!
    Verrekte Koekwous

  14. #14
    Join Date
    Feb 2006
    Location
    Amsterdam
    Posts
    13,691
    Mentioned
    146 Post(s)
    Quoted
    130 Post(s)

    Default

    Quote Originally Posted by mastaraymond View Post
    Or you could just run an obfuscater over readable code, one-liners for free!
    Not really. Note that my statement really belongs on one line. It simply is only one ``call'' with subcalls. And all the declarations are with lambda. A for loop for example can't really be a one liner. Stacking them all on one line separated by ; doesn't count.



    The best way to contact me is by email, which you can find on my website: http://wizzup.org
    I also get email notifications of private messages, though.

    Simba (on Twitter | Group on Villavu | Website | Stable/Unstable releases
    Documentation | Source | Simba Bug Tracker on Github and Villavu )


    My (Blog | Website)

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •