Advertisement
Guest User

Code Kata - Football

a guest
Aug 8th, 2012
204
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.11 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Text;
  6.  
  7. namespace Code_Katas
  8. {
  9.     public class Football
  10.     {
  11.         public static string FindTeamWithSmallestDifference3(string fileName)
  12.         {
  13.             var data = File.OpenText(fileName).ReadToEnd();
  14.  
  15.             int currentLeastDifference = int.MaxValue;
  16.             string currentLeastDifferenceTeam = String.Empty;
  17.            
  18.             var onlyData = data.Split(new[] { '\n' }, StringSplitOptions.RemoveEmptyEntries)
  19.                 .Where(r => char.IsNumber(r.TrimStart()[0]));
  20.  
  21.             foreach (var item in onlyData)
  22.             {
  23.                 var dataOfTeam = item.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
  24.  
  25.                 var difference = Math.Abs(int.Parse(dataOfTeam[6]) - int.Parse(dataOfTeam[8]));
  26.                 if (difference < currentLeastDifference)
  27.                 {
  28.                     currentLeastDifference = difference;
  29.                     currentLeastDifferenceTeam = dataOfTeam[1];
  30.                 }
  31.             }
  32.  
  33.             return currentLeastDifferenceTeam;
  34.         }
  35.     }
  36. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement