Задача 5. от Objects and Classes Exercise - Teamwork Projects
Здравейте колеги!
Ще може ли малко помощ по кода ми за задача 5.Teamwork Projects. Тествах различни варианти на входа, филтрирах ексепшъните, условието в документацията прегледах дума по дума, но повече от 83/100 в Judge не ми дава, а изходните данни си излизат.
Условието на задачата е:
It's time for the teamwork projects and you are responsible for gathering the teams. First you will receive an integer - the count of the teams you will have to register. You will be given a user and a team, separated with “-”. The user is the creator of the team. For every newly created team you should print a message:
"Team {teamName} has been created by {user}!".
Next, you will receive an user with a team, separated with "->", which means that the user wants to join that team. Upon receiving the command: “end of assignment”, you should print every team, ordered by the count of its members (descending) and then by name (ascending). For each team, you have to print its members sorted by name (ascending). However, there are several rules:
- If an user tries to create a team more than once, a message should be displayed:
- "Team {teamName} was already created!"
- A creator of a team cannot create another team – the following message should be thrown:
- "{user} cannot create another team!"
- If an user tries to join a non-existent team, a message should be displayed:
- "Team {teamName} does not exist!"
- A member of a team cannot join another team – the following message should be thrown:
- "Member {user} cannot join team {team Name}!"
- In the end, teams with zero members (with only a creator) should disband and you have to print them ordered by name in ascending order.
- Every valid team should be printed ordered by name (ascending) in the following format:
"{teamName}: - {creator} -- {member}…" |
Examples
Input |
Output |
Comments |
2 Didi-PowerPuffsCoders Toni-Toni is the best Petq->PowerPuffsCoders Toni->Toni is the best end of assignment |
Team PowerPuffsCoders has been created by Didi! Team Toni is the best has been created by Toni! Member Toni cannot join team Toni is the best! PowerPuffsCoders - Didi -- Petq Teams to disband: Toni is the best |
Toni created a team, which he attempted to join later and this action resulted in throwing a certain message. Since nobody else tried to join his team, the team had to disband. |
3 Tatyana-CloneClub Helena-CloneClub Trifon-AiNaBira Pesho->aiNaBira Pesho->AiNaBira Tatyana->Leda PeshO->AiNaBira Cossima->CloneClub end of assignment |
Team CloneClub has been created by Tatyana! Team CloneClub was already created! Team AiNaBira has been created by Trifon! Team aiNaBira does not exist! Team Leda does not exist! AiNaBira - Trifon -- Pesho -- PeshO CloneClub - Tatyana -- Cossima Teams to disband: |
Note that when a user joins a team, you should first check if the team exists and then check if the user is already in a team:
Tatyana has created CloneClub, then she tried to join a non-existent team and the concrete message was displayed.
|
Ето кодът ми:
using System;
using System.Collections.Generic;
using System.Linq;
namespace TeamworkProjects
{
class Program
{
static void Main(string[] args)
{
int countTeam = int.Parse(Console.ReadLine());
List<Team> teams = CreatTeams(countTeam);
DisplayTeams(teams);
}
private static List<Team> CreatTeams(int num)
{
List<Team> teams = new List<Team>();
for (int i = 0; i < num; i++)
{
Team newTeam = new Team();
//string input = Console.ReadLine();
//if (!input.Select(c => c).Contains('-'))
// break;
string[] line = Console.ReadLine().Split("-", StringSplitOptions.RemoveEmptyEntries).ToArray();
string creator = line[0];
string team = line[1];
if (!teams.Select(team => team.TeamName).Contains(team))
{
if (!teams.Select(team => team.Creator).Contains(creator))
{
newTeam.Add(creator, team);
teams.Add(newTeam);
Console.WriteLine($"Team {team} has been created by {creator}!");
}
else
Console.WriteLine($"{creator} cannot create another team!");
}
else
Console.WriteLine($"Team {team} was already created!");
}
teams = CreateMembers(teams).ToList();
return teams;
}
private static List<Team> CreateMembers(List<Team> teams)
{
Team newMember = new Team();
string line = string.Empty;
while ((line = Console.ReadLine()) != "end of assignment")
{
string[] newLine = line.Split("->", StringSplitOptions.RemoveEmptyEntries).ToArray();
string member = newLine[0];
string team = newLine[1];
if (!teams.Select(team => team.TeamName).Contains(team))
Console.WriteLine($"Team {team} does not exist!");
else if (teams.Select(team => team.Creator).Contains(member) ||
teams.Select(team => team.Member).Any(m => m.Contains(member)))
Console.WriteLine($"Member {member} cannot join team {team}!");
else
{
int creatorIndex = teams.FindIndex(i => i.TeamName == team);
teams[creatorIndex].Member.Add(member);
}
//if (teams.Select(team => team.TeamName).Contains(team))
//{
// if (!teams.Select(team => team.Creator).Contains(member) &&
// !teams.Select(team => team.Member).Any(m => m.Contains(member)))
// {
// int creatorIndex = teams.FindIndex(i => i.TeamName == team);
// teams[creatorIndex].Member.Add(member);
// }
// else
// Console.WriteLine($"Member {member} cannot join team {team}!");
//}
//else
// Console.WriteLine($"Team {team} does not exist!");
}
return teams;
}
static void DisplayTeams(List<Team> teams)
{
var disbandList = teams.OrderBy(x => x.TeamName).Where(x => x.Member.Count == 0);
teams = teams.OrderByDescending(x => x.Member.Count)
.ThenBy(x => x.TeamName)
.OrderBy(x => x.TeamName)
.Where(x => x.Member.Count > 0)
.ToList();
//teams.Sort((x, y) => y.Member.Count.CompareTo(x.Member.Count)); //descending
//teams.Sort((x, y) => x.TeamName.CompareTo(y.TeamName)); //ascending
foreach (var team in teams.Where(t => t.Member.Count > 0))
{
Console.WriteLine($"{team.TeamName}");
Console.WriteLine($"- {team.Creator}");
team.Member.Sort();
foreach (var member in team.Member)
{
Console.WriteLine($"-- {member}");
}
}
Console.WriteLine("Teams to disband:");
foreach (var member in disbandList)
{
Console.WriteLine($"{member.TeamName}");
}
}
}
class Team
{
public string TeamName { get; set; }
public string Creator { get; set; }
public List<string> Member { get; set; } = new List<string>();
public void Add(string user, string team)
{
this.TeamName = team;
this.Creator = user;
}
}
}
Благодаря много. Престаралсъм се с тези проверки и сортиране :). Благодаря и за коментара....реших да карам стъпка по стъпка, та като си търся работа да имам обективна и професионална /SoftUni/ оценка на знанията ми. Така подкарах C# от бейсик, сега фундаментал, WordPress изкарах и есентиналс и Dev.
Благодаря за помоща!
Having urged you to move to another region when you delayed down out, it is in like way key to add a changing remark to request that you battle through the issue in your undertaking. I don't construe that you should oblige making when unquestionably you may need to uncover some basic upgrades or do genuinely more research on a given point.
By: https://articleoftheweek.com/2020/02/17/is-chewing-ice-bad-for-your-health-and-teeth/