12 April 2017

deleteDigit

My solution:
int deleteDigit(int n) 
{
    return int.Parse(n.ToString().Select((c, i) => n.ToString().Remove(i, 1)).Max());;
}

Previous Next

chessKnight

My solution:
int chessKnight(string cell) 
{
    var possible_moves = new List<string>();

    // Upward
    if (int.Parse(cell[1].ToString()) + 2 <= 8)
    {
        if (cell[0] >= 'b')
            possible_moves.Add((char)(cell[0] - 1) + ((char) (cell[1] + 2)).ToString());

        if (cell[0] <= 'g')
            possible_moves.Add((char)(cell[0] + 1) + ((char)(cell[1] + 2)).ToString());
    }

    // Rightward
    if (cell[0] <= 'f')
    {
        if (int.Parse(cell[1].ToString()) <= 7)
            possible_moves.Add((char)(cell[0] + 2) + ((char)(cell[1] + 1)).ToString());

        if (int.Parse(cell[1].ToString()) >= 2)
            possible_moves.Add((char)(cell[0] + 2) + ((char)(cell[1] - 1)).ToString());
    }

    // Downward
    if (int.Parse(cell[1].ToString()) - 2 >= 1)
    {
        if (cell[0] >= 'b')
            possible_moves.Add((char)(cell[0] - 1) + ((char)(cell[1] - 2)).ToString());

        if (cell[0] <= 'g')
            possible_moves.Add((char)(cell[0] + 1) + ((char)(cell[1] - 2)).ToString());
    }

    // Leftward
    if (cell[0] >= 'c')
    {
        if (int.Parse(cell[1].ToString()) <= 7)
            possible_moves.Add((char)(cell[0] - 2) + ((char)(cell[1] + 1)).ToString());

        if (int.Parse(cell[1].ToString()) >= 2)
            possible_moves.Add((char)(cell[0] - 2) + ((char)(cell[1] - 1)).ToString());
    }

    return possible_moves.Count;
}

Previous Next

lineEncoding

My solution:
string lineEncoding(string s) 
{
    var c = new char();
    var answer = string.Empty;
    var counter = 0;
    for (var i = 0; i < s.Length; i++)
    {
        if (c == '\0')
        {
            c = s[i];
            counter++;
        }
        else if (c == s[i])
        {
            counter++;
            if (i == s.Length - 1)
                answer += counter.ToString() + c;
        }
        else
        {
            if (counter > 1)
                answer += counter.ToString() + c;
            else
                answer += c;

            c = s[i];
            counter = 1;
            if (i == s.Length - 1)
                answer += c;
        }
    }

    return answer;
}

Previous Next

isDigit

My solution:
bool isDigit(char symbol) 
{
    return char.IsDigit(symbol);
}

Previous Next