Skip to content

Instantly share code, notes, and snippets.

@chrisyco
Created October 2, 2011 00:38
Show Gist options
  • Save chrisyco/1256873 to your computer and use it in GitHub Desktop.
Save chrisyco/1256873 to your computer and use it in GitHub Desktop.
Sort a list of items interactively
yes_letters = set('yt')
no_letters = set('nf')
def read_bool(prompt):
'''Read a boolean value from standard input.'''
answer = None
while answer not in yes_letters and answer not in no_letters:
answer = raw_input(prompt).strip()[0].lower()
return answer in yes_letters
def input_cmp(this, that):
'''Ask the user to choose between two items.
Return -1 if the first item was chosen, otherwise 1.
'''
this_better_than_that = read_bool('Is %s better than %s? (y/n)' % (this, that))
if this_better_than_that:
return -1 # This comes *before* that
else:
return 1 # This comes *after* that
def main():
# Feel free to add your own
languages = '''C C++ C# Python Ruby
Java Scala Haskell OCaml Forth
Scheme Ada BASIC D Go'''.split()
languages.sort(cmp=input_cmp)
print '\n'.join(languages)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment