Created

Embed URL

HTTPS clone URL

SSH clone URL

You can clone with HTTPS or SSH.

Download Gist

LCD numbers kata (C#)

View LCD.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
using System.Collections.Generic;
using System.Linq;
 
namespace ViveCodigo.Katas.LCD
{
/// <summary>
/// Basado en la idea original de @rodrigo_salado
/// </summary>
public class LCD
{
/// <summary>
/// Diccionario que define los diferentes símbolos para pintar los números.
/// </summary>
private static readonly Dictionary<int, string> Leds =
new Dictionary<int, string>
{
{0, " "},
{1, " _ "},
{2, "| |"},
{3, "|_|"},
{5, " _|"},
{6, "|_ "},
{7, " |"},
{8, "| "},
};
 
/// <summary>
/// Diccionario que define los diferentes dígitos disponibles.
/// </summary>
private static readonly Dictionary<char, LCD> Digitos =
new Dictionary<char, LCD>
{
{'0', new LCD(1, 2, 3)},
{'1', new LCD(0, 7, 7)},
{'2', new LCD(1, 5, 6)},
{'3', new LCD(1, 5, 5)},
{'4', new LCD(0, 3, 7)},
{'5', new LCD(1, 6, 5)},
{'6', new LCD(1, 6, 3)},
{'7', new LCD(1, 7, 7)},
{'8', new LCD(1, 3, 3)},
{'9', new LCD(1, 3, 5)}
};
 
/// <summary>
/// Arreglo que contiene la representación de cadena del dígito.
/// </summary>
private readonly string[] filas;
 
private LCD(int top, int mid, int bot) :
this(new[] {Leds[top], Leds[mid], Leds[bot]}) {}
 
private LCD(string[] filas)
{
this.filas = filas;
}
 
private LCD()
{
filas = new[] { "", "", "" };
}
 
public override string ToString()
{
return string.Join("\n", filas);
}
 
public static LCD operator +(LCD a, LCD b)
{
return new LCD(a.filas.Select((t, i) => t + b.filas[i]).ToArray());
}
 
public static string ToLCD(int numero)
{
var result = new LCD();
foreach (var digito in numero.ToString())
{
result += Digitos[digito];
}
return result.ToString();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.