Created
March 4, 2014 22:55
-
-
Save wkta/9357480 to your computer and use it in GitHub Desktop.
#MonthOfCode day 3 - binary
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import string, random | |
print 'enter a list of movies you\'ve watched, separated by comas' | |
movies = string.split( raw_input() , ',') | |
# initializing var. for movie ranking | |
scores = dict(); | |
for mov in movies: | |
scores[mov]={ 'won':1, 'lost':1, 'better_th':list() } | |
for i in xrange( int(round(len(movies)*1.5)) ): # 50% extra questions | |
valid_answer=False | |
while( (movies[1] in scores[movies[0]]['better_th']) or | |
(movies[0] in scores[movies[1]]['better_th']) ): | |
random.shuffle( movies) | |
while(not valid_answer): | |
print 'do yo prefer', | |
print movies[0],'to',movies[1]+'? y/n ', | |
answer = raw_input() | |
if( not ('y'==answer or 'n'==answer)): | |
print 'your answer isn\'t recognized' | |
continue | |
valid_answer=True | |
if( 'y'==answer): | |
scores[movies[0]]['won']+=1 | |
scores[movies[1]]['lost']+=1 | |
scores[movies[0]]['better_th'].append( movies[1]) | |
else: | |
scores[movies[0]]['lost']+=1 | |
scores[movies[1]]['won']+=1 | |
scores[movies[1]]['better_th'].append( movies[0]) | |
# computing the ranking | |
final_list = list() | |
for mov in movies: | |
final_list.append( | |
(mov, float(scores[mov]['won'])/scores[mov]['lost']) ) | |
# sorting according to the score (2nd component) | |
final_list =sorted( final_list, key=lambda v:v[1] ) | |
final_list.reverse() | |
print '*** estimated ranking ***' | |
for i in xrange(len(final_list)): | |
print str(i+1)+'- '+final_list[i][0] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment