Me han dado la siguiente pregunta .NET en una entrevista. No sé por qué tengo bajas calificaciones. Lamentablemente no recibí ningún comentario.Entrevista .NET, estructura de código y el diseño
Pregunta:
El hockey.csv archivo contiene los resultados de la Premier League de hockey. Las columnas 'Por' y 'Contra' contienen el número total de goles marcados a favor y en contra de cada equipo en esa temporada (por lo que Alabama anotó 79 goles contra oponentes, y anotó 36 goles en contra de ellos).
Escriba un programa para imprimir el nombre del equipo con la menor diferencia en los objetivos "a favor" y "en contra".
la estructura de la hockey.csv se parece a esto (que es un archivo CSV válido, pero acabo de copiar los valores aquí para tener una idea)
equipo - Para - Contra
Alabama 79 36
Washinton 67 30
Indiana 87 45
Newcastle 74 52
Florida 53 37
Nueva York 46 47
Sunderland 29 51
Lova 41 64
Nevada 33 63
Boston 30 64
Nevada 33 63
Boston 30 64
Solución:
class Program
{
static void Main(string[] args)
{
string path = @"C:\Users\<valid csv path>";
var resultEvaluator = new ResultEvaluator(string.Format(@"{0}\{1}",path, "hockey.csv"));
var team = resultEvaluator.GetTeamSmallestDifferenceForAgainst();
Console.WriteLine(
string.Format("Smallest difference in ‘For’ and ‘Against’ goals > TEAM: {0}, GOALS DIF: {1}",
team.Name, team.Difference));
Console.ReadLine();
}
}
public interface IResultEvaluator
{
Team GetTeamSmallestDifferenceForAgainst();
}
public class ResultEvaluator : IResultEvaluator
{
private static DataTable leagueDataTable;
private readonly string filePath;
private readonly ICsvExtractor csvExtractor;
public ResultEvaluator(string filePath){
this.filePath = filePath;
csvExtractor = new CsvExtractor();
}
private DataTable LeagueDataTable{
get
{
if (leagueDataTable == null)
{
leagueDataTable = csvExtractor.GetDataTable(filePath);
}
return leagueDataTable;
}
}
public Team GetTeamSmallestDifferenceForAgainst() {
var teams = GetTeams();
var lowestTeam = teams.OrderBy(p => p.Difference).First();
return lowestTeam;
}
private IEnumerable<Team> GetTeams() {
IList<Team> list = new List<Team>();
foreach (DataRow row in LeagueDataTable.Rows)
{
var name = row["Team"].ToString();
var @for = int.Parse(row["For"].ToString());
var against = int.Parse(row["Against"].ToString());
var team = new Team(name, against, @for);
list.Add(team);
}
return list;
}
}
public interface ICsvExtractor
{
DataTable GetDataTable(string csvFilePath);
}
public class CsvExtractor : ICsvExtractor
{
public DataTable GetDataTable(string csvFilePath)
{
var lines = File.ReadAllLines(csvFilePath);
string[] fields;
fields = lines[0].Split(new[] { ',' });
int columns = fields.GetLength(0);
var dt = new DataTable();
//always assume 1st row is the column name.
for (int i = 0; i < columns; i++)
{
dt.Columns.Add(fields[i].ToLower(), typeof(string));
}
DataRow row;
for (int i = 1; i < lines.GetLength(0); i++)
{
fields = lines[i].Split(new char[] { ',' });
row = dt.NewRow();
for (int f = 0; f < columns; f++)
row[f] = fields[f];
dt.Rows.Add(row);
}
return dt;
}
}
public class Team
{
public Team(string name, int against, int @for)
{
Name = name;
Against = against;
For = @for;
}
public string Name { get; private set; }
public int Against { get; private set; }
public int For { get; private set; }
public int Difference
{
get { return (For - Against); }
}
}
Salida: La diferencia más pequeña en for' and
contra objetivos'> Equipo: Boston, OBJETIVOS DIF: -34
Puede alguien por favor revisar mi código y ver algo obviamente mal aquí? Solo estaban interesados en la estructura/diseño del código y si el programa produce el resultado correcto (es decir, la diferencia más baja). Muy apreciado.
¿Cómo sabes que tienes bajas calificaciones si no recibes ningún comentario? A veces, otra persona simplemente se adapta mejor para el puesto, eso es todo. –
Por curiosidad. ¿Cuánto tiempo te dieron para completar esta pregunta? –
@ClaudioRedi eso es lo que también me sorprende, simplemente me dieron mala nota y dijeron que no tuvieron éxito en esta etapa. No dije por qué. –