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