Skip to content

Instantly share code, notes, and snippets.

@archagon
Created June 14, 2012 19:26
Show Gist options
  • Save archagon/2932377 to your computer and use it in GitHub Desktop.
Save archagon/2932377 to your computer and use it in GitHub Desktop.
import sys
import operator
import os
from random import shuffle
def main(args):
try:
if (len(args) == 2):
data = parse(args[1])
rank(data)
else:
print "Invalid number of arguments! Please input a text file with your items."
except:
return 1 # exit on error
else:
return 0 # exit errorlessly
def parse(filename):
output = []
textfile = open(filename)
lines = textfile.readlines()
for line in lines:
line = line.rstrip()
output.append(line)
return output
def rank(data):
matchups = []
map = []
for i in range(len(data)):
map.append([i, 0])
for i in range(len(data)):
datum = data[i]
for j in range((i+1), len(data)):
matchups.append([i, j])
shuffle(matchups)
for i in range(len(matchups)):
clearscreen()
print str(len(matchups)-i) + " rakings left"
print "1 -- " + data[matchups[i][0]]
print "vs."
print "2 -- " + data[matchups[i][1]]
answer = -1
while answer < 0:
print "Which one do you prefer (or 3 for a tie): "
# make sure that we get a number
try:
answer = int(raw_input())
except:
answer = -1
continue
if answer == 1:
map[matchups[i][0]][1] += 1
elif answer == 2:
map[matchups[i][1]][1] += 1
elif answer == 3:
tie = 1 # ho hum, nothing to do here
else:
answer = -1
sorted_map = sorted(map, key = lambda obj : obj[1])
sorted_map.reverse()
clearscreen()
print "Your ranked list:"
for i in range(len(sorted_map)):
print str(sorted_map[i][1]) + " -- " + data[sorted_map[i][0]]
def clearscreen():
if os.name == "posix":
print chr(27) + "[2J"
else:
print
# not supported in Windows
if __name__ == '__main__':
sys.exit(main(sys.argv))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment