Advertisement
Guest User

Untitled

a guest
Mar 28th, 2017
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.14 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3. using System.IO;
  4. using System.Collections.Generic;
  5.  
  6. namespace cs_console
  7. {
  8.  
  9. class CsvStats
  10. {
  11. readonly IEnumerable<IEnumerable<int>> lines;
  12.  
  13. public CsvStats(string filename)
  14. {
  15.  
  16. lines = File
  17. .ReadAllLines(filename)
  18. .Skip(1)
  19. .Select(line => line
  20. .Trim()
  21. .Split(new[] { ',' })
  22. .Select(e=>int.Parse(e))
  23. )
  24. ;
  25. }
  26.  
  27. public double SumOfColumn(int col)
  28. {
  29. return Column(col).Sum();
  30. }
  31.  
  32. public double MeanOfColumn(int col)
  33. {
  34. return SumOfColumn(col) / Column(col).Count();
  35. }
  36.  
  37. IEnumerable<int> Column(int col)
  38. {
  39. return lines
  40. .Select(line => line.Skip(col).First())
  41. ;
  42. }
  43. }
  44.  
  45.  
  46. class Program
  47. {
  48. static void Main(string[] args)
  49. {
  50. try
  51. {
  52. GenerateData();
  53. var stats = new CsvStats("test.csv");
  54. Console.WriteLine($"1[MEAN]: {stats.MeanOfColumn(1)}");
  55. Console.WriteLine($"2[SUM]: {stats.SumOfColumn(2)}");
  56. }
  57. catch (Exception e)
  58. {
  59. Console.WriteLine(e);
  60. }
  61. }
  62.  
  63. static void GenerateData()
  64. {
  65. var text = @"col 0,col 1,col 2
  66. 542,414,26
  67. 335,950,113
  68. 378,954,231";
  69. File.WriteAllText("test.csv", text);
  70. }
  71. }
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement