27 April 2017

Metro Card

My solution:
int[] metroCard(int lastNumberOfDays) 
{
    return lastNumberOfDays == 28 || lastNumberOfDays == 30 ? new [] { 31 } : new [] { 28, 30, 31 };
}

Previous Next

Will You?

My solution:
bool willYou(bool young, bool beautiful, bool loved) 
{
    return (young && beautiful && !loved) || ((!young || !beautiful) && loved);
}

Previous Next

Tennis Set

My solution:
bool tennisSet(int score1, int score2) 
{
    return (Math.Min(score1, score2) < 5 && Math.Max(score1, score2) == 6) || ((Math.Min(score1, score2) == 5 || Math.Min(score1, score2) == 6) && Math.Max(score1, score2) == 7);
}

Previous Next

Arithmetic Expression

My solution:
bool arithmeticExpression(int A, int B, int C) 
{
    return A + B == C || A - B == C || A * B == C || A / B == C && A % B == 0;
}

Previous Next

Is Infinite Process?

My solution:
bool isInfiniteProcess(int a, int b) 
{
    return a > b || Math.Abs(a - b) % 2 == 1;
}

Previous Next

Extra Number

My solution:
int extraNumber(int a, int b, int c) 
{
    return (a == b) ? c : (a == c) ? b : a;
}

Previous Next

Knapsack Light

My solution:
int knapsackLight(int value1, int weight1, int value2, int weight2, int maxW) 
{
    if (weight1 + weight2 <= maxW)
        return value1 + value2;
    else if (weight1 > maxW && weight2 > maxW)
        return 0;
    else if (weight1 > maxW)
        return value2;
    else if (weight2 > maxW)
        return value1;
    else
        return value1 > value2 ? value1 : value2;
}

Previous Next

Reach Next Level

My solution:
bool reachNextLevel(int experience, int threshold, int reward) 
{
    return experience + reward >= threshold;
}

Previous Next

Phone Call

My solution:
int phoneCall(int min1, int min2_10, int min11, int s) 
{
    var min = 0;
    while (s > 0)
    {
        if (min == 0)
        {
            if (s >= min1)
            {
                s -= min1;
                min = 1;
            }
            else
                s = 0;
        }
        else if (min == 1)
        {
            if (s >= min2_10 * 9)
            {
                min = 10;
                s -= min2_10 * 9;
            }
            else if (s < min2_10)
            {
                min = 2;
                s = 0;
            }
            else
            {
                min += s / min2_10;
                s = 0;
            }
            
        }
        else if (min == 10)
        {
            if (s < min11)
                min++;
            else
                min += s / min11;
            
            s = 0;
            
        }
    }
    
    return min;
}

Previous Next

Late Ride

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

Previous Next

Circle Of Numbers

My solution:
int circleOfNumbers(int n, int firstNumber) 
{
    return (firstNumber + n / 2) % n;
}

Previous Next

Max Multiple

My solution:
int maxMultiple(int divisor, int bound) 
{
    return (bound / divisor) * divisor;
}

Previous Next

Seats In Theater

My solution:
int seatsInTheater(int nCols, int nRows, int col, int row) 
{
    return (nCols - col + 1) * (nRows - row);
}

Previous Next

Candies

My solution:
int candies(int n, int m) 
{
    return m / n * n;
}

Largest Number

My solution:
int largestNumber(int n) 
{
    return int.Parse(new string('9', n));
}

Previous Next

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

25 April 2017

Hanzi Week 3

Characters and Words:

Week 3
Lesson 9:
1. xué  2. shēng (shēng hái zi, xué sheng, nǚ shēng)  3.  (xué xí)  4. lǎo (lǎo rén)  5. shī (lǎo shī)  6. wén (wén xué, wén zǐ, zuò wén)

Lesson 10:
1.  (dà rén, dà xué, dà xué shēng)  2. xiǎo (xiǎo háir, xiǎo xué, xiǎo xué shēng, dà xiǎo)  3. duō  4. shǎo (duō shao)  5. hǎo (hǎo duō, hǎo rén)  6. huài (huài rén., huài hǎo)

Lesson 11:
1. lái  2.   3. jìn (jìn lai, jìn qu)  4. chū (chū lai, chū qu, chū shēng)  5. kāi (kāi xué)  6. guān (kāi guān)

Lesson 12:
1. cháng  2. duǎn (cháng duǎn)  3. gāo (gāo gè zi)  4. xìng (gāo xìng)  5. xīn (xīn shēng)  6. jiù (xīn jiù)

Previous
from Coursera Chinese Characters for beginner 汉字 by Peking University
Link: https://www.coursera.org/learn/hanzi/home

22 April 2017

Hanzi Week 2

Characters and Words:

Week 2
Lesson 5:
1.   2.   3. men (rén men, nǐ men, tā men)  4. zuò (gōng zuò)  5. shén  6. me (shén me)

Lesson 6:
1.  (nǚ rén) 2.  (tā men)  3.  (mā ma)  4. jiě (jiě jie)  5. mèi (mèi mei, jiě mèi)  6. nǎi (nǎi nai)

Lesson 7:
1.   2. qīn (fù qin)  3.  (bà ba)  4.  (yé ye)  5.  (mǔ qin, fù mǔ)  6.  (wǒ men)

Lesson 8:
1. ér (nǚ ér)  2.  (ér zi, gè zi, zǐ nǚ)  3. sūn (sūn zi, sūn nü)  4. hái (hái zi, nü háir)  5. míng  6.  ( míng zi)

Previous Next
from Coursera Chinese Characters for beginner 汉字 by Peking University
Link: https://www.coursera.org/learn/hanzi/home

21 April 2017

Hanzi Week 1

Characters and Words:

Week 1
Lesson 1:
1.   2. èr  3. sān  4. liù  5.   6. shí (shí yī, shí èr, shí sān, shí liù, shí bā, èr shí, sān shí, liù shí, bā shí)

Lesson 2:
1.  (shí sì, sì shí)  2.  (shí wǔ, wǔ shí)  3.  (shí qī, qī shí)  4. jiǔ (shí jiǔ, jiǔ shí) 5.   6. liǎng (liǎng gè)

Lesson 3:
1.  (jǐ gè)  2. bàn (bàn gè, yí bàn)  3. bǎi (yì bǎi, sān bǎi)  4. qiān (yì qiān, wǔ qiān)  5. wàn (yí wàn)  6. rén

Lesson 4:
1. gōng (gōng rén) 2. gān  3. tiān (yì tiān, liǎng tiān, bàn tiān)  4. jīn (jīn tiān) 5,   6. zhàng (zhàng fu)

Next
from Coursera Chinese Characters for beginner 汉字 by Peking University
Link: https://www.coursera.org/learn/hanzi/home

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

11 April 2017

Is MAC48 Address?

My solution:
bool isMAC48Address(string inputString) 
{
    return Regex.IsMatch(inputString, "^([0-9A-F]{2}-){5}[0-9A-F]{2}$");
}

Previous Next

Elections Winners

My solution:
int electionsWinners(int[] votes, int k) 
{
    var max = votes.Max();
    return k == 0 && votes.Where(d => d == votes.Max()).Count() == 1 ? 1 : votes.Count(t => t + k > max);
}

Previous Next

buildPalindrome

My solution:
string buildPalindrome(string st) 
{
    var i = 0;
    while (!st.Substring(i).ToArray().SequenceEqual(st.Substring(i).Reverse().ToArray()) && i < st.Length)
        i++;

    return st + new string(st.Substring(0, i).Reverse().ToArray());
}

Previous Next

Find Email Domain

My solution:
string findEmailDomain(string address) 
{
    return Regex.Match(address, "@[A-z0-9\\.-]+$").Value.Substring(1);
}

Previous Next

isBeautifulString

My solution:
bool isBeautifulString(string inputString) 
{
    var sorted = Enumerable.Range('a', inputString.Max(c => c) - 'a' + 1).Select(i => (char)i).ToArray();
    for (var i = 1; i < sorted.Length; i++)
        if (inputString.Count(c => c == sorted[i]) > inputString.Count(c => c == sorted[i - 1]))
            return false;

    return true;
}
Terms of use: http://stackoverflow.com/a/314508 by http://stackoverflow.com/users/4685/pop-catalin, modified under https://creativecommons.org/licenses/by-sa/3.0/.

Previous Next

08 April 2017

Bishop and Pawn

My solution:
bool bishopAndPawn(string bishop, string pawn) 
{
    return Math.Abs(bishop[1] - pawn[1]).Equals(Math.Abs(bishop[0] - pawn[0]));
}

Previous

digitDegree

My solution:
int digitDegree(int n) 
{
    if (n < 10)
        return 0;

    var times = 0;
    var sum = n.ToString().Select(x => int.Parse(x.ToString())).Sum();
    while (sum >= 10 || n >= 10)
    {
        times++;
        n = sum;
        sum = n.ToString().Select(x => int.Parse(x.ToString())).Sum();
    }

    return times;
}

Previous Next

longestDigitsPrefix

My solution:
string longestDigitsPrefix(string inputString) 
{
    return Regex.Match(inputString, "^\\d+").Value;
}

Previous Next

Knapsack Light

My solution:
int knapsackLight(int value1, int weight1, int value2, int weight2, int maxW) 
{
    if (weight1 + weight2 <= maxW)
        return value1 + value2;
    else if (weight1 > maxW && weight2 > maxW)
        return 0;
    else if (weight1 <= maxW && weight2 > maxW)
        return value1;
    else if (weight2 <= maxW && weight1 > maxW)
        return value2;
    else if (value1 >= value2)
        return value1;
    else if (value1 < value2)
        return value2;
    
    return 0;
}

Previous Next

growingPlant

My solution:
int growingPlant(int upSpeed, int downSpeed, int desiredHeight) 
{
    var days = 0;
    
    // Day = true, Night = false.
    var isday = true;
    while (desiredHeight > 0)
        if (isday)
        {
            desiredHeight -= upSpeed;
            days++;
            isday = false;
        }
        else
        {
            desiredHeight += downSpeed;
            isday = true;
        }
    
    return days;
}

Previous Next

arrayMaxConsecutiveSum

My solution:
int arrayMaxConsecutiveSum(int[] inputArray, int k) 
{
    var max = 0;
    var rem = 0;
    for (var i = 0; i <= inputArray.Length - k; i++)
    {
        if (i == 0)
            rem = inputArray.Where((i1, i2) => i2 >= i && i2 < i + k).Sum();
        else
            rem = rem + inputArray[i + k - 1] - inputArray[i - 1];

        max = Math.Max(rem, max);
    }

    return max;
}

Previous Next

differentSymbolsNaive

My solution:
int differentSymbolsNaive(string s) 
{
    return s.Distinct().Count();
}

Previous Next

firstDigit

My solution:
char firstDigit(string inputString) 
{
    return Regex.Match(inputString, "\\d{1}").Value[0];
}

Previous Next

extractEachKth

My solution:
int[] extractEachKth(int[] inputArray, int k) 
{
    return inputArray.Where((i, i1) => i1 % k != k - 1).ToArray();
}

Previous Next

01 April 2017

stringsRearrangement

My solution:
bool stringsRearrangement(string[] inputArray) 
{
    IEnumerable<IEnumerable<int>> permutations =
    GetPermutations(Enumerable.Range(1, inputArray.Length), inputArray.Length);
    Dictionary<int, bool> dictionary = new Dictionary<int, bool>();
    int key = 0;
    foreach (IEnumerable<int> intArray in permutations)
    {
        key++;
        List<int> intList = intArray.ToList();
        for (int i = 0; i < intList.Count - 1; i++)
        {
            int diffcount = 0;
            for (int j = 0; j < inputArray[intList[i] - 1].Length; j++)
                if (inputArray[intList[i] - 1][j] != inputArray[intList[i + 1] - 1][j])
                    diffcount++;

            if (diffcount != 1)
            {
                dictionary.Add(key, false);
                break;
            }
        }

        if (!dictionary.ContainsKey(key))
            dictionary.Add(key, true);
    }

    return dictionary.Any(x => x.Value);
}

IEnumerable<IEnumerable<T>> GetPermutations<T>(IEnumerable<T> list, int length)
{
    if (length == 1) return list.Select(t => (IEnumerable<T>)new T[] { t });

    return GetPermutations(list, length - 1)
        .SelectMany(t => list.Where(e => !t.Contains(e)),
            (t1, t2) => t1.Concat(new T[] { t2 }));
}
Terms of use: Method by pengyang, used under CC BY-SA 3.0

Previous Next

absoluteValuesSumMinimization

My solution:
int absoluteValuesSumMinimization(int[] A) 
{
    int smallest = int.MaxValue;
    int x = 0;
    foreach (int i in A)
    {
        if (A.Sum(t => Math.Abs(t - i)) >= smallest) continue;
        smallest = A.Sum(t => Math.Abs(t - i));
        x = i;
    }

    return x;
}

Previous Next

depositProfit

My solution:
int depositProfit(int deposit, int rate, int threshold) 
{
    return Convert.ToInt32(Math.Ceiling(Math.Log(Convert.ToDouble(threshold) / Convert.ToDouble(deposit), 1 + Convert.ToDouble(rate) / 100)));
}

Previous Next

Circle of Numbers

My solution:
int circleOfNumbers(int n, int firstNumber) 
{
    return (firstNumber + n / 2) % n;
}

Previous Next

chessBoardCellColor

My solution:
bool chessBoardCellColor(string cell1, string cell2) 
{
    return (cell1[0] - 'A' % 2 + cell1[1] - '1' % 2) % 2 == (cell2[0] - 'A' % 2 + cell2[1] - '1' % 2) % 2;
}

Previous Next

alphabeticShift

My solution:
string alphabeticShift(string inputString) 
{
    return new string(inputString.ToCharArray().Select(x => x == 'z' ? 'a' : (char)(x + 1)).ToArray());
}

Previous Next

variableName

My solution:
bool variableName(string name)
{
    if (Regex.IsMatch(name, "^\\d"))
        return false;
    if (name.All(c => ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')) || char.IsDigit(c) || c == '_'))
        return true;

    return false;
}

Previous Next

evenDigitsOnly

My solution:
bool evenDigitsOnly(int n)
{
    return n.ToString().All(c => int.Parse(c.ToString()) % 2 == 0);
}

Previous Next

Array Replace

My solution:
int[] arrayReplace(int[] inputArray, int elemToReplace, int substitutionElem)
{
    for (int i = 0; i < inputArray.Length; i++)
        if (inputArray[i] == elemToReplace)
            inputArray[i] = substitutionElem;

    return inputArray;
}

Previous Next

Minesweeper

My solution:
int[][] minesweeper(bool[][] matrix) 
{
    int rows = matrix.Length;
    int columns = matrix[0].Length;
    int[][] solution = new int[rows][];
    for (var index = 0; index < solution.Length; index++)
        solution[index] = new int[columns];

    for (int i = 0; i < rows; i++)
        for (int j = 0; j < columns; j++)
        {
            int count = 0;
            if (i - 1 >= 0)
            {
                if (j - 1 >= 0)
                    if (matrix[i - 1][j - 1])
                        count++;
                if (j + 1 >= 0 & j + 1 < columns)
                    if (matrix[i - 1][j + 1])
                        count++;
                if (matrix[i - 1][j])
                    count++;
            }

            if (i + 1 >= 0 && i + 1 < rows)
            {
                if (j - 1 >= 0)
                    if (matrix[i + 1][j - 1])
                        count++;
                if (j + 1 >= 0 & j + 1 < columns)
                    if (matrix[i + 1][j + 1])
                        count++;
                if (matrix[i + 1][j])
                    count++;
            }

            if (j - 1 >= 0)
                if (matrix[i][j - 1])
                    count++;
            if (j + 1 >= 0 & j + 1 < columns)
                if (matrix[i][j + 1])
                    count++;

            solution[i][j] = count;
        }

    return solution;
}

Previous Next

Box Blur

My solution:
int[][] boxBlur(int[][] image) 
{
    int rows = image.Length;
    int columns = image[0].Length;
    int[][] solution = new int[rows - 2][];
    for (int index = 0; index < solution.Length; index++)
        solution[index] = new int[columns - 2];

    for (int i = 0; i < rows - 2; i++)
        for (int j = 0; j < columns - 2; j++)
            solution[i][j] = (image[i][j] + image[i][j + 1] + image[i][j + 2] + image[i + 1][j] + image[i + 1][j + 1] + image[i + 1][j + 2] + image[i + 2][j] + image[i + 2][j + 1] + image[i + 2][j + 2]) / 9;

    return solution;
}

Previous Next

avoidObstacles

My solution:
int avoidObstacles(int[] inputArray) 
{
    for (int i = 2; ; i++)
        if (!inputArray.Any(x => x % i == 0))
            return i;
}

Previous Next

isIPv4Address

My solution:
bool isIPv4Address(string inputString) 
{
    return Regex.IsMatch(inputString,
                @"^([1][0-9][0-9]|[2][0-4][0-9]|[2][5][0-5]|[1-9][0-9]|[0-9])\.([1][0-9][0-9]?|[2][0-4][0-9]|[2][5][0-5]|[1-9][0-9]|[0-9])\.([1][0-9][0-9]?|[2][0-4][0-9]|[2][5][0-5]|[1-9][0-9]|[0-9])\.([1][0-9][0-9]?|[2][0-4][0-9]|[2][5][0-5]|[1-9][0-9]|[0-9])$");
}

Previous Next

arrayMaximalAdjacentDifference

My solution:
int arrayMaximalAdjacentDifference(int[] inputArray) 
{
    return inputArray.Take(inputArray.Length - 1).Select((a, i) => Math.Abs(inputArray[i + 1] - a)).Max();
}

Previous Next

areEquallyStrong

My solution:
bool areEquallyStrong(int yourLeft, int yourRight, int friendsLeft, int friendsRight) 
{
    return Math.Max(yourLeft, yourRight) == Math.Max(friendsLeft, friendsRight) && Math.Min(yourLeft, yourRight) == Math.Min(friendsLeft, friendsRight);
}

Previous Next

palindromeRearranging

My solution:
bool palindromeRearranging(string inputString)
{
    if (inputString.Length % 2 == 0 && inputString.Distinct().Any(c => inputString.Count(x => x == c) % 2 != 0))
        return false;
    if (inputString.Length % 2 == 1)
    {
        int count = 0;
        foreach (var c in inputString.Distinct())
        {
            if (inputString.Count(x => x == c) % 2 != 0)
                count++;
            if (count > 1)
                return false;
        }
    }

    return true;
}

Previous Next

arrayChange

My solution:
int arrayChange(int[] inputArray)
{
    int change = 0;
    for (int i = 0; i < inputArray.Length - 1; i++)
        while (inputArray[i] >= inputArray[i + 1])
        {
            inputArray[i + 1]++;
            change++;
        }

    return change;
}

Previous Next

Are Similar?

My solution:
bool areSimilar(int[] A, int[] B) 
{
    if (A.Where((t, i) => t != B[i]).Count() > 2)
        return false;

    return A.Distinct().OrderBy(x => x).SequenceEqual(B.Distinct().OrderBy(x => x).ToArray());
}

Previous Next

Add Border

My solution:
string[] addBorder(string[] picture)
{
    return new[] { new string('*', picture.Max(x => x.Length) + 2) }.Concat(picture.Select(x => "*" + x + "*")).Concat(new[] { new string('*', picture.Max(x => x.Length) + 2) }).ToArray();
}

Previous Next

alternatingSums

My solution:
int[] alternatingSums(int[] a)
{
    return new[] { a.Where((b, c) => c % 2 == 0).Sum(), a.Where((b, c) => c % 2 == 1).Sum() };
}

Previous Next

reverseParentheses

My solution:
string reverseParentheses(string s)
{
    string cs = s;
    while (cs.Contains("("))
    {
        string r = Regex.Match(cs, "\\([^\\(\\)]+\\)").Value;
        string r2 = r.Remove(r.Length - 1, 1);
        r2 = r2.Remove(0, 1);
        string r3 = string.Concat(r2.Reverse());
        cs = cs.Replace(r, r3);
    }

    return cs;
}

Previous Next

Sort by Height

My solution:
int[] sortByHeight(int[] a)
{
    int b = 0;
    return a.Select(c => c == -1 ? c : a.Where(d => d != -1).OrderBy(d => d).ToArray()[b++]).ToArray();
}

Previous Next

isLucky

My solution:
bool isLucky(int n) 
{
    return Equals(n.ToString().Substring(0, n.ToString().Length / 2).Sum(x => int.Parse(x.ToString())), n.ToString().Substring(n.ToString().Length / 2, n.ToString().Length / 2).Sum(x => int.Parse(x.ToString())));
}

Previous Next

commonCharacterCount

My solution:
int commonCharacterCount(string s1, string s2) 
{
    return s1.ToCharArray().Distinct().Sum(c => Math.Min(s1.Count(x => x == c), s2.Count(x => x == c)));
}

Previous Next

All Longest Strings

My solution:
string[] allLongestStrings(string[] inputArray)
{
    return inputArray.Where(x => x.Length == inputArray.Max(y => y.Length)).ToArray();
}

Previous Next

matrixElementsSum

My solution:
int matrixElementsSum(int[][] matrix)
{
    int sum = 0;
    int rows = matrix.GetLength(0);
    int columns = matrix[0].Length;
    for (int x = 0; x < columns; x++)
        for (int y = 0; y < rows; y++)
        {
            if (matrix[y][x] == 0)
                break;

            sum += matrix[y][x];
        }

    return sum;
}

Previous Next

almostIncreasingSequence

My solution:
bool almostIncreasingSequence(int[] sequence)
{
    bool erased = false;
    for (int i = sequence.Length - 1; i > 0; i--)
        if (sequence[i] <= sequence[i - 1])
            if (erased != true)
                erased = true;
            else
                return false;

    return true;
}

Previous Next

Make Array Consecutive 2

My solution:
int makeArrayConsecutive2(int[] statues)
{
    int min = statues.Min();
    int max = statues.Max();
    int st = 0;
    for (int i = min; i <= max; i++)
        if (!statues.Contains(i))
            st++;

    return st;
}

Previous Next

shapeArea

My solution:
int shapeArea(int n) 
{
    return 2 * n * (n - 1) + 1;
}

Previous Next

checkPalindrome

My solution:
bool checkPalindrome(string inputString)
{
    char[] arr = inputString.ToCharArray();
    Array.Reverse(arr);
    return string.Equals(new string(arr), inputString);
}

Previous Next

centuryFromYear

My solution:
int centuryFromYear(int year)
{
    return year % 100 == 0 ? year / 100 : year / 100 + 1;
}

Previous Next