Loading

Linq-面试题

1.用逗号分隔表示成绩的字符串,计算成绩的平均值

//10,60,30,20,70,90
string str = "10,60,30,20,70,90";
string[] strs = str.Split(',');
IEnumerable<int> nums = strs.Select(it => Convert.ToInt32(it));
double avg = nums.Average();
Console.WriteLine(avg);
Console.ReadLine();

合并成一句

double avg = str.Split(',').Select(it=>Convert.ToInt32(it)).Average();

2.统计一个字符串中,每个字母出现的频率(忽略大小写),然后按照从高到低顺序输出出现频率高于两次的单词和其出现的频率

string s = "hello world,hahaha,heiheihei";

//去空格,去标点符号
//IsLetter 是否是字母

var items = s.Where(it => char.IsLetter(it)).Select(it => char.ToLower(it))

    .GroupBy(it => it).Select(it => new { it.Key, Count = it.Count() })

    .OrderByDescending(it => it.Key).Where(it => it.Count > 2);

foreach (var item in items)
{
    Console.WriteLine(item);
}

Console.WriteLine();
Console.ReadLine();
posted @ 2022-08-24 16:46  DotNeter-Hpf  阅读(100)  评论(0编辑  收藏  举报