比较2个或多个排序数组javascript

Compare 2 or more sorted arrays javascript

本文关键字:排序 数组 javascript 2个 比较      更新时间:2023-09-26

我正在尝试手动推出一个扑克解决方案。我已经掌握了所有的逻辑来确定最好的5张牌手。我在比较多个数组时遇到了问题,这些数组在平局的情况下具有较高的元素(关于手型)。

假设我们有一些潜在的同花顺赢家。

var low_flush = [2, 3, 4, 5, 7]
var medium_flush = [3, 4, 5, 6, 8]
var high_flush = [2, 3, 4, 5, 9]

我想构建一个函数,我将任意数量的数组传递给它,它会在实际平局的情况下返回"最高牌手"或一个牌手数组:

function bestHand(hands){
  // 
  return high_hand
}

到目前为止,我读到的所有内容都是如何比较两个数组,通常情况下,它只会看到是否有相等的数组。如果这有助于这里我的源代码

我的第一个想法是在手上迭代。对于每一次迭代,再次在指针上迭代进行比较。一想到这个伪代码就头疼,我想它们可能是这个和/或库(但不是扑克库)的更优雅的解决方案

我在代码库的其他部分使用下划线,所以也可以随意使用它作为答案!

您可以先使用reduce()来计算每个数组的总和

var total3 = high_flush.reduce(function(previousValue, currentValue) {
  return previousValue + currentValue;
}, 0);

然后将每个推送到具有数组的totalwinner 名称的数组

allhands.push({total: total1 , name: "low_flush"});

然后将它们与比较函数进行比较,并对阵列进行排序

function compare(a,b) {
  if (a.total < b.total)
    return -1;
  else if (a.total > b.total)
    return 1;
  else 
    return 0;
}
allhands.sort(compare);

工作此处的示例:

var low_flush = [2, 3, 4, 5, 7];
var medium_flush = [2, 3, 4, 5, 8];
var high_flush = [2, 3, 4, 5, 9];
var allhands = [];
var total3 = high_flush.reduce(function(previousValue, currentValue) {
  return previousValue + currentValue;
}, 0);
allhands.push({total: total3 , name: "high_flush"});
var total1 = low_flush.reduce(function(previousValue, currentValue) {
  return previousValue + currentValue;
}, 0);
allhands.push({total: total1 , name: "low_flush"});
var total2 = medium_flush.reduce(function(previousValue, currentValue) {
  return previousValue + currentValue;
}, 0);
allhands.push({total: total2 , name: "medium_flush"});
function compare(a,b) {
  if (a.total < b.total)
    return -1;
  else if (a.total > b.total)
    return 1;
  else 
    return 0;
}
allhands.sort(compare);
console.log("The winner is "+allhands[allhands.length - 1].name +"With total:"+ allhands[allhands.length - 1].total );
<script src="http://gh-canon.github.io/stack-snippet-console/console.min.js"></script>

这可能看起来很傻。但这似乎对我有用。

var hands = [ [2, 3, 4, 5, 7], [2, 3, 5, 9, 8], [2, 3, 4, 5, 9] ];
var values = [];
function bestHand(hands){ 
   hands.forEach(function(arr, index) {
    var temp = arr.slice();
    values[index] = parseInt(temp.sort().reverse().join(''));
   });
   var max = Math.max.apply(Math, values);
   return hands[values.indexOf(max)];
}
bestHand(hands);

显然,您需要将一个手对象数组传递给类似的对象。

function getBest(...hands){
    return hands.sort((p,c) => p.weight() <= c.weight() ? -1:1)[hands.length-1]
}

当涉及到在平局条件下找出手部物体的重量时,可以首先通过手部颜色(黑桃击败所有)来确定,然后通过2、3、4、5、6、7、8、9、10、11(j)、12(q)、13(k)、14(a)等球杆的牌值之和来确定。它们的总和是104,所以对于菱形卡的值可以是(104+2)、(104+3)、(104+4)等。对于红心,你可以将值偏移208,对于黑桃,你可以偏移312。

hand.prototype.weight = function(){
                          return this.reduce((p,c) => p.value + c.value)
                        }

当然,这只会处理平局的情况。它分不清同花顺和同花顺的皇室成员。

在这里查看数组的reduce函数。

示例:

var result = hands.reduce(function(prevHand, currentHand) {
    return compareHands(prevHand,currentHand) ? prevHand : currentHand;
});

这回答了关于比较多个数组的问题,但请记住,它会将其减少到一个值,这可能不是完美的解决方案,因为您必须考虑绘制。在这种情况下,您的结果应该是一个数组,在比较中,将其推入数组或重新初始化并添加到数组中。

function bestHands(DescOrderedHands){
  bestHand = DescOrderedHands[0]
  bestHandScore = parseInt(_.map(bestHand, function(num){return num.toString}).join(""))
  _.each(DescOrderedHands, function(hand){
    stringHand = _.map(hand, function(num){return num.toString}).join("")
    if (parseInt(stringHand) > bestHandScore){
      bestHand = hand
    } 
  }
  return bestHand
}

首先,你必须确保函数中的所有数组都有相同的长度,然后你必须对每个数组进行排序,最后你必须比较每个数组的每个值。

你应该有这样的

function bestHand(hands){
  var best_hand = null;
  for(var i = 0; i < hands.length; ++i){
    hands[i] = hands[i].sort();
    if(i > 0){
      for(var j = 0; j < hands[i].length; ++j){
        if(best_hand[j] < hands[i][j]){
          best_hand = hands[i];
          break;
        }
        else if(best_hand[j] > hands[i][j]){
          break;
        }
      }
    }
    else{
      best_hand = hands[i];
    }
  }
  return best_hand;
}

请确保在参数中传递相同长度的数组