Ad

Transform a number so it resembles digits like on a digital clock. (lcd display)
Each digit is a 3 x 3 square.
You may only use the symbols | and _
If a number consists of more than 1 digit, each digit is separated by 2 spaces

ex:

 1 --> "   \n  |\n  |\n"
11 --> "        \n  |    |\n  |    |\n"

Maybe it grows to a series, things that come to mind :

  • From lcd back to digit
  • error detection
  • error correction
  • ...
using System;
using System.Collections.Generic;
using System.Text;

public class LCD
    {
        const string SpacingBetween2Numbers = "  ";
        private static string[] nr1 = { "   ", "  |", "  |" };
        private static string[] nr2 = { " _ ", " _|", "|__" };
        private static string[] nr3 = { "__ ", "__|", "__|" };
        private static string[] nr4 = { "   ", "|_|", "  |" };
        private static string[] nr5 = { " __", "|_ ", "__|" };
        private static string[] nr6 = { "   ", "|_ ", "|_|" };
        private static string[] nr7 = { "__ ", "  |", "  |" };
        private static string[] nr8 = { " _ ", "|_|", "|_|" };
        private static string[] nr9 = { " _ ", "|_|", "  |" };
        private static string[] nr0 = { " _ ", "| |", "|_|" };

        private static Dictionary<int, string[]> Lookup = new Dictionary<int, string[]>();

        static LCD()
        {
            Lookup.Add(1, nr1);
            Lookup.Add(2, nr2);
            Lookup.Add(3, nr3);
            Lookup.Add(4, nr4);
            Lookup.Add(5, nr5);
            Lookup.Add(6, nr6);
            Lookup.Add(7, nr7);
            Lookup.Add(8, nr8);
            Lookup.Add(9, nr9);
            Lookup.Add(0, nr0);
        }


        public static string ToLcd(int number)
        {
            var numbersToPrint = new List<int>();

            foreach (var item in number.ToString())
                numbersToPrint.Add(int.Parse(item.ToString()));

            var result = new StringBuilder();

            for (int rowindex = 0; rowindex < 3; rowindex++)
            {
                for (int currentNumberIndex = 0; currentNumberIndex < numbersToPrint.Count; currentNumberIndex++)
                {
                    var currentNumber = numbersToPrint[currentNumberIndex];

                    result.Append(Lookup[currentNumber][rowindex]);

                    if (currentNumberIndex < numbersToPrint.Count - 1) result.Append(SpacingBetween2Numbers);
                }
                result.Append("\n");
            }

            return result.ToString();
        }


    }