Sorting a set of objects by a user's preference

前端 未结 10 1876
终归单人心
终归单人心 2020-12-13 16:09

Given a set of n objects in no specific order (n = 5 in this example):

{
    apple,
    orange,
    banana,
    cherry,
    cabbage
}

相关标签:
10条回答
  • 2020-12-13 16:26

    Use a map (javascript object can act so) with object as the key and integer (set to 0 initially for all objects) as the value.

    For each question, you increment the value of the respective chosen key. Since no preference affects none of them, nothing to be done in case it's chosen.

    Then iterate the map, finding the key-value pair with highest value. Return the key.

    0 讨论(0)
  • 2020-12-13 16:29

    Make it simple:

        var options = {
          A: 0,
          B: 0,
          C: 0,
          D: 0
        };
        var optionsNames = Object.keys(options);
    
        var results = document.getElementById('results');
        function printResults() {
          var res = optionsNames.map(function(option) {
            return option + ': ' + options[option];
          });
          
          results.innerHTML = res.join('<br>');
        }
        
        function getNextQuestion() {
          var unclear = [];
          optionsNames.sort(function(a, b) {
            if (options[a] == options[b]) unclear.push([a, b]);
            else return options[b] - options[a];
            return 0;
          });
        
          return unclear[0];
        }
    
        var next;
        function choose(nextIdx) {
          if (next && next[nextIdx] && options[next[nextIdx]] !== undefined) {
            options[next[nextIdx]] += 1;
          }
          
          console.log('click', options, next[nextIdx]);
          
          updateView();
        }
        
        var btn1 = document.getElementById('one');
        var btn2 = document.getElementById('two');
        
        function updateView() {
          next = getNextQuestion();
          if (next) {
            btn1.innerHTML = next[0];
            btn2.innerHTML = next[1];
          } else {
            btn1.innerHTML = 'FINISH';
            btn2.innerHTML = 'FINISH';
          }
          
          printResults();
        }
        
        updateView();
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <button id="one" onclick="choose(0)"></button> vs. <button id="two" onclick="choose(1)"></button>
    <div id="results"></div>

    0 讨论(0)
  • 2020-12-13 16:32

    You can use sort:

    var sorted = "cherry,cabbage,orange,apple,banana".split(",").sort(function(a,b) {
      return prompt([
        "If you prefer " + a + ", enter -1",
        "If you prefer " + b + ", enter 1",
        "If you don't mind , enter 0"
      ].join("\n"));
    });
    
    0 讨论(0)
  • 2020-12-13 16:36
    1. Keep all of the items in an array. Use a separate array / hashmap for relations between pairs. Then use your favorite comparison-based search algorithm (say quicksort).

    2. When two elements are to be compared, find out relative precedence using the map. If there is no known precedence, ask the user.

    3. When a new precedence is added, adjust the precedence order of the whole matrix. For example when the user says apple < banana and then separately banana < cherry, add apple < cherry to the hashmap.

    The comparison-based search algorithms are designed to optimize number of comparisons and this translates to optimization of the number of questions asked to the user in this case.

    0 讨论(0)
提交回复
热议问题