09. ForceBook (Associative Arrays - Exercise)- 90/100
The problem is in Test #8 (Incorrect answer). Can somebody please help me to correct my code, because I was not abble to.
https://softuni.bg/trainings/resources/officedocument/49587/associative-arrays-exercise-csharp-fundamentals-may-2020/2830
https://judge.softuni.bg/Contests/Compete/Index/1213#8
https://pastebin.com/EfGVcGrd
Hello Axiomatik and thanks for your quick responce. :)
I've already understood the idea of this ForceBook. I've tryed to refactoring my code added this, but the result is still 90/100.
else if(dictOfAllMembers.ContainsKey(forceSide) == false)
{
bool theUserAlreadyExist = false;
string currentSide = string.Empty;
foreach (var item in dictOfAllMembers)
{
if (item.Value.Contains(forceUser))
{
theUserAlreadyExist = true;
currentSide = item.Key;
}
}
if (theUserAlreadyExist == true)
{
dictOfAllMembers[currentSide].Remove(forceUser);//can remove the user
dictOfAllMembers[forceSide] = new List<string>();
dictOfAllMembers[forceSide].Add(forceUser);
Console.WriteLine($"{forceUser} joins the {forceSide} side!");
}
else if(theUserAlreadyExist == false)
{
dictOfAllMembers[forceSide] = new List<string>();
dictOfAllMembers[forceSide].Add(forceUser);
Console.WriteLine($"{forceUser} joins the {forceSide} side!");
}
}
OK, I am going to take a look at the whole code tomorrow. You can compare your solution with my code and maybe you can find out what's not working.
Best,
using System;
using System.Linq;
using System.Collections.Generic;
namespace forcebook
{
class Program
{
static void Main(string[] args)
{
Dictionary<string, List<string>> sideList = new Dictionary<string, List<string>>();
string command = Console.ReadLine();
while (command != "Lumpawaroo")
{
string user = string.Empty;
string side = string.Empty;
if (command.Contains(" | "))
{
string[] spliter = command.Split(" | ", StringSplitOptions.RemoveEmptyEntries);
if (spliter.Length < 2)
{
command = Console.ReadLine();
continue;
}
side = spliter[0];
user = spliter[1];
AddOperator(side, user, sideList);
}
else if (command.Contains(" -> "))
{
string[] spliter = command.Split(" -> ");
side = spliter[1];
user = spliter[0];
SideOperator(side, user, sideList);
}
command = Console.ReadLine();
}
SortReverse(sideList);
sideList = sideList.OrderByDescending(x => x.Value.Count).
ThenBy(x => x.Key).ToDictionary(key => key.Key, value => value.Value);
ListPrinter(sideList);
}
static void ListPrinter(Dictionary<string, List<string>> sideList)
{
foreach (var side in sideList)
{
int counter = 0;
if (side.Value.Count >= 1)
{
Console.WriteLine($"Side: {side.Key}, Members: {side.Value.Count}");
while (counter != side.Value.Count)
{
Console.WriteLine($"! {side.Value[counter]}");
counter++;
}
}
}
}
static Dictionary<string, List<string>> SortReverse(Dictionary<string, List<string>> sideList)
{
foreach (var side in sideList)
{
side.Value.Sort();
}
return sideList;
}
static Dictionary<string, List<string>> AddOperator(string side, string user, Dictionary<string, List<string>> sideList)
{
if (!sideList.ContainsKey(side))
{
sideList.Add(side, new List<string>());
foreach (var group in sideList)
{
int index = 0;
while (index != group.Value.Count)
{
string currentUser = group.Value[index];
string searchUser = user;
if (currentUser == searchUser)
{
return sideList;
}
index++;
}
}
sideList[side].Add(user);
return sideList;
}
else
{
foreach (var group in sideList)
{
int index = 0;
while (index != group.Value.Count)
{
string currentUser = group.Value[index];
string searchUser = user;
if (currentUser == searchUser)
{
return sideList;
}
index++;
}
}
sideList[side].Add(user);
}
return sideList;
}
static Dictionary<string, List<string>> SideOperator(string side, string user, Dictionary<string, List<string>> sideList)
{
foreach (var group in sideList)
{
if (group.Key == side)
{
foreach (var member in group.Value)
{
if (member == user)
{
Console.WriteLine($"{user} joins the {side} side!");
return sideList;
}
}
}
}
if (!sideList.ContainsKey(side))
{
sideList.Add(side, new List<string>());
foreach (var group in sideList)
{
foreach (var person in group.Value)
{
if (person == user)
{
group.Value.Remove(user);
sideList[side].Add(user);
Console.WriteLine($"{user} joins the {side} side!");
return sideList;
}
}
}
}
else
{
foreach (var group in sideList)
{
foreach (var person in group.Value)
{
if (person == user)
{
group.Value.Remove(user);
sideList[side].Add(user);
Console.WriteLine($"{user} joins the {side} side!");
return sideList;
}
}
}
}
sideList[side].Add(user);
Console.WriteLine($"{user} joins the {side} side!");
return sideList;
}
}
}
OK, so I compared with my code and the only difference I was able to pick up was at lines 53-56, when an user is re-joining his own group.
I've also used an expanded input to test new cases, so try to come up with new possible cases when the code still doesn't give 100%.
Lighter | Royal
Darker | DCay
Ivan Ivanov -> Lighter
Ivan Ivanov -> Lighter
Ivan Ivanov -> PeshoMen
DCay -> Lighter
Zebra -> Lighter
Lumpawaroo
Code:
using System;
using System.Linq;
using System.Collections.Generic;
namespace dict
{
class MainClass
{
public static void Main()
{
var dictOfAllMembers = new Dictionary<string, List<string>>();
string input = string.Empty;
while ((input = Console.ReadLine()) != "Lumpawaroo")
{
if (input.Contains('|'))
{
string[] inputArray = input.Split(" | ").ToArray();
string forceSide = inputArray[0];
string forceUser = inputArray[1];
bool CheckIfUserAlreadyExist = false;
foreach (var item in dictOfAllMembers)
{
if (item.Value.Contains(forceUser))
{
CheckIfUserAlreadyExist = true;
}
}
if (dictOfAllMembers.ContainsKey(forceSide) == true && CheckIfUserAlreadyExist == false)
{
dictOfAllMembers[forceSide].Add(forceUser);
}
else if (dictOfAllMembers.ContainsKey(forceSide) == false && CheckIfUserAlreadyExist == false)
{
dictOfAllMembers[forceSide] = new List<string>();
dictOfAllMembers[forceSide].Add(forceUser);
}
}
else if (input.Contains('>'))
{
string[] inputArray = input.Split(" -> ").ToArray();
string forceUser = inputArray[0];
string forceSide = inputArray[1];
if (dictOfAllMembers.ContainsKey(forceSide) == true)
{
bool haveChange = false;
List<string> listOfCurrentUsers = dictOfAllMembers[forceSide];
// Added this validation, when user is re-joining his own group
if (listOfCurrentUsers.Contains(forceUser) == true)
{
Console.WriteLine($"{forceUser} joins the {forceSide} side!");
}
else if (listOfCurrentUsers.Contains(forceUser) == false)//if user doesn't contains in currentList
{
foreach (var item in dictOfAllMembers)
{
string currentSide = string.Empty;
if (item.Value.Contains(forceUser))//if user contains in dictionary where is
{
currentSide = item.Key;
dictOfAllMembers[currentSide].Remove(forceUser);//can remove the user
dictOfAllMembers[forceSide].Add(forceUser);
Console.WriteLine($"{forceUser} joins the {forceSide} side!");
haveChange = true;
break;
}
}
if (haveChange == false)
{
dictOfAllMembers[forceSide].Add(forceUser);
Console.WriteLine($"{forceUser} joins the {forceSide} side!");
}
}
}
else if (dictOfAllMembers.ContainsKey(forceSide) == false)
{
bool CheckIfUserAlreadyExist = false;
foreach (var item in dictOfAllMembers)
{
if (item.Value.Contains(forceUser))
{
dictOfAllMembers[forceSide] = new List<string>();
dictOfAllMembers[item.Key].Remove(forceUser);
dictOfAllMembers[forceSide].Add(forceUser);
CheckIfUserAlreadyExist = true;
break;
}
}
if (CheckIfUserAlreadyExist == false)
{
dictOfAllMembers[forceSide] = new List<string>();
dictOfAllMembers[forceSide].Add(forceUser);
Console.WriteLine($"{forceUser} joins the {forceSide} side!");
}
}
}
}
foreach (var item in dictOfAllMembers
.Where(x => x.Value.Count > 0)
.OrderByDescending(x => x.Value.Count()).ThenBy(x => x.Key))
{
Console.WriteLine($"Side: {item.Key}, Members: {item.Value.Count()}");
List<string> listOfUsers = dictOfAllMembers[item.Key];
foreach (var users in listOfUsers.OrderBy(x => x))
{
Console.WriteLine($"! {users}");
}
}
}
}
}