26 April 2017

Add Two Digits

My solution:
int addTwoDigits(int n) 
{
    return n % 10 + n / 10;
}

Previous Next

Sudoku

My solution:
bool sudoku(int[][] grid) 
{
    var horizontalLists = new List<List<int>>();
    var verticalLists = new List<List<int>>();
    var clustersLists = new List<List<int>>();
    for (var y = 0; y < 9; y++)
        verticalLists.Add(grid[y].ToList());

    for (var x = 0; x < 9; x++)
    {
        var horizontalList = new List<int>();
        for (var y = 0; y < 9; y++)
            horizontalList.Add(grid[y][x]);

        horizontalLists.Add(horizontalList);
    }

    for (var x = 0; x < 9; x += 3)
        for (var y = 0; y < 9; y += 3)
        {
            var clustersList = new List<int>();
            clustersList.Add(grid[y][x]);
            clustersList.Add(grid[y + 1][x]);
            clustersList.Add(grid[y + 2][x]);
            clustersList.Add(grid[y][x + 1]);
            clustersList.Add(grid[y + 1][x + 1]);
            clustersList.Add(grid[y + 2][x + 1]);
            clustersList.Add(grid[y][x + 2]);
            clustersList.Add(grid[y + 1][x + 2]);
            clustersList.Add(grid[y + 2][x + 2]);
            clustersLists.Add(clustersList);
        }

    return !horizontalLists.Any(
               horizontalList => horizontalList.Any(i => horizontalList.Count(i1 => i1 == i) != 1)) && !verticalLists.Any(
               verticalList => verticalList.Any(i => verticalList.Count(i1 => i1 == i) != 1)) && !clustersLists.Any(
               clustersList => clustersList.Any(i => clustersList.Count(i1 => i1 == i) != 1));
}

Previous Next

spiralNumbers

My solution:
int[][] spiralNumbers(int n) 
{
    var answer = new int[n][];
    for (var i = 0; i < n; i++)
        answer[i] = new int[n];

    var j = 1;
    var start = 0;
    var row = start;
    var col = start;
    var end = n - 1;
    while (j <= n * n)
    {
        // Horizontal to the right
        while (true)
        {
            if (answer[row][col] == 0)
            {
                answer[row][col] = j;
                j++;
            }

            if (col == end)
                break;

            col++;
        }

        // Vertically down
        while (true)
        {
            if (answer[row][col] == 0)
            {
                answer[row][col] = j;
                j++;
            }

            if (row == end)
                break;

            row++;
        }

        // Horizontal to the left
        while (true)
        {
            if (answer[row][col] == 0)
            {
                answer[row][col] = j;
                j++;
            }

            if (col == start)
                break;

            col--;
        }

        // Vertically up
        while (true)
        {
            if (answer[row][col] == 0)
            {
                answer[row][col] = j;
                j++;
            }

            if (row == start)
                break;

            row--;
        }

        row++;
        start++;
        end--;
    }

    return answer;
}

Previous Next

messageFromBinaryCode

My solution:
string messageFromBinaryCode(string code) 
{
    var answer = string.Empty;
    for (var i = 0; i < code.Length / 8; i++)
        answer += (char) (128 * int.Parse(code[i * 8].ToString()) + 64 * int.Parse(code[i * 8 + 1].ToString()) + 32 * int.Parse(code[i * 8 + 2].ToString()) + 16 * int.Parse(code[i * 8 + 3].ToString()) + 8 * int.Parse(code[i * 8 + 4].ToString()) + 4 * int.Parse(code[i * 8 + 5].ToString()) + 2 * int.Parse(code[i * 8 + 6].ToString()) + int.Parse(code[i * 8 + 7].ToString()));

    return answer;
}

Previous Next

File Naming

My solution:
string[] fileNaming(string[] names)
{
    var list = new List<string>();
    foreach (var name in names)
        if (!list.Contains(name))
            list.Add(name);
        else
            for (var i = 1; i < 16; i++)
            {
                if (list.Contains(name + "(" + i + ")")) continue;
                list.Add(name + "(" + i + ")");
                break;
            }

    return list.ToArray();
}

Previous Next

digitsProduct

My solution:
int digitsProduct(int product) 
{
    switch (product)
    {
        case 0:
            return 10;
        case 1:
            return 1;
        default:
            var i = 9;
            var answer = string.Empty;
            var copyOfProduct = product;
            while (product != 1 && i != 1)
            {
                if (product % i == 0)
                {
                    product /= i;
                    answer += i.ToString();
                }
                else
                    i--;
            }

            if (i == 1 && copyOfProduct == product)
                return -1;

            return i == 1 && copyOfProduct == product ? -1 : int.Parse(new string(answer.Reverse().ToArray()));
    }
}

Previous Next

Different Squares

My solution:
int differentSquares(int[][] matrix) 
{
    var two_x_two_s = new HashSet<Tuple<int, int, int, int>>();
    for (var row = 0; row < matrix.Length - 1; row++)
        for (var column = 0; column < matrix[0].Length - 1; column++)
            two_x_two_s.Add(new Tuple<int, int, int, int>(matrix[row][column], matrix[row][column + 1],
                matrix[row + 1][column], matrix[row + 1][column + 1]));

    return two_x_two_s.Count;
}

Previous Next

sumUpNumbers

My solution:
int sumUpNumbers(string inputString) 
{
    return Regex.Matches(inputString, "\\d+").Cast<Match>().Sum(match => int.Parse(match.Value));
}

Previous Next

Valid Time

My solution:
bool validTime(string time) 
{
    return Regex.IsMatch(time, "^([01]\\d|2[0-3]):([0-5]\\d)$");
}

Previous Next

longestWord

My solution:
string longestWord(string text) 
{
    var list = (from Match match in Regex.Matches(text, "[A-Za-z]+") select match.Value).ToList();
    return list.First(s => s.Length == list.Select(s1 => s1.Length).Max());
}

Previous Next