Advertisement
-Annie-

EXAM*02.SoftUniKaraoke

Jul 6th, 2017
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.15 KB | None | 0 0
  1. namespace Exam
  2. {
  3.     using System;
  4.     using System.Collections.Generic;
  5.     using System.Linq;
  6.     using System.Text.RegularExpressions;
  7.  
  8.     public class SoftUniKaraoke
  9.     {
  10.         static void Main()
  11.         {
  12.             string[] allParticipants = Regex.Split(Console.ReadLine(), @"\s*,\s*");
  13.             string[] allSongs = Console.ReadLine().Split(',').Select(s => s.Trim()).ToArray();
  14.             var awardsByParticipant = new Dictionary<string, HashSet<string>>(); //name, awards(unique)
  15.  
  16.             foreach(var p in allParticipants)
  17.             {
  18.                 awardsByParticipant[p] = new HashSet<string>();
  19.             }
  20.  
  21.             while (true)
  22.             {
  23.                 var line = Console.ReadLine();
  24.                 if (line == "dawn")
  25.                 {
  26.                     break;
  27.                 }
  28.  
  29.                 var tokens = Regex.Split(line, @"\s*,\s*");
  30.                 var participant = tokens[0];
  31.                 var song = tokens[1];
  32.                 var award = tokens[2];
  33.  
  34.                 if (allParticipants.Contains(participant) && allSongs.Contains(song))
  35.                 {
  36.                     awardsByParticipant[participant].Add(award);
  37.                 }
  38.             }
  39.  
  40.             var result = awardsByParticipant.Select(item => new
  41.             {
  42.                 playerName = item.Key,
  43.                 awards = item.Value.Distinct().OrderBy(a => a),
  44.                 awardsCount = item.Value.Distinct().Count()
  45.             })
  46.             .OrderByDescending(p => p.awardsCount) //sort by awards count in descending order
  47.             .ThenBy(p => p.playerName)  //sort alphabetically by name
  48.             .Where(p => p.awardsCount > 0) //remove people with 0 awards
  49.             .ToArray();
  50.  
  51.             foreach(var p in result)
  52.             {
  53.                 Console.WriteLine(p.playerName + ": " + p.awardsCount + " awards");
  54.                 foreach(var aw in p.awards)
  55.                 {
  56.                     Console.WriteLine("--" + aw);
  57.                 }
  58.             }
  59.  
  60.             if(result.Length == 0)
  61.             {
  62.                 Console.WriteLine("No awards");
  63.             }
  64.         }
  65.     }
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement