Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
#MonthOfCode day 3 - binary
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