Advertisement
dimipan80

Serbian Unleashed

Mar 18th, 2017
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.38 KB | None | 0 0
  1. /*
  2. Admit it – the СРЪБСКО is your favorite sort of music. You never miss a concert and you have become quite the geek concerning everything involved with СРЪБСКО. You can’t decide between all the singers you know who your favorite one is.
  3. One way to find out is to keep statistics of how much money their concerts make.
  4. Write a program that does all the boring calculations for you.
  5. On each input line you’ll be given data in format: "singer @venue ticketsPrice ticketsCount".
  6. There will be no redundant whitespaces anywhere in the input. Aggregate the data by venue and by singer.
  7. For each venue, print the singer and the total amount of money his/her concerts have made on a separate line.
  8. Venues should be kept in the same order they were entered; the singers should be sorted by how much money they have made in descending order. If two singers have made the same amount of money, keep them in the order in which they were entered.
  9. Keep in mind that if a line is in incorrect format, it should be skipped and its data should not be added to the output.
  10. Each of the four tokens must be separated by a space, everything else is invalid.
  11. The venue should be denoted with @ in front of it, such as @Sunny Beach
  12. !!! SKIP THOSE: Ceca@Belgrade125 12378, Ceca @Belgrade12312 123
  13. The singer and town name may consist of one to three words.
  14. Input
  15. The input data should be read from the console.
  16. It consists of a variable number of lines and ends when the command “End" is received.
  17. The input data will always be valid and in the format described. There is no need to check it explicitly.
  18. Output
  19. The output should be printed on the console.
  20. Print the aggregated data for each venue and singer in the format shown below.
  21. Format for singer lines is #{2*space}{singer}{space}->{space}{total money}
  22. Constraints
  23. The number of input lines will be in the range [2 … 50].
  24. The ticket price will be an integer in the range [0 … 200].
  25. The ticket count will be an integer in the range [0 … 100 000]
  26. Singers and venues are case sensitive
  27. Allowed working time for your program: 0.1 seconds. Allowed memory: 16 MB.
  28. */
  29.  
  30. namespace Serbian_Singers
  31. {
  32.     using System;
  33.     using System.Collections.Generic;
  34.     using System.Linq;
  35.     using System.Text.RegularExpressions;
  36.  
  37.     internal static class TicketsProfit
  38.     {
  39.         private static void Main()
  40.         {
  41.             var singerVenues = new Dictionary<string, Dictionary<string, long>>();
  42.             var checkPattern = @"(.*?) @(.*?) (\d+) (\d+)";
  43.  
  44.             while (true)
  45.             {
  46.                 var inputLine = Console.ReadLine();
  47.                 if (inputLine == "End")
  48.                 {
  49.                     break;
  50.                 }
  51.  
  52.                 if (!Regex.IsMatch(inputLine, checkPattern))
  53.                 {
  54.                     continue;
  55.                 }
  56.  
  57.                 var singerName = string.Empty;
  58.                 var venue = string.Empty;
  59.                 var ticketPrice = 0;
  60.                 long tickets = 0;
  61.  
  62.                 MatchCollection matches = Regex.Matches(inputLine, checkPattern);
  63.                 foreach (Match match in matches)
  64.                 {
  65.                     singerName = match.Groups[1].Value;
  66.                     venue = match.Groups[2].Value;
  67.                     ticketPrice = int.Parse(match.Groups[3].Value.Trim());
  68.                     tickets = long.Parse(match.Groups[4].Value.Trim());
  69.                 }
  70.  
  71.                 var ticketsAmount = tickets * ticketPrice;
  72.                 if (!singerVenues.ContainsKey(venue))
  73.                 {
  74.                     singerVenues.Add(venue, new Dictionary<string, long>());
  75.                 }
  76.  
  77.                 if (!singerVenues[venue].ContainsKey(singerName))
  78.                 {
  79.                     singerVenues[venue].Add(singerName, 0);
  80.                 }
  81.  
  82.                 singerVenues[venue][singerName] += ticketsAmount;
  83.             }
  84.  
  85.             foreach (var item in singerVenues)
  86.             {
  87.                 Console.WriteLine(item.Key);
  88.                 List<string> sortedSingers = item.Value
  89.                     .OrderByDescending(s => s.Value)
  90.                     .Select(s => s.Key)
  91.                     .ToList();
  92.  
  93.                 foreach (var singer in sortedSingers)
  94.                 {
  95.                     Console.WriteLine("#  {0} -> {1}", singer, item.Value[singer]);
  96.                 }
  97.             }
  98.         }
  99.     }
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement