简体   繁体   English

c#中列表中的对数和最大值

[英]number of pairs and max value from a list in c#

A poker game card representation is like below扑克游戏卡表示如下

public class Card
{ 
    public string Rank { get; set; } 
    public int Value { get; set; }
    public char Suit { get; set; }
}

From a list of Cards, i have to find number of pairs and max.从卡片列表中,我必须找到对数和最大值。 value from the pairs对的价值

Sample values样本值

    [
    { "A" , 14, 'D' }
    { "A" , 14, 'H' }
    { "T" , 10, 'D' }
    { "T" , 10, 'S' } 
    { "5" , 5,  'S' } 
    ]  

In this case 2 pairs [ 1 pair of A and 1 pair of T and max value is 14 ]在这种情况下 2 对 [ 1 对 A 和 1 对 T 并且最大值为 14 ]

Sample values样本值

    [
    { "K" , 13, 'D' }
    { "K" , 13, 'H' }
    { "2" , 2, 'D' }
    { "T" , 10, 'S' } 
    { "5" , 5,  'S' } 
    ]  

In this case only 1 pair of K and max value is 13在这种情况下,只有一对 K,最大值为 13

I used below LINQ to find the number of pairs我在 LINQ 下面使用了查找对的数量

    var pairs_list = hand
        .GroupBy(i => i.Rank)
        .Where(g => g.Count() > 1)
        .Select(g => g.Key);

Any way to get the value of highest pair with this linq Or can we make a single query to find no of pairs and max value from the pairs ifany使用此 linq 获取最高对的值的任何方法,或者我们可以进行单个查询以从对中查找对的数量和最大值 ifany

Any way to get the value of highest pair with this linq使用此 linq 获得最高对值的任何方法

You're really close with your code.你真的很接近你的代码。 You've already selected all the Ranks that are pairs in your hand, so all that's left to do is pick only the largest value.您已经选择了手中所有成对的等级,所以剩下要做的就是只选择最大的值。

You should, however, do the grouping based on the integer Value , rather than the string Rank , as it's easier to find the "larger value" with integers.但是,您应该根据整数Value而不是字符串Rank进行分组,因为使用整数更容易找到“更大的值”。

int largestPairRankInHand = hand
    .GroupBy(card => card.Value)
    .Where(g => g.Count() > 1)
    .Select(g => g.Key)
    .OrderByDescending(val => val)
    .FirstOrDefault();

The OrDefault is just in case there are no items in your hand. OrDefault以防万一您手中没有物品。

Here's a fiddle example: https://dotnetfiddle.net/5Vo45H这是一个小提琴示例: https : //dotnetfiddle.net/5Vo45H

 var result = hand
      .GroupBy(i => i.Rank)
      .Where(g => g.Count() > 1)
      .Select((g, i) => (value: g.First().Value, index: i + 1))
      .Aggregate((value: 0, count: 0), (p1, p2) => p1.value > p2.value ? (p1.value, p2.index) : (p2.value, p2.index));

 //// or
 //       var result = hand
 //      .GroupBy(i => i.Value)
 //      .Where(g => g.Count() > 1)
 //      .Select((g, i) => (value: g.Key, index: i + 1))
 //     .Aggregate((value: 0, count: 0), (p1, p2) => p1.value > p2.value ? (p1.value, p2.index) : (p2.value, p2.index));

Console.WriteLine($" Pair Count: {result.count}, Max Value: {result.value}");

You could add class Result like :您可以添加类Result ,如:

public class Result
{
    public int NoRanks { get; set; } 
    public int MaxValue { get; set; }
}

And change little your code like :并稍微更改您的代码,例如:

var pairsList = hand
    .GroupBy(card => card.Rank)
    .Where(g => g.Skip(1).Any())
    .Select(o => new { Rank = o.Key, Value = o.Max(v => v.Value) });

And Build the result by using Max function like :并使用Max函数构建结果,例如:

Result result = new Result
{
    NoRanks = pairsList.Count(),
    MaxValue = pairsList.Max(o => o.Value)
};

Demo : https://dotnetfiddle.net/GitsRb演示https : //dotnetfiddle.net/GitsRb

public static void Main()
{
    var hand = new Card[]
    {
        new Card { Rank = "A", Value = 9, Suit = 'D' },
        new Card { Rank = "A", Value = 10, Suit = 'H' },
        new Card { Rank = "T", Value = 12, Suit = 'D' },
        new Card { Rank = "T", Value = 13, Suit = 'S' },
        new Card { Rank = "5", Value = 5, Suit = 'S' },
    };
    
    var pairsList = hand
        .GroupBy(card => card.Rank)
        .Where(g => g.Skip(1).Any())
        .Select(o => new { Rank = o.Key, Value = o.Max(v => v.Value) });
    
    Result result = new Result
    {
        NoRanks  = pairsList.Count(),
        MaxValue = pairsList.Max(o => o.Value)
    };
    
    Console.WriteLine($"Ranks: {string.Join(",", result.NoRanks )} and max value : {result.MaxValue}");

}

public class Card
{
    public string Rank { get; set; } 
    public int Value { get; set; }
    public char Suit { get; set; }
}

public class Result
{
    public int NoRanks  { get; set; } 
    public int MaxValue { get; set; }
}

I hope you find this helpful.我希望你觉得这有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM