r/dailyprogrammer • u/rya11111 3 1 • Mar 15 '12
[3/15/2012] Challenge #25 [easy]
In an election, the person with the majority of the votes is the winner. Sometimes due to similar number of votes, there are no winners.
Your challenge is to write a program that determines the winner of a vote, or shows that there are no winners due to a lack of majority.
12
Upvotes
4
u/Steve132 0 1 Mar 15 '12
Really short python.
1
u/prophile Mar 15 '12
Done in Python.
3
u/stevelosh Mar 15 '12
Clojure:
3
u/Steve132 0 1 Mar 15 '12
C++ fully generic
2
u/luxgladius 0 0 Mar 15 '12 edited Mar 15 '12
Perl
2
u/lukz 2 0 Mar 16 '12
Common Lisp
Input is a sequence, output is the winning element or nil.
Info: the standard find-if function searches through a sequence and returns an element that satisfies a predicate.
1
u/ixid 0 0 Mar 15 '12 edited Mar 15 '12
D
1
u/robin-gvx 0 2 Mar 15 '12
Déjà Vu: http://www.hastebin.com/raw/xuciyuyimu
1
u/HazzyPls 0 0 Mar 16 '12 edited Mar 16 '12
Haskell. It still feels... somewhat messy. _winner is always either a list 1 or 0 long, and I have no idea how to better do it. :/
1
u/lullabysinger Mar 16 '12
1
u/gthank Mar 16 '12
Python 3 (REPL session)
1
u/pohatu Mar 16 '12 edited Mar 16 '12
Echo "A B C A B C A" | tr ' ' '\n' | sort | uniq -c | sort -nr | awk '{ SUM += $1} END { print SUM }
That's as far as i can get right now without doing more advanced awk scripting - at which point why not do it all in awk - or doing a second or third command.
edit: Here it is:
$ echo "A B C A B C A " | tr ' ' '\n' | sort | uniq -c | sort -nr | gawk '{a[i++]=$1; v[$1]=$2; sum+=$1} END{ if ((a[0] /sum)>.5) print v[a[0]] " wins by majority"}'
$ echo "A B A A " | tr ' ' '\n' | sort | uniq -c | sort -nr | gawk '{a[i++]=$1; v[$1]=$2; sum+=$1} END{ if ((a[0]/sum)>.5) print v[a[0]] " wins by majority"}'
A wins by majority
1
u/namekuseijin Mar 18 '12
plain Scheme plus sort and filter that everyone should have
1
u/Yuushi Mar 20 '12
Haskell:
1
u/[deleted] Mar 30 '12
That was pretty fun. PHP
http://pastebin.com/WFeKLbCG