Created
July 21, 2016 23:03
-
-
Save masaeedu/1255e64711f27e5c02712da078c13e4f to your computer and use it in GitHub Desktop.
Interview question
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<Query Kind="Program" /> | |
void Main() | |
{ | |
const string fileName = @"C:\Users\asaeeduddin\Downloads\Interview\scores.csv"; | |
String[] lines = File.ReadAllLines(fileName); | |
//todo calculate the average for each student and display the student’s name with the highest average. | |
var averages = lines | |
// Parse rows (probably could have si | |
.Skip(1) | |
.Select(l => l.Split(',')) | |
.Select(chunks => new { Name = chunks[1], Score = double.Parse(chunks[3]) }) | |
// Group scores by user and average | |
.GroupBy(records => records.Name) | |
.Select(group => new { Name = group.Key, Average = group.Average(item => item.Score) }); | |
// Pick person with highest average | |
var winner = averages.Aggregate((last, curr) => last.Average > curr.Average ? last : curr); | |
Console.WriteLine($"Congrats {winner.Name}, you genius you! With {winner.Average:f}%, you have the highest average in the class!"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment