D2L Interview Question
Software Engineer / DevelopersCountry: United States
Interview Type: In-Person
static long ReadDigitalNumber(string file)
{
char[,] digitalEight = new char[,] {
{ '|', '-', '|'},
{ '|', '-', '|'},
{ '|', '_', '|'},
};
char[,] digitalSix = new char[,] {
{ '|', '-', '-'},
{ '|', '-', '-'},
{ '|', '_', '|'},
};
char[,] digitalThree = new char[,] {
{ '-', '-', '|'},
{ '_', '_', '|'},
{ '_', '_', '|'},
};
List<char[,]> numbers = new List<char[,]>(3);
string[] lines = File.ReadAllLines(file);
int x = 0, y = 0, index = 0;
foreach (string line in lines)
{
index = 0;
for (int i = 0; i < line.Length; i++) {
if (i != (line.Length - 1) && (i % 3) == 0)
{
if (i != 0)
index++;
y = 0;
if (numbers.Count < (index + 1))
numbers.Add(new char[3, 3]);
}
numbers[index][x,y] = line[i];
y++;
}
x++;
}
long result = 0;
numbers.ForEach(digit =>
{
int[] match = new int[10];
for (int i = 0; i < digit.GetLength(0); i++) {
for (int j = 0; j < digit.GetLength(1); j++) {
if (digit[i, j] == digitalEight[i, j])
match[8]++;
if (digit[i, j] == digitalSix[i, j])
match[6]++;
if (digit[i, j] == digitalThree[i, j])
match[3]++;
}
}
if (match[8] == digit.GetLength(0) * digit.GetLength(1))
{
if (result > 0)
result *= 10;
result += 8;
}
if (match[6] == digit.GetLength(0) * digit.GetLength(1))
{
if (result > 0)
result *= 10;
result += 6;
}
if (match[3] == digit.GetLength(0) * digit.GetLength(1))
{
if (result > 0)
result *= 10;
result += 3;
}
});
return result;
}
Set up a map of the symbol (just 9 characters in a row, left/right, top/bottom) to digit.
Read the input file, store the 3 keys you are looking for by appending to each key in a list (column / 3 tells you which one you are appending too).
I assumed no space in the input file between numbers, so it is a 9x9 file input file exactly.
./lednumbers.py led_image
860
- Dan August 18, 2014