fork download
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5.  
  6. public class Test
  7. {
  8. public static void Main()
  9. {
  10. // your code goes here
  11. try
  12. {
  13. String line;
  14. var inputLines = new List<String>();
  15. while((line = Console.ReadLine()) != null) {
  16. line = line.Trim();
  17. if (line != "")
  18. inputLines.Add(line);
  19. }
  20. var retVal = processData(inputLines);
  21. foreach(var res in retVal)
  22. Console.WriteLine(res);
  23. }
  24. catch (IOException ex)
  25. {
  26. Console.WriteLine(ex.Message);
  27. }
  28. }
  29.  
  30. public static List<String> processData(
  31. IEnumerable<string> lines)
  32. {
  33. /*
  34.   * Do not make any changes outside this method.
  35.   *
  36.   * Modify this method to process `array` as indicated
  37.   * in the question. At the end, return a List containing
  38.   * the appropriate values
  39.   *
  40.   * Do not print anything in this method
  41.   *
  42.   * Submit this entire program (not just this function)
  43.   * as your answer
  44.   */
  45. List<String> retVal = new List<String>();
  46. List<Sale> saleList = new List<Sale>();
  47. Dictionary<string, string> productDict = new Dictionary<string, string>();
  48. foreach(var line in lines)
  49. {
  50. string[] saleArray = line.Split(", ");
  51. string[] priceArray = saleArray[4].Split(" ");
  52. if(productDict.ContainsKey(saleArray[3]))
  53. {
  54. int productPrice = int. Parse(productDict[saleArray[3]]);
  55. int price = int.Parse(priceArray[1]);
  56. if(price < productPrice)
  57. productDict[saleArray[3]] = price.ToString();
  58. }
  59. else
  60. {
  61. productDict[saleArray[3]] = priceArray[1];
  62. }
  63.  
  64. Sale objSale = new Sale();
  65. objSale.CustomerName = saleArray[0];
  66. objSale.Price = int.Parse(priceArray[1]);
  67. objSale.ProductName = saleArray[3];
  68. saleList.Add(objSale);
  69. }
  70. foreach(var discountedProd in productDict.Keys)
  71. {
  72. var sale = saleList.Where(s => s.ProductName == discountedProd).OrderByDescending(p => p.Price).First();
  73. if(sale.Price > int.Parse(productDict[discountedProd]))
  74.  
  75. foreach(var saleItem in saleList)
  76. {
  77. if(saleItem.ProductName == discountedProd && saleItem.Price == int.Parse(productDict[discountedProd]))
  78. retVal.Add(saleItem.CustomerName);
  79. }
  80. }
  81. return retVal;
  82. }
  83.  
  84. public class Sale
  85. {
  86. public string CustomerName {get; set;}
  87. public string ProductName {get; set;}
  88. public int Price {get; set;}
  89. }
  90. }
Success #stdin #stdout 0.06s 17464KB
stdin
Rajan Patil, Aundh, 1, Phone Cover, Rs 170, Cash
Mohit Gupta, Baner, 1, Samsung Battery, Rs 900, Credit Card
Rajan Patil, Aundh, 3, Samsung Battery, Rs 1000, Cash
Nina Kothari, Baner, 4, Earphones, Rs 500, Credit Card
T Sunitha, Shivajinagar, 5, Earphones, Rs 550, Credit Card
Rohan Gade, Aundh, 10, Motorola Battery, Rs 1000, Credit Card
Rajan Patil, Shivajinagar, 21, Earphones, Rs 550, Credit Card
Rajan Patil, Aundh, 22, USB Cable, Rs 150, UPI
Meena Kothari, Baner, 23, USB Cable, Rs 100, Cash
Nina Kothari, Baner, 24, USB Cable, Rs 200, UPI
Mohit Gupta, Baner, 25, USB Cable, Rs 150, UPI
stdout
Mohit Gupta
Nina Kothari
Meena Kothari