Skip to content

Instantly share code, notes, and snippets.

@masaeedu
Created July 21, 2016 23:03
Show Gist options
  • Save masaeedu/1255e64711f27e5c02712da078c13e4f to your computer and use it in GitHub Desktop.
Save masaeedu/1255e64711f27e5c02712da078c13e4f to your computer and use it in GitHub Desktop.
Interview question
<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