I did this before.
[Reinventing the Wheel] Human Power Tournament Sort [Python]
It was fun to think and verify the algorithm, but it became so ridiculous that I'll include a smart way to just change the sort call function.
__ References: __ entertainment-lab: Implementation of original sorting in Python:
Since the essential sort part is left to the genuine Python sort, it is naturally very short.
ranking_sort.py
#!/usr/bin/env python
# -*- coding:utf-8 -*-
#
# written by ssh0, December 2014.
description = """
Ranking interface for self-analysis.
In this application
Create a comparative question based on the given list
The ranking is completed when the user answers it.
              """
import argparse
import sys
def human_cmp(a, b):
    print '\nwhich ones do you like?'
    print '  [j]: %s, [k]: %s. [=]:eq' % (a, b)
    key_input = raw_input(">>> ")
    if key_input == 'j':
        ans = -1
    elif key_input == 'k':
        ans = 1
    elif key_input == '=':
        ans = 0
    else:
        raise ValueError('please select by "j", "k" or "=".')
    return ans
if __name__ == '__main__':
    parse = argparse.ArgumentParser(description=description)
    parse.add_argument('-l', '--list',
                       dest='objects',
                       nargs='*',
                       type=str,
                       help='list of some objects',
                       default=argparse.SUPPRESS
                       )
    args = parse.parse_args()
    data = args.objects
    data.sort(cmp=human_cmp)
    print data
The function passed to the sort function is human_cmp. Only be careful about the value returned by the function. Compare the two arguments and return a negative value if the first argument is smaller than the second argument, 0 if they are the same, and a positive value if they are larger.
➤ python ranking_sort.py -l 3 1 4 8 5 2 6 7
which ones do you like?
  [j]: 1, [k]: 3. [=]:eq
>>> j
which ones do you like?
  [j]: 4, [k]: 1. [=]:eq
>>> k
which ones do you like?
  [j]: 4, [k]: 3. [=]:eq
>>> k
which ones do you like?
  [j]: 8, [k]: 3. [=]:eq
>>> k
which ones do you like?
  [j]: 8, [k]: 4. [=]:eq
>>> k
which ones do you like?
  [j]: 5, [k]: 4. [=]:eq
>>> k
which ones do you like?
  [j]: 5, [k]: 8. [=]:eq
>>> j
which ones do you like?
  [j]: 2, [k]: 4. [=]:eq
>>> j
which ones do you like?
  [j]: 2, [k]: 3. [=]:eq
>>> j
which ones do you like?
  [j]: 2, [k]: 1. [=]:eq
>>> k
which ones do you like?
  [j]: 6, [k]: 4. [=]:eq
>>> k
which ones do you like?
  [j]: 6, [k]: 8. [=]:eq
>>> j
which ones do you like?
  [j]: 6, [k]: 5. [=]:eq
>>> k
which ones do you like?
  [j]: 7, [k]: 4. [=]:eq
>>> k
which ones do you like?
  [j]: 7, [k]: 6. [=]:eq
>>> k
which ones do you like?
  [j]: 7, [k]: 8. [=]:eq
>>> j
['1', '2', '3', '4', '5', '6', '7', '8']
This is human power, but of course you can define some function yourself like a reference source.
The last time was ... It was good because it was fun. I have no choice but to say so ... Or rather, it wasn't necessary to write it when I noticed it.
Recommended Posts