├── CodeSignal ├── Arcade │ ├── Intro │ │ ├── 01 - add.cs │ │ ├── 02 - centuryFromYear.cs │ │ ├── 03 - checkPalindrome.cs │ │ ├── 04 - adjacentElementsProduct.cs │ │ ├── 05 - shapeArea.cs │ │ ├── 06 - MakeArrayConsecutive2.cs │ │ ├── 07 - almostIncreasingSequence.cs │ │ ├── 08 - matrixElementsSum.cs │ │ ├── 09 - All_Longest_Strings.cs │ │ ├── 10 - commonCharacterCount.cs │ │ ├── 11 - isLucky.cs │ │ ├── 12 - Sort_By_Height.cs │ │ ├── 13 - reverseInParentheses.cs │ │ ├── 14 - alternatingSums.cs │ │ ├── 15 - Add_Border.cs │ │ ├── 16 - Are_Similar?.cs │ │ ├── 17 - arrayChange.cs │ │ ├── 18 - palindromeRearranging.cs │ │ ├── 19 - areEquallyStrong.cs │ │ ├── 20 - arrayMaximalAdjacentDifference.cs │ │ ├── 21 - isIPv4Address.cs │ │ ├── 22 - avoidObstacles.cs │ │ ├── 23 - BoxBlur.cs │ │ ├── 24 - mineSweeper.cs │ │ ├── 25 - ArrayReplace.cs │ │ ├── 26 - evenDigitsOnly.cs │ │ ├── 27 - variableName.cs │ │ ├── 28 - alphabeticShift.cs │ │ ├── 29 - chessBoardCellColor.cs │ │ ├── 30 - circleOfNumbers.cs │ │ ├── 31 - depositProfit.cs │ │ ├── 32 - absoluteValuesSumMinimization.cs │ │ ├── 33 - stringsRearrangement.cs │ │ ├── 34 - extractEachKth.cs │ │ ├── 35 - firstDigit.cs │ │ ├── 36 - differentSymbolsNaive.cs │ │ ├── 37 - arrayMaxConsecutiveSum.cs │ │ ├── 38 - growingPlant.cs │ │ ├── 39 - Knapsack_Light.cs │ │ ├── 40 - longestDigitsPrefix.cs │ │ ├── 41 - digitDegree.cs │ │ ├── 42 - Bishop_and_Pawn.cs │ │ ├── 43 - isBeautifulString.cs │ │ ├── 44 - FindEmailDomain.cs │ │ ├── 45 - buildPalindrome.cs │ │ ├── 46 - Elections_Winners.cs │ │ ├── 47 - Is_MAC48_Address?.cs │ │ ├── 48 - isDigit.cs │ │ ├── 49 - lineEncoding.cs │ │ ├── 50 - chessKnight.cs │ │ ├── 51 - deleteDigit.cs │ │ ├── 52 - longestWord.cs │ │ ├── 53 - Valid_Time.cs │ │ ├── 54 - sumUpNumbers.cs │ │ ├── 55 - Different_Squares.cs │ │ ├── 56 - digitsProduct.cs │ │ ├── 57 - File_Naming.cs │ │ ├── 58 - messageFromBinaryCode.cs │ │ ├── 59 - spiralNumbers.cs │ │ └── 60 - Sudoku.cs │ └── The_Core │ │ ├── 001 - Add_Two_Digits.cs │ │ ├── 002 - Largest_Number.cs │ │ ├── 003 - Candies.cs │ │ ├── 004 - Seats_in_Theater.cs │ │ ├── 005 - Max_Multiple.cs │ │ ├── 006 - Circle_of_Numbers.cs │ │ ├── 007 - Late_Ride.cs │ │ ├── 008 - PhoneCall.cs │ │ ├── 009 - ReachNextLevel.cs │ │ ├── 010 - Knapsack_Light.cs │ │ ├── 011 - Extra_Number.cs │ │ ├── 012 - Is_Infinite_Process?.cs │ │ ├── 013 - Arithmetic_Expression.cs │ │ ├── 014 - Tennis_Set.cs │ │ ├── 015 - Will_You?.cs │ │ ├── 016 - Metro_Card.cs │ │ ├── 017 - Kill_K-th_Bit.cs │ │ ├── 018 - Array_Packing.cs │ │ ├── 019 - Range_Bit_Count.cs │ │ ├── 020 - Mirror_Bits.cs │ │ ├── 021 - Second-RightMostBit.cs │ │ ├── 022 - Swap_Adjacent_Bits.cs │ │ ├── 023 - Different_Rightmost_Bit.cs │ │ ├── 024 - Equal_Pair_of_Bits.cs │ │ ├── 025 - Least_Factorial.cs │ │ ├── 026 - Count_Sum_of_Two_Representations_2.cs │ │ ├── 027 - Magical_Well.cs │ │ ├── 028 - Lineup.cs │ │ ├── 029 - AdditionWithoutCarrying.cs │ │ ├── 030 - Apple_Boxes.cs │ │ ├── 031 - IncreaseNumberRoundness.cs │ │ ├── 032 - Rounder.cs │ │ ├── 033 - Candles.cs │ │ ├── 034 - Count_Black_Cells.cs │ │ ├── 035 - Create_Array.cs │ │ ├── 036 - ArrayReplace.cs │ │ ├── 037 - FirstReverseTry.cs │ │ ├── 038 - Concatenate_Arrays.cs │ │ ├── 039 - RemoveArrayPart.cs │ │ ├── 040 - Is_Smooth?.cs │ │ ├── 041 - ReplaceMiddle.cs │ │ ├── 042 - MakeArrayConsecutive2.cs │ │ ├── 043 - IsPower?.cs │ │ ├── 044 - IsSumOfConsecutive2.cs │ │ ├── 045 - SquareDigitsSequence.cs │ │ ├── 046 - PagesNumberingWithInk.cs │ │ ├── 047 - ComfortableNumbers.cs │ │ ├── 048 - WeakNumbers.cs │ │ ├── 049 - RectangleRotation.cs │ │ ├── 050 - CrosswordFormation.cs │ │ ├── 051 - EncloseInBrackets.cs │ │ ├── 052 - ProperNounCorrection.cs │ │ ├── 053 - IsTandemRepeat.cs │ │ ├── 054 - IsCaseInsensitivePalindrome.cs │ │ ├── 055 - FindEmailDomain.cs │ │ ├── 056 - HtmlEndTagByStartTag.cs │ │ ├── 057 - IsMAC48Address.cs │ │ ├── 058 - IsUnstablePair.cs │ │ ├── 059 - StringsConstruction.cs │ │ ├── 060 - IsSubstitutionCipher.cs │ │ ├── 061 - Create_Anagram.cs │ │ ├── 062 - ConstructSquare.cs │ │ ├── 063 - Numbers_Grouping.cs │ │ ├── 064 - DifferentSquares.cs │ │ ├── 065 - MostFrequentDigitSum.cs │ │ ├── 066 - NumberOfClans.cs │ │ ├── 067 - HouseNumbersSum.cs │ │ ├── 068 - AllLongestStrings.cs │ │ ├── 069 - HouseOfCats.cs │ │ ├── 070 - AlphabetSubsequence.cs │ │ ├── 071 - MinimalNumberOfCoins.cs │ │ ├── 072 - AddBorder.cs │ │ ├── 073 - SwitchLights.cs │ │ ├── 074 - TimedReading.cs │ │ ├── 075 - ElectionsWinners.cs │ │ ├── 076 - IntegerToStringOfFixedWidth.cs │ │ ├── 077 - AreSimilar?.cs │ │ ├── 078 - AdaNumber.cs │ │ ├── 079 - ThreeSplit.cs │ │ ├── 080 - CharacterParity.cs │ │ ├── 081 - ReflectString.cs │ │ ├── 082 - NewNumeralSystem.cs │ │ ├── 083 - Cipher26.cs │ │ ├── 084 - StolenLunch.cs │ │ ├── 085 - HigherVersion.cs │ │ ├── 086 - Decipher.cs │ │ ├── 087 - AlphanumericLess.cs │ │ ├── 088 - ArrayConversion.cs │ │ ├── 089 - ArrayPreviousLess.cs │ │ ├── 090 - PairOfShoes.cs │ │ ├── 091 - Combs.cs │ │ ├── 092 - StringsCrossover.cs │ │ ├── 093 - CyclicString.cs │ │ ├── 094 - Beautiful_Text.cs │ │ ├── 095 - RunnersMeetings.cs │ │ ├── 096 - ChristmasTree.cs │ │ ├── 097 - FileNaming.cs │ │ ├── 098 - ExtractMatrixColumn.cs │ │ ├── 099 - Are Isomorphic?.cs │ │ ├── 100 - ReverseOnDiagonals.cs │ │ ├── 101 - Swap_Diagonals.cs │ │ ├── 102 - CrossingSum.cs │ │ ├── 103 - DrawRectangle.cs │ │ ├── 104 - Volleyball_Positions.cs │ │ ├── 105 - Star_Rotation.cs │ │ ├── 106 - Sudoku.cs │ │ ├── 107 - MineSweeper.cs │ │ ├── 108 - BoxBlur.cs │ │ ├── 109 - ContoursShifting.cs │ │ ├── 110 - PolygonPerimeter.cs │ │ ├── 111 - Gravitation.cs │ │ ├── 112 - IsInformationConsistent?.cs │ │ ├── 113 - CorrectNonogram.cs │ │ ├── 114 - ShuffledArray.cs │ │ ├── 115 - SortByHeight.cs │ │ ├── 116 - SortByLength.cs │ │ ├── 117 - BoxesPacking.cs │ │ ├── 118 - MaximumSum.cs │ │ ├── 119 - RowsRearranging.cs │ │ ├── 120 - DigitDifferenceSort.cs │ │ ├── 121 - UniqueDigitProducts.cs │ │ ├── 122 - BishopAndPawn.cs │ │ ├── 123 - ChessKnightMoves.cs │ │ ├── 124 - BishopDiagonal.cs │ │ ├── 125 - Whose_Turn?.cs │ │ ├── 126 - ChessBishopDream.cs │ │ ├── 127 - ChessTriangle.cs │ │ ├── 128 - AmazonCheckmate.cs │ │ ├── 129 - PawnRace.cs │ │ ├── 130 - ValidTime.cs │ │ ├── 131 - VideoPart.cs │ │ ├── 132 - DayOfWeek.cs │ │ ├── 133 - CuriousClock.cs │ │ ├── 134 - NewYearCelebrations.cs │ │ ├── 135 - RegularMonths.cs │ │ ├── 136 - MissedClasses.cs │ │ ├── 137 - Holiday.cs │ │ ├── 138 - IsSentenceCorrect?.cs │ │ ├── 139 - ReplaceAllDigitsRegExp.cs │ │ ├── 140 - SwapAdjacentWords.cs │ │ ├── 141 - N-th_Number.cs │ │ ├── 142 - IsSubsequence?.cs │ │ ├── 143 - EyeRhyme.cs │ │ ├── 144 - ProgramTranslation.cs │ │ ├── 145 - RepetitionEncryption.cs │ │ ├── 146 - BugsAndBugfixes.cs │ │ ├── 147 - LRCToSubRip.cs │ │ ├── 148 - HTML_Table.cs │ │ ├── 149 - ChessNotation.cs │ │ ├── 150 - CellsJoining.cs │ │ ├── 151 - FirstOperationCharacter.cs │ │ ├── 152 - CountElements.cs │ │ ├── 153 - TreeBottom.cs │ │ ├── 154 - Befunge-93.cs │ │ ├── 155 - PipesGame.cs │ │ ├── 156 - Game2048.cs │ │ ├── 157 - SnakeGame.cs │ │ ├── 158 - TetrisGame.cs │ │ ├── 159 - PyraminxPuzzle.cs │ │ ├── 160 - LinesGame.cs │ │ ├── 161 - Fractal.cs │ │ └── 162 - TimeASCII_Representation.cs └── Interview Practice │ ├── Arrays │ ├── firstDuplicate.cs │ ├── firstNotRepeatingCharacter.cs │ ├── isCryptSolution.cs │ ├── rotateImage.cs │ └── sudoku2.cs │ └── Linked Lists │ ├── isListPalindrome.cs │ └── removeKFromList.cs └── README.md /CodeSignal/Arcade/Intro/01 - add.cs: -------------------------------------------------------------------------------- 1 | // Basic solution 2 | int add(int param1, int param2) { 3 | return param1 + param2; 4 | } 5 | 6 | /*--------------------------------------------------------*/ 7 | /*--------------------------------------------------------*/ 8 | 9 | /* You can also make the method a one-liner */ 10 | int add(int param1, int param2) => param1 + param2; 11 | -------------------------------------------------------------------------------- /CodeSignal/Arcade/Intro/02 - centuryFromYear.cs: -------------------------------------------------------------------------------- 1 | // Basic solution 2 | int centuryFromYear(int year) { 3 | double centuryRoundUp = Math.Ceiling(year / 100.0); 4 | 5 | return Convert.ToInt32(centuryRoundUp); 6 | } 7 | 8 | /*------------------------------------------------*/ 9 | /*------------------------------------------------*/ 10 | 11 | 12 | // Same solution as the one above except it's all in one return statement 13 | int centuryFromYear(int year) { 14 | return Convert.ToInt32(Math.Ceiling(year / 100.0)); 15 | } 16 | 17 | /*------------------------------------------------*/ 18 | /*------------------------------------------------*/ 19 | 20 | 21 | // Less readable one-liner but it works just as well 22 | int centuryFromYear(int year) => (int)Math.Ceiling(year++ / 100.0); 23 | -------------------------------------------------------------------------------- /CodeSignal/Arcade/Intro/03 - checkPalindrome.cs: -------------------------------------------------------------------------------- 1 | // Basic solution 2 | // Checks if the reversed string is the same as the original string 3 | 4 | bool checkPalindrome(string inputString) { 5 | string Reverse( string s ) 6 | { 7 | char[] charArray = s.ToCharArray(); 8 | Array.Reverse( charArray ); 9 | return new string( charArray ); 10 | } 11 | 12 | 13 | return inputString == Reverse(inputString); 14 | } 15 | 16 | /*-------------------------------------------*/ 17 | /*-------------------------------------------*/ 18 | 19 | // A faster and more efficient solution 20 | // Checks if the letters on the start/end of the string matches and it moves to the middle of the string where the loop stops 21 | 22 | bool checkPalindrome(string inputString) { 23 | int half = inputString.Length / 2; 24 | if((inputString.Length % 2) == 0) half--; // We don't want uneven numbers 25 | 26 | for(int i = 0; i <= half; i++) { 27 | if(inputString[i] != inputString[inputString.Length - 1 - i]) { 28 | return false; 29 | } 30 | } 31 | 32 | 33 | return true; 34 | } 35 | 36 | /*------------------------------------------*/ 37 | /*------------------------------------------*/ 38 | 39 | // Same as the one above except the code is shorter 40 | 41 | bool checkPalindrome(string inputString) { 42 | int half = inputString.Length / 2; 43 | if((inputString.Length & 1) == 0) half--; 44 | for(int i = 0; i <= half; i++) if(inputString[i] != inputString[inputString.Length - 1 - i]) return false; 45 | 46 | 47 | return true; 48 | } 49 | -------------------------------------------------------------------------------- /CodeSignal/Arcade/Intro/04 - adjacentElementsProduct.cs: -------------------------------------------------------------------------------- 1 | // Basic solution 2 | 3 | int adjacentElementsProduct(int[] inputArray) { 4 | int max = 1000 * -1000; // Lowest possible prduct according to the constraints 5 | for(int i = 0; i < inputArray.Length - 1; i++) { 6 | if(inputArray[i] * inputArray[i + 1] > max) { 7 | max = inputArray[i] * inputArray[i + 1]; 8 | } 9 | } 10 | 11 | 12 | return max; 13 | } 14 | 15 | /*------------------------------------------------*/ 16 | /*------------------------------------------------*/ 17 | 18 | // Same as above except it's been shortend 19 | 20 | int adjacentElementsProduct(int[] inputArray) { 21 | int max = 1000 * -1000; 22 | for(int i = 0; i < inputArray.Length - 1; i++) if(inputArray[i] * inputArray[i + 1] > max) max = inputArray[i] * inputArray[i + 1]; 23 | 24 | 25 | return max; 26 | } 27 | -------------------------------------------------------------------------------- /CodeSignal/Arcade/Intro/05 - shapeArea.cs: -------------------------------------------------------------------------------- 1 | // A tricky math challange 2 | int shapeArea(int n) { 3 | return (2 * (n * n)) - (2 * n) + 1; 4 | } 5 | 6 | /*--------------------------------------*/ 7 | /*--------------------------------------*/ 8 | 9 | // A shorter version 10 | int shapeArea(int n) => 2 * n * n - 2 * n + 1; 11 | -------------------------------------------------------------------------------- /CodeSignal/Arcade/Intro/06 - MakeArrayConsecutive2.cs: -------------------------------------------------------------------------------- 1 | // Basic solution 2 | 3 | int makeArrayConsecutive2(int[] statues) { 4 | int count = 0; 5 | 6 | int limit = statues.Max(); 7 | int start = statues.Min(); 8 | 9 | for(int i = start; i < limit; i++) { 10 | if(Array.IndexOf(statues, i) == -1) { 11 | count++; 12 | } 13 | } 14 | 15 | return count; 16 | } 17 | 18 | /*--------------------------------------------*/ 19 | /*--------------------------------------------*/ 20 | 21 | // Same but with a while-loop instead 22 | 23 | int makeArrayConsecutive2(int[] statues) { 24 | int count = 0; 25 | 26 | int limit = statues.Max(); 27 | int start = statues.Min(); 28 | 29 | while(start < limit) if(Array.IndexOf(statues, start++) == -1) count++; 30 | 31 | return count; 32 | } 33 | -------------------------------------------------------------------------------- /CodeSignal/Arcade/Intro/07 - almostIncreasingSequence.cs: -------------------------------------------------------------------------------- 1 | // Simple solution 2 | bool almostIncreasingSequence(int[] sequence) { 3 | int strike = 0; // Tracks the elements in the array that are deviating from strictly increasing 4 | int max = -100000; // Lowest possible value 5 | int backupMax = -100000; 6 | 7 | foreach(int i in sequence) { 8 | if(i > max) { 9 | backupMax = max; 10 | max = i; 11 | } else if(i > backupMax) { 12 | strike++; 13 | max = i; 14 | } else strike++; 15 | 16 | } 17 | 18 | return strike <= 1; // Deviation count cannot be greater than 1 19 | } 20 | -------------------------------------------------------------------------------- /CodeSignal/Arcade/Intro/08 - matrixElementsSum.cs: -------------------------------------------------------------------------------- 1 | // Solution 2 | 3 | int matrixElementsSum(int[][] matrix) { 4 | int totCost = 0; 5 | string haunt = ""; 6 | 7 | for(int i = 0; i < matrix.Length; i++) { 8 | 9 | 10 | for(int j = 0; j < matrix[i].Length; j++) { 11 | if(matrix[i][j] == 0) { 12 | haunt += $"{j}"; 13 | continue; 14 | } 15 | if(haunt.Contains($"{j}")) continue; 16 | totCost += matrix[i][j]; 17 | } 18 | } 19 | 20 | return totCost; 21 | } 22 | -------------------------------------------------------------------------------- /CodeSignal/Arcade/Intro/09 - All_Longest_Strings.cs: -------------------------------------------------------------------------------- 1 | // Basic solution 2 | 3 | string[] allLongestStrings(string[] inputArray) { 4 | int maxLen = 0; 5 | List ret = new List(); 6 | 7 | for(int i = 0; i < inputArray.Length; i++) { 8 | if(inputArray[i].Length > maxLen) { 9 | maxLen = inputArray[i].Length; 10 | } 11 | } 12 | 13 | for(int j = 0; j < inputArray.Length; j++) { 14 | if(inputArray[j].Length == maxLen) { 15 | ret.Add(inputArray[j]); 16 | } 17 | } 18 | 19 | 20 | return ret.ToArray(); 21 | } 22 | 23 | /*-------------------------------------*/ 24 | /*-------------------------------------*/ 25 | 26 | // Shorter foreach solution 27 | 28 | string[] allLongestStrings(string[] inputArray) { 29 | int maxLen = 0; 30 | List ret = new List(); 31 | 32 | foreach(string i in inputArray) if(i.Length > maxLen) maxLen = i.Length; 33 | 34 | foreach(string j in inputArray) if(j.Length == maxLen) ret.Add(j); 35 | 36 | 37 | return ret.ToArray(); 38 | } 39 | -------------------------------------------------------------------------------- /CodeSignal/Arcade/Intro/10 - commonCharacterCount.cs: -------------------------------------------------------------------------------- 1 | // Solution 2 | 3 | int commonCharacterCount(string s1, string s2) { 4 | int[] first = new int[26]; 5 | int[] second = new int[26]; 6 | Array.Fill(first, 0); 7 | Array.Fill(second, 0); 8 | 9 | int commonChar = 0; 10 | 11 | foreach(char i in s1) first[(int)i - 97]++; // Counting down how many of each letter 12 | foreach(char i in s2) second[(int)i - 97]++; 13 | 14 | for(int i = 0; i < 26; i++) { 15 | 16 | while(first[i] != 0 && second[i] != 0) { // Counting the common letters 17 | first[i]--; 18 | second[i]--; 19 | commonChar++; 20 | } 21 | } 22 | 23 | return commonChar; 24 | } 25 | -------------------------------------------------------------------------------- /CodeSignal/Arcade/Intro/11 - isLucky.cs: -------------------------------------------------------------------------------- 1 | // Basic solution 2 | 3 | bool isLucky(int n) { 4 | string str = n.ToString(); 5 | 6 | string firstHalf = str.Substring(0, str.Length / 2); 7 | string secondHalf = str.Substring(str.Length / 2); 8 | 9 | int firstSum = 0; 10 | int secondSum = 0; 11 | 12 | for(int i = 0; i < firstHalf.Length; i++) { 13 | firstSum += (firstHalf[i] - '0'); 14 | secondSum += (secondHalf[i] - '0'); 15 | } 16 | 17 | return firstSum == secondSum; 18 | } 19 | 20 | /*--------------------------------------------------*/ 21 | /*--------------------------------------------------*/ 22 | 23 | // A variant of the solution above 24 | 25 | bool isLucky(int n) { 26 | string str = n.ToString().Substring(0, n.ToString().Length / 2) + "-" + n.ToString().Substring(n.ToString().Length / 2); 27 | int difference = 0; 28 | 29 | bool add = true; 30 | foreach(char c in str) { 31 | if(c == '-') { 32 | add = false; 33 | continue; 34 | } 35 | if(add) difference += (c - '0'); 36 | else difference -= (c - '0'); 37 | } 38 | 39 | return difference == 0; 40 | } 41 | 42 | /*--------------------------------------------------*/ 43 | /*--------------------------------------------------*/ 44 | 45 | // An efficient mathematical solution 46 | 47 | bool isLucky(int n) { 48 | int log = (int)Math.Log10(n) + 1; 49 | int first = 0; 50 | int sec = 0; 51 | 52 | for(int i = log; i > 0; i--) { 53 | int pow = (int)Math.Pow(10, i - 1); 54 | n %= pow * 10; 55 | int N = (n - n % pow) / pow; 56 | 57 | if(i <= (log / 2)) first += N; 58 | else sec += N; 59 | } 60 | 61 | return first == sec; 62 | } 63 | -------------------------------------------------------------------------------- /CodeSignal/Arcade/Intro/12 - Sort_By_Height.cs: -------------------------------------------------------------------------------- 1 | // Solution 2 | 3 | int[] sortByHeight(int[] a) { 4 | int[] b = new int[a.Length]; 5 | List order = new List(); 6 | 7 | for(int i = 0; i < a.Length; i++) { 8 | if(a[i] == -1) b[i] = -1; 9 | else order.Add(a[i]); 10 | } 11 | int[] s = order.ToArray(); 12 | Array.Sort(s); 13 | 14 | int index = 0; 15 | for(int i = 0; i < b.Length; i++) { 16 | if(b[i] == -1) continue; 17 | b[i] = s[index++]; 18 | } 19 | 20 | 21 | return b; 22 | } 23 | -------------------------------------------------------------------------------- /CodeSignal/Arcade/Intro/13 - reverseInParentheses.cs: -------------------------------------------------------------------------------- 1 | // Regex solution 2 | 3 | string reverseInParentheses(string inputString) { 4 | Regex d = new Regex(@"(\([a-z]*\))"); 5 | while(d.IsMatch(inputString)) { 6 | int count = 0; 7 | 8 | var rep = d.Matches(inputString)[count++]; 9 | string repo = Reverse(rep.ToString().Substring(1, rep.Length - 2)); 10 | 11 | Console.WriteLine(repo); 12 | inputString = d.Replace(inputString, repo, 1); 13 | } 14 | 15 | 16 | return inputString; 17 | } 18 | 19 | 20 | public static string Reverse( string s ) { 21 | char[] charArray = s.ToCharArray(); 22 | Array.Reverse( charArray ); 23 | return new string( charArray ); 24 | } 25 | -------------------------------------------------------------------------------- /CodeSignal/Arcade/Intro/14 - alternatingSums.cs: -------------------------------------------------------------------------------- 1 | // Solution 2 | 3 | int[] alternatingSums(int[] a) { 4 | int[] b = new int[] {0, 0}; 5 | 6 | for(int i = 0; i < a.Length; i++) { 7 | if((i & 1) == 0) b[0] += a[i]; 8 | else b[1] += a[i]; 9 | } 10 | 11 | return b; 12 | } 13 | -------------------------------------------------------------------------------- /CodeSignal/Arcade/Intro/15 - Add_Border.cs: -------------------------------------------------------------------------------- 1 | // Solution 2 | 3 | string[] addBorder(string[] picture) { 4 | int len = picture.Length; 5 | 6 | string border = new String('*', picture[0].Length + 2); 7 | string[] arr = new string[len + 2]; 8 | arr[0] = border; 9 | arr[arr.Length - 1] = border; 10 | 11 | for(int i = 1; i < arr.Length - 1; i++) arr[i] = "*" + picture[i - 1] + "*"; 12 | 13 | return arr; 14 | } 15 | -------------------------------------------------------------------------------- /CodeSignal/Arcade/Intro/16 - Are_Similar?.cs: -------------------------------------------------------------------------------- 1 | // Solution 2 | 3 | bool areSimilar(int[] a, int[] b) { 4 | int count = 0; 5 | int[] atwo = new int[2]; 6 | int[] btwo = new int[2]; 7 | int index = 0; 8 | 9 | for(int i = 0; i < a.Length; i++) { 10 | if(a[i] != b[i]) { 11 | count++; 12 | if(count == 3) return false; 13 | atwo[index] = a[i]; 14 | btwo[index++] = b[i]; 15 | } 16 | } 17 | 18 | Array.Reverse(atwo); 19 | 20 | for(int i = 0; i < atwo.Length; i++) if(atwo[i] != btwo[i]) return false; 21 | 22 | return true; 23 | } 24 | -------------------------------------------------------------------------------- /CodeSignal/Arcade/Intro/17 - arrayChange.cs: -------------------------------------------------------------------------------- 1 | // Solution 2 | 3 | int arrayChange(int[] inputArray) { 4 | int sum = 0; 5 | 6 | for(int i = 1; i < inputArray.Length; i++) { 7 | int prev = inputArray[i - 1]; 8 | 9 | if(prev >= inputArray[i]) { 10 | sum += prev - inputArray[i] + 1; 11 | inputArray[i] = prev + 1; 12 | 13 | } 14 | } 15 | 16 | 17 | return sum; 18 | } 19 | -------------------------------------------------------------------------------- /CodeSignal/Arcade/Intro/18 - palindromeRearranging.cs: -------------------------------------------------------------------------------- 1 | // Solution 2 | 3 | bool palindromeRearranging(string inputString) { 4 | int[] alfbet = new int[26]; 5 | bool strike = true; 6 | 7 | foreach(char i in inputString) alfbet[(int)i - 97]++; 8 | foreach(int i in alfbet) { 9 | if((i & 1) == 1 && !strike) return false; 10 | if((i & 1) == 1) strike = false; 11 | } 12 | 13 | 14 | return true; 15 | } 16 | -------------------------------------------------------------------------------- /CodeSignal/Arcade/Intro/19 - areEquallyStrong.cs: -------------------------------------------------------------------------------- 1 | // Basic Solution 2 | 3 | bool areEquallyStrong(int yourLeft, int yourRight, int friendsLeft, int friendsRight) { 4 | int ymax = Math.Max(yourLeft, yourRight); 5 | int fmax = Math.Max(friendsLeft, friendsRight); 6 | 7 | int ymin = Math.Min(yourLeft, yourRight); 8 | int fmin = Math.Min(friendsLeft, friendsRight); 9 | 10 | 11 | return ymax == fmax && ymin == fmin; 12 | } 13 | -------------------------------------------------------------------------------- /CodeSignal/Arcade/Intro/20 - arrayMaximalAdjacentDifference.cs: -------------------------------------------------------------------------------- 1 | // Basic solution 2 | 3 | int arrayMaximalAdjacentDifference(int[] inputArray) { 4 | int max = 0; 5 | 6 | for(int i = 0; i < inputArray.Length - 1; i++) { 7 | int diff = Math.Abs(inputArray[i] - inputArray[i + 1]); 8 | if(max < diff) max = diff; 9 | } 10 | 11 | 12 | return max; 13 | } 14 | 15 | /*----------------------------------------------------*/ 16 | /*----------------------------------------------------*/ 17 | 18 | // Shorter solution 19 | 20 | int arrayMaximalAdjacentDifference(int[] inputArray) { 21 | int max = 0; 22 | 23 | for(int i = 0; i < inputArray.Length - 1; i++) if(max < Math.Abs(inputArray[i] - inputArray[i + 1])) max = Math.Abs(inputArray[i] - inputArray[i + 1]); 24 | 25 | 26 | return max; 27 | } 28 | -------------------------------------------------------------------------------- /CodeSignal/Arcade/Intro/21 - isIPv4Address.cs: -------------------------------------------------------------------------------- 1 | // Solution 2 | 3 | bool isIPv4Address(string inputString) { 4 | Regex l = new Regex(@"\d+\.\d+\.\d+\.\d+"); 5 | if(!l.IsMatch(inputString)) return false; 6 | if(inputString != l.Matches(inputString)[0].ToString()) return false; 7 | 8 | String[] arr = inputString.Split('.'); 9 | 10 | foreach(string i in arr) if(i.Length > 3 || int.Parse(i) > 255 || (i.Length > 1 && i[0] == '0')) return false; 11 | 12 | return true; 13 | } 14 | -------------------------------------------------------------------------------- /CodeSignal/Arcade/Intro/22 - avoidObstacles.cs: -------------------------------------------------------------------------------- 1 | // Basic solution 2 | 3 | int avoidObstacles(int[] inputArray) { 4 | int k = 2; 5 | 6 | while(All(inputArray, k) != true) k++; 7 | 8 | 9 | return k; 10 | } 11 | 12 | 13 | public static bool All(int[] arr, int k) { 14 | foreach(int i in arr) if(i % k == 0) return false; 15 | 16 | return true; 17 | } 18 | 19 | 20 | /*--------------------------------------------------------*/ 21 | /*--------------------------------------------------------*/ 22 | 23 | // Using System.Linq 24 | 25 | int avoidObstacles(int[] inputArray) { 26 | int k = 2; 27 | 28 | while(inputArray.Any(x => x % k == 0) == true) k++; 29 | 30 | 31 | return k; 32 | } 33 | -------------------------------------------------------------------------------- /CodeSignal/Arcade/Intro/23 - BoxBlur.cs: -------------------------------------------------------------------------------- 1 | // Solution 2 | 3 | int[][] boxBlur(int[][] image) { 4 | int len = image.Length - 2; 5 | int iLen = image[0].Length - 2; 6 | int[][] pic = new int[len][]; 7 | 8 | for(int i = 1; i <= len; i++) { 9 | int[] row = new int[iLen]; 10 | 11 | for(int j = 1; j <= iLen; j++) { 12 | int sum = 0; 13 | 14 | for(int k = -1; k < 2; k++) for(int l = -1; l < 2; l++) sum += image[i + k][j + l]; 15 | row[j - 1] = ~~(sum / 9); 16 | } 17 | 18 | pic[i - 1] = row; 19 | } 20 | 21 | 22 | return pic; 23 | } 24 | -------------------------------------------------------------------------------- /CodeSignal/Arcade/Intro/24 - mineSweeper.cs: -------------------------------------------------------------------------------- 1 | // Solution 2 | 3 | int[][] minesweeper(bool[][] matrix) { 4 | int len = matrix.Length; 5 | int iLen = matrix[0].Length; 6 | int[][] newMat = new int[len][]; 7 | 8 | for(int i = 0; i < len; i++) { 9 | int[] row = new int[iLen]; 10 | 11 | for(int j = 0; j < iLen; j++) { 12 | int sum = 0; 13 | 14 | for(int k = -1; k < 2; k++) { 15 | if(i + k < 0 || i + k >= len) continue; 16 | for(int l = -1; l < 2; l++) { 17 | if(j + l < 0 || j + l >= iLen) continue; 18 | if(k == 0 && l == 0) continue; 19 | if(matrix[i + k][j + l]) sum++; 20 | } 21 | } 22 | 23 | row[j] = sum; 24 | } 25 | 26 | newMat[i] = row; 27 | } 28 | 29 | 30 | return newMat; 31 | 32 | } 33 | -------------------------------------------------------------------------------- /CodeSignal/Arcade/Intro/25 - ArrayReplace.cs: -------------------------------------------------------------------------------- 1 | // Solution 2 | 3 | int[] arrayReplace(int[] inputArray, int elemToReplace, int substitutionElem) { 4 | for(int i = 0; i < inputArray.Length; i++) if(inputArray[i] == elemToReplace) inputArray[i] = substitutionElem; 5 | 6 | return inputArray; 7 | } 8 | -------------------------------------------------------------------------------- /CodeSignal/Arcade/Intro/26 - evenDigitsOnly.cs: -------------------------------------------------------------------------------- 1 | // Solution 2 | 3 | bool evenDigitsOnly(int n) { 4 | while(true) { 5 | if(n == 0) return true; 6 | if((n & 1) == 1) return false; 7 | n = ~~(n / 10); 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /CodeSignal/Arcade/Intro/27 - variableName.cs: -------------------------------------------------------------------------------- 1 | // Basic solution 2 | 3 | bool variableName(string name) { 4 | bool firstCharNum = true; 5 | 6 | try { Console.Write(int.Parse(name[0].ToString()));} 7 | catch(Exception e) { firstCharNum = false; } 8 | if(firstCharNum) return false; 9 | 10 | Regex reg = new Regex(@"[\d\w]*"); 11 | 12 | 13 | return reg.Matches(name)[0].ToString() == name; 14 | } 15 | 16 | /*---------------------------------------------------------*/ 17 | /*---------------------------------------------------------*/ 18 | 19 | // Shorter solution 20 | 21 | bool variableName(string name) { 22 | bool firstCharNum = true; 23 | 24 | try { Console.Write(int.Parse(name[0].ToString()));} 25 | catch(Exception e) { firstCharNum = false; } 26 | if(firstCharNum) return false; 27 | 28 | 29 | return new Regex(@"[\d\w]*").Matches(name)[0].ToString() == name; 30 | } 31 | -------------------------------------------------------------------------------- /CodeSignal/Arcade/Intro/28 - alphabeticShift.cs: -------------------------------------------------------------------------------- 1 | // Solution 2 | 3 | string alphabeticShift(string inputString) { 4 | string ret = ""; 5 | 6 | foreach(char i in inputString) { 7 | if(i == 'z') ret += "a"; 8 | else ret += (char)((int)i + 1); 9 | } 10 | 11 | return ret; 12 | } 13 | -------------------------------------------------------------------------------- /CodeSignal/Arcade/Intro/29 - chessBoardCellColor.cs: -------------------------------------------------------------------------------- 1 | // Solution 2 | 3 | bool chessBoardCellColor(string cell1, string cell2) { 4 | bool[][] chess = new bool[8][]; 5 | bool[] even = new bool[] {false, true, false, true, false, true, false, true}; 6 | bool[] odd = new bool[] {true, false, true, false, true, false, true, false}; 7 | 8 | for(int i = 0; i < chess.Length; i++) { 9 | if((i & 1) == 0) chess[i] = even; 10 | else chess[i] = odd; 11 | } 12 | 13 | return chess[(int)cell1[0] - 65][(int)cell1[1] - 49] == chess[(int)cell2[0] - 65][(int)cell2[1] - 49]; 14 | } 15 | -------------------------------------------------------------------------------- /CodeSignal/Arcade/Intro/30 - circleOfNumbers.cs: -------------------------------------------------------------------------------- 1 | // Basic solution 2 | 3 | int circleOfNumbers(int n, int firstNumber) { 4 | int position = firstNumber + n / 2; 5 | return position % n; 6 | } 7 | 8 | /*--------------------------------------------------*/ 9 | /*--------------------------------------------------*/ 10 | 11 | // One-liner 12 | 13 | int circleOfNumbers(int n, int firstNumber) => (firstNumber + n / 2) % n; 14 | -------------------------------------------------------------------------------- /CodeSignal/Arcade/Intro/31 - depositProfit.cs: -------------------------------------------------------------------------------- 1 | // Solution 2 | 3 | int depositProfit(int deposit, int rate, int threshold) { 4 | int count = 0; 5 | double dep = deposit * 1.0; 6 | 7 | while(threshold > dep) { 8 | dep *= 1 + rate * .01; 9 | count++; 10 | } 11 | 12 | return count; 13 | } 14 | -------------------------------------------------------------------------------- /CodeSignal/Arcade/Intro/32 - absoluteValuesSumMinimization.cs: -------------------------------------------------------------------------------- 1 | // Solution 2 | 3 | int absoluteValuesSumMinimization(int[] a) { 4 | int min = -1; 5 | int ret = 0; 6 | 7 | foreach(int i in a) { 8 | int sum = 0; 9 | 10 | foreach(int j in a) sum += Math.Abs(i - j); 11 | 12 | if(min == -1 || sum < min) { 13 | min = sum; 14 | ret = i; 15 | } 16 | } 17 | 18 | return ret; 19 | } 20 | -------------------------------------------------------------------------------- /CodeSignal/Arcade/Intro/33 - stringsRearrangement.cs: -------------------------------------------------------------------------------- 1 | // Solution 2 | 3 | bool stringsRearrangement(string[] inputArray) { 4 | 5 | bool Check(string one, string two) { 6 | int strike = 0; 7 | for(int i = 0; i < one.Length; i++) { 8 | if(one[i] != two[i]) { 9 | strike++; 10 | if(strike > 1) return false; 11 | } 12 | } 13 | 14 | return strike == 1; 15 | } 16 | 17 | string[][] GetPermutation(string[] arr) { 18 | int len = arr.Length; 19 | List sequence = new List(); 20 | List permutations = new List(); 21 | 22 | void Permutate(List a) { 23 | for(int i = 0; i < a.Count; i++) { 24 | string str = a[i]; 25 | sequence.Add(str); 26 | if(sequence.Count == len) { 27 | permutations.Add(sequence.ToArray()); 28 | } else { 29 | var remain = a.Take(i).Concat(a.Skip(i + 1)); 30 | Permutate(remain.ToList()); 31 | } 32 | sequence.RemoveAt(sequence.Count - 1); 33 | } 34 | } 35 | 36 | Permutate(arr.ToList()); 37 | return permutations.ToArray(); 38 | } 39 | 40 | string[][] array = GetPermutation(inputArray); 41 | 42 | for(int i = 0; i < array.Length; i++) { 43 | int count = 0; 44 | for(int j = 1; j < array[0].Length; j++) { 45 | if(!Check(array[i][j - 1], array[i][j])) count++; 46 | } 47 | if(count == 0) return true; 48 | } 49 | 50 | return false; 51 | } 52 | -------------------------------------------------------------------------------- /CodeSignal/Arcade/Intro/34 - extractEachKth.cs: -------------------------------------------------------------------------------- 1 | // Solution 2 | 3 | int[] extractEachKth(int[] inputArray, int k) { 4 | List arr = new List(); 5 | 6 | for(int i = 0; i < inputArray.Length; i++) if((i + 1) % k != 0) arr.Add(inputArray[i]); 7 | 8 | 9 | return arr.ToArray(); 10 | } 11 | -------------------------------------------------------------------------------- /CodeSignal/Arcade/Intro/35 - firstDigit.cs: -------------------------------------------------------------------------------- 1 | // Basic solution 2 | 3 | char firstDigit(string inputString) { 4 | Regex rgx = new Regex(@"[\d]"); 5 | string str = rgx.Matches(inputString)[0].ToString(); 6 | return char.Parse(str); 7 | } 8 | 9 | /*------------------------------------------------------*/ 10 | /*------------------------------------------------------*/ 11 | 12 | // One-liner 13 | 14 | char firstDigit(string inputString) => char.Parse(new Regex(@"[\d]").Matches(inputString)[0].ToString()); 15 | -------------------------------------------------------------------------------- /CodeSignal/Arcade/Intro/36 - differentSymbolsNaive.cs: -------------------------------------------------------------------------------- 1 | // Basic Solution 2 | 3 | int differentSymbolsNaive(string s) { 4 | int[] cab = new int[27]; 5 | int letters = 0; 6 | 7 | for(int i = 0; i < s.Length; i++) { 8 | cab[(int)s[i] - 97]++; 9 | } 10 | 11 | for(int i = 0; i < cab.Length; i++) { 12 | if(cab[i] > 0) letters++; 13 | } 14 | 15 | return letters; 16 | } 17 | 18 | /*--------------------------------------------------*/ 19 | /*--------------------------------------------------*/ 20 | 21 | // Shorter Solution 22 | 23 | int differentSymbolsNaive(string s) { 24 | int[] cab = new int[27]; 25 | int letters = 0; 26 | 27 | foreach(char a in s) cab[(int)a - 97]++; 28 | 29 | foreach(int i in cab) if(i > 0) letters++; 30 | 31 | return letters; 32 | } 33 | -------------------------------------------------------------------------------- /CodeSignal/Arcade/Intro/37 - arrayMaxConsecutiveSum.cs: -------------------------------------------------------------------------------- 1 | // Basic Solution 2 | 3 | int arrayMaxConsecutiveSum(int[] inputArray, int k) { 4 | int max = 0; 5 | int kernel = 0; 6 | 7 | for(int i = 0; i < k; i++) kernel += inputArray[i]; 8 | 9 | max = kernel; 10 | 11 | for(int i = k; i < inputArray.Length; i++) { 12 | kernel += inputArray[i] - inputArray[i - k]; 13 | if(max < kernel) max = kernel; 14 | } 15 | 16 | 17 | return max; 18 | } 19 | -------------------------------------------------------------------------------- /CodeSignal/Arcade/Intro/38 - growingPlant.cs: -------------------------------------------------------------------------------- 1 | // Basic Solution 2 | 3 | int growingPlant(int upSpeed, int downSpeed, int desiredHeight) { 4 | int total = 0; 5 | int days = 0; 6 | 7 | while(total < desiredHeight) { 8 | days++; 9 | total += upSpeed; 10 | if(total >= desiredHeight) break; 11 | total -= downSpeed; 12 | } 13 | 14 | return days; 15 | } 16 | -------------------------------------------------------------------------------- /CodeSignal/Arcade/Intro/39 - Knapsack_Light.cs: -------------------------------------------------------------------------------- 1 | // Basic Solution 2 | 3 | int knapsackLight(int value1, int weight1, int value2, int weight2, int maxW) { 4 | if(weight1 + weight2 <= maxW) return value1 + value2; 5 | if(maxW < weight1 && maxW < weight2) return 0; 6 | if(weight1 > maxW) return value2; 7 | if(weight2 > maxW) return value1; 8 | return Math.Max(value1,value2); 9 | } 10 | 11 | /*------------------------------------------------------------------------*/ 12 | /*------------------------------------------------------------------------*/ 13 | 14 | // Dynamic programming solution 15 | 16 | int knapsackLight(int value1, int weight1, int value2, int weight2, int maxW) { 17 | int[] values = new int[] {value1, value2}; 18 | int[] weights = new int[] {weight1, weight2}; 19 | 20 | int[][] cap = new int[values.Length + 1][]; 21 | 22 | for(int i = 0; i <= weights.Length; i++) { 23 | int[] row = new int[maxW + 1]; 24 | for(int j = 0; j <= maxW; j++) { 25 | if(i == 0) row[j] = 0; 26 | else { 27 | if(weights[i - 1] <= j) row[j] = Math.Max(values[i - 1] + cap[i - 1][j - weights[i - 1]], cap[i - 1][j]); 28 | else row[j] = cap[i - 1][j]; 29 | } 30 | } 31 | cap[i] = row; 32 | } 33 | 34 | 35 | return cap[weights.Length][maxW]; 36 | } 37 | -------------------------------------------------------------------------------- /CodeSignal/Arcade/Intro/40 - longestDigitsPrefix.cs: -------------------------------------------------------------------------------- 1 | // Solution 2 | 3 | string longestDigitsPrefix(string inputString) { 4 | string rt = ""; 5 | if(!Char.IsDigit(inputString[0])) return rt; 6 | else { 7 | foreach(char a in inputString) { 8 | if(!Char.IsDigit(a)) break; 9 | else rt += a; 10 | } 11 | } 12 | 13 | 14 | return rt; 15 | } 16 | -------------------------------------------------------------------------------- /CodeSignal/Arcade/Intro/41 - digitDegree.cs: -------------------------------------------------------------------------------- 1 | // Basic Solution 2 | 3 | int digitDegree(int n) { 4 | int count = 0; 5 | 6 | int Sum(int num) { 7 | if(num < 10) return count; 8 | 9 | int tot = 0; 10 | int expon = (int)Math.Log10(n); 11 | 12 | while(expon >= 0) tot += Convert.ToInt32((num - num % Math.Pow(10, expon)) / Math.Pow(10, expon--) % 10); 13 | count++; 14 | 15 | 16 | return Sum(tot); 17 | } 18 | 19 | return Sum(n); 20 | } 21 | -------------------------------------------------------------------------------- /CodeSignal/Arcade/Intro/42 - Bishop_and_Pawn.cs: -------------------------------------------------------------------------------- 1 | // Solution 2 | 3 | bool bishopAndPawn(string bishop, string pawn) { 4 | int letter = (int)bishop[0]; 5 | int dig = bishop[1] - '0'; 6 | string Combiner(int let, int digit) => $"{(char)let}{digit}"; 7 | 8 | for(int i = 1; i <= 7; i++) { 9 | if(Combiner(letter + i, dig + i) == pawn) return true; 10 | if(Combiner(letter - i, dig - i) == pawn) return true; 11 | if(Combiner(letter - i, dig + i) == pawn) return true; 12 | if(Combiner(letter + i, dig - i) == pawn) return true; 13 | } 14 | 15 | 16 | return false; 17 | } 18 | -------------------------------------------------------------------------------- /CodeSignal/Arcade/Intro/43 - isBeautifulString.cs: -------------------------------------------------------------------------------- 1 | // Solution 2 | 3 | bool isBeautifulString(string inputString) { 4 | int[] letter = new int[27]; 5 | 6 | foreach(char c in inputString) letter[c - 'a']++; 7 | for(int i = 1; i < letter.Length; i++) if(letter[i - 1] < letter[i]) return false; 8 | 9 | 10 | return true; 11 | } 12 | -------------------------------------------------------------------------------- /CodeSignal/Arcade/Intro/44 - FindEmailDomain.cs: -------------------------------------------------------------------------------- 1 | // Solution 2 | 3 | string findEmailDomain(string address) { 4 | string ret = ""; 5 | 6 | for(int i = address.Length - 1; i >= 0; i--) { 7 | char c = address[i]; 8 | if(c == '@') break; 9 | ret = c + ret; 10 | } 11 | 12 | return ret; 13 | } 14 | -------------------------------------------------------------------------------- /CodeSignal/Arcade/Intro/45 - buildPalindrome.cs: -------------------------------------------------------------------------------- 1 | // Solution 2 | 3 | string buildPalindrome(string st) { 4 | string build = ""; 5 | 6 | bool CheckPal(string fir) { 7 | int len = fir.Length / 2; 8 | for(int i = 0; i < len; i++) if(fir[i] != fir[fir.Length - 1 - i]) return false; 9 | 10 | return true; 11 | } 12 | 13 | foreach(char c in st) { 14 | if(CheckPal(st + build)) return st + build; 15 | build = c + build; 16 | } 17 | 18 | return "wasd"; 19 | } 20 | -------------------------------------------------------------------------------- /CodeSignal/Arcade/Intro/46 - Elections_Winners.cs: -------------------------------------------------------------------------------- 1 | int electionsWinners(int[] votes, int k) { 2 | int max = votes.Max(); 3 | int count = 0; 4 | 5 | if(k == 0) { 6 | votes[Array.IndexOf(votes, max)] = -1; 7 | if(votes.Max() == max) return 0; 8 | else votes[Array.IndexOf(votes, -1)] = max; 9 | 10 | return 1; 11 | } 12 | 13 | foreach(int i in votes) { 14 | if(max - k < i) count++; 15 | } 16 | 17 | return count; 18 | } 19 | -------------------------------------------------------------------------------- /CodeSignal/Arcade/Intro/47 - Is_MAC48_Address?.cs: -------------------------------------------------------------------------------- 1 | // Basic Solution 2 | 3 | bool isMAC48Address(string inputString) { 4 | Regex reg = new Regex(@"[A-F\d-]{14}-[A-F\d]{2}"); 5 | if(reg.IsMatch(inputString)) { 6 | if(reg.Matches(inputString)[0].ToString() == inputString) { 7 | return true; 8 | } 9 | } 10 | 11 | return false; 12 | } 13 | 14 | /*--------------------------------------------------------------------*/ 15 | /*--------------------------------------------------------------------*/ 16 | 17 | // Shorter Solution 18 | 19 | bool isMAC48Address(string inputString) { 20 | Regex reg = new Regex(@"[A-F\d-]{14}-[A-F\d]{2}"); 21 | if(reg.IsMatch(inputString)) if(reg.Matches(inputString)[0].ToString() == inputString) return true; 22 | 23 | return false; 24 | } 25 | -------------------------------------------------------------------------------- /CodeSignal/Arcade/Intro/48 - isDigit.cs: -------------------------------------------------------------------------------- 1 | // Basic Solution 2 | 3 | bool isDigit(char symbol) { 4 | if(Char.IsDigit(symbol)) return true; 5 | else return false; 6 | } 7 | 8 | 9 | /*----------------------------------------------------*/ 10 | /*----------------------------------------------------*/ 11 | 12 | // One-liner 13 | 14 | bool isDigit1(char symbol) => Char.IsDigit(symbol); 15 | -------------------------------------------------------------------------------- /CodeSignal/Arcade/Intro/49 - lineEncoding.cs: -------------------------------------------------------------------------------- 1 | // Solution 2 | 3 | string lineEncoding(string s) { 4 | string newLine = ""; 5 | int len = s.Length; 6 | 7 | for(int i = 0; i < len; i++) { 8 | char c = s[i]; 9 | int j = i; 10 | int count = 0; 11 | bool breaker = true; 12 | while(breaker) { 13 | j++; 14 | if(j - len >= 0) { 15 | count++; 16 | newLine += Str(c, count); 17 | return newLine; 18 | } 19 | if(s[j] == c) count++; 20 | else { 21 | j--; 22 | breaker = false; 23 | count++; 24 | } 25 | } 26 | newLine += Str(c, count); 27 | i = j; 28 | breaker = true; 29 | } 30 | 31 | string Str(char inp, int times) => (times == 1) ? $"{inp}" : $"{times}{inp}"; 32 | 33 | return newLine; 34 | } 35 | -------------------------------------------------------------------------------- /CodeSignal/Arcade/Intro/50 - chessKnight.cs: -------------------------------------------------------------------------------- 1 | // Solution 2 | 3 | int chessKnight(string cell) { 4 | int letter = (int)cell[0]; 5 | int num = cell[1] - '0'; 6 | int count = 0; 7 | 8 | bool IsValid(int c, int i) { 9 | if(!(97 <= c && c < 105)) return false; 10 | if(!(0 < i && i < 9)) return false; 11 | 12 | return true; 13 | } 14 | 15 | if(IsValid(letter - 2, num - 1)) count++; 16 | if(IsValid(letter - 2, num + 1)) count++; 17 | if(IsValid(letter - 1, num - 2)) count++; 18 | if(IsValid(letter - 1, num + 2)) count++; 19 | 20 | if(IsValid(letter + 1, num - 2)) count++; 21 | if(IsValid(letter + 1, num + 2)) count++; 22 | if(IsValid(letter + 2, num - 1)) count++; 23 | if(IsValid(letter + 2, num + 1)) count++; 24 | 25 | return count; 26 | } 27 | -------------------------------------------------------------------------------- /CodeSignal/Arcade/Intro/51 - deleteDigit.cs: -------------------------------------------------------------------------------- 1 | // Basic Solution 2 | 3 | int deleteDigit(int n) { 4 | string num = n.ToString(); 5 | int max = 0; 6 | 7 | for(int i = 0; i < num.Length; i++) { 8 | int processedNum = int.Parse(num.Remove(i, 1)); 9 | 10 | if(max < processedNum) max = processedNum; 11 | } 12 | 13 | 14 | return max; 15 | } 16 | 17 | 18 | /*-------------------------------------------------------*/ 19 | /*-------------------------------------------------------*/ 20 | 21 | // Non-string solution 22 | 23 | int deleteDigit(int n) { 24 | int l = (int)Math.Log10(n) + 1; 25 | double max = 0; 26 | 27 | for(int i = l; i > 0; i--) { 28 | double product = (n - (n % Math.Pow(10, i))) / 10 + n % Math.Pow(10, i - 1); 29 | if(max < product) max = product; 30 | } 31 | 32 | 33 | return (int)max; 34 | } 35 | -------------------------------------------------------------------------------- /CodeSignal/Arcade/Intro/52 - longestWord.cs: -------------------------------------------------------------------------------- 1 | // Solution 2 | 3 | string longestWord(string text) { 4 | string winner = ""; 5 | Regex rgx = new Regex(@"[A-Za-z]*"); 6 | 7 | foreach(Match matches in rgx.Matches(text)) { 8 | if(winner.Length < matches.Length) winner = matches.ToString(); 9 | } 10 | 11 | 12 | return winner; 13 | } 14 | -------------------------------------------------------------------------------- /CodeSignal/Arcade/Intro/53 - Valid_Time.cs: -------------------------------------------------------------------------------- 1 | // Basic Solution 2 | 3 | bool validTime(string time) { 4 | if((time[0] - '0' == 2 && time[1] - '0' >= 4) || time[0] - '0' > 2 || time[3] - '0' > 5) return false; 5 | 6 | return true; 7 | } 8 | 9 | 10 | /*--------------------------------------------------------------------------*/ 11 | /*--------------------------------------------------------------------------*/ 12 | 13 | // One-liner solution 14 | 15 | bool validTime1(string time) => !((time[0] - '0' == 2 && time[1] - '0' >= 4) || time[0] - '0' > 2 || time[3] - '0' > 5); 16 | -------------------------------------------------------------------------------- /CodeSignal/Arcade/Intro/54 - sumUpNumbers.cs: -------------------------------------------------------------------------------- 1 | // Solution 2 | 3 | int sumUpNumbers(string inputString) { 4 | int tot = 0; 5 | 6 | foreach(Match match in Regex.Matches(inputString, @"\d+")) { 7 | tot += int.Parse(match.ToString()); 8 | } 9 | 10 | return tot; 11 | } 12 | -------------------------------------------------------------------------------- /CodeSignal/Arcade/Intro/55 - Different_Squares.cs: -------------------------------------------------------------------------------- 1 | // Solution 2 | 3 | int differentSquares(int[][] matrix) { 4 | List stringContainer = new List(); 5 | 6 | string contain = ""; 7 | for(int i = 0; i < matrix.Length - 1; i++) { 8 | 9 | for(int j = 0; j < matrix[0].Length - 1; j++) { 10 | contain = $"{matrix[i][j]}{matrix[i][j + 1]}{matrix[i + 1][j]}{matrix[i + 1][j + 1]}"; 11 | if(!stringContainer.Contains(contain)) stringContainer.Add(contain); 12 | contain = ""; 13 | } 14 | } 15 | 16 | 17 | return stringContainer.Count(); 18 | } 19 | -------------------------------------------------------------------------------- /CodeSignal/Arcade/Intro/56 - digitsProduct.cs: -------------------------------------------------------------------------------- 1 | // Solution 2 | 3 | int digitsProduct(int product) { 4 | if(product == 0) return 10; 5 | if(product == 1) return 1; 6 | 7 | int sum = 0; 8 | int exponent = 0; 9 | 10 | for(int i = 9; 2 <= i; i--){ 11 | while(product % i == 0) { 12 | product /= i; 13 | sum += i * (int)Math.Pow(10, exponent++); 14 | } 15 | } 16 | 17 | return product == 1 ? sum : -1; 18 | } 19 | -------------------------------------------------------------------------------- /CodeSignal/Arcade/Intro/57 - File_Naming.cs: -------------------------------------------------------------------------------- 1 | // Solution 2 | 3 | string[] fileNaming(string[] names) { 4 | List newNames = new List(); 5 | List inst = new List(); 6 | 7 | for(int i = 0; i < names.Length; i++) { 8 | if(newNames.Contains(names[i])) { 9 | int listIndex = newNames.IndexOf(names[i]); 10 | 11 | int fileIndex = inst[listIndex]; 12 | string newName = names[i] + "(" + fileIndex + ")"; 13 | 14 | while(newNames.Contains(newName)) { 15 | fileIndex++; 16 | newName = names[i] + "(" + fileIndex + ")"; 17 | } 18 | 19 | inst[listIndex] = fileIndex; 20 | newNames.Add(newName); 21 | inst.Add(1); 22 | } else { 23 | newNames.Add(names[i]); 24 | inst.Add(1); 25 | } 26 | } 27 | 28 | return newNames.ToArray(); 29 | } 30 | -------------------------------------------------------------------------------- /CodeSignal/Arcade/Intro/58 - messageFromBinaryCode.cs: -------------------------------------------------------------------------------- 1 | // Solution 2 | 3 | string messageFromBinaryCode(string code) { 4 | string decode = ""; 5 | 6 | for(int i = 0; i < code.Length; i += 8) { 7 | string bin = ""; 8 | 9 | for(int j = i; j < i + 8; j++) { 10 | bin += code[j]; 11 | } 12 | 13 | decode += (char)Convert.ToInt32(bin, 2); 14 | } 15 | 16 | return decode; 17 | } 18 | -------------------------------------------------------------------------------- /CodeSignal/Arcade/Intro/59 - spiralNumbers.cs: -------------------------------------------------------------------------------- 1 | // Solution 2 | 3 | int[][] spiralNumbers(int n) { 4 | int[][] mat = new int[n][]; 5 | for(int l = 0; l < n; l++) { 6 | int[] row = new int[n]; 7 | mat[l] = row; 8 | } 9 | int direction = 0; 10 | 11 | int i = 0; 12 | int j = 0; 13 | for(int num = 1; num <= n * n; num++) { 14 | switch(direction) { 15 | case 0: 16 | if(j + 1 < n) { 17 | if(mat[i][j + 1] == 0) { 18 | mat[i][j] = num; 19 | j++; 20 | } else { 21 | direction = 1; 22 | mat[i][j] = num; 23 | i++; 24 | } 25 | } else { 26 | mat[i][j] = num; 27 | direction = 1; 28 | i++; 29 | } 30 | break; 31 | case 1: 32 | if(i + 1 < n) { 33 | if(mat[i + 1][j] == 0) { 34 | mat[i][j] = num; 35 | i++; 36 | } else { 37 | direction = 2; 38 | mat[i][j] = num; 39 | j--; 40 | } 41 | } else { 42 | mat[i][j] = num; 43 | direction = 2; 44 | j--; 45 | } 46 | break; 47 | case 2: 48 | if(j > 0) { 49 | if(mat[i][j - 1] == 0) { 50 | mat[i][j] = num; 51 | j--; 52 | } else { 53 | direction = 3; 54 | mat[i][j] = num; 55 | i--; 56 | } 57 | } else { 58 | mat[i][j] = num; 59 | direction = 3; 60 | i--; 61 | } 62 | break; 63 | case 3: 64 | if(i > 0) { 65 | if(mat[i - 1][j] == 0) { 66 | mat[i][j] = num; 67 | i--; 68 | } else { 69 | mat[i][j] = num; 70 | direction = 0; 71 | j++; 72 | } 73 | } else { 74 | mat[i][j] = num; 75 | direction = 0; 76 | j++; 77 | } 78 | break; 79 | default: 80 | Console.WriteLine(num); 81 | break; 82 | } 83 | } 84 | 85 | return mat; 86 | } 87 | -------------------------------------------------------------------------------- /CodeSignal/Arcade/Intro/60 - Sudoku.cs: -------------------------------------------------------------------------------- 1 | // Sudoku 2 | 3 | bool sudoku(int[][] grid) { 4 | int[] digit = new int[] {1, 2, 3, 4, 5, 6, 7, 8, 9}; 5 | int len = grid.Length; 6 | 7 | for(int i = 1; i < len; i += 3) { 8 | 9 | for(int j = 1; j < len; j += 3) { 10 | int sum = 0; 11 | for(int k = -1; k < 2; k++) { 12 | for(int l = -1; l < 2; l++) { 13 | sum += grid[i + k][j + l]; 14 | } 15 | } 16 | if(sum != 45) return false; 17 | } 18 | } 19 | 20 | for(int i = 0; i < len; i++) { 21 | List uniRow = new List(); 22 | List uniCol = new List(); 23 | for(int j = 0; j < len; j++) { 24 | uniRow.Add(grid[i][j]); 25 | uniCol.Add(grid[j][i]); 26 | } 27 | 28 | if(uniRow.Count() != new HashSet(uniRow).Count) return false; 29 | if(uniCol.Count() != new HashSet(uniCol).Count) return false; 30 | } 31 | 32 | return true; 33 | } 34 | -------------------------------------------------------------------------------- /CodeSignal/Arcade/The_Core/001 - Add_Two_Digits.cs: -------------------------------------------------------------------------------- 1 | // Basic Solution 2 | 3 | int addTwoDigits(int n) { 4 | return n % 10 + (n - n % 10) / 10; 5 | } 6 | 7 | 8 | /*------------------------------------------------------*/ 9 | /*------------------------------------------------------*/ 10 | 11 | // One-liner 12 | 13 | int addTwoDigits(int n) => n % 10 + (n - n % 10) / 10; 14 | -------------------------------------------------------------------------------- /CodeSignal/Arcade/The_Core/002 - Largest_Number.cs: -------------------------------------------------------------------------------- 1 | // Basic Solution 2 | 3 | int largestNumber(int n) { 4 | int pow = (int)Math.Pow(10, n); 5 | 6 | return pow - 1; 7 | } 8 | 9 | 10 | /*------------------------------------------------------*/ 11 | /*------------------------------------------------------*/ 12 | 13 | // One-liner 14 | 15 | int largestNumber(int n) => (int)(Math.Pow(10, n) - 1); 16 | -------------------------------------------------------------------------------- /CodeSignal/Arcade/The_Core/003 - Candies.cs: -------------------------------------------------------------------------------- 1 | // Basic Solution 2 | 3 | int candies(int n, int m) { 4 | return m - m % n; 5 | } 6 | 7 | 8 | /*------------------------------------------------------*/ 9 | /*------------------------------------------------------*/ 10 | 11 | // One-liner 12 | 13 | int candies(int n, int m) => m - m % n; 14 | -------------------------------------------------------------------------------- /CodeSignal/Arcade/The_Core/004 - Seats_in_Theater.cs: -------------------------------------------------------------------------------- 1 | // Basic Solution 2 | 3 | int seatsInTheater(int nCols, int nRows, int col, int row) { 4 | int affect = (nCols - col + 1) * (nRows - row); 5 | return affect; 6 | } 7 | 8 | /*------------------------------------------------------------------*/ 9 | /*------------------------------------------------------------------*/ 10 | 11 | // One-liner 12 | 13 | int seatsInTheater(int nCols, int nRows, int col, int row) => (nCols - col + 1) * (nRows - row); 14 | -------------------------------------------------------------------------------- /CodeSignal/Arcade/The_Core/005 - Max_Multiple.cs: -------------------------------------------------------------------------------- 1 | // Basic Solution 2 | 3 | int maxMultiple(int divisor, int bound) { 4 | return divisor * (bound / divisor); 5 | } 6 | 7 | /*--------------------------------------------------*/ 8 | /*--------------------------------------------------*/ 9 | 10 | // One-liner 11 | 12 | int maxMultiple(int divisor, int bound) => divisor * (bound / divisor); 13 | -------------------------------------------------------------------------------- /CodeSignal/Arcade/The_Core/006 - Circle_of_Numbers.cs: -------------------------------------------------------------------------------- 1 | // Basic Solution 2 | 3 | int circleOfNumbers(int n, int firstNumber) { 4 | return (n / 2 + firstNumber) % n; 5 | } 6 | 7 | /*----------------------------------------------------*/ 8 | /*----------------------------------------------------*/ 9 | 10 | // One-liner 11 | 12 | int circleOfNumbers(int n, int firstNumber) => (n / 2 + firstNumber) % n; 13 | -------------------------------------------------------------------------------- /CodeSignal/Arcade/The_Core/007 - Late_Ride.cs: -------------------------------------------------------------------------------- 1 | // Basic Solution 2 | 3 | int lateRide(int n) { 4 | int hour = n / 60; 5 | int minute = n - hour * 60; 6 | int hourSum = hour % 10 + hour / 10; 7 | int minuteSum = minute % 10 + minute / 10; 8 | 9 | 10 | return hourSum + minuteSum; 11 | } 12 | 13 | /*---------------------------------------------------------*/ 14 | /*---------------------------------------------------------*/ 15 | 16 | // One-liner 17 | 18 | int lateRide(int n) => (n / 60) % 10 + (n - (n / 60) * 60) % 10 + (n / 60) / 10 + (n - (n / 60) * 60) / 10; 19 | -------------------------------------------------------------------------------- /CodeSignal/Arcade/The_Core/008 - PhoneCall.cs: -------------------------------------------------------------------------------- 1 | // Solution 2 | 3 | int phoneCall(int min1, int min2_10, int min11, int s) { 4 | int call = 0; 5 | 6 | if(s - min1 >= 0) { 7 | s -= min1; 8 | call++; 9 | } else return call; 10 | 11 | for(int i = 0; i < 9; i++) { 12 | if(s - min2_10 >= 0) { 13 | s -= min2_10; 14 | call++; 15 | } else return call; 16 | } 17 | 18 | while(s - min11 >= 0) { 19 | s -= min11; 20 | call++; 21 | } 22 | 23 | return call; 24 | } 25 | -------------------------------------------------------------------------------- /CodeSignal/Arcade/The_Core/009 - ReachNextLevel.cs: -------------------------------------------------------------------------------- 1 | // Solution 2 | 3 | bool reachNextLevel(int experience, int threshold, int reward) => experience + reward >= threshold; 4 | -------------------------------------------------------------------------------- /CodeSignal/Arcade/The_Core/010 - Knapsack_Light.cs: -------------------------------------------------------------------------------- 1 | // Basic Solution 2 | 3 | int knapsackLight(int value1, int weight1, int value2, int weight2, int maxW) { 4 | if(weight1 + weight2 <= maxW) return value1 + value2; 5 | if(maxW < weight1 && maxW < weight2) return 0; 6 | if(weight1 > maxW) return value2; 7 | if(weight2 > maxW) return value1; 8 | return Math.Max(value1,value2); 9 | } 10 | 11 | /*------------------------------------------------------------------------*/ 12 | /*------------------------------------------------------------------------*/ 13 | 14 | // Dynamic programming solution 15 | 16 | int knapsackLight(int value1, int weight1, int value2, int weight2, int maxW) { 17 | int[] values = new int[] {value1, value2}; 18 | int[] weights = new int[] {weight1, weight2}; 19 | 20 | int[][] cap = new int[values.Length + 1][]; 21 | 22 | for(int i = 0; i <= weights.Length; i++) { 23 | int[] row = new int[maxW + 1]; 24 | for(int j = 0; j <= maxW; j++) { 25 | if(i == 0) row[j] = 0; 26 | else { 27 | row[j] = cap[i - 1][j] 28 | if(weights[i - 1] <= j) row[j] = Math.Max(values[i - 1] + cap[i - 1][j - weights[i - 1]], cap[i - 1][j]); 29 | } 30 | } 31 | cap[i] = row; 32 | } 33 | 34 | 35 | return cap[weights.Length][maxW]; 36 | } 37 | -------------------------------------------------------------------------------- /CodeSignal/Arcade/The_Core/011 - Extra_Number.cs: -------------------------------------------------------------------------------- 1 | // Solution 2 | 3 | int extraNumber(int a, int b, int c) { 4 | int diffAB = a - b; 5 | int diffAC = a - c; 6 | int diffBC = b - c; 7 | 8 | if(diffAB == 0) return c; 9 | if(diffAC == 0) return b; 10 | if(diffBC == 0) return a; 11 | 12 | return 1; 13 | } 14 | -------------------------------------------------------------------------------- /CodeSignal/Arcade/The_Core/012 - Is_Infinite_Process?.cs: -------------------------------------------------------------------------------- 1 | // Solution 2 | 3 | bool isInfiniteProcess(int a, int b) { 4 | 5 | while(a != b) { 6 | a++; 7 | b--; 8 | if(a > b) return true; 9 | } 10 | 11 | return false; 12 | } 13 | -------------------------------------------------------------------------------- /CodeSignal/Arcade/The_Core/013 - Arithmetic_Expression.cs: -------------------------------------------------------------------------------- 1 | // Basic Solution 2 | 3 | bool arithmeticExpression(int a, int b, int c) { 4 | 5 | if(a + b == c) return true; 6 | if(a - b == c) return true; 7 | if(a * b == c) return true; 8 | if(c * b == a) return true; 9 | 10 | return false; 11 | } 12 | 13 | /*------------------------------------------------------------*/ 14 | /*------------------------------------------------------------*/ 15 | 16 | // One-liner 17 | 18 | bool arithmeticExpression(int a, int b, int c) => a + b == c || a - b == c || a * b == c || c * b == a; 19 | -------------------------------------------------------------------------------- /CodeSignal/Arcade/The_Core/014 - Tennis_Set.cs: -------------------------------------------------------------------------------- 1 | // Solution 2 | 3 | bool tennisSet(int score1, int score2) { 4 | if(score1 == score2) return false; 5 | if(score1 > 7 || score2 > 7) return false; 6 | if((score1 == 7 && score2 < 5) || (score2 == 7 && score1 < 5) || score1 + score2 == 11) return false; 7 | if(score1 < 6 && score2 < 6) return false; 8 | 9 | 10 | return true; 11 | } 12 | -------------------------------------------------------------------------------- /CodeSignal/Arcade/The_Core/015 - Will_You?.cs: -------------------------------------------------------------------------------- 1 | // Basic Solution 2 | 3 | bool willYou(bool young, bool beautiful, bool loved) { 4 | if(!loved && beautiful && young) return true; 5 | if(loved && (!beautiful || !young)) return true; 6 | 7 | return false; 8 | } 9 | 10 | 11 | /*------------------------------------------------------*/ 12 | /*------------------------------------------------------*/ 13 | 14 | // One-liner 15 | 16 | bool willYou(bool young, bool beautiful, bool loved) => ((!loved && beautiful && young) || (loved && (!beautiful || !young))); 17 | -------------------------------------------------------------------------------- /CodeSignal/Arcade/The_Core/016 - Metro_Card.cs: -------------------------------------------------------------------------------- 1 | // Basic Solution 2 | 3 | int[] metroCard(int lastNumberOfDays) { 4 | if(lastNumberOfDays == 31) { 5 | return new [] { 28, 30, 31 }; 6 | } else { 7 | return new [] { 31 }; 8 | } 9 | } 10 | 11 | /*------------------------------------------------*/ 12 | /*------------------------------------------------*/ 13 | 14 | // One-liner 15 | 16 | int[] metroCard(int lastNumberOfDays) => (lastNumberOfDays == 31) ? new [] { 28, 30, 31 } : new [] { 31 }; 17 | -------------------------------------------------------------------------------- /CodeSignal/Arcade/The_Core/017 - Kill_K-th_Bit.cs: -------------------------------------------------------------------------------- 1 | // Basic Solution 2 | 3 | int killKthBit(int n, int k) 4 | { 5 | return n & ~(int)Math.Pow(2, k - 1); 6 | } 7 | 8 | /*------------------------------------------------*/ 9 | /*------------------------------------------------*/ 10 | 11 | // Pure Bitwise 12 | 13 | int killKthBit(int n, int k) 14 | { 15 | return n & ~(1 << (k - 1)); 16 | } 17 | -------------------------------------------------------------------------------- /CodeSignal/Arcade/The_Core/018 - Array_Packing.cs: -------------------------------------------------------------------------------- 1 | // Basic Solution 2 | 3 | int arrayPacking(int[] a) { 4 | int k = 0; 5 | int total = 0; 6 | 7 | foreach(int i in a) { 8 | total += i << (8 * k); 9 | k++; 10 | } 11 | 12 | 13 | return total; 14 | } 15 | 16 | /*---------------------------------------------*/ 17 | /*---------------------------------------------*/ 18 | 19 | // Compact Solution 20 | 21 | int arrayPacking(int[] a) { 22 | int k = 0; 23 | int total = 0; 24 | foreach(int i in a) total += i << (8 * k++); 25 | return total; 26 | } 27 | 28 | /*----------------------------------------------*/ 29 | /*----------------------------------------------*/ 30 | 31 | // One-liner 32 | 33 | int arrayPacking(int[] a) => a.Reverse().Aggregate((sigBit, total) => (sigBit << 8) + total); 34 | -------------------------------------------------------------------------------- /CodeSignal/Arcade/The_Core/019 - Range_Bit_Count.cs: -------------------------------------------------------------------------------- 1 | // Solution 2 | 3 | int rangeBitCount(int a, int b) { 4 | int count = 0; 5 | 6 | for(int i = a; i <= b; i++) { 7 | int num = i; 8 | 9 | while(num > 0) { 10 | if((num & 1) == 1) count++; 11 | num >>= 1; 12 | } 13 | } 14 | 15 | 16 | return count; 17 | } 18 | -------------------------------------------------------------------------------- /CodeSignal/Arcade/The_Core/020 - Mirror_Bits.cs: -------------------------------------------------------------------------------- 1 | // Bitwise Solution 2 | 3 | int mirrorBits(int a) { 4 | int newNum = 0; 5 | 6 | while(a > 1) { 7 | if((a & 1) == 1) newNum++; 8 | newNum <<= 1; 9 | a >>= 1; 10 | } 11 | 12 | return newNum + 1; 13 | } 14 | 15 | /*-------------------------------------------*/ 16 | /*-------------------------------------------*/ 17 | 18 | // Binary string solution 19 | 20 | int mirrorBits(int a) => Convert.ToInt32(Reverse(Convert.ToString(a, 2)), 2); 21 | 22 | static string Reverse(string s) { 23 | char[] charArray = s.ToCharArray(); 24 | Array.Reverse( charArray ); 25 | return new string( charArray ); 26 | } 27 | -------------------------------------------------------------------------------- /CodeSignal/Arcade/The_Core/021 - Second-RightMostBit.cs: -------------------------------------------------------------------------------- 1 | // Solution 2 | 3 | int secondRightmostZeroBit(int n) 4 | { 5 | return ~(n | (n + 1)) & (n | (n + 1)) + 1; 6 | } 7 | -------------------------------------------------------------------------------- /CodeSignal/Arcade/The_Core/022 - Swap_Adjacent_Bits.cs: -------------------------------------------------------------------------------- 1 | // Solution 2 | 3 | int swapAdjacentBits(int n) 4 | { 5 | return ((n & 0x15555555) << 1) | ((n & 0x2AAAAAAA) >> 1); 6 | } 7 | -------------------------------------------------------------------------------- /CodeSignal/Arcade/The_Core/023 - Different_Rightmost_Bit.cs: -------------------------------------------------------------------------------- 1 | // Solution 2 | 3 | int differentRightmostBit(int n, int m) 4 | { 5 | return (n ^ m) & -(n ^ m); 6 | } 7 | -------------------------------------------------------------------------------- /CodeSignal/Arcade/The_Core/024 - Equal_Pair_of_Bits.cs: -------------------------------------------------------------------------------- 1 | // Solution 2 | 3 | int equalPairOfBits(int n, int m) 4 | { 5 | return ~(n ^= m) & ++n; 6 | } 7 | -------------------------------------------------------------------------------- /CodeSignal/Arcade/The_Core/025 - Least_Factorial.cs: -------------------------------------------------------------------------------- 1 | // Basic Solution 2 | 3 | int leastFactorial(int n) { 4 | int product = 1; 5 | int factor = 1; 6 | 7 | while(n > product) { 8 | product *= factor++; 9 | } 10 | 11 | 12 | return product; 13 | } 14 | 15 | /*------------------------------------------------*/ 16 | /*------------------------------------------------*/ 17 | 18 | // Recursive solution 19 | 20 | int leastFactorial(int n) => factorial(n, 1, 1); 21 | static int factorial(int n, int i, int f) => (i >= n) ? i : factorial(n, i * ++f, f); 22 | -------------------------------------------------------------------------------- /CodeSignal/Arcade/The_Core/026 - Count_Sum_of_Two_Representations_2.cs: -------------------------------------------------------------------------------- 1 | // Solution 2 | 3 | int countSumOfTwoRepresentations2(int n, int l, int r) { 4 | double half = n * 0.5; 5 | int count = (int)Math.Floor((double)Math.Min(half - l, r - half)) + 1; 6 | 7 | 8 | return (count < 0) ? 0 : count; 9 | } 10 | -------------------------------------------------------------------------------- /CodeSignal/Arcade/The_Core/027 - Magical_Well.cs: -------------------------------------------------------------------------------- 1 | // Solution 2 | 3 | int magicalWell(int a, int b, int n) { 4 | int sum = 0; 5 | 6 | while(n-- != 0) { 7 | sum += a++ * b++; 8 | } 9 | 10 | return sum; 11 | } 12 | -------------------------------------------------------------------------------- /CodeSignal/Arcade/The_Core/028 - Lineup.cs: -------------------------------------------------------------------------------- 1 | // Solution 2 | 3 | int lineUp(string commands) { 4 | int count = 0; 5 | bool direction = true; 6 | 7 | foreach(char c in commands) { 8 | if(c != 'A') direction = !direction; 9 | if(direction) count++; 10 | } 11 | 12 | return count; 13 | } 14 | -------------------------------------------------------------------------------- /CodeSignal/Arcade/The_Core/029 - AdditionWithoutCarrying.cs: -------------------------------------------------------------------------------- 1 | // Solution 2 | 3 | int additionWithoutCarrying(int param1, int param2) { 4 | int newNum = 0; 5 | int log = (int)Math.Log10(Math.Max(param1, param2)) + 1; 6 | 7 | for(int i = 1; i <= log; i++) { 8 | int trailingNums = (int)Math.Pow(10 , i - 1); 9 | int par1 = (param1 % (trailingNums * 10) - param1 % trailingNums) / trailingNums; 10 | int par2 = (param2 % (trailingNums * 10) - param2 % trailingNums) / trailingNums; 11 | 12 | newNum += (((par1 + par2) % (trailingNums * 10)) % 10) * trailingNums; 13 | } 14 | 15 | 16 | return newNum; 17 | } 18 | -------------------------------------------------------------------------------- /CodeSignal/Arcade/The_Core/030 - Apple_Boxes.cs: -------------------------------------------------------------------------------- 1 | // Basic Solution 2 | 3 | int appleBoxes(int k) { 4 | int total = 0; 5 | bool countTurn = true; 6 | 7 | for(int i = 1; i <= k; i++) { 8 | if(countTurn) total -= i * i; 9 | else total += i * i; 10 | countTurn = !countTurn; 11 | } 12 | 13 | return total; 14 | } 15 | 16 | /*------------------------------------------*/ 17 | /*------------------------------------------*/ 18 | 19 | // Solution with a ternary operator 20 | 21 | int appleBoxes(int k) { 22 | int total = 0; 23 | bool countTurn = true; 24 | 25 | for(int i = 1; i <= k; i++) { 26 | total += countTurn ? -(i * i) : i * i; 27 | countTurn = !countTurn; 28 | } 29 | 30 | return total; 31 | } 32 | -------------------------------------------------------------------------------- /CodeSignal/Arcade/The_Core/031 - IncreaseNumberRoundness.cs: -------------------------------------------------------------------------------- 1 | // Solution 2 | 3 | bool increaseNumberRoundness(int n) { 4 | int log = (int)Math.Log10(n) + 1; 5 | bool strike = false; 6 | 7 | for(int i = 1; i <= log; i++) { 8 | int trailingNums = (int)Math.Pow(10 , i - 1); 9 | int digit = (n % (trailingNums * 10) - n % trailingNums) / trailingNums; 10 | 11 | if(strike && digit == 0) return true; 12 | if(digit != 0) strike = true; 13 | } 14 | 15 | 16 | return false; 17 | 18 | } 19 | -------------------------------------------------------------------------------- /CodeSignal/Arcade/The_Core/032 - Rounder.cs: -------------------------------------------------------------------------------- 1 | // Basic Solution 2 | 3 | int rounders(int n) { 4 | int log = (int)Math.Log10(n) + 1; 5 | 6 | for(int i = 1; i <= log; i++) { 7 | int trailingNums = (int)Math.Pow(10 , i - 1); 8 | int digit = (n % (trailingNums * 10)) / trailingNums; 9 | n -= n % trailingNums; 10 | if(5 <= digit && log != i) n += trailingNums * 10; 11 | } 12 | 13 | 14 | return n; 15 | } 16 | 17 | /*---------------------------------------------------*/ 18 | /*---------------------------------------------------*/ 19 | 20 | // Recursive solution 21 | 22 | int rounders(int n) { 23 | int log = (int)Math.Log10(n) + 1; 24 | 25 | int shrink(int num, int limit) { 26 | if(log < limit) return num; 27 | 28 | int trailingNums = (int)Math.Pow(10 , limit - 1); 29 | int digit = (num % (trailingNums * 10)) / trailingNums; 30 | num -= num % trailingNums; 31 | if(5 <= digit && log != limit) num += trailingNums * 10; 32 | return shrink(num, limit + 1); 33 | } 34 | 35 | 36 | return shrink(n, 1); 37 | } 38 | -------------------------------------------------------------------------------- /CodeSignal/Arcade/The_Core/033 - Candles.cs: -------------------------------------------------------------------------------- 1 | // Solution 2 | 3 | int candles(int candlesNumber, int makeNew) { 4 | int count = 0; 5 | int leftOver = 0; 6 | 7 | while(true) { 8 | count += candlesNumber; 9 | leftOver += candlesNumber; 10 | candlesNumber = (leftOver - leftOver % makeNew) / makeNew; 11 | leftOver -= candlesNumber * makeNew; 12 | 13 | if(leftOver == 0 && candlesNumber == 0) return count + candlesNumber; 14 | if(leftOver == leftOver % makeNew && candlesNumber == 0) return count; 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /CodeSignal/Arcade/The_Core/034 - Count_Black_Cells.cs: -------------------------------------------------------------------------------- 1 | // Solution 2 | 3 | int countBlackCells(int n, int m) { 4 | int commonDividor = 0; 5 | 6 | for(int i = 1; i <= n; i++) { 7 | if(n % i == 0 && m % i == 0) commonDividor = i; 8 | } 9 | 10 | return --n + m + --commonDividor; 11 | } 12 | -------------------------------------------------------------------------------- /CodeSignal/Arcade/The_Core/035 - Create_Array.cs: -------------------------------------------------------------------------------- 1 | // Solution 2 | 3 | int[] createArray(int size) => Enumerable.Repeat(1, size).ToArray(); 4 | -------------------------------------------------------------------------------- /CodeSignal/Arcade/The_Core/036 - ArrayReplace.cs: -------------------------------------------------------------------------------- 1 | // Basic Solution 2 | 3 | int[] arrayReplace(int[] inputArray, int elemToReplace, int substitutionElem) { 4 | for(int i = 0; i < inputArray.Length; i++) { 5 | if(inputArray[i] == elemToReplace) inputArray[i] = substitutionElem; 6 | } 7 | 8 | return inputArray; 9 | } 10 | 11 | /*-----------------------------------------------------------------------------*/ 12 | /*-----------------------------------------------------------------------------*/ 13 | 14 | // One-liner 15 | 16 | int[] arrayReplace(int[] inputArray, int elemToReplace, int substitutionElem) => inputArray.Select(x => x == elemToReplace ? substitutionElem : x).ToArray(); 17 | -------------------------------------------------------------------------------- /CodeSignal/Arcade/The_Core/037 - FirstReverseTry.cs: -------------------------------------------------------------------------------- 1 | // Solution 2 | 3 | int[] firstReverseTry(int[] arr) { 4 | if(arr.Length < 2) return arr; 5 | int firstItem = arr[0]; 6 | arr[0] = arr[arr.Length - 1]; 7 | arr[arr.Length - 1] = firstItem; 8 | 9 | return arr; 10 | } 11 | -------------------------------------------------------------------------------- /CodeSignal/Arcade/The_Core/038 - Concatenate_Arrays.cs: -------------------------------------------------------------------------------- 1 | // Basic Solution 2 | 3 | int[] concatenateArrays(int[] a, int[] b) { 4 | int[] arr = new int[a.Length + b.Length]; 5 | 6 | for(int i = 0; i < arr.Length; i++) { 7 | if(i < a.Length) arr[i] = a[i]; 8 | else arr[i] = b[i - a.Length]; 9 | } 10 | 11 | return arr; 12 | } 13 | 14 | /*-------------------------------------------------*/ 15 | /*-------------------------------------------------*/ 16 | 17 | // One-liner 18 | 19 | int[] concatenateArrays(int[] a, int[] b) => a.Concat(b).ToArray(); 20 | -------------------------------------------------------------------------------- /CodeSignal/Arcade/The_Core/039 - RemoveArrayPart.cs: -------------------------------------------------------------------------------- 1 | // Basic Solution 2 | 3 | int[] removeArrayPart(int[] inputArray, int l, int r) { 4 | List list = new List(); 5 | 6 | for(int i = 0; i < inputArray.Length; i++) { 7 | if(!(l <= i && i <= r)) list.Add(inputArray[i]); 8 | } 9 | 10 | return list.ToArray(); 11 | } 12 | 13 | /*----------------------------------------------------------*/ 14 | /*----------------------------------------------------------*/ 15 | 16 | // One-liner 17 | 18 | int[] removeArrayPart(int[] inputArray, int l, int r) => inputArray.Take(l).Concat(inputArray.Skip(++r)).ToArray(); 19 | -------------------------------------------------------------------------------- /CodeSignal/Arcade/The_Core/040 - Is_Smooth?.cs: -------------------------------------------------------------------------------- 1 | // Basic Solution 2 | 3 | bool isSmooth1(int[] arr) { 4 | if(arr.Length % 2 == 1) { 5 | int len = (arr.Length - 1) / 2; 6 | 7 | if(arr[0] == arr[len] && arr[len * 2] == arr[len]) return true; 8 | } else { 9 | int len = arr.Length / 2; 10 | 11 | if(arr[0] == arr[len] + arr[len - 1] && arr[len * 2 - 1] == arr[len] + arr[len - 1]) return true; 12 | } 13 | 14 | return false; 15 | } 16 | 17 | /*---------------------------------------------------------------------*/ 18 | /*---------------------------------------------------------------------*/ 19 | 20 | // One-liner ternary solution 21 | 22 | bool isSmooth(int[] arr) => ((arr.Length % 2 == 1) ? (arr[0] == arr[(arr.Length - 1) / 2] && arr[arr.Length - 1] == arr[(arr.Length - 1) / 2]) : (arr[0] == arr[arr.Length / 2] + arr[arr.Length / 2 - 1] && arr[arr.Length - 1] == arr[arr.Length / 2] + arr[arr.Length / 2 - 1])); 23 | -------------------------------------------------------------------------------- /CodeSignal/Arcade/The_Core/041 - ReplaceMiddle.cs: -------------------------------------------------------------------------------- 1 | // Basic Solution 2 | 3 | int[] replaceMiddle(int[] arr) { 4 | if(arr.Length % 2 == 1) { 5 | return arr; 6 | } else { 7 | int[] sum = new int[1] { arr[arr.Length / 2 - 1] + arr[arr.Length / 2] }; 8 | 9 | return arr.Take(arr.Length / 2 - 1).Concat(sum.Concat(arr.Skip(arr.Length / 2 + 1))).ToArray(); 10 | } 11 | } 12 | 13 | /*----------------------------------------------------------------------------*/ 14 | /*----------------------------------------------------------------------------*/ 15 | 16 | // One-liner 17 | 18 | int[] replaceMiddle(int[] arr) => (arr.Length % 2 == 1) ? arr : arr.Take(arr.Length / 2 - 1).Concat((new int[1] { arr[arr.Length / 2 - 1] + arr[arr.Length / 2] }).Concat(arr.Skip(arr.Length / 2 + 1))).ToArray(); 19 | -------------------------------------------------------------------------------- /CodeSignal/Arcade/The_Core/042 - MakeArrayConsecutive2.cs: -------------------------------------------------------------------------------- 1 | // Basic Solution 2 | 3 | int makeArrayConsecutive2(int[] statues) { 4 | int smallest = statues.Min(); 5 | int biggest = statues.Max(); 6 | 7 | return biggest - smallest - statues.Length + 1; 8 | } 9 | 10 | /*--------------------------------------------------------------------*/ 11 | /*--------------------------------------------------------------------*/ 12 | 13 | // One-liner 14 | 15 | int makeArrayConsecutive2(int[] statues) => statues.Max() - statues.Min() - statues.Length + 1; 16 | -------------------------------------------------------------------------------- /CodeSignal/Arcade/The_Core/043 - IsPower?.cs: -------------------------------------------------------------------------------- 1 | // Solution 2 | 3 | bool isPower(int n) { 4 | if(n == 1) return true; 5 | for(int i = 2; i <= 20; i++) { 6 | int exp = 1; 7 | while(Math.Pow(i, exp++) <= 400) { 8 | if(Math.Pow(i, exp) == n) return true; 9 | } 10 | } 11 | 12 | return false; 13 | } 14 | -------------------------------------------------------------------------------- /CodeSignal/Arcade/The_Core/044 - IsSumOfConsecutive2.cs: -------------------------------------------------------------------------------- 1 | // Solution 2 | 3 | int isSumOfConsecutive2(int n) { 4 | int count = 0; 5 | int start = 1; 6 | 7 | while(start <= n / 2) { 8 | int sum = start; 9 | int iteration = start + 1; 10 | 11 | while(sum < n) { 12 | sum += iteration++; 13 | if(sum == n) count++; 14 | } 15 | start++; 16 | } 17 | 18 | return count; 19 | } 20 | -------------------------------------------------------------------------------- /CodeSignal/Arcade/The_Core/045 - SquareDigitsSequence.cs: -------------------------------------------------------------------------------- 1 | // Solution 2 | 3 | int squareDigitsSequence(int a0) { 4 | int count = 0; 5 | List uniqeNums = new List(); 6 | 7 | int Sum(int num) { 8 | if(uniqeNums.Contains(num)) return count; 9 | uniqeNums.Add(num); 10 | 11 | int tot = 0; 12 | int expon = (int)Math.Log10(num); 13 | 14 | while(expon >= 0) tot += Convert.ToInt32(Math.Pow((num - num % Math.Pow(10, expon)) / Math.Pow(10, expon--) % 10, 2)); 15 | count++; 16 | 17 | 18 | return Sum(tot); 19 | } 20 | 21 | return Sum(a0) + 1; 22 | } 23 | -------------------------------------------------------------------------------- /CodeSignal/Arcade/The_Core/046 - PagesNumberingWithInk.cs: -------------------------------------------------------------------------------- 1 | // Solution 2 | 3 | int pagesNumberingWithInk(int current, int numberOfDigits) { 4 | while(0 < numberOfDigits) { 5 | int digits = (int)Math.Log10(current++) + 1; 6 | numberOfDigits -= digits; 7 | } 8 | 9 | return numberOfDigits < 0 ? --current - 1 : --current; 10 | } 11 | -------------------------------------------------------------------------------- /CodeSignal/Arcade/The_Core/047 - ComfortableNumbers.cs: -------------------------------------------------------------------------------- 1 | // Solution 2 | 3 | int comfortableNumbers(int l, int r) { 4 | int count = 0; 5 | int Sum(int num) { 6 | int tot = 0; 7 | int expon = (int)Math.Log10(num); 8 | 9 | while(expon >= 0) tot += Convert.ToInt32((num - num % Math.Pow(10, expon)) / Math.Pow(10, expon--) % 10); 10 | 11 | return tot; 12 | } 13 | 14 | bool mutualCom(int n, int m, int digSum) => Math.Abs(m - n) <= Math.Min(digSum, Sum(m)); 15 | 16 | for(int i = l; i <= r; i++) { 17 | int digit = Sum(i); 18 | 19 | int j = 1; 20 | while(0 <= digit - j && i + j <= r) if(mutualCom(i, i + j++, digit)) count++; 21 | 22 | j = 1; 23 | while(0 <= digit - j && l <= i - j) if(mutualCom(i, i - j++, digit)) count++; 24 | } 25 | 26 | return count / 2; 27 | } 28 | -------------------------------------------------------------------------------- /CodeSignal/Arcade/The_Core/048 - WeakNumbers.cs: -------------------------------------------------------------------------------- 1 | // Solution 2 | 3 | int[] weakNumbers(int n) { 4 | int[] nums = new int[n]; 5 | int[] weakness = new int[n]; 6 | int max = 1; 7 | int weaknessMax = 0; 8 | 9 | for(int i = 1; i <= n; i++) { 10 | int j = 1; 11 | 12 | while(0 <= i - j) { 13 | if(i % j++ == 0) nums[i - 1]++; 14 | } 15 | 16 | if(max < nums[i - 1]) max = nums[i - 1]; 17 | else weakness[i - 1] = nums.Where(x => nums[i - 1] < x).ToArray().Length; 18 | if(weaknessMax < weakness[i - 1]) weaknessMax = weakness[i - 1]; 19 | } 20 | 21 | return new int[] { weaknessMax, weakness.Where(x => x == weaknessMax).ToArray().Length }; 22 | } 23 | -------------------------------------------------------------------------------- /CodeSignal/Arcade/The_Core/049 - RectangleRotation.cs: -------------------------------------------------------------------------------- 1 | // Solution 2 | 3 | int rectangleRotation(int a, int b) { 4 | int big = a < b ? b : a; 5 | int count = 0; 6 | double sqroot2 = 1 / Math.Pow(2, 0.5); 7 | double upperLimit(int c) => Math.Min(-c + a * sqroot2, c + b * sqroot2); 8 | double lowerLimit(int c) => Math.Max(c - b * sqroot2, -c - a * sqroot2); 9 | 10 | for(int i = -big; i <= big; i++) for(int j = -big; j <= big; j++) if(lowerLimit(j) < i && i < upperLimit(j)) count++; 11 | 12 | return count; 13 | } 14 | -------------------------------------------------------------------------------- /CodeSignal/Arcade/The_Core/050 - CrosswordFormation.cs: -------------------------------------------------------------------------------- 1 | // Solution 2 | 3 | int crosswordFormation(string[] words) { 4 | return Iteration(words[0], words[1], words[2], words[3]) + 5 | Iteration(words[0], words[1], words[3], words[2]) + 6 | Iteration(words[0], words[2], words[1], words[3]) + 7 | Iteration(words[0], words[2], words[3], words[1]) + 8 | Iteration(words[0], words[3], words[1], words[2]) + 9 | Iteration(words[0], words[3], words[2], words[1]) + 10 | Iteration(words[1], words[0], words[2], words[3]) + 11 | Iteration(words[1], words[0], words[3], words[2]) + 12 | Iteration(words[1], words[2], words[0], words[3]) + 13 | Iteration(words[1], words[2], words[3], words[0]) + 14 | Iteration(words[1], words[3], words[0], words[2]) + 15 | Iteration(words[1], words[3], words[2], words[0]) + 16 | Iteration(words[2], words[0], words[1], words[3]) + 17 | Iteration(words[2], words[0], words[3], words[1]) + 18 | Iteration(words[2], words[1], words[3], words[0]) + 19 | Iteration(words[2], words[1], words[0], words[3]) + 20 | Iteration(words[2], words[3], words[0], words[1]) + 21 | Iteration(words[2], words[3], words[1], words[0]) + 22 | Iteration(words[3], words[0], words[1], words[2]) + 23 | Iteration(words[3], words[0], words[2], words[1]) + 24 | Iteration(words[3], words[1], words[0], words[2]) + 25 | Iteration(words[3], words[1], words[2], words[0]) + 26 | Iteration(words[3], words[2], words[0], words[1]) + 27 | Iteration(words[3], words[2], words[1], words[0]); 28 | 29 | } 30 | 31 | 32 | int Iteration(string fWord, string sWord, string tWord, string foWord) { 33 | int count = 0; 34 | 35 | for(int i = 0; i < fWord.Length - 2; i++) { 36 | 37 | for(int k = 0; k < tWord.Length - 2; k++) { 38 | if(fWord[i] == tWord[k]) { 39 | 40 | for(int j = i + 2; j < fWord.Length; j++) { 41 | 42 | for(int l = 0; l < foWord.Length - 2; l++) { 43 | if(fWord[j] == foWord[l]) { 44 | 45 | int hPoint = j - i; 46 | int vPoint = l - k; 47 | 48 | for(int p = 0; p < sWord.Length - hPoint; p++) { 49 | 50 | for(int q = k + 2; q < tWord.Length; q++) { 51 | if(foWord.Length > q + vPoint) { 52 | if(sWord[p] == tWord[q] && sWord[p + hPoint] == foWord[q + vPoint]) count++; 53 | } 54 | } 55 | } 56 | } 57 | } 58 | } 59 | } 60 | } 61 | } 62 | 63 | return count; 64 | } 65 | -------------------------------------------------------------------------------- /CodeSignal/Arcade/The_Core/051 - EncloseInBrackets.cs: -------------------------------------------------------------------------------- 1 | // Solution 2 | 3 | string encloseInBrackets(string inputString) => $"({inputString})"; 4 | -------------------------------------------------------------------------------- /CodeSignal/Arcade/The_Core/052 - ProperNounCorrection.cs: -------------------------------------------------------------------------------- 1 | // Basic Solution 2 | 3 | string properNounCorrection(string noun) { 4 | string fixedNoun = noun.ToLower().Substring(1); 5 | 6 | if(64 < (int)noun[0] && (int)noun[0] < 91) { 7 | return noun[0] + fixedNoun; 8 | } else { 9 | return (char)((int)noun[0] - 32) + fixedNoun; 10 | } 11 | } 12 | 13 | /*-------------------------------------------------------------------------*/ 14 | /*-------------------------------------------------------------------------*/ 15 | 16 | // One-liner 17 | 18 | string properNounCorrection(string noun) => ((64 < (int)noun[0] && (int)noun[0] < 91) ? noun[0] : (char)(noun[0] - ' ')) + noun.ToLower().Substring(1); 19 | -------------------------------------------------------------------------------- /CodeSignal/Arcade/The_Core/053 - IsTandemRepeat.cs: -------------------------------------------------------------------------------- 1 | // Solution 2 | 3 | bool isTandemRepeat(string inputString) => inputString.Substring(0, inputString.Length / 2) == inputString.Substring(inputString.Length / 2); 4 | -------------------------------------------------------------------------------- /CodeSignal/Arcade/The_Core/054 - IsCaseInsensitivePalindrome.cs: -------------------------------------------------------------------------------- 1 | // Solution 2 | 3 | bool isCaseInsensitivePalindrome(string inputString) { 4 | string str = inputString.ToLower(); 5 | 6 | for(int i = 0; i < str.Length / 2; i++) { 7 | if(str[i] != str[str.Length - i - 1]) return false; 8 | } 9 | 10 | return true; 11 | } 12 | -------------------------------------------------------------------------------- /CodeSignal/Arcade/The_Core/055 - FindEmailDomain.cs: -------------------------------------------------------------------------------- 1 | // Solution 2 | 3 | string findEmailDomain(string address) => address.Split("@").Last(); 4 | -------------------------------------------------------------------------------- /CodeSignal/Arcade/The_Core/056 - HtmlEndTagByStartTag.cs: -------------------------------------------------------------------------------- 1 | // Solution 2 | 3 | string htmlEndTagByStartTag(string startTag) { 4 | string tag = startTag.Split(" ")[0].Substring(1); 5 | 6 | return "' ? "" : ">"); 7 | } 8 | -------------------------------------------------------------------------------- /CodeSignal/Arcade/The_Core/057 - IsMAC48Address.cs: -------------------------------------------------------------------------------- 1 | // Solution 2 | 3 | bool isMAC48Address(string inputString) { 4 | Regex reg = new Regex(@"[A-F\d-]{14}-[A-F\d]{2}"); 5 | if(reg.IsMatch(inputString)) { 6 | if(reg.Matches(inputString)[0].ToString() == inputString) { 7 | return true; 8 | } 9 | } 10 | 11 | return false; 12 | } 13 | -------------------------------------------------------------------------------- /CodeSignal/Arcade/The_Core/058 - IsUnstablePair.cs: -------------------------------------------------------------------------------- 1 | // Solution 2 | 3 | bool isUnstablePair(string filename1, string filename2) { 4 | if(String.Compare(filename2, filename1, StringComparison.Ordinal) > 0) return String.Compare(filename2.ToUpper(), filename1.ToUpper(), StringComparison.Ordinal) < 0; 5 | else if(String.Compare(filename2, filename1, StringComparison.Ordinal) < 0) return String.Compare(filename2.ToLower(), filename1.ToLower(), StringComparison.Ordinal) > 0; 6 | 7 | return false; 8 | } 9 | -------------------------------------------------------------------------------- /CodeSignal/Arcade/The_Core/059 - StringsConstruction.cs: -------------------------------------------------------------------------------- 1 | // Solution 2 | 3 | int stringsConstruction(string a, string b) { 4 | int[] letters = new int[26]; 5 | int[] target = new int[26]; 6 | int count = -1; 7 | 8 | foreach(char c in a) letters[c - 'a']++; 9 | foreach(char c in b) target[c - 'a']++; 10 | 11 | while(!target.Any(x => x < 0)) { 12 | for(int i = 0; i < target.Length; i++) { 13 | target[i] -= letters[i]; 14 | } 15 | count++; 16 | } 17 | 18 | return count; 19 | } 20 | -------------------------------------------------------------------------------- /CodeSignal/Arcade/The_Core/060 - IsSubstitutionCipher.cs: -------------------------------------------------------------------------------- 1 | // Solution 2 | 3 | bool isSubstitutionCipher(string string1, string string2) { 4 | char[] letters = new char[26].Select(x => ' ').ToArray(); 5 | char[] letters2 = letters.ToArray(); 6 | 7 | for(int i = 0; i < string1.Length; i++) { 8 | if(letters[string1[i] - 'a'] == ' ') letters[string1[i] - 'a'] = string2[i]; 9 | else if(letters[string1[i] - 'a'] != string2[i]) return false; 10 | } 11 | 12 | for(int i = 0; i < string1.Length; i++) { 13 | if(letters2[string2[i] - 'a'] == ' ') letters2[string2[i] - 'a'] = string1[i]; 14 | else if(letters2[string2[i] - 'a'] != string1[i]) return false; 15 | } 16 | 17 | return true; 18 | } 19 | -------------------------------------------------------------------------------- /CodeSignal/Arcade/The_Core/061 - Create_Anagram.cs: -------------------------------------------------------------------------------- 1 | // Solution 2 | 3 | int createAnagram(string s, string t) { 4 | int[] letters1 = new int[26]; 5 | int[] letters2 = new int[26]; 6 | 7 | for(int i = 0; i < s.Length; i++) { 8 | letters1[s[i] - 'A']++; 9 | letters2[t[i] - 'A']++; 10 | } 11 | 12 | for(int i = 0; i < letters1.Length; i++) { 13 | while(letters1[i] > 0 && letters2[i] > 0) { 14 | letters1[i]--; 15 | letters2[i]--; 16 | } 17 | } 18 | 19 | return letters1.Concat(letters2).Sum() / 2; 20 | } 21 | -------------------------------------------------------------------------------- /CodeSignal/Arcade/The_Core/062 - ConstructSquare.cs: -------------------------------------------------------------------------------- 1 | // Solution 2 | 3 | int constructSquare(string s) { 4 | int top = (int)Math.Pow(10, s.Length) - 1; 5 | int upper = (int)Math.Floor(Math.Pow((top), 0.5)); 6 | int lower = (int)Math.Pow((top + 1) / 10, 0.5); 7 | var a = s.ToLookup(c => c); 8 | int distinct = a.Count(); 9 | int[] arr = a.Select(x => x.Count()).ToArray(); 10 | 11 | 12 | for(int i = upper; i > lower; i--) { 13 | string sqr = (i * i).ToString(); 14 | if(distinct == sqr.Distinct().Count()) { 15 | var digitArr = sqr.ToLookup(c => c).Select(x => x.Count()); 16 | if(!arr.Any(x => !digitArr.Contains(x))) return int.Parse(sqr); 17 | } 18 | } 19 | 20 | return -1; 21 | } 22 | -------------------------------------------------------------------------------- /CodeSignal/Arcade/The_Core/063 - Numbers_Grouping.cs: -------------------------------------------------------------------------------- 1 | // Basic Solution 2 | 3 | int numbersGrouping(int[] a) { 4 | int[] num = new int[a.Length]; 5 | 6 | for(int i = 0; i < a.Length; i++) { 7 | a[i] = (a[i] - 1) / 10000; 8 | } 9 | 10 | return a.Distinct().Count() + a.Length; 11 | } 12 | 13 | /*--------------------------------------------------------*/ 14 | /*--------------------------------------------------------*/ 15 | 16 | // One-liner 17 | 18 | int numbersGrouping(int[] a) => a.Select(x => --x / 10000).Distinct().Count() + a.Length; 19 | -------------------------------------------------------------------------------- /CodeSignal/Arcade/The_Core/064 - DifferentSquares.cs: -------------------------------------------------------------------------------- 1 | // Solution 2 | 3 | int differentSquares(int[][] matrix) { 4 | List stringContainer = new List(); 5 | 6 | string contain = ""; 7 | for(int i = 0; i < matrix.Length - 1; i++) { 8 | 9 | for(int j = 0; j < matrix[0].Length - 1; j++) { 10 | contain = $"{matrix[i][j]}{matrix[i][j + 1]}{matrix[i + 1][j]}{matrix[i + 1][j + 1]}"; 11 | if(!stringContainer.Contains(contain)) stringContainer.Add(contain); 12 | contain = ""; 13 | } 14 | } 15 | 16 | 17 | return stringContainer.Count(); 18 | } 19 | -------------------------------------------------------------------------------- /CodeSignal/Arcade/The_Core/065 - MostFrequentDigitSum.cs: -------------------------------------------------------------------------------- 1 | // Solution 2 | 3 | int mostFrequentDigitSum(int n) { 4 | List sum = new List(); 5 | 6 | while(0 < n) { 7 | string num = n.ToString(); 8 | int digSum = 0; 9 | for(int i = 0; i < num.Length; i++) digSum += num[i] - '0'; 10 | sum.Add(digSum); 11 | n -= digSum; 12 | } 13 | 14 | return sum.GroupBy(i => i).OrderBy(g => g.Count()).Select(g => g.Key).ToList().Last(); 15 | } 16 | -------------------------------------------------------------------------------- /CodeSignal/Arcade/The_Core/066 - NumberOfClans.cs: -------------------------------------------------------------------------------- 1 | // Solution 2 | 3 | int numberOfClans(int[] divisors, int k) { 4 | List unique = new List(); 5 | int count = 0; 6 | 7 | for(int i = 1; i <= k; i++) { 8 | string num = ""; 9 | foreach(int n in divisors) num += (i % n == 0 ? "1" : "0"); 10 | count += (unique.Contains(num) ? 0 : 1); 11 | unique.Add(num); 12 | } 13 | 14 | return count; 15 | } 16 | -------------------------------------------------------------------------------- /CodeSignal/Arcade/The_Core/067 - HouseNumbersSum.cs: -------------------------------------------------------------------------------- 1 | // Solution 2 | 3 | int houseNumbersSum(int[] inputArray) => inputArray.Take(Array.IndexOf(inputArray, 0)).Sum(); 4 | -------------------------------------------------------------------------------- /CodeSignal/Arcade/The_Core/068 - AllLongestStrings.cs: -------------------------------------------------------------------------------- 1 | // Basic Solution 2 | 3 | string[] allLongestStrings(string[] inputArray) { 4 | int maxLen = 0; 5 | List ret = new List(); 6 | 7 | for(int i = 0; i < inputArray.Length; i++) { 8 | if(inputArray[i].Length > maxLen) { 9 | maxLen = inputArray[i].Length; 10 | } 11 | } 12 | 13 | for(int j = 0; j < inputArray.Length; j++) { 14 | if(inputArray[j].Length == maxLen) { 15 | ret.Add(inputArray[j]); 16 | } 17 | } 18 | 19 | 20 | return ret.ToArray(); 21 | } 22 | 23 | /*---------------------------------------------------------------------*/ 24 | /*---------------------------------------------------------------------*/ 25 | 26 | // Functional Solution 27 | 28 | string[] allLongestStrings(string[] inputArray) { 29 | int length = inputArray.Max(word => word.Length); 30 | 31 | return inputArray.Where(x => x.Length == length).ToArray(); 32 | } 33 | 34 | /*---------------------------------------------------------------------*/ 35 | /*---------------------------------------------------------------------*/ 36 | 37 | // Less efficient one-liner 38 | 39 | string[] allLongestStrings(string[] inputArray) => inputArray.Where(x => x.Length == inputArray.Max(word => word.Length)).ToArray(); 40 | -------------------------------------------------------------------------------- /CodeSignal/Arcade/The_Core/069 - HouseOfCats.cs: -------------------------------------------------------------------------------- 1 | // Solution 2 | 3 | int[] houseOfCats(int legs) { 4 | int[] arr = new int[1 + legs / 4]; 5 | arr[0] = (legs % 4) / 2; 6 | 7 | for(int i = 1; i < arr.Length; i++) { 8 | arr[i] = arr[i - 1] + 2; 9 | } 10 | 11 | return arr; 12 | } 13 | -------------------------------------------------------------------------------- /CodeSignal/Arcade/The_Core/070 - AlphabetSubsequence.cs: -------------------------------------------------------------------------------- 1 | // Solution 2 | 3 | bool alphabetSubsequence(string s) { 4 | for(int i = 1; i < s.Length; i++) { 5 | if((int)s[i] <= (int)s[i - 1]) return false; 6 | } 7 | 8 | return true; 9 | } 10 | -------------------------------------------------------------------------------- /CodeSignal/Arcade/The_Core/071 - MinimalNumberOfCoins.cs: -------------------------------------------------------------------------------- 1 | // Solution 2 | 3 | int minimalNumberOfCoins(int[] coins, int price) { 4 | int[] dp = new int[price + 1]; 5 | 6 | for(int i = 1; i <= price; i++) { 7 | 8 | for(int j = 0; j < coins.Length && (0 <= i - coins[j]); j++) { 9 | int placeHolder = (j == 0 ? 1000 : dp[i]); 10 | dp[i] = Math.Min(dp[i - coins[j]] + 1, placeHolder); 11 | } 12 | } 13 | 14 | return dp.Last(); 15 | } 16 | -------------------------------------------------------------------------------- /CodeSignal/Arcade/The_Core/072 - AddBorder.cs: -------------------------------------------------------------------------------- 1 | // Solution 2 | 3 | string[] addBorder(string[] picture) { 4 | string ast = ""; 5 | foreach(char c in picture[0]) ast += "*"; 6 | string[] astBorder = new string[] {ast}; 7 | 8 | return astBorder.Concat(picture).Concat(astBorder).Select(x => "*" + x + "*").ToArray(); 9 | } 10 | -------------------------------------------------------------------------------- /CodeSignal/Arcade/The_Core/073 - SwitchLights.cs: -------------------------------------------------------------------------------- 1 | // Solution 2 | 3 | int[] switchLights(int[] a) { 4 | 5 | for(int i = 0; i < a.Length; i++) { 6 | if(a[i] == 1) { 7 | a[i] = 0; 8 | for(int j = 0; j < i; j++) { 9 | a[j] = (a[j] + 1) % 2; 10 | } 11 | } 12 | } 13 | 14 | return a; 15 | } 16 | -------------------------------------------------------------------------------- /CodeSignal/Arcade/The_Core/074 - TimedReading.cs: -------------------------------------------------------------------------------- 1 | // Basic Solution 2 | 3 | int timedReading(int maxLength, string text) { 4 | Regex rgx = new Regex(@"\w+"); 5 | int count = 0; 6 | 7 | foreach(Match match in rgx.Matches(text)) { 8 | if(match.Value.Length <= maxLength) count++; 9 | } 10 | 11 | return count; 12 | } 13 | 14 | /*----------------------------------------------------------*/ 15 | /*----------------------------------------------------------*/ 16 | 17 | // One-liner 18 | 19 | int timedReading(int maxLength, string text) => new Regex(@"\w+").Matches(text).Where(x => x.Value.Length <= maxLength).Count(); 20 | -------------------------------------------------------------------------------- /CodeSignal/Arcade/The_Core/075 - ElectionsWinners.cs: -------------------------------------------------------------------------------- 1 | // Basic Solution 2 | 3 | int electionsWinners(int[] votes, int k) { 4 | int max = votes.Max(); 5 | int count = 0; 6 | 7 | if(k == 0) { 8 | votes[Array.IndexOf(votes, max)] = -1; 9 | if(votes.Max() == max) return 0; 10 | else votes[Array.IndexOf(votes, -1)] = max; 11 | 12 | return 1; 13 | } 14 | 15 | foreach(int i in votes) { 16 | if(max - k < i) count++; 17 | } 18 | 19 | return count; 20 | } 21 | 22 | /*-----------------------------------------------------------------*/ 23 | /*-----------------------------------------------------------------*/ 24 | 25 | // Functional Solution 26 | 27 | int electionsWinners(int[] votes, int k) { 28 | int max = votes.Max(); 29 | int[] winners = votes.Where(x => (k != 0 ? max < (x + k) : x == max)).ToArray(); 30 | 31 | if(k == 0) return (winners.Length != 1 ? 0 : 1); 32 | return winners.Length; 33 | } 34 | -------------------------------------------------------------------------------- /CodeSignal/Arcade/The_Core/076 - IntegerToStringOfFixedWidth.cs: -------------------------------------------------------------------------------- 1 | // Solution 2 | 3 | string integerToStringOfFixedWidth(int number, int width) { 4 | string num = new String('0', 45) + number.ToString(); 5 | return num.Substring(num.Length - width); 6 | } 7 | -------------------------------------------------------------------------------- /CodeSignal/Arcade/The_Core/077 - AreSimilar?.cs: -------------------------------------------------------------------------------- 1 | // Basic Solution 2 | 3 | bool areSimilar(int[] a, int[] b) { 4 | int count = 0; 5 | int[] atwo = new int[2]; 6 | int[] btwo = new int[2]; 7 | int index = 0; 8 | 9 | for(int i = 0; i < a.Length; i++) { 10 | if(a[i] != b[i]) { 11 | count++; 12 | if(count == 3) return false; 13 | atwo[index] = a[i]; 14 | btwo[index++] = b[i]; 15 | } 16 | } 17 | 18 | Array.Reverse(atwo); 19 | 20 | for(int i = 0; i < atwo.Length; i++) if(atwo[i] != btwo[i]) return false; 21 | 22 | return true; 23 | } 24 | 25 | /*--------------------------------------------------------------------------------*/ 26 | /*--------------------------------------------------------------------------------*/ 27 | 28 | // Shorter Solution 29 | 30 | bool areSimilar(int[] a, int[] b) { 31 | List list = a.ToList(); 32 | int strike = 0; 33 | 34 | for(int i = 0; i < a.Length; i++) { 35 | if(a[i] != b[i]) strike++; 36 | if(list.Contains(b[i])) list.Remove(b[i]); 37 | } 38 | 39 | return ((list.Count() == 0) && (strike == 0 || strike == 2)); 40 | } 41 | -------------------------------------------------------------------------------- /CodeSignal/Arcade/The_Core/078 - AdaNumber.cs: -------------------------------------------------------------------------------- 1 | // Solution 2 | 3 | bool adaNumber(string line) { 4 | string num = string.Join("", line.Split('_')); 5 | string[] elem = num.Split('#'); 6 | char[] c = new char[] {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'}; 7 | 8 | if(elem[0] == "") return false; 9 | if(!num.Contains('#')) return !(new Regex(@"[^\d]").IsMatch(elem[0])); 10 | if(num.Count(f => f == '#') == 2) { 11 | if(elem[0] == "" || elem[1] == "") return false; 12 | int bas = int.Parse(elem[0]); 13 | if(bas < 2 || 16 < bas) return false; 14 | elem[1] = elem[1].ToUpper(); 15 | if(!(new Regex(@"[^A-F\d]").IsMatch(elem[1]))) { 16 | char[] range = c.Take(bas).ToArray(); 17 | foreach(char ch in elem[1]) if(Array.IndexOf(range, ch) == -1) return false; 18 | return true; 19 | } 20 | } 21 | 22 | return false; 23 | } 24 | -------------------------------------------------------------------------------- /CodeSignal/Arcade/The_Core/079 - ThreeSplit.cs: -------------------------------------------------------------------------------- 1 | // Solution 2 | 3 | int threeSplit(int[] a) { 4 | long sum = 0; 5 | foreach(int i in a) sum += i; 6 | int cut = 0; 7 | int total = 0; 8 | long currSum = 0; 9 | 10 | for(int i = 0; i < a.Length; i++) { 11 | currSum += a[i]; 12 | 13 | if(currSum == 2 * sum / 3) { 14 | total += cut; 15 | if(sum / 3 == 0) total--; 16 | } 17 | if(currSum == sum / 3) cut++; 18 | } 19 | 20 | return total + (sum == 0 ? 1 : 0); 21 | } 22 | -------------------------------------------------------------------------------- /CodeSignal/Arcade/The_Core/080 - CharacterParity.cs: -------------------------------------------------------------------------------- 1 | // Basic Solution 2 | 3 | string characterParity(char symbol) { 4 | bool digit = char.IsDigit(symbol); 5 | 6 | if(digit) { 7 | if((symbol - '0') % 2 == 1) { 8 | return "odd"; 9 | } else { 10 | return "even"; 11 | } 12 | } 13 | 14 | return "not a digit"; 15 | } 16 | 17 | /*--------------------------------------------------------*/ 18 | /*--------------------------------------------------------*/ 19 | 20 | // One-liner 21 | 22 | string characterParity(char symbol) => (char.IsDigit(symbol) ? ((symbol - '0') % 2 == 1 ? "odd" : "even") : "not a digit"); 23 | -------------------------------------------------------------------------------- /CodeSignal/Arcade/The_Core/081 - ReflectString.cs: -------------------------------------------------------------------------------- 1 | // Basic Solution 2 | 3 | string reflectString1(string inputString) { 4 | string str = ""; 5 | 6 | foreach(char c in inputString) { 7 | str += (char)('z' - c + 'a'); 8 | } 9 | 10 | return str; 11 | } 12 | 13 | /*-------------------------------------------------------*/ 14 | /*-------------------------------------------------------*/ 15 | 16 | // One-liner 17 | 18 | string reflectString(string inputString) => string.Join("", inputString.ToCharArray().Select(x => (char)('z' - x + 'a'))); 19 | -------------------------------------------------------------------------------- /CodeSignal/Arcade/The_Core/082 - NewNumeralSystem.cs: -------------------------------------------------------------------------------- 1 | // Solution 2 | 3 | string[] newNumeralSystem(char number) { 4 | List arr = new List(); 5 | char num = 'A'; 6 | 7 | while((int)num <= (int)number) { 8 | arr.Add($"{num} + {number}"); 9 | num = (char)((int)num + 1); 10 | number = (char)((int)number - 1); 11 | } 12 | 13 | return arr.ToArray(); 14 | } 15 | -------------------------------------------------------------------------------- /CodeSignal/Arcade/The_Core/083 - Cipher26.cs: -------------------------------------------------------------------------------- 1 | // Solution 2 | 3 | string cipher26(string message) { 4 | string decryp = $"{message[0]}"; 5 | int tot = message[0] - 'a'; 6 | 7 | for(int i = 1; i < message.Length; i++) { 8 | decryp += (char)(message[i] - tot + (message[i] - tot - 'a' < 0 ? 26 : 0)); 9 | tot += decryp[i] - 'a'; 10 | tot = tot % 26; 11 | } 12 | 13 | return decryp; 14 | } 15 | -------------------------------------------------------------------------------- /CodeSignal/Arcade/The_Core/084 - StolenLunch.cs: -------------------------------------------------------------------------------- 1 | // Solution 2 | 3 | string stolenLunch(string note) { 4 | string dec = ""; 5 | 6 | foreach(char c in note) { 7 | if(0 <= c - 'a' && c - 'a' < 10) dec += c - 'a'; 8 | else if(0 <= c - '0' && c - '0' < 10) dec += (char)(c + '1'); 9 | else dec += c; 10 | } 11 | 12 | return dec; 13 | } 14 | -------------------------------------------------------------------------------- /CodeSignal/Arcade/The_Core/085 - HigherVersion.cs: -------------------------------------------------------------------------------- 1 | // Solution 2 | 3 | bool higherVersion(string ver1, string ver2) { 4 | string[] arr1 = ver1.Split('.'); 5 | string[] arr2 = ver2.Split('.'); 6 | 7 | for(int i = 0; i < arr1.Length; i++) { 8 | if(arr1[i] == arr2[i]) continue; 9 | return int.Parse(arr2[i]) < int.Parse(arr1[i]); 10 | } 11 | 12 | return false; 13 | } 14 | -------------------------------------------------------------------------------- /CodeSignal/Arcade/The_Core/086 - Decipher.cs: -------------------------------------------------------------------------------- 1 | // Basic Solution 2 | 3 | string decipher(string cipher) { 4 | string decryp = ""; 5 | 6 | int letter = 0; 7 | for(int i = 0; i < cipher.Length; i++) { 8 | letter = letter * 10 + (cipher[i] - '0'); 9 | if(96 < letter) { 10 | decryp += (char)letter; 11 | letter = 0; 12 | } 13 | } 14 | 15 | return decryp; 16 | } 17 | 18 | /*----------------------------------------------------------------*/ 19 | /*----------------------------------------------------------------*/ 20 | 21 | // Solution with foreach 22 | 23 | string decipher(string cipher) { 24 | string decryp = ""; 25 | 26 | int letter = 0; 27 | foreach(char c in cipher) { 28 | letter = letter * 10 + (c - '0'); 29 | if(96 < letter) { 30 | decryp += (char)letter; 31 | letter = 0; 32 | } 33 | } 34 | 35 | return decryp; 36 | } 37 | -------------------------------------------------------------------------------- /CodeSignal/Arcade/The_Core/087 - AlphanumericLess.cs: -------------------------------------------------------------------------------- 1 | // Solution 2 | 3 | bool alphanumericLess(string s1, string s2) { 4 | List s11 = fillList(s1); 5 | List s22 = fillList(s2); 6 | int len = Math.Min(s11.Count(), s22.Count()); 7 | int leadZero = -2; 8 | 9 | for(int i = 0; i < len; i++) { 10 | if(s11[i] != s22[i]) { 11 | bool a = long.TryParse(s11[i], out long ai); 12 | bool b = long.TryParse(s22[i], out long bi); 13 | 14 | string[] comp = new string[] { s22[i], s11[i] }; 15 | comp = comp.OrderBy(x => x).ToArray(); 16 | 17 | if((a && b) && (ai == bi)) { 18 | if(leadZero == -2) leadZero = i; 19 | continue; 20 | } 21 | if(a && b) return ai < bi; 22 | 23 | return s11[i] == comp[0]; 24 | } 25 | } 26 | 27 | if(leadZero != -2) return isSmaller(s11[leadZero], s22[leadZero]); 28 | 29 | return s1.Length < s2.Length; 30 | } 31 | 32 | List fillList(string s) { 33 | List list = new List(); 34 | 35 | string temp = ""; 36 | for(int i = 0; i < s.Length; i++) { 37 | if(!char.IsDigit(s[i])) { 38 | if(temp != "") list.Add(temp); 39 | list.Add($"{s[i]}"); 40 | temp = ""; 41 | } else { 42 | temp += s[i]; 43 | } 44 | if(i == s.Length - 1 && temp != "") list.Add(temp); 45 | } 46 | 47 | return list; 48 | } 49 | 50 | static bool isSmaller(string str1, string str2) { 51 | int count = 0; 52 | foreach(char c in str1) { 53 | if(c != '0') break; 54 | count++; 55 | } 56 | foreach(char c in str2) { 57 | if(c != '0') break; 58 | count--; 59 | } 60 | 61 | return 0 < count; 62 | } 63 | -------------------------------------------------------------------------------- /CodeSignal/Arcade/The_Core/088 - ArrayConversion.cs: -------------------------------------------------------------------------------- 1 | // Solution 2 | 3 | int arrayConversion(int[] inputArray) { 4 | List even = new List(); 5 | List odd = new List(inputArray); 6 | 7 | for(int i = 1; ; i++) { 8 | if(even.Count() == 1 || odd.Count() == 1) return (even.Count() == 1 ? even[0] : odd[0]); 9 | if(i % 2 == 1) { 10 | for(int j = 0; j < odd.Count(); j += 2) even.Add(odd[j] + odd[j + 1]); 11 | odd.Clear(); 12 | } else { 13 | for(int j = 0; j < even.Count(); j += 2) odd.Add(even[j] * even[j + 1]); 14 | even.Clear(); 15 | } 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /CodeSignal/Arcade/The_Core/089 - ArrayPreviousLess.cs: -------------------------------------------------------------------------------- 1 | // Solution 2 | 3 | int[] arrayPreviousLess(int[] items) { 4 | 5 | for(int i = items.Length - 1; 0 <= i; i--) { 6 | bool breaker = true; 7 | 8 | for(int j = i - 1; (0 <= j) && breaker; j--) { 9 | if(items[j] < items[i]) { 10 | items[i] = items[j]; 11 | breaker = false; 12 | } 13 | } 14 | if(breaker) items[i] = -1; 15 | } 16 | 17 | return items; 18 | } 19 | -------------------------------------------------------------------------------- /CodeSignal/Arcade/The_Core/090 - PairOfShoes.cs: -------------------------------------------------------------------------------- 1 | // Solution 2 | 3 | bool pairOfShoes(int[][] shoes) { 4 | List left = new List(); 5 | List right = new List(); 6 | 7 | foreach(int[] arr in shoes) { 8 | if(arr[0] == 0) left.Add(arr[1]); 9 | else right.Add(arr[1]); 10 | } 11 | 12 | List leftMock = left.ToList(); 13 | 14 | foreach(int i in left) { 15 | if(right.Contains(i)) { 16 | leftMock.Remove(i); 17 | right.Remove(i); 18 | } else return false; 19 | } 20 | 21 | return (leftMock.Count() == 0 && right.Count() == 0); 22 | } 23 | -------------------------------------------------------------------------------- /CodeSignal/Arcade/The_Core/091 - Combs.cs: -------------------------------------------------------------------------------- 1 | int combs(string comb1, string comb2) { 2 | int c1 = 0; 3 | int c2 = 0; 4 | int len = comb2.Length + comb1.Length; 5 | 6 | for(int i = 0; i < comb1.Length; i++) c1 = (c1 << 1) + (comb1[i] == '*' ? 1 : 0); 7 | c1 <<= comb2.Length; 8 | for(int i = 0; i < comb2.Length; i++) c2 = (c2 << 1) + (comb2[i] == '*' ? 1 : 0); 9 | c2 <<= len; 10 | 11 | int min = 100; 12 | 13 | for(int i = 0; i < len; i++) { 14 | if((c1 & c2) == 0) { 15 | if(GetLength(c1 | c2) < min) min = GetLength(c1 | c2); 16 | } 17 | c2 >>= 1; 18 | } 19 | 20 | 21 | return min; 22 | } 23 | 24 | static int GetLength(int bin) { 25 | bool on = false; 26 | 27 | while(0 < bin) { 28 | if(bin % 2 == 1 && !on) on = true; 29 | if(on) return (int)Math.Log(bin, 2) + 1; 30 | bin >>= 1; 31 | } 32 | 33 | return 1; 34 | } 35 | -------------------------------------------------------------------------------- /CodeSignal/Arcade/The_Core/092 - StringsCrossover.cs: -------------------------------------------------------------------------------- 1 | // Solution 2 | 3 | int stringsCrossover(string[] inputArray, string result) { 4 | List pairs = new List(); 5 | int count = 0; 6 | 7 | for(int i = 0; i < inputArray.Length; i++) { 8 | for(int j = i + 1; j < inputArray.Length; j++) { 9 | pairs.Add(new string[] { inputArray[i], inputArray[j] } ); 10 | } 11 | } 12 | 13 | bool conf; 14 | foreach(string[] pair in pairs) { 15 | conf = true; 16 | for(int i = 0; i < pair[0].Length; i++) { 17 | if(result[i] != pair[0][i] && result[i] != pair[1][i]) { 18 | i = pair[0].Length; 19 | conf = false; 20 | } 21 | } 22 | if(conf) count++; 23 | } 24 | 25 | 26 | return count; 27 | } 28 | -------------------------------------------------------------------------------- /CodeSignal/Arcade/The_Core/093 - CyclicString.cs: -------------------------------------------------------------------------------- 1 | // Solution 2 | 3 | int cyclicString(string s) { 4 | if(s.Distinct().Count() == 1) return 1; 5 | List sub = new List(); 6 | int min = 0; 7 | 8 | for(int i = 0; i < s.Length; i++) { 9 | string str = ""; 10 | for(int j = i; j < s.Length; j++) { 11 | str += s[j]; 12 | if(j != s.Length - 1) sub.Add(str); 13 | } 14 | sub.Add(str); 15 | } 16 | 17 | string[] arr = sub.Where(x => s.All(x.Contains)).ToArray(); 18 | 19 | for(int i = 0; i < arr.Length; i++) { 20 | int len = arr[i].Length; 21 | string rep = arr[i] = String.Concat(Enumerable.Repeat(arr[i], 5)); 22 | 23 | for(int j = 0; j < rep.Length - s.Length; j++) { 24 | if(rep[j] == s[0]) { 25 | if(s == rep.Substring(j, s.Length)) min = ((min == 0 || len < min) ? len : min); 26 | } 27 | } 28 | } 29 | 30 | return min; 31 | } 32 | -------------------------------------------------------------------------------- /CodeSignal/Arcade/The_Core/094 - Beautiful_Text.cs: -------------------------------------------------------------------------------- 1 | bool beautifulText(string inputString, int l, int r) { 2 | List str = new List(); 3 | char[] arr = inputString.ToCharArray(); 4 | bool doesExist = false; 5 | for(int i = l; i <= r + 1; i++) { 6 | char[] chr = arr.ToArray(); 7 | bool access = true; 8 | for(int j = 1; j <= chr.Length; j++) { 9 | if(j % i == 0) { 10 | if(chr[j - 1] != ' ') { 11 | chr[j - 1] = '*'; 12 | access = false; 13 | break; 14 | } else { 15 | chr[j - 1] = '!'; 16 | } 17 | } 18 | } 19 | if(access) str.Add(String.Join("", chr)); 20 | } 21 | if(str.Count() == 1) Console.WriteLine(str[0]); 22 | 23 | foreach(string s in str) { 24 | string[] a = s.Split('!'); 25 | if(a.Where(x => a[0].Length == x.Length).Count() == a.Length && (l <= a[0].Length && a[0].Length <= r)) doesExist = true; 26 | } 27 | 28 | return doesExist; 29 | } 30 | -------------------------------------------------------------------------------- /CodeSignal/Arcade/The_Core/095 - RunnersMeetings.cs: -------------------------------------------------------------------------------- 1 | // Solution 2 | 3 | int runnersMeetings(int[] startPosition, int[] speed) { 4 | int len = startPosition.Length; 5 | double[] st = startPosition.Select(x => (double)x).ToArray(); 6 | double[] sp = speed.Select(x => (double)x).ToArray(); 7 | 8 | List value = new List(); 9 | List freq = new List(); 10 | 11 | for(int i = 0; i < len - 1; i++) { 12 | for(int j = i + 1; j < len; j++) { 13 | double x = (sp[i] == sp[j] ? - 1 : ((st[j] - st[i]) / (sp[i] - sp[j]))); 14 | double y = sp[i] * x + st[i]; 15 | x = Math.Round(x, 3); 16 | y = Math.Round(y, 3); 17 | 18 | if(x <= 0) {} 19 | else if(value.Contains($"{x},{y}")) { 20 | freq[value.IndexOf($"{x},{y}")]++; 21 | } else { 22 | value.Add($"{x},{y}"); 23 | freq.Add(1); 24 | } 25 | } 26 | } 27 | return freq.Count == 0 ? -1 : inverseTriangular(freq.Max()); 28 | } 29 | 30 | public static int inverseTriangular(int f) { 31 | return (int)((1 + Math.Sqrt(1 + 8 * f)) / 2); 32 | } 33 | -------------------------------------------------------------------------------- /CodeSignal/Arcade/The_Core/096 - ChristmasTree.cs: -------------------------------------------------------------------------------- 1 | // Solution 2 | 3 | string[] christmasTree(int levelNum, int levelHeight) { 4 | string[] tree = new string[3 + levelNum * levelHeight + levelNum]; 5 | 6 | // The crown, the initial value 7 | tree[0] = "*"; 8 | tree[1] = "*"; 9 | tree[2] = "***"; 10 | 11 | // Defining the foot 12 | string foot = "" + (levelHeight % 2 == 0 ? "*" : ""); 13 | for(int i = 0; i < levelHeight; i++) { 14 | foot += "*"; 15 | } 16 | 17 | // First top of the tree 18 | for(int i = 3; i < levelHeight + 3; i++) { 19 | tree[i] = tree[i - 1] + "**"; 20 | } 21 | 22 | // The rest of the tops of the tree 23 | for(int i = 3 + levelHeight; i < levelNum * levelHeight + 3; i++) { 24 | if((i - 3 + levelHeight) % levelHeight == 0) { 25 | tree[i] = tree[i - levelHeight] + "**"; 26 | } else { 27 | tree[i] = tree[i] = tree[i - 1] + "**"; 28 | } 29 | } 30 | 31 | // Assigning the foot to the tree 32 | for(int i = 0; i < levelNum; i++) { 33 | tree[tree.Length - 1 - i] = foot; 34 | } 35 | 36 | // Adding whitespaces to the beginning of the tree 37 | for(int i = 0; i < tree.Length; i++) { 38 | tree[i] = addWhiteSpace(tree[i].Length, levelNum + levelHeight) + tree[i]; 39 | } 40 | 41 | return tree; 42 | } 43 | 44 | public static string addWhiteSpace(int segmentLength, int midwayPoint) { 45 | int marginLeft = midwayPoint - (segmentLength - 1) / 2; 46 | string newSegment = ""; 47 | 48 | while(0 < marginLeft) { 49 | newSegment += " "; 50 | marginLeft--; 51 | } 52 | 53 | return newSegment; 54 | } 55 | -------------------------------------------------------------------------------- /CodeSignal/Arcade/The_Core/097 - FileNaming.cs: -------------------------------------------------------------------------------- 1 | // Solution 2 | 3 | string[] fileNaming(string[] names) { 4 | List keys = new List(); 5 | List values = new List(); 6 | List alteredArr = new List(); 7 | 8 | bool keysContain = false; 9 | bool alteredArrContain = false; 10 | foreach(string name in names) { 11 | keysContain = keys.Contains(name); 12 | alteredArrContain = alteredArr.Contains(name); 13 | if(!keysContain && !alteredArrContain) { 14 | keys.Add(name); 15 | values.Add(1); 16 | alteredArr.Add(name); 17 | } else if(keysContain) { 18 | int idx = values[keys.IndexOf(name)]; 19 | while(alteredArr.Contains(name + "(" + idx + ")")) { 20 | idx++; 21 | } 22 | alteredArr.Add(name + "(" + idx + ")"); 23 | values[keys.IndexOf(name)] = idx; 24 | } else { 25 | keys.Add(name); 26 | values.Add(1); 27 | alteredArr.Add(name + "(" + 1 + ")"); 28 | } 29 | } 30 | 31 | return alteredArr.ToArray(); 32 | } 33 | -------------------------------------------------------------------------------- /CodeSignal/Arcade/The_Core/098 - ExtractMatrixColumn.cs: -------------------------------------------------------------------------------- 1 | // Basic Solution 2 | 3 | int[] extractMatrixColumn(int[][] matrix, int column) { 4 | int[] cln = new int[matrix.Length]; 5 | for(int i = 0; i < matrix.Length; i++) { 6 | cln[i] = matrix[i][column]; 7 | } 8 | 9 | return cln; 10 | } 11 | 12 | /*----------------------------------------------------*/ 13 | /*----------------------------------------------------*/ 14 | 15 | // One-liner 16 | 17 | int[] extractMatrixColumn(int[][] matrix, int column) => matrix.Select(x => x[column]).ToArray(); 18 | -------------------------------------------------------------------------------- /CodeSignal/Arcade/The_Core/099 - Are Isomorphic?.cs: -------------------------------------------------------------------------------- 1 | // Solution 2 | 3 | bool areIsomorphic(int[][] array1, int[][] array2) { 4 | if(array1.Length != array2.Length) { 5 | return false; 6 | } else { 7 | for(int i = 0; i < array1.Length; i++) { 8 | if(array1[i].Length != array2[i].Length) return false; 9 | } 10 | 11 | return true; 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /CodeSignal/Arcade/The_Core/100 - ReverseOnDiagonals.cs: -------------------------------------------------------------------------------- 1 | // Solution 2 | 3 | int[][] reverseOnDiagonals(int[][] matrix) { 4 | int len = matrix.Length - 1; 5 | int iteraionSteps = matrix.Length / 2; 6 | 7 | for(int i = 0; i < iteraionSteps; i++) { 8 | // Swap upper left element with lower right 9 | int temp = matrix[i][i]; 10 | matrix[i][i] = matrix[len - i][len - i]; 11 | matrix[len - i][len - i] = temp; 12 | 13 | // Swap lower right element with upper left 14 | temp = matrix[len - i][i]; 15 | matrix[len - i][i] = matrix[i][len - i]; 16 | matrix[i][len - i] = temp; 17 | } 18 | 19 | return matrix; 20 | } 21 | -------------------------------------------------------------------------------- /CodeSignal/Arcade/The_Core/101 - Swap_Diagonals.cs: -------------------------------------------------------------------------------- 1 | // Solution 2 | 3 | int[][] swapDiagonals(int[][] matrix) { 4 | int len = matrix.Length - 1; 5 | int iteraionSteps = matrix.Length / 2; 6 | 7 | for(int i = 0; i < iteraionSteps; i++) { 8 | // Swap upper left element with upper right 9 | int temp = matrix[i][i]; 10 | matrix[i][i] = matrix[i][len - i]; 11 | matrix[i][len - i] = temp; 12 | 13 | // Swap lower left element with lower left 14 | temp = matrix[len - i][i]; 15 | matrix[len - i][i] = matrix[len - i][len - i]; 16 | matrix[len - i][len - i] = temp; 17 | } 18 | 19 | return matrix; 20 | } 21 | -------------------------------------------------------------------------------- /CodeSignal/Arcade/The_Core/102 - CrossingSum.cs: -------------------------------------------------------------------------------- 1 | // Basic Solution 2 | 3 | int crossingSum(int[][] matrix, int a, int b) { 4 | int sum = 0; 5 | 6 | for(int i = 0; i < matrix.Length; i++) { 7 | sum += matrix[i][b]; 8 | } 9 | 10 | return sum + matrix[a].Sum() - matrix[a][b]; 11 | } 12 | 13 | /*------------------------------------------------*/ 14 | /*------------------------------------------------*/ 15 | 16 | // One-liner 17 | 18 | int crossingSum(int[][] matrix, int a, int b) => matrix.Select(x => x[b]).Sum() + matrix[a].Sum() - matrix[a][b]; 19 | -------------------------------------------------------------------------------- /CodeSignal/Arcade/The_Core/103 - DrawRectangle.cs: -------------------------------------------------------------------------------- 1 | // Solution 2 | 3 | char[][] drawRectangle(char[][] canvas, int[] rectangle) { 4 | canvas[rectangle[1]][rectangle[0]] = '*'; 5 | canvas[rectangle[3]][rectangle[0]] = '*'; 6 | canvas[rectangle[1]][rectangle[2]] = '*'; 7 | canvas[rectangle[3]][rectangle[2]] = '*'; 8 | 9 | for(int i = rectangle[0] + 1; i < rectangle[2]; i++) { 10 | canvas[rectangle[1]][i] = canvas[rectangle[3]][i] = '-'; 11 | } 12 | 13 | for(int i = rectangle[1] + 1; i < rectangle[3]; i++) { 14 | canvas[i][rectangle[0]] = canvas[i][rectangle[2]] = '|'; 15 | } 16 | 17 | return canvas; 18 | } 19 | -------------------------------------------------------------------------------- /CodeSignal/Arcade/The_Core/104 - Volleyball_Positions.cs: -------------------------------------------------------------------------------- 1 | // Solution 2 | 3 | string[][] volleyballPositions(string[][] formation, int k) { 4 | k %= 6; 5 | if(k == 0) return formation; 6 | 7 | int[] pos = new int[] { 01, 10, 30, 21, 32, 12}; // First digit is y cord second is x cord 8 | string[] players = new string[] { 9 | formation[0][1], formation[1][0], formation[3][0], 10 | formation[2][1], formation[3][2], formation[1][2] 11 | }; 12 | 13 | for(int i = 6 - k; i < 12 - k; i++) { 14 | formation[pos[k - 6 + i] / 10][pos[k - 6 + i] % 10] = players[i % 6]; 15 | } 16 | 17 | return formation; 18 | } 19 | -------------------------------------------------------------------------------- /CodeSignal/Arcade/The_Core/105 - Star_Rotation.cs: -------------------------------------------------------------------------------- 1 | /* 2 | This solution involves rotating each of the outer segment of the rays of the star. 3 | The algorithm works it way inward until it reaches the center. 4 | One can also use stacks to achieve the same goal however they are not used in this algorithm. 5 | */ 6 | 7 | // Solution 8 | 9 | int[][] starRotation(int[][] matrix, int width, int[] center, int t) { 10 | t %= 8; 11 | if(t == 0) return matrix; 12 | 13 | void swap(int[][] arr, int i1, int i2, int j1, int j2) { 14 | int temp = arr[i1][i2]; 15 | arr[i1][i2] = arr[j1][j2]; 16 | arr[j1][j2] = temp; 17 | } 18 | 19 | int[][] sqr = new int[width][]; 20 | int len = width - 1; 21 | int r = len / 2; 22 | 23 | for(int i = center[0] - r; i <= center[0] + r; i++) { 24 | int[] row = new int[width]; 25 | for(int j = center[1] - r; j <= center[1] + r; j++) { 26 | row[r - center[1] + j] = matrix[i][j]; 27 | } 28 | sqr[r - center[0] + i] = row; 29 | } 30 | 31 | if(t == 4) { 32 | int[] ram = new int[r]; 33 | for(int i = 0; i < r; i++) { 34 | swap(sqr, i, i, len - i, len - i); 35 | swap(sqr, i, len - i, len - i, i); 36 | swap(sqr, i, r, len - i, r); 37 | swap(sqr, r, len - i, r, i); 38 | } 39 | } else { 40 | int holderElem = 0; 41 | void traverse(int steps, int i, int j, int padding, int stepElem, int direction) { 42 | int vr = 1; 43 | int vd = 0; 44 | 45 | while(0 < steps) { 46 | switch(direction) { 47 | case 0: 48 | vr = 1; 49 | vd = 0; 50 | direction = (len < j + 2 + padding ? 1 : 0); 51 | break; 52 | case 1: 53 | vr = 0; 54 | vd = 1; 55 | direction = (len < i + 2 + padding ? 2 : 1); 56 | break; 57 | case 2: 58 | vr = -1; 59 | vd = 0; 60 | direction = (j - 2 < padding ? 3 : 2); 61 | break; 62 | case 3: 63 | vr = 0; 64 | vd = -1; 65 | direction = (i - 2 < padding ? 0 : 3); 66 | break; 67 | default: 68 | Console.WriteLine("ERROR!!!"); 69 | break; 70 | } 71 | i += vd; 72 | j += vr; 73 | 74 | steps--; 75 | } 76 | holderElem = sqr[i][j]; 77 | sqr[i][j] = stepElem; 78 | } 79 | 80 | for(int i = 0; i < r; i++) { 81 | int location = 0; 82 | for(int j = 0; j < 8; j++) { 83 | switch(location) { 84 | case 0: 85 | traverse(t * (r - i), i, i, i, sqr[i][i], 0); 86 | break; 87 | case 1: 88 | traverse(t * (r - i), i, 1 * r, i, holderElem, 0); 89 | break; 90 | case 2: 91 | traverse(t * (r - i), i, 2 * r - i, i, holderElem, 1); 92 | break; 93 | case 3: 94 | traverse(t * (r - i), 1 * r, 2 * r - i, i, holderElem, 1); 95 | break; 96 | case 4: 97 | traverse(t * (r - i), 2 * r - i, 2 * r - i, i, holderElem, 2); 98 | break; 99 | case 5: 100 | traverse(t * (r - i), 2 * r - i, 1 * r, i, holderElem, 2); 101 | break; 102 | case 6: 103 | traverse(t * (r - i), 2 * r - i, i, i, holderElem, 3); 104 | break; 105 | case 7: 106 | traverse(t * (r - i), 1 * r, i, i, holderElem, 3); 107 | break; 108 | default: 109 | Console.WriteLine("ERRONOUS!!!"); 110 | break; 111 | 112 | } 113 | location = (location + t) % 8; 114 | } 115 | } 116 | } 117 | 118 | for(int i = center[0] - r; i <= center[0] + r; i++) { 119 | for(int j = center[1] - r; j <= center[1] + r; j++) { 120 | matrix[i][j] = sqr[r - center[0] + i][r - center[1] + j]; 121 | } 122 | } 123 | 124 | return matrix; 125 | } 126 | -------------------------------------------------------------------------------- /CodeSignal/Arcade/The_Core/106 - Sudoku.cs: -------------------------------------------------------------------------------- 1 | // Solution 2 | 3 | bool sudoku(int[][] grid) { 4 | 5 | int[] digits = new int[9]; 6 | for(int i = 1; i < 9; i+=3) { 7 | for(int j = 1; j < 9; j+=3) { 8 | for(int k = -1; k < 2; k++) { 9 | for(int l = -1; l < 2; l++) { 10 | digits[grid[i + k][j + l] - 1]++; 11 | } 12 | } 13 | if(!digits.All(x => x == 1)) return false; 14 | digits = new int[9]; 15 | } 16 | } 17 | 18 | int[] auxiliary = new int[9]; 19 | for(int i = 0; i < 9; i++) { 20 | for(int j = 0; j < 9; j++) { 21 | digits[grid[i][j] - 1]++; 22 | auxiliary[grid[j][i] - 1]++; 23 | } 24 | if(!digits.All(x => x == 1) || !auxiliary.All(x => x == 1)) return false; 25 | digits = new int[9]; 26 | auxiliary = new int[9]; 27 | } 28 | 29 | return true; 30 | } 31 | -------------------------------------------------------------------------------- /CodeSignal/Arcade/The_Core/107 - MineSweeper.cs: -------------------------------------------------------------------------------- 1 | // Solution 2 | 3 | int[][] minesweeper(bool[][] matrix) { 4 | int len = matrix.Length; 5 | int lenRow = matrix[0].Length; 6 | int[][] grid = new int[len][]; 7 | 8 | for(int i = 0; i < len; i++) { 9 | int[] row = new int[lenRow]; 10 | for(int j = 0; j < lenRow; j++) { 11 | int count = 0; 12 | for(int k = -1; k < 2; k++) { 13 | if(-1 < i + k && i + k < len) { 14 | for(int l = -1; l < 2; l++) { 15 | if(-1 < j + l && j + l < lenRow && !(k == 0 && l == 0)) { 16 | count += (matrix[i + k][j + l] ? 1 : 0); 17 | } 18 | } 19 | } 20 | } 21 | row[j] = count; 22 | } 23 | grid[i] = row; 24 | } 25 | 26 | return grid; 27 | } 28 | -------------------------------------------------------------------------------- /CodeSignal/Arcade/The_Core/108 - BoxBlur.cs: -------------------------------------------------------------------------------- 1 | // Solution 2 | 3 | int[][] boxBlur(int[][] image) { 4 | int len = image.Length - 1; 5 | int lenRow = image[0].Length - 1; 6 | int[][] grid = new int[len - 1][]; 7 | 8 | for(int i = 1; i < len; i++) { 9 | int[] row = new int[lenRow - 1]; 10 | for(int j = 1; j < lenRow; j++) { 11 | int count = 0; 12 | for(int k = -1; k < 2; k++) { 13 | for(int l = -1; l < 2; l++) { 14 | count += image[i + k][j + l]; 15 | } 16 | } 17 | row[j - 1] = ~~(count / 9); 18 | } 19 | grid[i - 1] = row; 20 | } 21 | 22 | return grid; 23 | } 24 | -------------------------------------------------------------------------------- /CodeSignal/Arcade/The_Core/109 - ContoursShifting.cs: -------------------------------------------------------------------------------- 1 | // Solution 2 | 3 | int[][] contoursShifting(int[][] matrix) { 4 | int holder; 5 | int nextHolder; 6 | int wid = matrix[0].Length; 7 | int hei = matrix.Length; 8 | int limit = Math.Min(hei, wid) / 2; 9 | bool IsBackwards = false; 10 | bool isVertLeft = (wid < hei && wid % 2 == 1); 11 | bool isHorzLeft = (hei < wid && hei % 2 == 1); 12 | 13 | for(int i = 0; i < limit; i++) { 14 | traverse(2 * (wid + hei - 2 - (4 * i)), i, i, i, (IsBackwards ? 1 : 0)); 15 | IsBackwards = !IsBackwards; 16 | } 17 | 18 | void traverse(int steps, int i, int j, int padding, int direction) { 19 | nextHolder = matrix[i][j]; 20 | int vr = 1; 21 | int vd = 0; 22 | while(0 < steps) { 23 | holder = nextHolder; 24 | switch(direction) { 25 | case 0: 26 | vr = 1; 27 | vd = 0; 28 | direction = (IsBackwards ? (wid < j + 3 + padding ? 3 : 0) : (wid < j + 3 + padding ? 1 : 0)); 29 | break; 30 | case 1: 31 | vr = 0; 32 | vd = 1; 33 | direction = (IsBackwards ? (hei < i + 3 + padding ? 0 : 1) : (hei < i + 3 + padding ? 2 : 1)); 34 | break; 35 | case 2: 36 | vr = -1; 37 | vd = 0; 38 | direction = (IsBackwards ? (j - 2 < padding ? 1 : 2) : (j - 2 < padding ? 3 : 2)); 39 | break; 40 | case 3: 41 | vr = 0; 42 | vd = -1; 43 | direction = (IsBackwards ? (i - 2 < padding ? 2 : 3) : (i - 2 < padding ? 0 : 3)); 44 | break; 45 | default: 46 | Console.WriteLine("ERROR!!!"); 47 | break; 48 | } 49 | i += vd; 50 | j += vr; 51 | nextHolder = matrix[i][j]; 52 | matrix[i][j] = holder; 53 | 54 | steps--; 55 | } 56 | } 57 | 58 | int prev; 59 | if(isVertLeft) { 60 | if(IsBackwards) { 61 | int mem = matrix[hei - limit - 1][limit]; 62 | for(int i = hei - limit - 1; limit <= i; i--) { 63 | prev = mem; 64 | mem = matrix[i][limit]; 65 | matrix[i][limit] = prev; 66 | } 67 | matrix[hei - limit - 1][limit] = mem; 68 | } else { 69 | int mem = matrix[limit][limit]; 70 | for(int i = limit + 1; i < hei - limit; i++) { 71 | prev = mem; 72 | mem = matrix[i][limit]; 73 | matrix[i][limit] = prev; 74 | } 75 | matrix[limit][limit] = mem; 76 | } 77 | } else if(isHorzLeft) { 78 | if(IsBackwards) { 79 | int mem = matrix[limit][wid - limit - 1]; 80 | for(int i = wid - limit - 1; limit <= i; i--) { 81 | prev = mem; 82 | mem = matrix[limit][i]; 83 | matrix[limit][i] = prev; 84 | } 85 | matrix[limit][wid - limit - 1] = mem; 86 | } else { 87 | int mem = matrix[limit][limit]; 88 | for(int i = limit + 1; i < wid - limit; i++) { 89 | prev = mem; 90 | mem = matrix[limit][i]; 91 | matrix[limit][i] = prev; 92 | } 93 | matrix[limit][limit] = mem; 94 | } 95 | } 96 | 97 | return matrix; 98 | } 99 | -------------------------------------------------------------------------------- /CodeSignal/Arcade/The_Core/110 - PolygonPerimeter.cs: -------------------------------------------------------------------------------- 1 | // Solution 2 | 3 | int polygonPerimeter(bool[][] matrix) { 4 | int blackCellCount = 0; 5 | int connections = 0; 6 | 7 | for(int i = 0; i < matrix.Length; i++) { 8 | for(int j = 0; j < matrix[i].Length; j++) { 9 | if(matrix[i][j]) { 10 | blackCellCount++; 11 | if(0 < i && matrix[i - 1][j]) connections++; 12 | if(0 < j && matrix[i][j - 1]) connections++; 13 | if(i + 1 < matrix.Length && matrix[i + 1][j]) connections++; 14 | if(j + 1 < matrix[i].Length && matrix[i][j + 1]) connections++; 15 | } 16 | } 17 | } 18 | 19 | return blackCellCount * 4 - connections; 20 | } 21 | -------------------------------------------------------------------------------- /CodeSignal/Arcade/The_Core/111 - Gravitation.cs: -------------------------------------------------------------------------------- 1 | // Solution 2 | 3 | int[] gravitation(string[] rows) { 4 | int wid = rows[0].Length; 5 | int hei = rows.Length; 6 | int[] motionLess = new int[wid]; 7 | int minIterations = 10000; 8 | int minIterationsDuplicates = 0; 9 | 10 | for(int i = 0; i < wid; i++) { 11 | int emptySteps = 0; 12 | int gapCount = 0; 13 | 14 | for(int j = hei - 1; j >= 0; j--) { 15 | emptySteps += (rows[j][i] == '.' ? 1 : 0); 16 | if(rows[j][i] == '#') { 17 | gapCount += emptySteps; 18 | emptySteps = 0; 19 | } 20 | } 21 | 22 | minIterationsDuplicates += (gapCount == minIterations ? 1 : 0); 23 | if(gapCount < minIterations) { 24 | minIterations = gapCount; 25 | minIterationsDuplicates = 1; 26 | } 27 | 28 | motionLess[i] = gapCount; 29 | } 30 | 31 | int[] indicies = new int[minIterationsDuplicates]; 32 | for(int i = 0; i < wid; i++) { 33 | if(motionLess[i] == minIterations) { 34 | indicies[--minIterationsDuplicates] = i; 35 | } 36 | } 37 | 38 | Array.Reverse(indicies); 39 | return indicies; 40 | } 41 | -------------------------------------------------------------------------------- /CodeSignal/Arcade/The_Core/112 - IsInformationConsistent?.cs: -------------------------------------------------------------------------------- 1 | // Solution 2 | 3 | bool isInformationConsistent(int[][] evidences) { 4 | int response = 2; 5 | 6 | for(int i = 0; i < evidences[0].Length; i++) { 7 | response = 2; 8 | for(int j = 0; j < evidences.Length; j++) { 9 | if(evidences[j][i] == 0) continue; 10 | response = (response == 2 ? evidences[j][i] : response); 11 | if(response != evidences[j][i]) return false; 12 | } 13 | } 14 | 15 | return true; 16 | } 17 | -------------------------------------------------------------------------------- /CodeSignal/Arcade/The_Core/113 - CorrectNonogram.cs: -------------------------------------------------------------------------------- 1 | // Solution 2 | 3 | bool correctNonogram(int size, string[][] nonogramField) { 4 | int startInd = (size + 1) / 2; 5 | int totSize = nonogramField.Length; 6 | 7 | for(int i = startInd; i < totSize; i++) { 8 | Queue queRow = new Queue(); 9 | Queue queCol = new Queue(); 10 | 11 | for(int j = 0; j < startInd; j++) { 12 | if(nonogramField[i][j] != "-") queRow.Enqueue(Int32.Parse(nonogramField[i][j])); 13 | if(nonogramField[j][i] != "-") queCol.Enqueue(Int32.Parse(nonogramField[j][i])); 14 | } 15 | if(!IsRowValid(startInd, queRow, totSize, nonogramField[i])) return false; 16 | if(!IsColValid(startInd, queCol, totSize, i, nonogramField)) return false; 17 | } 18 | 19 | bool IsColValid(int startInd, Queue q, int totSize, int j, string[][] arr) { 20 | int countPix = 0; 21 | for(int i = startInd; i < totSize; i++) { 22 | countPix += (arr[i][j] == "#" ? 1 : 0); 23 | if((arr[i][j] == "." || i == totSize - 1) && countPix != 0) { 24 | if(Convert.ToInt32(q.Peek()) == countPix) { 25 | q.Dequeue(); 26 | countPix = 0; 27 | } else { 28 | return false; 29 | } 30 | } 31 | } 32 | return q.Count == 0; 33 | } 34 | 35 | bool IsRowValid(int startInd, Queue q, int totSize, string[] arr) { 36 | int countPix = 0; 37 | for(int i = startInd; i < totSize; i++) { 38 | countPix += (arr[i] == "#" ? 1 : 0); 39 | 40 | if((arr[i] == "." || i == totSize - 1) && countPix != 0) { 41 | if(q.Count != 0 && Convert.ToInt32(q.Peek()) == countPix) { 42 | q.Dequeue(); 43 | countPix = 0; 44 | } else { 45 | return false; 46 | } 47 | } 48 | } 49 | return q.Count == 0; 50 | } 51 | 52 | return true; 53 | } 54 | -------------------------------------------------------------------------------- /CodeSignal/Arcade/The_Core/114 - ShuffledArray.cs: -------------------------------------------------------------------------------- 1 | // Solution 2 | 3 | int[] solution(int[] shuffled) { 4 | // Sort arr 5 | Array.Sort(shuffled); 6 | 7 | // Find the sum element 8 | int sum = shuffled.Sum(); 9 | int index = 0; 10 | while(sum != 2 * shuffled[index]) index++; 11 | 12 | // Define new array with one size less 13 | int[] originalArr = new int[shuffled.Length - 1]; 14 | 15 | // Assign elements from old array to the new array 16 | int offset = 0; 17 | for(int i = 0; i < shuffled.Length; i++) { 18 | if(i != index) { 19 | originalArr[i - offset] = shuffled[i]; 20 | } else offset = 1; 21 | } 22 | 23 | // return the new array 24 | return originalArr; 25 | } 26 | -------------------------------------------------------------------------------- /CodeSignal/Arcade/The_Core/115 - SortByHeight.cs: -------------------------------------------------------------------------------- 1 | // Solution 2 | 3 | int[] solution(int[] a) { 4 | List list = new List(); 5 | 6 | // Collect every non-tree 7 | foreach(int height in a) if(height != -1) list.Add(height); 8 | 9 | // Sort all heights in ascending order 10 | list.Sort(); 11 | 12 | // Replace people in a with list to get sorted array 13 | int idx = 0; 14 | for(int i = 0; i < a.Length; i++) { 15 | if(a[i] != -1) { 16 | a[i] = list[idx]; 17 | idx++; 18 | } 19 | } 20 | 21 | return a; 22 | } 23 | -------------------------------------------------------------------------------- /CodeSignal/Arcade/The_Core/116 - SortByLength.cs: -------------------------------------------------------------------------------- 1 | // Basic Solution 2 | 3 | string[] solution(string[] inputArray) { 4 | // Bubble sort, stable sort 5 | for(int i = 1; i < inputArray.Length; i++) { 6 | for(int j = 0; j < inputArray.Length - i; j++) { 7 | if(inputArray[j + 1].Length < inputArray[j].Length) { 8 | string holder = inputArray[j]; 9 | inputArray[j] = inputArray[j + 1]; 10 | inputArray[j + 1] = holder; 11 | } 12 | } 13 | } 14 | 15 | return inputArray; 16 | } 17 | 18 | /*--------------------------------------------------------*/ 19 | /*--------------------------------------------------------*/ 20 | 21 | // Using Linq to sort 22 | 23 | string[] solution(string[] inputArray) { 24 | // Using LINQ OrderBy, not memory efficient 25 | return inputArray.OrderBy(s => s.Length).ToArray(); 26 | } 27 | -------------------------------------------------------------------------------- /CodeSignal/Arcade/The_Core/117 - BoxesPacking.cs: -------------------------------------------------------------------------------- 1 | // Solution 2 | 3 | bool solution(int[] length, int[] width, int[] height) { 4 | int len = length.Length; 5 | int[][] allBoxes = new int[len][]; 6 | int[] largestSide = new int[len]; 7 | int[] secLargestSide = new int[len]; 8 | int[] thiLargestSide = new int[len]; 9 | 10 | // Rotate all boxes so that length becomes the largest 11 | for(int i = 0; i < len; i++) { 12 | int[] dimensions = {length[i], width[i], height[i]}; 13 | Array.Sort(dimensions); 14 | Array.Reverse(dimensions); 15 | largestSide[i] = dimensions[0]; 16 | secLargestSide[i] = dimensions[1]; 17 | thiLargestSide[i] = dimensions[2]; 18 | allBoxes[i] = dimensions; 19 | } 20 | 21 | // Remove duplicates 22 | if(largestSide.Distinct().ToArray().Length != len 23 | || secLargestSide.Distinct().ToArray().Length != len 24 | || thiLargestSide.Distinct().ToArray().Length != len) return false; 25 | 26 | // Bubble sort the largest length to the top of the dimensions array 27 | for(int i = 1; i < len; i++) { 28 | for(int j = 0; j < len - i; j++) { 29 | if(allBoxes[j][0] < allBoxes[j + 1][0]) { 30 | int holder = allBoxes[j][0]; 31 | allBoxes[j][0] = allBoxes[j + 1][0]; 32 | allBoxes[j + 1][0] = holder; 33 | 34 | holder = allBoxes[j][1]; 35 | allBoxes[j][1] = allBoxes[j + 1][1]; 36 | allBoxes[j + 1][1] = holder; 37 | 38 | holder = allBoxes[j][2]; 39 | allBoxes[j][2] = allBoxes[j + 1][2]; 40 | allBoxes[j + 1][2] = holder; 41 | } 42 | } 43 | } 44 | 45 | // Eliminate each box until there is none left or if condidtion is false 46 | for(int i = 0; i < len - 1; i++) { 47 | if(!(allBoxes[i][0] > allBoxes[i + 1][0] 48 | && allBoxes[i][1] > allBoxes[i + 1][1] 49 | && allBoxes[i][2] > allBoxes[i + 1][2])) return false; 50 | } 51 | 52 | return true; 53 | } 54 | -------------------------------------------------------------------------------- /CodeSignal/Arcade/The_Core/118 - MaximumSum.cs: -------------------------------------------------------------------------------- 1 | // Solution 2 | 3 | int solution(int[] a, int[][] q) { 4 | int[] occurences = new int[a.Length]; 5 | int maxSum = 0; 6 | 7 | // Sort "a" to get access to the largest values 8 | Array.Sort(a); 9 | 10 | // Increment occurences 11 | foreach(int[] bound in q) { 12 | for(int j = bound[0]; j <= bound[1]; j++) { 13 | occurences[j]++; 14 | } 15 | } 16 | 17 | // Sort increment for later addition 18 | Array.Sort(occurences); 19 | 20 | // Add to maxSum 21 | for(int i = 0; i < occurences.Length; i++) { 22 | maxSum += (occurences[i] * a[i]); 23 | } 24 | 25 | // Return the maximum value 26 | return maxSum; 27 | } 28 | -------------------------------------------------------------------------------- /CodeSignal/Arcade/The_Core/119 - RowsRearranging.cs: -------------------------------------------------------------------------------- 1 | // Solution 2 | 3 | bool solution(int[][] matrix) { 4 | // Sort all collumn values by switching the row for each column 5 | for(int i = 0; i < matrix[0].Length; i++) { 6 | // Bubble sort the columns 7 | for(int j = 1; j < matrix.Length; j++) { 8 | for(int k = 0; k < matrix.Length - j; k++) { 9 | if(matrix[k + 1][i] < matrix[k][i]) { 10 | int[] holder = matrix[k]; 11 | matrix[k] = matrix[k + 1]; 12 | matrix[k + 1] = holder; 13 | } 14 | } 15 | } 16 | } 17 | 18 | // Check if all columns are strictly increasing 19 | for(int i = 0; i < matrix[0].Length; i++) { 20 | for(int j = 0; j < matrix.Length - 1; j++) { 21 | // Check if it's stricly increasing 22 | if(matrix[j + 1][i] <= matrix[j][i]) { 23 | return false; 24 | } 25 | } 26 | } 27 | 28 | // Return true, since is strictly increasing 29 | return true; 30 | } 31 | -------------------------------------------------------------------------------- /CodeSignal/Arcade/The_Core/120 - DigitDifferenceSort.cs: -------------------------------------------------------------------------------- 1 | // Solution 2 | 3 | int[] solution(int[] a) { 4 | // An array to hold the digit difference for each element in the "a" array 5 | int[] digitDiffrenceHolder = new int[a.Length]; 6 | // Reversing it so that the larger index comes first 7 | Array.Reverse(a); 8 | 9 | // Filling the digitDiffrence array with the maximum digit difference of each number 10 | for(int i = 0; i < a.Length; i++) digitDiffrenceHolder[i] = digitDiffrence(a[i]); 11 | 12 | // Bubble sort 13 | // Even though bubble sort is a naive sorting algorithm it's still useful since it sorts with stabillity intact 14 | for(int i = 1; i < a.Length; i++) { 15 | for(int j = 0; j < a.Length - i; j++) { 16 | if(digitDiffrenceHolder[j + 1] < digitDiffrenceHolder[j]) { 17 | int holder = digitDiffrenceHolder[j]; 18 | digitDiffrenceHolder[j] = digitDiffrenceHolder[j + 1]; 19 | digitDiffrenceHolder[j + 1] = holder; 20 | 21 | holder = a[j]; 22 | a[j] = a[j + 1]; 23 | a[j + 1] = holder; 24 | } 25 | } 26 | } 27 | 28 | return a; 29 | } 30 | 31 | int digitDiffrence(int num) { 32 | int max = 0; 33 | int min = 10; 34 | 35 | int firstDig = num % 10; 36 | while(num != 0) { 37 | if(max < firstDig) max = firstDig; 38 | if(firstDig < min) min = firstDig; 39 | 40 | num /= 10; 41 | firstDig = num % 10; 42 | } 43 | 44 | return max - min; 45 | } 46 | -------------------------------------------------------------------------------- /CodeSignal/Arcade/The_Core/121 - UniqueDigitProducts.cs: -------------------------------------------------------------------------------- 1 | // Basic Solution 2 | 3 | int solution(int[] a) { 4 | // Change every number to its digit product 5 | for(int i = 0; i < a.Length; i++) { 6 | a[i] = product(a[i]); 7 | } 8 | 9 | // Return distinct number of elements 10 | return a.Distinct().Count(); 11 | } 12 | 13 | /*-----------------------------------------------------------------------------------------*/ 14 | /*-----------------------------------------------------------------------------------------*/ 15 | 16 | // Solution using HashSets 17 | 18 | int solution(int[] a) { 19 | HashSet uniqueNumbers = new HashSet(); 20 | int uniqueCount = 0; 21 | 22 | for(int i = 0; i < a.Length; i++) { 23 | if(!uniqueNumbers.Contains(product(a[i]))) { 24 | uniqueNumbers.Add(product(a[i])); 25 | uniqueCount++; 26 | } 27 | } 28 | 29 | return uniqueCount; 30 | } 31 | 32 | 33 | //--------------------------------------------------------------------------------------------- 34 | // Common used functions: 35 | 36 | int product(int num) { 37 | int prod = 1; 38 | 39 | int digit = num % 10; 40 | while(num != 0) { 41 | prod *= digit; 42 | 43 | num /= 10; 44 | digit = num % 10; 45 | } 46 | 47 | return prod; 48 | } 49 | -------------------------------------------------------------------------------- /CodeSignal/Arcade/The_Core/122 - BishopAndPawn.cs: -------------------------------------------------------------------------------- 1 | // Basic Solution 2 | 3 | bool solution(string bishop, string pawn) { 4 | bool[][] board = new bool[8][]; 5 | 6 | for(int i = 0; i < 8; i++) board[i] = new bool[] { false, false, false, false, false, false, false, false }; 7 | 8 | board[pawn[1] - '1'][pawn[0] - 'a'] = true; 9 | 10 | int bishopCordX = bishop[0] - 'a'; 11 | int bishopCordY = bishop[1] - '1'; 12 | 13 | // Checking first diagonal 14 | for(int i = 1; bishopCordX + i < 8 && bishopCordY + i < 8; i++) { 15 | if(board[bishopCordY + i][bishopCordX + i]) return true; 16 | } 17 | 18 | // Checking second diagonal 19 | for(int i = 1; bishopCordX + i < 8 && 0 <= bishopCordY - i; i++) { 20 | if(board[bishopCordY - i][bishopCordX + i]) return true; 21 | } 22 | 23 | // Checking third diagonal 24 | for(int i = 1; 0 <= bishopCordX - i && bishopCordY + i < 8; i++) { 25 | if(board[bishopCordY + i][bishopCordX - i]) return true; 26 | } 27 | 28 | // Checking fourth diagonal 29 | for(int i = 1; 0 <= bishopCordX - i && 0 <= bishopCordY - i; i++) { 30 | if(board[bishopCordY - i][bishopCordX - i]) return true; 31 | } 32 | 33 | // False because the pawn cannot be reached by the bishop in one move 34 | return false; 35 | } 36 | 37 | /*---------------------------------------------------------------------------------------*/ 38 | /*---------------------------------------------------------------------------------------*/ 39 | 40 | // Clever one-liner solution with the math library 41 | 42 | bool solution(string bishop, string pawn) => Math.Abs(bishop[0] - pawn[0]) == Math.Abs(bishop[1] - pawn[1]); 43 | -------------------------------------------------------------------------------- /CodeSignal/Arcade/The_Core/123 - ChessKnightMoves.cs: -------------------------------------------------------------------------------- 1 | // Solution 2 | 3 | int solution(string cell) { 4 | int x = cell[0] - 'a' + 1; 5 | int y = cell[1] - '0'; 6 | int count = 0; 7 | 8 | // To see if the coordinates are in the board 9 | int mesh(int a, int b) => ((0 < a && a < 9) && (0 < b && b < 9) ? 1 : 0); 10 | 11 | // Add to counter if the coordinates are in the board 12 | for(int i = -2; i < 3; i+=4) count += mesh(x + i, y - 1) + mesh(x + i, y + 1); 13 | for(int i = -1; i < 2; i+=2) count += mesh(x + i, y - 2) + mesh(x + i, y + 2); 14 | 15 | return count; 16 | } 17 | -------------------------------------------------------------------------------- /CodeSignal/Arcade/The_Core/124 - BishopDiagonal.cs: -------------------------------------------------------------------------------- 1 | // Solution 2 | 3 | string[] solution(string bishop1, string bishop2) { 4 | int letter1 = bishop1[0] - 'a'; 5 | int digit1 = bishop1[1] - '1'; 6 | 7 | int letter2 = bishop2[0] - 'a'; 8 | int digit2 = bishop2[1] - '1'; 9 | 10 | if(Math.Abs(bishop1[0] - bishop2[0]) != Math.Abs(bishop1[1] - bishop2[1])) { 11 | if(digit1 < digit2) { 12 | return new string[] { bishop1, bishop2 }; 13 | } else return new string[] { bishop2, bishop1 }; 14 | } else { 15 | if(letter1 - letter2 < 0 && 0 < digit1 - digit2) { 16 | // If bishop2 is down-right of bishop1 17 | while(0 < letter1 && digit1 < 7) { 18 | letter1--; 19 | digit1++; 20 | } 21 | while(0 < digit2 && letter2 < 7) { 22 | letter2++; 23 | digit2--; 24 | } 25 | } else if(0 < letter1 - letter2 && 0 < digit1 - digit2) { 26 | // If bishop2 is down-left of bishop1 27 | while(letter1 < 7 && digit1 < 7) { 28 | letter1++; 29 | digit1++; 30 | } 31 | while(0 < digit2 && 0 < letter2) { 32 | letter2--; 33 | digit2--; 34 | } 35 | } else if(0 < letter1 - letter2 && digit1 - digit2 < 0) { 36 | // If bishop2 is up-left of bishop1 37 | while(0 < digit1 && letter1 < 7) { 38 | letter1++; 39 | digit1--; 40 | } 41 | while(0 < letter2 && digit2 < 7) { 42 | letter2--; 43 | digit2++; 44 | } 45 | } else { 46 | // If bishop2 is up-right of bishop1 47 | while(0 < digit1 && 0 < letter1) { 48 | letter1--; 49 | digit1--; 50 | } 51 | while(letter2 < 7 && digit2 < 7) { 52 | letter2++; 53 | digit2++; 54 | } 55 | } 56 | } 57 | 58 | 59 | if(letter1 < letter2) { 60 | return new string[] { (char)(letter1 + 97) + "" + ++digit1, (char)(letter2 + 97) + "" + ++digit2 }; 61 | } else { 62 | return new string[] { (char)(letter2 + 97) + "" + ++digit2, (char)(letter1 + 97) + "" + ++digit1 }; 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /CodeSignal/Arcade/The_Core/125 - Whose_Turn?.cs: -------------------------------------------------------------------------------- 1 | // Solution 2 | 3 | bool solution(string p) { 4 | int[] pos1 = new int[] { p[0] - 'a', (int)p[1] }; 5 | int[] pos2 = new int[] { p[3] - 'a', (int)p[4] }; 6 | int[] pos3 = new int[] { p[6] - 'a', (int)p[7] }; 7 | int[] pos4 = new int[] { p[9] - 'a', (int)p[10] } 8 | 9 | return ( 10 | (++pos1[0] + pos1[1] + ++pos2[0] + pos2[1]) % 2 == 11 | (++pos3[0] + pos3[1] + ++pos4[0] + pos4[1]) % 2 12 | ); 13 | } 14 | 15 | /*---------------------------------------------------------------------------*/ 16 | /*---------------------------------------------------------------------------*/ 17 | 18 | // Unclear but cool one-liner 19 | 20 | bool solution(string p) => (p[0] + p[1] + p[3] + p[4] - p[6] - p[7] - p[9] - p[10]) % 2 == 0; 21 | -------------------------------------------------------------------------------- /CodeSignal/Arcade/The_Core/126 - ChessBishopDream.cs: -------------------------------------------------------------------------------- 1 | // Solution 2 | 3 | int[] solution(int[] boardSize, int[] initPosition, int[] initDirection, int k) { 4 | // Final position coordinate 5 | int[] endCoord = new int[2]; 6 | 7 | int[] repeatingIPos = new int[3 * boardSize[0]]; 8 | int[] repeatingJPos = new int[3 * boardSize[1]]; 9 | int ki = k % (2 * boardSize[0]); 10 | int kj = k % (2 * boardSize[1]); 11 | 12 | for(int i = 0; i < boardSize[0]; i++) { 13 | repeatingIPos[i] = i; 14 | repeatingIPos[i + 2 * boardSize[0]] = i; 15 | 16 | repeatingIPos[i + boardSize[0]] = boardSize[0] - i - 1; 17 | } 18 | 19 | for(int i = 0; i < boardSize[1]; i++) { 20 | repeatingJPos[i] = i; 21 | repeatingJPos[i + 2 * boardSize[1]] = i; 22 | 23 | repeatingJPos[i + boardSize[1]] = boardSize[1] - i - 1; 24 | } 25 | 26 | if(initDirection[0] == 1) { 27 | endCoord[0] = repeatingIPos[initPosition[0] + ki]; 28 | } else { endCoord[0] = repeatingIPos[initPosition[0] - ki + 2*boardSize[0]]; } 29 | 30 | if(initDirection[1] == 1) { 31 | endCoord[1] = repeatingJPos[initPosition[1] + kj]; 32 | } else { endCoord[1] = repeatingJPos[initPosition[1] - kj + 2*boardSize[1]]; } 33 | 34 | return endCoord; 35 | } 36 | 37 | /*--------------------------------------------------------------------------------------------------*/ 38 | /*--------------------------------------------------------------------------------------------------*/ 39 | 40 | // A less repetitive and more elegant solution 41 | 42 | int[] solution(int[] boardSize, int[] initPosition, int[] initDirection, int k) { 43 | // Final position coordinate 44 | int[] endCoord = new int[2]; 45 | 46 | for(int I = 0; I < 2; I++) { 47 | int[] repeatingPattern = new int[3 * boardSize[I]]; 48 | int kI = k % (2 * boardSize[I]); 49 | 50 | // Filling pattern array 51 | for(int i = 0; i < boardSize[I]; i++) { 52 | // For the 'i' position in the example: [0, 1, 2, 2, 1, 0, 0, 1, 2]; 53 | // 'j' position in the example: [0, 1, 2, 3, 4, 5, 6, 6, 5, 4, 3, 2, 1, 0, 0, 1, 2, 3, 4, 5, 6]; 54 | repeatingPattern[i] = i; 55 | repeatingPattern[i + 2 * boardSize[I]] = i; 56 | repeatingPattern[i + boardSize[I]] = boardSize[I] - i - 1; 57 | } 58 | 59 | // Calculate end position for the respective axis 60 | endCoord[I] = repeatingPattern[initPosition[I] + initDirection[I] * kI + (1 - initDirection[I]) * boardSize[I]]; 61 | } 62 | 63 | // Return ending coordinates 64 | return endCoord; 65 | } 66 | -------------------------------------------------------------------------------- /CodeSignal/Arcade/The_Core/127 - ChessTriangle.cs: -------------------------------------------------------------------------------- 1 | // Brute Force Solution 2 | 3 | int solution(int n, int m) { 4 | List potentialAttacks = new List(); 5 | int totCount = 0; 6 | 7 | // Iterate through the coord system for horse 8 | for(int i = 0; i < n; i++) { 9 | for(int j = 0; j < m; j++) { 10 | if(0 <= i - 1 && 0 <= j - 2) potentialAttacks.Add(new int[] { i, j, i - 1, j - 2}); 11 | if(0 <= i - 2 && 0 <= j - 1) potentialAttacks.Add(new int[] { i, j, i - 2, j - 1}); 12 | if(i + 1 < n && 0 <= j - 2) potentialAttacks.Add(new int[] { i, j, i + 1, j - 2}); 13 | if(i + 2 < n && 0 <= j - 1) potentialAttacks.Add(new int[] { i, j, i + 2, j - 1}); 14 | if(0 <= i - 1 && j + 2 < m) potentialAttacks.Add(new int[] { i, j, i - 1, j + 2}); 15 | if(0 <= i - 2 && j + 1 < m) potentialAttacks.Add(new int[] { i, j, i - 2, j + 1}); 16 | if(i + 1 < n && j + 2 < m) potentialAttacks.Add(new int[] { i, j, i + 1, j + 2}); 17 | if(i + 2 < n && j + 1 < m) potentialAttacks.Add(new int[] { i, j, i + 2, j + 1}); 18 | } 19 | } 20 | 21 | // Check if horse attacking a rook will lead to a triangle 22 | foreach(int[] arr in potentialAttacks) { 23 | int i = 1; 24 | while(0 <= arr[2] - i) totCount += (canBishopStrike(arr[0], arr[1], arr[2] - i++, arr[3]) ? 1 : 0); // Upp 25 | i = 1; 26 | while(arr[3] + i < m) totCount += (canBishopStrike(arr[0], arr[1], arr[2], arr[3] + i++) ? 1 : 0); // Right 27 | i = 1; 28 | while(0 <= arr[3] - i) totCount += (canBishopStrike(arr[0], arr[1], arr[2], arr[3] - i++) ? 1 : 0); // Left 29 | i = 1; 30 | while(arr[2] + i < n) totCount += (canBishopStrike(arr[0], arr[1], arr[2] + i++, arr[3]) ? 1 : 0); // Down 31 | } 32 | 33 | // Check if horse attacking a bishop will lead to a triangle 34 | foreach(int[] arr in potentialAttacks) { 35 | int i = 1; 36 | while(0 <= arr[2] - i && 0 <= arr[3] - i) totCount += (canRookStrike(arr[0], arr[1], arr[2] - i, arr[3] - i++) ? 1 : 0); // Upp-left 37 | i = 1; 38 | while(arr[2] + i < n && 0 <= arr[3] - i) totCount += (canRookStrike(arr[0], arr[1], arr[2] + i, arr[3] - i++) ? 1 : 0); // Down-left 39 | i = 1; 40 | while(0 <= arr[2] - i && arr[3] + i < m) totCount += (canRookStrike(arr[0], arr[1], arr[2] - i, arr[3] + i++) ? 1 : 0); // Upp-right 41 | i = 1; 42 | while(arr[2] + i < n && arr[3] + i < m) totCount += (canRookStrike(arr[0], arr[1], arr[2] + i, arr[3] + i++) ? 1 : 0); // Down-right 43 | } 44 | 45 | // Return solution 46 | return totCount; 47 | } 48 | 49 | bool canRookStrike(int knightI, int knightJ, int rookI, int rookJ) => (rookI == knightI || rookJ == knightJ); 50 | 51 | bool canBishopStrike(int knightI, int knightJ, int bishopI, int bishopJ) => Math.Abs(bishopI - knightI) == Math.Abs(bishopJ - knightJ); 52 | 53 | /*-------------------------------------------------------------------------------------------------*/ 54 | /*-------------------------------------------------------------------------------------------------*/ 55 | 56 | // A mathematical solution 57 | 58 | int solution(int n, int m) => times(n, m, 2, 3) + times(n, m, 3, 3) + times(n, m, 2, 4) + times(n, m, 3, 4); 59 | 60 | int ways(int n, int m, int x, int y) => ((n < x || m < y) ? 0 : (n - x + 1) * (m - y + 1)); 61 | int times(int n, int m, int x, int y) => (ways(n, m, x, y) + ways(m, n, x, y)) * 8; 62 | -------------------------------------------------------------------------------- /CodeSignal/Arcade/The_Core/128 - AmazonCheckmate.cs: -------------------------------------------------------------------------------- 1 | // Solution 2 | 3 | int[] solution(string king, string amazon) { 4 | int[] kingPos = new int[] { king[0] - 'a', king[1] - '1' }; 5 | int[] amazPos = new int[] { amazon[0] - 'a', amazon[1] - '1' }; 6 | int[] returnArr = new int[4]; 7 | 8 | char[][] board = new char[8][]; 9 | 10 | for(int i = 0; i < 8; i++) { 11 | board[i] = new char[] { '0', '0', '0', '0', '0', '0', '0', '0' }; 12 | } 13 | 14 | board[kingPos[1]][kingPos[0]] = 'K'; 15 | board[amazPos[1]][amazPos[0]] = 'A'; 16 | bool amazonStandsAlone = determineIfAmazonStandsAlone(amazPos[1], amazPos[0], board); 17 | 18 | // Up 19 | int j = 1; 20 | while(0 <= amazPos[1] - j && board[amazPos[1] - j][amazPos[0]] != 'K') { 21 | board[amazPos[1] - j][amazPos[0]] = '+'; 22 | j++; 23 | } 24 | // Down 25 | j = 1; 26 | while(amazPos[1] + j < 8 && board[amazPos[1] + j][amazPos[0]] != 'K') { 27 | board[amazPos[1] + j][amazPos[0]] = '+'; 28 | j++; 29 | } 30 | // Left 31 | j = 1; 32 | while(0 <= amazPos[0] - j && board[amazPos[1]][amazPos[0] - j] != 'K') { 33 | board[amazPos[1]][amazPos[0] - j] = '+'; 34 | j++; 35 | } 36 | // Right 37 | j = 1; 38 | while(amazPos[0] + j < 8 && board[amazPos[1]][amazPos[0] + j] != 'K') { 39 | board[amazPos[1]][amazPos[0] + j] = '+'; 40 | j++; 41 | } 42 | 43 | // Up-left 44 | j = 1; 45 | while(0 <= amazPos[1] - j && 0 <= amazPos[0] - j && board[amazPos[1] - j][amazPos[0] - j] != 'K') { 46 | board[amazPos[1] - j][amazPos[0] - j] = '+'; 47 | j++; 48 | } 49 | // Up-right 50 | j = 1; 51 | while(0 <= amazPos[1] - j && amazPos[0] + j < 8 && board[amazPos[1] - j][amazPos[0] + j] != 'K') { 52 | board[amazPos[1] - j][amazPos[0] + j] = '+'; 53 | j++; 54 | } 55 | // Down-Left 56 | j = 1; 57 | while(amazPos[1] + j < 8 && 0 <= amazPos[0] - j && board[amazPos[1] + j][amazPos[0] - j] != 'K') { 58 | board[amazPos[1] + j][amazPos[0] - j] = '+'; 59 | j++; 60 | } 61 | // Down-Right 62 | j = 1; 63 | while(amazPos[1] + j < 8 && amazPos[0] + j < 8 && board[amazPos[1] + j][amazPos[0] + j] != 'K') { 64 | board[amazPos[1] + j][amazPos[0] + j] = '+'; 65 | j++; 66 | } 67 | 68 | for(int i = -2; i < 3; i++) { 69 | if(0 <= amazPos[1] + i && amazPos[1] + i < 8) { 70 | for(int k = -2; k < 3; k++) { 71 | if(0 <= amazPos[0] + k && amazPos[0] + k < 8) { 72 | board[amazPos[1] + i][amazPos[0] + k] = (board[amazPos[1] + i][amazPos[0] + k] == '0' ? '+' : board[amazPos[1] + i][amazPos[0] + k]); 73 | } 74 | } 75 | } 76 | 77 | } 78 | 79 | // Clear Space Around King 80 | // Up-left 81 | if(0 <= kingPos[1] - 1 && 0 <= kingPos[0] - 1 && board[kingPos[1] - 1][kingPos[0] - 1] != 'A') board[kingPos[1] - 1][kingPos[0] - 1] = ' '; 82 | // Up 83 | if(0 <= kingPos[1] - 1 && board[kingPos[1] - 1][kingPos[0]] != 'A') board[kingPos[1] - 1][kingPos[0]] = ' '; 84 | // Up-right 85 | if(0 <= kingPos[1] - 1 && kingPos[0] + 1 < 8 && board[kingPos[1] - 1][kingPos[0] + 1] != 'A') board[kingPos[1] - 1][kingPos[0] + 1] = ' '; 86 | 87 | // Left 88 | if(0 <= kingPos[0] - 1 && board[kingPos[1]][kingPos[0] - 1] != 'A') board[kingPos[1]][kingPos[0] - 1] = ' '; 89 | // Right 90 | if(kingPos[0] + 1 < 8 && board[kingPos[1]][kingPos[0] + 1] != 'A') board[kingPos[1]][kingPos[0] + 1] = ' '; 91 | 92 | // Down-left 93 | if(kingPos[1] + 1 < 8 && 0 <= kingPos[0] - 1 && board[kingPos[1] + 1][kingPos[0] - 1] != 'A') board[kingPos[1] + 1][kingPos[0] - 1] = ' '; 94 | // Down 95 | if(kingPos[1] + 1 < 8 && board[kingPos[1] + 1][kingPos[0]] != 'A') board[kingPos[1] + 1][kingPos[0]] = ' '; 96 | // Down-right 97 | if(kingPos[1] + 1 < 8 && kingPos[0] + 1 < 8 && board[kingPos[1] + 1][kingPos[0] + 1] != 'A') board[kingPos[1] + 1][kingPos[0] + 1] = ' '; 98 | 99 | // Begin Heuristic 100 | for(int i = 0; i < 8; i++) { 101 | j = 0; 102 | for(; j < 8; j++) { 103 | if(board[i][j] == '0') { 104 | returnArr[func(i, j, board)]++; 105 | } else if(board[i][j] == '+') { 106 | returnArr[func1(i, j, board, amazonStandsAlone)]++; 107 | } 108 | } 109 | } 110 | 111 | // Return Array 112 | return returnArr; 113 | } 114 | 115 | int func(int i, int j, char[][] board) { 116 | for(int l = -1; l < 2; l++) { 117 | if(0 <= i + l && i + l < 8) { 118 | for(int k = -1; k < 2; k++) { 119 | if(0 <= j + k && j + k < 8) { 120 | if(l != 0 || k != 0) { 121 | if(board[i + l][j + k] == '0') return 3; 122 | } 123 | } 124 | } 125 | } 126 | } 127 | return 2; 128 | } 129 | int func1(int i, int j, char[][] board, bool amazonStandsAlone) { 130 | for(int l = -1; l < 2; l++) { 131 | if(0 <= i + l && i + l < 8) { 132 | for(int k = -1; k < 2; k++) { 133 | if(0 <= j + k && j + k < 8) { 134 | if(l != 0 || k != 0) { 135 | if(board[i + l][j + k] == '0' 136 | || (board[i + l][j + k] == 'A' && amazonStandsAlone)) return 1; 137 | 138 | } 139 | } 140 | } 141 | } 142 | } 143 | return 0; 144 | } 145 | 146 | bool determineIfAmazonStandsAlone(int i, int j, char[][] board) { 147 | for(int l = -1; l < 2; l++) { 148 | if(0 <= i + l && i + l < 8) { 149 | for(int k = -1; k < 2; k++) { 150 | if(0 <= j + k && j + k < 8) { 151 | if(l != 0 || k != 0) { 152 | if(board[i + l][j + k] == 'K') return false; 153 | } 154 | } 155 | } 156 | } 157 | } 158 | return true; 159 | } 160 | -------------------------------------------------------------------------------- /CodeSignal/Arcade/The_Core/129 - PawnRace.cs: -------------------------------------------------------------------------------- 1 | // Solution 2 | 3 | string solution(string white, string black, char toMove) { 4 | if(white[0] - black[0] == 0 && white[1] < black[1]) return "draw"; 5 | if(Math.Abs(white[0] - black[0]) == 1 && white[1] < black[1]) { 6 | // If they are in potential striking distance 7 | 8 | int whiteLetter = white[1] - '0'; 9 | int whiteNum = white[0] - 'a'; 10 | int blackLetter = black[1] - '0'; 11 | int blackNum = black[0] - 'a'; 12 | 13 | int[][] winPosition = new int[][] { 14 | new int[] { 2, 5 }, 15 | new int[] { 2, 6 }, 16 | new int[] { 3, 6 }, 17 | new int[] { 4, 7 } 18 | }; 19 | if(toMove == 'w') { 20 | foreach(int[] pos in winPosition) if(whiteLetter == pos[0] && blackLetter == pos[1]) return "white"; 21 | if (whiteLetter + 1 == blackLetter) return "white"; 22 | return "black"; 23 | } else { 24 | foreach(int[] pos in winPosition) if(whiteLetter == 9 - pos[1] && blackLetter == 9 - pos[0]) return "black"; 25 | if(whiteLetter + 1 == blackLetter) return "black"; 26 | return "white"; 27 | } 28 | } else { // If they are out of reach for direct contact 29 | int whiteTurnsLeftForWin = 8 - (white[1] - '0'); 30 | int blackTurnsLeftForWin = (black[1] - '0') - 1; 31 | 32 | string second; 33 | if(toMove == 'w') { 34 | whiteTurnsLeftForWin--; 35 | second = "black"; 36 | } else { 37 | blackTurnsLeftForWin--; 38 | second = "white"; 39 | } 40 | 41 | whiteTurnsLeftForWin += (white[1] == '2' ? -1 : 0); 42 | blackTurnsLeftForWin += (black[1] == '7' ? -1 : 0); 43 | 44 | if(whiteTurnsLeftForWin == blackTurnsLeftForWin) { 45 | return second; 46 | } else if(whiteTurnsLeftForWin < blackTurnsLeftForWin) { 47 | return "white"; 48 | } else return "black"; 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /CodeSignal/Arcade/The_Core/130 - ValidTime.cs: -------------------------------------------------------------------------------- 1 | // Solution 2 | 3 | bool solution(string time) { 4 | // Compare hour 5 | bool isHourValid = (time[0] - '0') * 10 + (time[1] - '0') < 24; 6 | 7 | // Compare minute 8 | bool isMinuteValid = (time[3] - '0') * 10 + (time[4] - '0') < 60; 9 | 10 | // Return evaluation 11 | return isHourValid && isMinuteValid; 12 | } 13 | 14 | /*----------------------------------------------------------------------------*/ 15 | /*----------------------------------------------------------------------------*/ 16 | 17 | // One-line with the built-in DateTime function 18 | 19 | bool solution(string time) => DateTime.TryParse(time, out _); 20 | -------------------------------------------------------------------------------- /CodeSignal/Arcade/The_Core/131 - VideoPart.cs: -------------------------------------------------------------------------------- 1 | // Solution 2 | 3 | /* 4 | Sub-optimal solution since we are iterating from i = 2 to i = total time. 5 | Can be further reduced and optimized if wanted. 6 | */ 7 | 8 | int[] solution(string part, string total) { 9 | // Get time for part - watched 10 | int partTime = (10 * (part[0] - '0') + (part[1] - '0')) * 60 * 60 11 | + (10 * (part[3] - '0') + (part[4] - '0')) * 60 12 | + (10 * (part[6] - '0') + (part[7] - '0')); 13 | 14 | // Get time for total 15 | int totalTime = (10 * (total[0] - '0') + (total[1] - '0')) * 60 * 60 16 | + (10 * (total[3] - '0') + (total[4] - '0')) * 60 17 | + (10 * (total[6] - '0') + (total[7] - '0')); 18 | 19 | // Turn result into fraction 20 | for(int i = 2; i <= totalTime; i++) { 21 | while((partTime / i) * i == partTime 22 | && (totalTime / i) * i == totalTime) { 23 | partTime /= i; 24 | totalTime /= i; 25 | } 26 | } 27 | 28 | // Return fraction 29 | return new int[] { partTime, totalTime }; 30 | } 31 | -------------------------------------------------------------------------------- /CodeSignal/Arcade/The_Core/132 - DayOfWeek.cs: -------------------------------------------------------------------------------- 1 | // Solution 2 | 3 | int solution(string birthdayDate) { 4 | int dayOfWeekSinceBirthDay = 0; 5 | int year = Convert.ToInt32(birthdayDate.Substring(6)); 6 | int month = Convert.ToInt32(birthdayDate.Substring(0, 2)); 7 | int day = Convert.ToInt32(birthdayDate.Substring(3, 2)); 8 | 9 | if(month == 2 && day == 29) { // If birthday is in Feb 29 10 | do { 11 | if(isLeapYear(year + 4)) { 12 | dayOfWeekSinceBirthDay += 5; 13 | year += 4; 14 | } else { 15 | dayOfWeekSinceBirthDay += 2; 16 | year += 8; 17 | } 18 | } while(dayOfWeekSinceBirthDay % 7 != 0); 19 | 20 | } else { // If birthday is not in Feb 29 21 | bool isBefore29Feb = (month == 1 || (month == 2 && day < 29)); 22 | 23 | do { 24 | dayOfWeekSinceBirthDay += ((isLeapYear(year) && isBefore29Feb) 25 | || (isLeapYear(year + 1) && !isBefore29Feb) ? 2 : 1); 26 | year++; 27 | } while(dayOfWeekSinceBirthDay % 7 != 0); 28 | } 29 | 30 | return year - Convert.ToInt32(birthdayDate.Substring(6)); 31 | } 32 | 33 | bool isLeapYear(int year) { 34 | return (((year % 4 == 0) && ((year / 100) * 100 != year)) || ((year / 400) * 400 == year)); 35 | } 36 | -------------------------------------------------------------------------------- /CodeSignal/Arcade/The_Core/133 - CuriousClock.cs: -------------------------------------------------------------------------------- 1 | // Solution 2 | 3 | string solution(string someTime, string leavingTime) { 4 | DateTime sTime = ConvertStringToDateTime(someTime); 5 | DateTime lTime = ConvertStringToDateTime(leavingTime); 6 | 7 | // (lTime - sTime) == time passed since the clock went backwards 8 | DateTime leavingTimeInClock = (sTime - (lTime - sTime)); 9 | 10 | // Return new time 11 | return completeDate(leavingTimeInClock.Year) + "-" 12 | + completeDate(leavingTimeInClock.Month) + "-" 13 | + completeDate(leavingTimeInClock.Day) + " " 14 | + completeDate(leavingTimeInClock.Hour) + ":" 15 | + completeDate(leavingTimeInClock.Minute) ; 16 | } 17 | 18 | string completeDate(int num) { 19 | // Fill in zero if integer is of size 1 digit 20 | return (num < 10 ? $"0{num}" : num.ToString()); 21 | } 22 | 23 | DateTime ConvertStringToDateTime(string time) { 24 | return new DateTime( 25 | Convert.ToInt32(time.Substring(0, 4)), 26 | 10 * (time[5] - '0') + (time[6] - '0'), 27 | 10 * (time[8] - '0') + (time[9] - '0'), 28 | 10 * (time[11] - '0') + (time[12] - '0'), 29 | 10 * (time[14] - '0') + (time[15] - '0'), 30 | 30 31 | ); 32 | } 33 | -------------------------------------------------------------------------------- /CodeSignal/Arcade/The_Core/134 - NewYearCelebrations.cs: -------------------------------------------------------------------------------- 1 | // Solution 2 | 3 | int solution(string takeOffTime, int[] minutes) { 4 | int counter = 0; // Celebration counter 5 | int midnight = 24 * 60; // Midnight, counted in minutes 6 | int time = Convert.ToInt32(takeOffTime.Substring(0, 2)) * 60 7 | + Convert.ToInt32(takeOffTime.Substring(3)); // Take off time in minutes 8 | time = ((time == 0) ? midnight : time); // To avoid negative numbers 9 | 10 | for(int i = 0; i < minutes.Length; i++) { 11 | int dt = minutes[i] - ((i == 0) ? 0 : minutes[i - 1]); 12 | int timeAfter = time + dt - 60; // After return 13 | 14 | if(time <= midnight && midnight <= time + dt) counter++; // Mid-air celebration 15 | time += (dt - 60); // Update 16 | } 17 | 18 | if(time <= midnight) counter++; // Time after last flight 19 | 20 | return counter; 21 | } 22 | -------------------------------------------------------------------------------- /CodeSignal/Arcade/The_Core/135 - RegularMonths.cs: -------------------------------------------------------------------------------- 1 | // Solution 2 | 3 | string solution(string currMonth) { 4 | // Declare essential variables 5 | int currentYear = Convert.ToInt32(currMonth.Substring(3)); 6 | int currentMonth = Convert.ToInt32(currMonth.Substring(0, 2)) - 1; 7 | int[] daysInMonth = new int[] { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; 8 | int dayOfTheWeek = Convert.ToInt32(new DateTime(currentYear, currentMonth + 1, 1).DayOfWeek); 9 | 10 | daysInMonth[1] = 28 + (isLeapYear(currentYear) ? 1 : 0); 11 | 12 | // Iterate days until a new Monday is reached 13 | do { 14 | if(currentMonth == 12) { 15 | currentMonth = 0; 16 | currentYear++; 17 | daysInMonth[1] = 28 + (isLeapYear(currentYear) ? 1 : 0); 18 | } 19 | dayOfTheWeek += (daysInMonth[currentMonth++] - 28); 20 | } while(dayOfTheWeek % 7 != 1); 21 | 22 | // Return Date 23 | return (++currentMonth < 10 ? $"0{currentMonth}" : $"{currentMonth}") + "-" + currentYear; 24 | } 25 | 26 | bool isLeapYear(int year) { 27 | return (((year % 4 == 0) && ((year / 100) * 100 != year)) || ((year / 400) * 400 == year)); 28 | } 29 | -------------------------------------------------------------------------------- /CodeSignal/Arcade/The_Core/136 - MissedClasses.cs: -------------------------------------------------------------------------------- 1 | // Solution 2 | 3 | int solution(int year, int[] daysOfTheWeek, string[] holidays) { 4 | // Declare essential variables 5 | int count = 0; 6 | List nonWeekendDays = daysOfTheWeek.ToList(); // RemoveAll() only works for lists 7 | 8 | // Remove weekends from nonWeekendDays 9 | //nonWeekendDays.RemoveAll(x => 5 < x); // Removing Saturday and Sunday 10 | 11 | // Iterate through the holidays 12 | foreach(string holiday in holidays) { 13 | int holidayMonth = Convert.ToInt32(holiday.Substring(0, 2)); 14 | int holidayDay = Convert.ToInt32(holiday.Substring(3)); 15 | DateTime day = new DateTime(year + (7 < holidayMonth ? 0 : 1), holidayMonth, holidayDay); 16 | 17 | if(nonWeekendDays.Contains((int)day.DayOfWeek == 0 ? 7 : (int)day.DayOfWeek)) { 18 | count++; 19 | } 20 | } 21 | 22 | // Return count 23 | return count; 24 | } 25 | 26 | /* 27 | Description stated that the lessons on the weekends didn't count, however the test cases expected that you take 28 | into account the weekends as well. 29 | */ 30 | -------------------------------------------------------------------------------- /CodeSignal/Arcade/The_Core/137 - Holiday.cs: -------------------------------------------------------------------------------- 1 | // Solution 2 | 3 | int solution(int x, string weekDay, string month, int yearNumber) { 4 | // Declare essential 5 | string[] months = new string[] { "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"}; 6 | int[] daysInMonth = new int[] { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; 7 | string[] weekDays = new string[] { "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday" }; 8 | int[] weekDayIndex = new int[] { 1, 2, 3, 4, 5, 6, 7 }; 9 | 10 | // Add a day to February if it's a leap year 11 | daysInMonth[1] += (((yearNumber % 4 == 0) && ((yearNumber / 100) * 100 != yearNumber)) 12 | || ((yearNumber / 400) * 400 == yearNumber) ? 1 : 0); 13 | int monthInt = Array.IndexOf(months, month) + 1; 14 | 15 | // Retrieve week day of the 1:st in month 16 | DateTime firstDate = new DateTime(yearNumber, monthInt, 1); 17 | int day = ((int)firstDate.DayOfWeek == 0 ? 7 : (int)firstDate.DayOfWeek); 18 | 19 | // Count how many days until the first occurence of the desired week days 20 | int firstWeekDayOccurenceInMonth = Array.IndexOf(weekDays, weekDay) + 1; 21 | int date = firstWeekDayOccurenceInMonth - day + 1 + (day <= firstWeekDayOccurenceInMonth ? 0 : 7); 22 | 23 | 24 | // Return date in month 25 | int dateInMonth = date + (x - 1) * 7; 26 | return (daysInMonth[Array.IndexOf(months, month)] < dateInMonth ? -1 : dateInMonth); 27 | } 28 | -------------------------------------------------------------------------------- /CodeSignal/Arcade/The_Core/138 - IsSentenceCorrect?.cs: -------------------------------------------------------------------------------- 1 | // Solution 2 | 3 | /* 4 | ^[A-Z] checks if the first character is a capital letter. 5 | [^!?.]* checks if the characters "!", "?" and "." don't appear in the middle of the sentence. 6 | [.!?]{1}$ checks if the sentence ends with one and only one "!", "?" or ".". 7 | */ 8 | 9 | bool solution(string sentence) 10 | { 11 | Regex regex = new Regex(@"^[A-Z][^!?.]*[.!?]{1}$"); 12 | return regex.IsMatch(sentence); 13 | } 14 | -------------------------------------------------------------------------------- /CodeSignal/Arcade/The_Core/139 - ReplaceAllDigitsRegExp.cs: -------------------------------------------------------------------------------- 1 | // Solution 2 | 3 | string solution(string input) { 4 | return new Regex(@"\d").Replace(input, "#"); 5 | } 6 | -------------------------------------------------------------------------------- /CodeSignal/Arcade/The_Core/140 - SwapAdjacentWords.cs: -------------------------------------------------------------------------------- 1 | // Solution 2 | 3 | string solution(string s) { 4 | return Regex.Replace(s, @"(\w+) (\w+)", "$2 $1"); 5 | } 6 | -------------------------------------------------------------------------------- /CodeSignal/Arcade/The_Core/141 - N-th_Number.cs: -------------------------------------------------------------------------------- 1 | // Solution 2 | 3 | string solution(string s, int n) 4 | { 5 | Regex regex = new Regex( 6 | @"(?:[^1-9]*(\d+)){" + n + "}" 7 | ); 8 | return regex.Match(s).Groups[1].Value; 9 | } 10 | -------------------------------------------------------------------------------- /CodeSignal/Arcade/The_Core/142 - IsSubsequence?.cs: -------------------------------------------------------------------------------- 1 | // Solution 2 | 3 | bool solution(string t, string s) 4 | { 5 | string pattern = ""; 6 | foreach (char ch in s) 7 | { 8 | pattern += $"[{ch}].*"; 9 | } 10 | Regex regex = new Regex(pattern); 11 | return regex.Match(t).Success; 12 | } 13 | -------------------------------------------------------------------------------- /CodeSignal/Arcade/The_Core/143 - EyeRhyme.cs: -------------------------------------------------------------------------------- 1 | // Solution 2 | 3 | bool solution(string pairOfLines) 4 | { 5 | Regex regex = new Regex("(.{3})\t.*(.{3})$"); 6 | Match match = regex.Match(pairOfLines); 7 | return match.Groups[1].Value == match.Groups[2].Value; 8 | } 9 | -------------------------------------------------------------------------------- /CodeSignal/Arcade/The_Core/144 - ProgramTranslation.cs: -------------------------------------------------------------------------------- 1 | // Solution 2 | 3 | string solution(string code, string[] args) { 4 | string argumentVariants = String.Join("|", args); 5 | string pattern = "(?<=[^a-zA-Z0-9$_])(" + argumentVariants + ")([^a-zA-Z0-9_])"; 6 | string sub = "$$$1$2"; 7 | return Regex.Replace(code, pattern, sub); 8 | } 9 | -------------------------------------------------------------------------------- /CodeSignal/Arcade/The_Core/145 - RepetitionEncryption.cs: -------------------------------------------------------------------------------- 1 | // Solution 2 | 3 | int solution(string letter) 4 | { 5 | Regex regex = new Regex(@"(?i)(?<=^|[^A-Za-z])([A-Za-z]+)[^A-Za-z]+\1(?=[^A-Za-z]|$)"); 6 | return regex.Matches(letter).Count; 7 | } 8 | -------------------------------------------------------------------------------- /CodeSignal/Arcade/The_Core/146 - BugsAndBugfixes.cs: -------------------------------------------------------------------------------- 1 | // Solution 2 | 3 | int solution(string rules) 4 | { 5 | Regex regex = new Regex(@"(\d*)d(\d+)([+-]\d+)*"); 6 | MatchCollection formulas = regex.Matches(rules); 7 | 8 | int res = 0; 9 | foreach (Match match in formulas) 10 | { 11 | GroupCollection formula = match.Groups; 12 | int rolls = String.IsNullOrEmpty(formula[1].Value) ? 13 | 1 : Int32.Parse(formula[1].Value); 14 | int dieType = Int32.Parse(formula[2].Value); 15 | int formulaMax = rolls * dieType; 16 | 17 | if (!String.IsNullOrEmpty(formula[3].Value)) 18 | { 19 | if (formula[3].Value[0] == '-') 20 | { 21 | formulaMax -= Int32.Parse(formula[3].Value.Substring(1)); 22 | } 23 | else 24 | { 25 | formulaMax += Int32.Parse(formula[3].Value.Substring(1)); 26 | } 27 | } 28 | 29 | res += formulaMax; 30 | } 31 | 32 | return res; 33 | } 34 | -------------------------------------------------------------------------------- /CodeSignal/Arcade/The_Core/147 - LRCToSubRip.cs: -------------------------------------------------------------------------------- 1 | // Solution 2 | 3 | string[] solution(string[] lrcLyrics, string songLength) { 4 | string[] output = new string[(lrcLyrics.Length * 4) - 1]; 5 | 6 | for (int i = 0; i < lrcLyrics.Length; i++) { 7 | int outputIndex = i * 4; 8 | 9 | output[outputIndex++] = (i + 1).ToString(); 10 | output[outputIndex++] = GetTime(lrcLyrics[i].Substring(1, 8), false) 11 | + " --> " + (((i + 1) <= (lrcLyrics.Length - 1)) 12 | ? GetTime(lrcLyrics[i + 1].Substring(1, 8), false) 13 | : GetTime(songLength, true)); 14 | output[outputIndex++] = (lrcLyrics[i].Length <= 10) ? "" : lrcLyrics[i].Substring(11); 15 | 16 | if (i != lrcLyrics.Length - 1) output[outputIndex] = ""; 17 | } 18 | 19 | return output; 20 | } 21 | 22 | string GetTime(string timeStr, bool isSongLength) { 23 | string t = ""; 24 | 25 | if (isSongLength) { 26 | t += timeStr.Substring(0, 2) + ":"; 27 | t += timeStr.Substring(3, 2) + ":"; 28 | t += timeStr.Substring(6, 2) + ",000"; 29 | } else { 30 | t += (int.Parse(timeStr.Substring(0, 2)) / 60).ToString("00") + ":"; 31 | t += (int.Parse(timeStr.Substring(0, 2)) % 60).ToString("00") + ":"; 32 | t += (int.Parse(timeStr.Substring(3, 2)) % 60).ToString("00") + ","; 33 | t += timeStr.Substring(6, 2); //Milliseconds 34 | } 35 | 36 | while (t.Length < 12) t += "0"; 37 | 38 | return t; 39 | } 40 | -------------------------------------------------------------------------------- /CodeSignal/Arcade/The_Core/148 - HTML_Table.cs: -------------------------------------------------------------------------------- 1 | // Solution 2 | 3 | string solution(string table, int row, int column) { 4 | var r = Regex.Matches(table, ".*?"); 5 | if (r.Count <= row) return "No such cell"; 6 | 7 | var col = Regex.Matches(r[row].Value, "(.*?)"); 8 | if (col.Count <= column) return "No such cell"; 9 | 10 | 11 | return col[column].Groups[1].Value; 12 | } 13 | -------------------------------------------------------------------------------- /CodeSignal/Arcade/The_Core/149 - ChessNotation.cs: -------------------------------------------------------------------------------- 1 | // Solution 2 | 3 | string solution(string notation) { 4 | string[] b = notation.Split('/'); 5 | 6 | char[][] board = new char[8][]; 7 | 8 | for(int p = 0; p < b.Length; p++) { 9 | char[] str = new char[8]; 10 | int k = 0; 11 | for(int i = 0; i < b[p].Length; i++) { 12 | if(0 <= b[p][i] - '0' && b[p][i] - '0' < 9) for(int j = 0; j < b[p][i] - '0'; j++) str[k++] = ' '; 13 | else str[k++] = b[p][i]; 14 | } 15 | 16 | board[p] = str; 17 | } 18 | string newNotation = ""; 19 | for(int i = 0; i < board[0].Length; i++) { 20 | for(int j = board.Length - 1; 0 <= j; j--) { 21 | int emptyCounter = 0; 22 | while(j != -1 && board[j][i] == ' ') { 23 | emptyCounter++; 24 | j--; 25 | } 26 | if(emptyCounter != 0) { 27 | newNotation += emptyCounter.ToString(); 28 | if(j == -1) continue; 29 | } 30 | newNotation += board[j][i]; 31 | } 32 | if(i != board[0].Length - 1) newNotation += '/'; 33 | } 34 | 35 | return newNotation; 36 | } 37 | -------------------------------------------------------------------------------- /CodeSignal/Arcade/The_Core/150 - CellsJoining.cs: -------------------------------------------------------------------------------- 1 | // Solution 2 | 3 | string[] solution(string[] table, int[][] coords) { 4 | int[] row = Enumerable.Range(0, table.Length).Where(s => table[s][0] == '+').ToArray(); 5 | int[] pos = Enumerable.Range(0, table[0].Length).Where(s => table[0][s] == '+').ToArray(); 6 | 7 | for(int i = row[coords[1][0]]; i <= row[coords[0][0] + 1]; i++) { 8 | int coordNWall = pos[coords[0][1]] + 1; 9 | 10 | string left = table[i].Substring(0, coordNWall); 11 | string middle = table[i].Substring(coordNWall, pos[coords[1][1] + 1] - coordNWall); 12 | string right = table[i].Substring(pos[coords[1][1] + 1]); 13 | 14 | if (i == 0 || i == table.Length - 1) { 15 | table[i] = left + new String('-', middle.Length) + right; 16 | } else if (i != row[coords[1][0]] && i != row[coords[0][0] + 1]) { 17 | if (left.Length == 1) left = "|"; 18 | if (right.Length == 1) right = "|"; 19 | table[i] = left + Regex.Replace(middle, "[-+|]", " ") + right; 20 | } 21 | } 22 | return table; 23 | } 24 | -------------------------------------------------------------------------------- /CodeSignal/Arcade/The_Core/151 - FirstOperationCharacter.cs: -------------------------------------------------------------------------------- 1 | // Solution 2 | 3 | int solution(string expr) { 4 | int l = 0; 5 | 6 | int m = -1; 7 | int p = -1; 8 | int mp = -1; 9 | int pp = -1; 10 | 11 | int i = 0; 12 | foreach (char t in expr) { 13 | switch(t) { 14 | case '(': 15 | l++; 16 | break; 17 | case ')': 18 | l--; 19 | break; 20 | case '*': 21 | if (l > m) { 22 | m = l; 23 | mp = i; 24 | } 25 | break; 26 | case '+': 27 | if (l > p) { 28 | p = l; 29 | pp = i; 30 | } 31 | break; 32 | default: 33 | Console.WriteLine("ERROR!"); 34 | break; 35 | } 36 | i++; 37 | } 38 | 39 | return (p > m) ? pp : mp; 40 | } 41 | -------------------------------------------------------------------------------- /CodeSignal/Arcade/The_Core/152 - CountElements.cs: -------------------------------------------------------------------------------- 1 | // Solution 2 | 3 | int solution(string inputString) { 4 | return Regex.Matches(inputString, @"\d+|true|false|""[^""]*""").Count; 5 | } 6 | -------------------------------------------------------------------------------- /CodeSignal/Arcade/The_Core/153 - TreeBottom.cs: -------------------------------------------------------------------------------- 1 | // Solution 2 | // Inspired by pythagoras_i 3 | 4 | int[] solution(string tree) { 5 | int mostDistant = 0; 6 | int distance = 0; 7 | 8 | List list = new List(); 9 | for (int i = 0; i < tree.Length; i++) { 10 | if (tree[i] == '(') { 11 | if (tree[i + 1] == ')') { i++; continue; } 12 | distance++; 13 | if (mostDistant <= distance) { 14 | if (mostDistant < distance) { 15 | mostDistant = distance; 16 | list.Clear(); 17 | } 18 | string s = ""; 19 | i++; 20 | while ('0' <= tree[i] && tree[i] <= '9') s += tree[i++]; 21 | list.Add(int.Parse(s)); 22 | } 23 | } 24 | distance -= (tree[i] == ')' ? 1 : 0); 25 | } 26 | 27 | return list.ToArray(); 28 | } 29 | -------------------------------------------------------------------------------- /CodeSignal/Arcade/The_Core/154 - Befunge-93.cs: -------------------------------------------------------------------------------- 1 | // Solution 2 | // Inspired by chen_c5 3 | 4 | string solution(string[] program) { 5 | char[][] arr = new char[program.Length][]; 6 | for(int i = 0; i < arr.Length; i++) arr[i] = program[i].ToCharArray(); 7 | Stack stack = new Stack(); 8 | string res = ""; 9 | int symbol = 0; 10 | solve(ref stack, arr, 0, 0, 0, ref res, false, symbol); 11 | return res; 12 | } 13 | 14 | void solve(ref Stack st, 15 | char[][] arr, 16 | int nr, 17 | int nc, 18 | int dir, 19 | ref string output, 20 | bool strMode, 21 | int sym){ 22 | if(output.Length == 100) return; 23 | sym++; 24 | if(sym == 10000) return; 25 | if(strMode && arr[nr][nc] != '"') st.Push(arr[nr][nc]); 26 | else { 27 | switch(arr[nr][nc]) { 28 | case '>': 29 | dir = 0; 30 | break; 31 | case '<': 32 | dir = 1; 33 | break; 34 | case 'v': 35 | dir = 2; 36 | break; 37 | case '^': 38 | dir = 3; 39 | break; 40 | case '#': 41 | getNext(dir, arr, ref nr, ref nc); 42 | break; 43 | case '_': 44 | int a = Convert.ToInt32(pop(ref st)); 45 | dir = (a == 0) ? 0 : 1; 46 | break; 47 | case '|': 48 | dir = (Convert.ToInt32(pop(ref st)) == 0)?2:3; 49 | break; 50 | case '+': 51 | getMath(ref st, '+'); 52 | break; 53 | case '-': 54 | getMath(ref st, '-'); 55 | break; 56 | case '*': 57 | getMath(ref st, '*'); 58 | break; 59 | case '/': 60 | getMath(ref st, '/'); 61 | break; 62 | case '%': 63 | getMath(ref st, '%'); 64 | break; 65 | case '!': 66 | int a1 = (Convert.ToInt32(pop(ref st)) == 0) ? 1 : 0; 67 | st.Push(a1); 68 | break; 69 | case '`': 70 | int a2 = Convert.ToInt32(pop(ref st)); 71 | int b2 = Convert.ToInt32(pop(ref st)); 72 | int c2 = (b2 > a2)?1:0; st.Push(c2); 73 | break; 74 | case ':': 75 | st.Push(peek(ref st)); 76 | break; 77 | case '\\': 78 | object a3 = pop(ref st); 79 | object b3 = pop(ref st); 80 | st.Push(a3); st.Push(b3); 81 | break; 82 | case '$': 83 | pop(ref st); 84 | break; 85 | case '.': 86 | object a4 = pop(ref st); 87 | output += Convert.ToInt32(a4) +" "; 88 | break; 89 | case ',': 90 | output += Convert.ToChar(pop(ref st)); 91 | break; 92 | case '"': 93 | strMode = !strMode; 94 | break; 95 | case '@': 96 | return; 97 | default: 98 | if(Char.IsDigit(arr[nr][nc])) st.Push((int)(arr[nr][nc] - '0')); 99 | break; 100 | } 101 | } 102 | getNext(dir, arr, ref nr, ref nc); 103 | solve(ref st, arr, nr, nc, dir, ref output, strMode, sym); 104 | } 105 | 106 | object pop(ref Stack st) => (0 < st.Count ? st.Pop() : 0); 107 | 108 | object peek(ref Stack st) => (0 < st.Count ? st.Peek() : 0); 109 | 110 | void getNext(int dir, char[][] arr, ref int nr, ref int nc){ 111 | switch(dir){ 112 | case 0: 113 | nc++; 114 | if(nc == arr[0].Length) nc = 0; 115 | break; 116 | case 1: 117 | nc--; 118 | if(nc == -1) nc = arr[0].Length - 1; 119 | break; 120 | case 2: 121 | nr++; 122 | if(nr == arr.Length) nr = 0; 123 | break; 124 | case 3: 125 | nr--; 126 | if(nr == -1) nr = arr.Length - 1; 127 | break; 128 | } 129 | } 130 | 131 | void getMath(ref Stack st, char v){ 132 | int a = Convert.ToInt32(pop(ref st)); 133 | int b = Convert.ToInt32(pop(ref st)); 134 | int c = 0; 135 | switch(v) { 136 | case '+': 137 | c = a + b; 138 | break; 139 | case '-': 140 | c = b - a; 141 | break; 142 | case '*': 143 | c = a * b; 144 | break; 145 | case '/': 146 | c = b / a; 147 | break; 148 | case '%': 149 | c = b % a; 150 | break; 151 | } 152 | st.Push(c); 153 | } 154 | -------------------------------------------------------------------------------- /CodeSignal/Arcade/The_Core/156 - Game2048.cs: -------------------------------------------------------------------------------- 1 | // Solution 2 | 3 | int[][] solution(int[][] grid, string path) { 4 | 5 | foreach(char c in path) { 6 | if('D' < c && c < 'U') moveHorizontaly(c == 'R'); 7 | else moveVerticaly(c == 'D'); 8 | } 9 | 10 | void moveHorizontaly(bool moveRight) { 11 | for(int i = 0; i < grid.Length; i++) { 12 | int j = moveRight ? grid[0].Length - 1 : 0; 13 | List alreadyMerged = new List(); 14 | for(; (moveRight ? 0 <= j : j < grid[0].Length); j += (moveRight ? -1 : 1)) { 15 | if(grid[i][j] != 0) { 16 | bool stay = true; 17 | int dk = moveRight ? 1 : -1; 18 | for(int k = 0; 19 | (moveRight ? (k + j < grid[0].Length - 1) : 1 <= j - k) && stay; k++) { 20 | int dkk = dk * (k + 1); 21 | if(grid[i][j + dkk] == 0) { 22 | grid[i][j + dkk] = grid[i][j + dk * k]; 23 | grid[i][j + dk * k] = 0; 24 | } else if(grid[i][j + dkk] == grid[i][j + dk * k] 25 | && !alreadyMerged.Contains(i * 10 + j + dkk)) { 26 | alreadyMerged.Add(i * 10 + j + dkk); 27 | grid[i][j + dkk] *= 2; 28 | grid[i][j + dk * k] = 0; 29 | k++; 30 | } else stay = false; 31 | } 32 | } 33 | } 34 | } 35 | } 36 | 37 | void moveVerticaly(bool moveDown) { 38 | for(int j = 0; j < grid[0].Length; j++) { 39 | int i = moveDown ? grid.Length - 1 : 0; 40 | List alreadyMerged = new List(); 41 | for(; (moveDown ? 0 <= i : i < grid.Length); i += (moveDown ? -1 : 1)) { 42 | if(grid[i][j] != 0) { 43 | bool stay = true; 44 | int dk = moveDown ? 1 : -1; 45 | for(int k = 0; 46 | (moveDown ? (k + i < grid.Length - 1) : 1 <= i - k) && stay; k++) { 47 | int dkk = dk * (k + 1); 48 | if(grid[i + dkk][j] == 0) { 49 | grid[i + dkk][j] = grid[i + dk * k][j]; 50 | grid[i + dk * k][j] = 0; 51 | } else if(grid[i + dkk][j] == grid[i + dk * k][j] 52 | && !alreadyMerged.Contains(i * 10 + j + dkk)) { 53 | alreadyMerged.Add(i * 10 + j + dkk); 54 | grid[i + dkk][j] *= 2; 55 | grid[i + dk * k][j] = 0; 56 | k++; 57 | } else stay = false; 58 | } 59 | } 60 | } 61 | } 62 | } 63 | 64 | 65 | return grid; 66 | } 67 | -------------------------------------------------------------------------------- /CodeSignal/Arcade/The_Core/157 - SnakeGame.cs: -------------------------------------------------------------------------------- 1 | // Solution 2 | // Inspired by peter_s76 3 | 4 | char[][] solution(char[][] gameBoard, string commands) { 5 | (int, int)[] dirs = new[] { (-1, 0), (0, -1), (1, 0), (0, 1) }; 6 | string dirchars = "<^>v"; 7 | List<(int, int)> segs = new List<(int, int)>(); 8 | int head = 0; 9 | void put((int, int) p, char ch) => gameBoard[p.Item2][p.Item1] = ch; 10 | char gt((int, int) p) => gameBoard[p.Item2][p.Item1]; 11 | int dir() => dirchars.IndexOf(gt(segs[head])); 12 | void setdir(int d) => put(segs[head], dirchars[d]); 13 | 14 | int x, y, w = gameBoard[0].Length, h = gameBoard.Length; 15 | for(y = 0; y < h; ++y) { 16 | for(x = 0; x < w; ++x) { 17 | char ch = gameBoard[y][x]; 18 | if(ch == '^' || ch == '>' || ch == 'v' || ch == '<') { 19 | segs.Add((x, y)); 20 | break; 21 | } 22 | } 23 | } 24 | 25 | var cur = segs[0]; 26 | HashSet<(int, int)> marked = new HashSet<(int, int)> { cur }; 27 | (int, int) findUnmarked(int x0, int y0) => 28 | dirs.Select(p => (x0 + p.Item1, y0 + p.Item2)) 29 | .Where(p => p.Item1 >= 0 30 | && p.Item1 < w && p.Item2 >= 0 31 | && p.Item2 < h 32 | && gt(p) == '*' 33 | && !marked.Contains(p)) 34 | .Append((-1, -1)) 35 | .First(); 36 | for(;;) { 37 | cur = findUnmarked(cur.Item1, cur.Item2); 38 | if(cur.Equals((-1, -1))) break; 39 | marked.Add(cur); 40 | segs.Add(cur); 41 | } 42 | 43 | 44 | bool dead = false; 45 | void move() { 46 | 47 | int curdir = dir(); 48 | cur = segs[head]; 49 | var next = (cur.Item1 + dirs[curdir].Item1, cur.Item2 + dirs[curdir].Item2); 50 | if(next.Item1 < 0 51 | || next.Item1 >= w 52 | || next.Item2 < 0 53 | || next.Item2 >= h 54 | || gt(next) != '.') { 55 | kill(); 56 | return; 57 | } 58 | 59 | int tail = head == 0 ? segs.Count - 1 : head - 1; 60 | put(cur, '*'); 61 | put(segs[tail], '.'); 62 | put(next, dirchars[curdir]); 63 | head = tail; 64 | segs[head] = next; 65 | } 66 | 67 | void turn(int cw) { 68 | int curdir = dir() + cw; 69 | curdir = curdir >= 0 ? curdir % 4 : 3; 70 | setdir(curdir); 71 | } 72 | 73 | void kill() { 74 | dead = true; 75 | foreach(var p in segs) put(p, 'X'); 76 | } 77 | 78 | foreach(char cmd in commands) { 79 | switch(cmd) { 80 | case 'F': 81 | move(); 82 | break; 83 | case 'L': 84 | turn(-1); 85 | break; 86 | case 'R': 87 | turn(1); 88 | break; 89 | } 90 | if(dead) break; 91 | } 92 | 93 | return gameBoard; 94 | } 95 | -------------------------------------------------------------------------------- /CodeSignal/Arcade/The_Core/158 - TetrisGame.cs: -------------------------------------------------------------------------------- 1 | // Solution 2 | // Inspired by codey2 3 | 4 | int solution(char[][][] pieces) { 5 | int res = 0; 6 | char[][] board = Enumerable.Range(0, 20) 7 | .Select(i => new string('.', 10) 8 | .ToCharArray()) 9 | .ToArray(); 10 | 11 | 12 | for (int j = 0; j < pieces.Length; j++) { 13 | char[][] p = pieces[j]; 14 | int[] choice = FindTheBestChoice(board, p); 15 | for (int i = 1; i <= choice[1]; i++) p = RotatePiece(p); 16 | FixThePiece(ref board, p, choice[3], choice[2]); 17 | List filled = GetFullLines(board); 18 | 19 | foreach (int i in filled) { 20 | ClearTheFilledLine(ref board, i); 21 | res++; 22 | } 23 | } 24 | 25 | return res; 26 | } 27 | 28 | char[][] RotatePiece(char[][] p) { 29 | return Enumerable.Range(0, p[0].Length). 30 | Select(i => Enumerable.Range(0, p.Length). 31 | Select(j => p[j][i]).Reverse().ToArray()).ToArray(); 32 | } 33 | 34 | int ThrowPiece(char[][] board, char[][] piece, int col) { 35 | int pi = piece.Length, pj = piece[0].Length, row = 0;; 36 | bool isFixed = true; 37 | 38 | if (board[0].Length < pj + col) return -1; 39 | 40 | while (isFixed) { 41 | isFixed = true; 42 | 43 | for (int i = 0; i < pi; i++) { 44 | for (int j = 0; j < pj; j++) { 45 | if (board[row + i][col + j] == '#' && piece[i][j] == '#') { 46 | isFixed = false; 47 | break; 48 | } 49 | } 50 | } 51 | row += (isFixed ? 1 : 0); 52 | if (row == board.Length - pi + 1) break; 53 | } 54 | row--; 55 | 56 | return row; 57 | } 58 | 59 | List GetFullLines(char[][] board) { 60 | List res = new List(0); 61 | for (int i = 0; i < board.Length; i++) if (!board[i].Contains('.')) res.Add(i); 62 | 63 | return res; 64 | } 65 | 66 | void ClearTheFilledLine(ref char[][] board, int i) { 67 | var res = board.ToList(); 68 | 69 | res.RemoveAt(i); 70 | res.Insert(0, new string('.', 10).ToArray()); 71 | board = res.ToArray(); 72 | } 73 | 74 | void FixThePiece(ref char[][] board, char[][] piece, int row, int col) { 75 | int pi = piece.Length, pj = piece[0].Length; 76 | 77 | for (int i = 0; i < pi; i++) { 78 | for (int j = 0; j < pj; j++) { 79 | board[row + i][col + j] = piece[i][j] == '#' ? '#' : board[row + i][col + j]; 80 | } 81 | } 82 | } 83 | 84 | int[] FindTheBestChoice(char[][] board, char[][] piece) { 85 | List choices = new List(0); 86 | char[][] p = piece.Select(x => x.Select(y => y).ToArray()).ToArray(); 87 | 88 | for (int r = 0; r < 4; r++) { 89 | if (0 < r) p = RotatePiece(p); 90 | 91 | for (int col = 0; col <= board[0].Length - p[0].Length; col++) { 92 | int row = ThrowPiece(board, p, col); 93 | int blocks = Enumerable.Range(row, p.Length) 94 | .Select(i => board[i].Where(y => y == '#') 95 | .Count()) 96 | .Sum(); 97 | choices.Add(new int[] { blocks, r, col, row }); 98 | } 99 | } 100 | 101 | return choices.OrderByDescending(x => x[0]) 102 | .ThenBy(x => x[1]) 103 | .ThenBy(x => x[2]) 104 | .FirstOrDefault(); 105 | } 106 | -------------------------------------------------------------------------------- /CodeSignal/Arcade/The_Core/159 - PyraminxPuzzle.cs: -------------------------------------------------------------------------------- 1 | // Solution 2 | // Inspired by me281 3 | 4 | char[][] solution(char[] faceColors, string[] moves) { 5 | 6 | char[][] result = Enumerable.Range(0, 4) 7 | .Select(p => Enumerable.Repeat(faceColors[p], 9).ToArray()) 8 | .ToArray(); 9 | 10 | Dictionary movelist = new Dictionary() { 11 | ['U'] = new int[] { 0, 2, 3 }, 12 | ['B'] = new int[] { 1, 3, 2 }, 13 | ['L'] = new int[] { 2, 0, 1 }, 14 | ['R'] = new int[] { 3, 1, 0 }, 15 | }; 16 | 17 | int[] smallmove = new int[] { 0, 4, 8 }; 18 | int[][] bigmove = new int[][] { 19 | new int[] { 1, 2, 3 }, 20 | new int[] { 6, 5, 1 }, 21 | new int[] { 3, 7, 6 } 22 | }; 23 | 24 | foreach (string move in moves.Reverse()) { 25 | char[][] res = result.Select(p => p.Select(q => q).ToArray()).ToArray(); 26 | 27 | string hm = move.ToUpper(); 28 | int[] ml = movelist[hm[0]]; 29 | 30 | for (int i = 0; i < 3; i++) { 31 | int j = (3 + i + ((move.Length == 2) ? 1 : -1)) % 3; 32 | result[ml[i]][smallmove[i]] = res[ml[j]][smallmove[j]]; 33 | if (hm != move) { 34 | for (int k = 0; k < 3; k++) { 35 | result[ml[i]][bigmove[i][k]] = res[ml[j]][bigmove[j][k]]; 36 | } 37 | } 38 | } 39 | } 40 | 41 | return result; 42 | } 43 | -------------------------------------------------------------------------------- /CodeSignal/Arcade/The_Core/160 - LinesGame.cs: -------------------------------------------------------------------------------- 1 | // Solution 2 | // Inspired by armin_k1 3 | 4 | int[] p, q; 5 | int n, s, r; 6 | 7 | int solution(char[][] F, int[][] C, char[] B, int[][] N) => 8 | C.Sum (c => { 9 | if (F[c[0]][c[1]] > 46) { 10 | p = c; 11 | return 0; 12 | } 13 | if (p == q || 1 > R(F, new int[9, 9], p[1], p[r = 0], c[1], c[0])) { 14 | p = q; 15 | return 0; 16 | } 17 | F[c[0]][c[1]] = F[p[0]][p[1]]; 18 | F[p[0]][p[1]] = '.'; 19 | p = q; 20 | s = S(F); 21 | return s > 0 ? s : " ".Max(_ => F[N[n][0]][N[n][1]] = B[n++]) * 0 + S(F); 22 | }); 23 | 24 | int R(dynamic F, int[,]V, int x, int y, int X, int Y) => 25 | x < 0 | x > 8 | y < 0 | y > 8 || r++ > 0 && F[y][x] + V[y,x]++ > 46 ? 0 : 26 | x == X & y == Y ? 1 : 27 | "681=".Max (d => R(F, V, x + d % 3 - 1, y + d/6 - 9, X, Y)); 28 | 29 | int S(dynamic F) { 30 | var D = new int[9,9]; 31 | int s = 0, m, M, i = 9, j, d; 32 | for (; i-- > 0; ) 33 | for (d = -1; d < 3; ++d) { 34 | j = d < 2 ? F[i][4] : F[4][i]; 35 | if (j > 46) { 36 | m = M = 0; 37 | try { 38 | for (; (d < 2 ? F[i + d * m - d][m + 3] : F[m + 3][i]) == j; ) m--; 39 | } catch {} 40 | try { 41 | for (; (d < 2 ? F[i + d * ++M][M + 4] : F[++M + 4][i]) == j;); 42 | } catch {} 43 | if (m + 4 < M) { 44 | for (s += M - m + 1; m < M; ) { 45 | if (d < 2) D[i + d * m, 4 + m++] = 1; 46 | else D[4 + m++, i] = 1; 47 | } 48 | } 49 | } 50 | } 51 | for (i = 9; i-- > 0; ) for (j = 9; j-- > 0; ) if (D[i,j] > 0) F[i][j] = '.'; 52 | return s > 0 ? s - 1 : 0; 53 | } 54 | -------------------------------------------------------------------------------- /CodeSignal/Arcade/The_Core/161 - Fractal.cs: -------------------------------------------------------------------------------- 1 | // Solution 2 | // Inspired by l_aramyan 3 | 4 | char[][] solution(int n) { 5 | char[][] start = new char[3][]; 6 | start[0] = new char[] { ' ', '_', ' ' }; 7 | start[1] = new char[] { ' ', ' ', '|' }; 8 | start[2] = new char[] { ' ', '_', ' ' }; 9 | 10 | for(int i = 2; i <= n; i++) start = NextIteration(start, i); 11 | 12 | return ReturnFinalView(start); 13 | } 14 | 15 | static char[][] Rotate(char[][] fr) { 16 | return Enumerable.Range(0,fr[0].Length).Select 17 | (j => Enumerable.Range(0,fr.Length).Select ( 18 | i => fr[i][j] =='_' ? '|' : fr[i][j] == '|' ? '_' : ' ') 19 | .Reverse().ToArray()).ToArray(); 20 | } 21 | 22 | static char[][] NextIteration(char[][] fr0, int n) { 23 | int len = fr0.Length; 24 | var fr1 = Rotate(fr0); 25 | var fr2 = Rotate(fr1); 26 | var fr3 = Rotate(fr2); 27 | char[][] res; 28 | if (n % 2 == 0) { 29 | char[] middle = $"|{new string(' ', 2 * len - 1)}|".ToCharArray(); 30 | res = Enumerable.Range(0, len) 31 | .Select(i => MergeArrays(fr0[i], fr2[i])) 32 | .Union(new List{middle}.Union 33 | (Enumerable.Range(0, len).Select(j => MergeArrays(fr1[j], fr1[j]))) 34 | ).ToArray(); 35 | res[len + 1][len] = '_'; 36 | } else { 37 | char[] middle = $"{new string(' ', 2 * len + 1)}".ToCharArray(); 38 | middle[len + 1] = '|'; 39 | res = Enumerable.Range(0, len).Select(i => MergeArrays(fr0[i], fr3[i])).Union( 40 | new List { middle }.Union 41 | (Enumerable.Range(0, len).Select(j => MergeArrays(fr2[j], fr3[j]))) 42 | ).ToArray(); 43 | res[0][len] = '_'; 44 | res[2 * len][len] = '_'; 45 | } 46 | 47 | return res; 48 | } 49 | 50 | static char[] MergeArrays(char[] a, char[] b) { 51 | List res = a.ToList(); 52 | res.Add(' '); 53 | res.AddRange(b); 54 | return res.ToArray(); 55 | } 56 | 57 | static char[][] ReturnFinalView(char[][] input) { 58 | List output = new List { input[0] }; 59 | 60 | for(int i = 2; i < input.Length; i+=2) { 61 | output.Add(Enumerable.Range(0, input[0].Length).Select 62 | (j => input[i][j] != ' ' ? input[i][j] : input[i - 1][j]).ToArray()); 63 | } 64 | 65 | char[][] res = output.ToArray(); 66 | 67 | for (int i = 0; i < res.Length; i++) { 68 | for (int j = 2; j < res[0].Length; j++) { 69 | if (res[i][j] == '_' 70 | && res[i][j - 1] == ' ' 71 | && res[i][j - 2] == '_') res[i][j-1] = '_'; 72 | } 73 | } 74 | 75 | return res; 76 | } 77 | -------------------------------------------------------------------------------- /CodeSignal/Arcade/The_Core/162 - TimeASCII_Representation.cs: -------------------------------------------------------------------------------- 1 | // Solution 2 | // Inspired by casper_h3 3 | 4 | char[][] solution(char[][] dtime) { 5 | TimeSpan time = ReadTime(dtime); 6 | char[][] result = Round(); 7 | 8 | SetHand(result, (time.Hours * 60.0 + time.Minutes) / 720.0); 9 | SetHand(result, (double)time.Minutes / 60.0); 10 | 11 | return result; 12 | } 13 | 14 | Dictionary digits = new Dictionary() { 15 | { "****.**.**.*", 0 }, 16 | { ".*..*..*..*.", 1 }, 17 | { "***..*****..", 2 }, 18 | { "***..****..*", 3 }, 19 | { "*.**.****..*", 4 }, 20 | { "****..***..*", 5 }, 21 | { "****..****.*", 6 }, 22 | { "***..*..*..*", 7 }, 23 | { "****.*****.*", 8 }, 24 | { "****.****..*", 9 }, 25 | }; 26 | 27 | TimeSpan ReadTime(char[][] dtime) { 28 | int time = 0; 29 | int[] arr = new int[] { 1, 5, 10, 14 }; 30 | foreach(int num in arr) { 31 | string key = new string(new [] {0, 7, 8, 9} 32 | .SelectMany(r => Enumerable.Range(num, 3) 33 | .Select(c => dtime[r][c])) 34 | .ToArray()); 35 | 36 | digits.TryGetValue(key, out int digit); 37 | time = 10 * time + digit; 38 | } 39 | 40 | return TimeSpan.FromMinutes((time / 100 * 60) + time % 100); 41 | } 42 | 43 | char[][] Round() { 44 | return new char[][] { 45 | "....*********....".ToArray(), 46 | "...**.......**...".ToArray(), 47 | "..**.........**..".ToArray(), 48 | ".**...........**.".ToArray(), 49 | "**.............**".ToArray(), 50 | "*...............*".ToArray(), 51 | "*...............*".ToArray(), 52 | "*...............*".ToArray(), 53 | "*...............*".ToArray(), 54 | "*...............*".ToArray(), 55 | "*...............*".ToArray(), 56 | "*...............*".ToArray(), 57 | "**.............**".ToArray(), 58 | ".**...........**.".ToArray(), 59 | "..**.........**..".ToArray(), 60 | "...**.......**...".ToArray(), 61 | "....*********....".ToArray(), 62 | }; 63 | } 64 | 65 | double maxDist = Math.Sqrt(0.5); 66 | void SetHand(char[][] clock, double pos) { 67 | clock[8][8] = '*'; 68 | double sin = Math.Sin(pos * Math.PI * 2.0); 69 | double cos = -Math.Cos((double) pos * Math.PI * 2.0); 70 | for (double i = 0.0; i <= 8.0; i += 0.2) { 71 | double yd = 8 + cos * i; 72 | double xd = 8 + sin * i; 73 | 74 | for(int y = (int)yd - 1; y <= (int)yd + 1; y++) { 75 | if (y < 0 || y > 17) continue; 76 | for (int x = (int)xd - 1; x <= (int)xd + 1; x++) { 77 | if (x < 0 || x > 17) continue; 78 | double dist = Math.Abs(Math.Sqrt((yd - y) * (yd - y) + (xd - x) * (xd - x))); 79 | if (dist < maxDist) clock[y][x] = '*'; 80 | } 81 | } 82 | } 83 | } 84 | -------------------------------------------------------------------------------- /CodeSignal/Interview Practice/Arrays/firstDuplicate.cs: -------------------------------------------------------------------------------- 1 | int solution(int[] a) { 2 | HashSet alreadyVisited = new HashSet(); 3 | 4 | foreach(int i in a) { 5 | if(alreadyVisited.Contains(i)) return i; 6 | alreadyVisited.Add(i); 7 | } 8 | 9 | return -1; 10 | } 11 | -------------------------------------------------------------------------------- /CodeSignal/Interview Practice/Arrays/firstNotRepeatingCharacter.cs: -------------------------------------------------------------------------------- 1 | /* Solution using only static arrays */ 2 | /* Time complexity: O(2n) */ 3 | /* Space complexity: O(1) */ 4 | 5 | char solution(string s) { 6 | int[] chrArr = new int[26]; 7 | int charInOrder = 2; 8 | // 0 = Char Not encountered 9 | // 100 = Char Duplicate 10 | // 2 to 28 = Order of first seen character 11 | 12 | // Populate array which non-duplicate chars in their order 13 | foreach(char c in s) { 14 | if(chrArr[c - 'a'] == 0) { 15 | chrArr[c - 'a'] = charInOrder; 16 | charInOrder++; 17 | } else { 18 | chrArr[c - 'a'] = 100; 19 | } 20 | } 21 | 22 | // Find the smalles charInOrder i.e. the first occuring non-duplicate 23 | int charOfInterest = -1; 24 | int minCharInOrder = 100; 25 | 26 | // Extract the first in order non-duplicate char 27 | for(int i = 0; i < chrArr.Length; i++) { 28 | if(chrArr[i] != 0 && chrArr[i] < minCharInOrder) { 29 | charOfInterest = i; 30 | minCharInOrder = chrArr[i]; 31 | } 32 | } 33 | 34 | return (charOfInterest == -1 ? '_' : (char)('a' + charOfInterest)); 35 | } 36 | -------------------------------------------------------------------------------- /CodeSignal/Interview Practice/Arrays/isCryptSolution.cs: -------------------------------------------------------------------------------- 1 | bool solution(string[] crypt, char[][] solution) { 2 | int[] termsAndSum = new int[3]; 3 | 4 | for(int i = 0; i < crypt.Length; i++) { 5 | int pow10 = 1; 6 | if(1 < crypt[i].Length && giveNum(crypt[i][0], solution) == 0) return false; 7 | for(int j = crypt[i].Length - 1; j >= 0; j--) { 8 | termsAndSum[i] += giveNum(crypt[i][j], solution) * pow10; 9 | pow10 *= 10; 10 | } 11 | } 12 | 13 | return termsAndSum[0] + termsAndSum[1] == termsAndSum[2]; 14 | } 15 | 16 | int giveNum(char c, char[][] solution) { 17 | foreach(char[] translation in solution) { 18 | if(translation[0] == c) return translation[1] - '0'; 19 | } 20 | 21 | return 0; 22 | } 23 | -------------------------------------------------------------------------------- /CodeSignal/Interview Practice/Arrays/rotateImage.cs: -------------------------------------------------------------------------------- 1 | /* Solution using only static arrays */ 2 | /* Time complexity: O(n) */ 3 | /* Space complexity: O(1) */ 4 | 5 | int[][] solution(int[][] a) { 6 | for(int i = 0; i < a.Length / 2; i++) { 7 | // Rotating each layer iteratively 8 | a = layerRotator(a, i, i, a.Length - 1 - i * 2); 9 | } 10 | return a; 11 | } 12 | 13 | int[][] layerRotator(int[][] a, int iStart, int jStart, int width) { 14 | for(int i = 0; i < width; i++) { 15 | int befSwapped = a[iStart + i][jStart + width]; 16 | int aftSwapped; 17 | 18 | a[iStart + i][jStart + width] = a[iStart][jStart + i]; 19 | 20 | aftSwapped = a[iStart + width][jStart + width - i]; 21 | a[iStart + width][jStart + width - i] = befSwapped; 22 | 23 | befSwapped = a[iStart + width - i][jStart]; 24 | a[iStart + width - i][jStart] = aftSwapped; 25 | 26 | aftSwapped = a[iStart][jStart + i]; 27 | a[iStart][jStart + i] = befSwapped; 28 | } 29 | 30 | return a; 31 | } 32 | -------------------------------------------------------------------------------- /CodeSignal/Interview Practice/Arrays/sudoku2.cs: -------------------------------------------------------------------------------- 1 | bool solution(char[][] grid) { 2 | for(int i = 0; i < grid.Length; i++) { 3 | int[] row = new int[9]; 4 | int[] col = new int[9]; 5 | for(int j = 0; j < 9; j++) { 6 | if(grid[i][j] != '.') { 7 | row[grid[i][j] - '1']++; 8 | if(row[grid[i][j] - '1'] == 2) return false; 9 | } 10 | if(grid[j][i] != '.') { 11 | col[grid[j][i] - '1']++; 12 | if(col[grid[j][i] - '1'] == 2) return false; 13 | } 14 | 15 | } 16 | } 17 | 18 | for(int i = 1; i < 9; i+=3) { 19 | for(int j = 1; j < 9; j+=3) { 20 | int[] alreadyCounted = new int[9]; 21 | for(int k = -1; k < 2; k++) { 22 | for(int l = -1; l < 2; l++) { 23 | if(grid[i + k][j + l] == '.') continue; 24 | alreadyCounted[grid[i + k][j + l] - '1']++; 25 | if(alreadyCounted[grid[i + k][j + l] - '1'] == 2) return false; 26 | } 27 | } 28 | } 29 | } 30 | 31 | return true; 32 | } 33 | -------------------------------------------------------------------------------- /CodeSignal/Interview Practice/Linked Lists/isListPalindrome.cs: -------------------------------------------------------------------------------- 1 | // Singly-linked lists are already defined with this interface: 2 | // class ListNode { 3 | // public T value { get; set; } 4 | // public ListNode next { get; set; } 5 | // } 6 | // 7 | bool solution(ListNode l) { 8 | if(l == null) return true; 9 | 10 | int lenDiv2 = 0; 11 | ListNode fastPointer = l; 12 | ListNode middPointer = l; 13 | 14 | // Find middlepoint of list 15 | while(fastPointer != null && fastPointer.next != null) { 16 | middPointer = middPointer.next; 17 | fastPointer = fastPointer.next.next; 18 | lenDiv2++; 19 | } 20 | 21 | // Reverse second half of linkedlist 22 | ListNode prevPointer = middPointer; 23 | middPointer = middPointer.next; 24 | ListNode nextPointer; 25 | while(middPointer != null) { 26 | nextPointer = middPointer.next; 27 | middPointer.next = prevPointer; 28 | prevPointer = middPointer; 29 | middPointer = nextPointer; 30 | } 31 | 32 | // Check if palindrome 33 | for(int i = 0; i < lenDiv2; i++) { 34 | if(l.value != prevPointer.value) return false; 35 | l = l.next; 36 | prevPointer = prevPointer.next; 37 | } 38 | 39 | return true; 40 | } 41 | -------------------------------------------------------------------------------- /CodeSignal/Interview Practice/Linked Lists/removeKFromList.cs: -------------------------------------------------------------------------------- 1 | // Singly-linked lists are already defined with this interface: 2 | // class ListNode { 3 | // public T value { get; set; } 4 | // public ListNode next { get; set; } 5 | // } 6 | // 7 | ListNode solution(ListNode l, int k) { 8 | if(l == null) return l; 9 | ListNode prev = null; 10 | ListNode current = l; 11 | ListNode start = current; 12 | 13 | while(current != null) { 14 | if(current.value == k) { 15 | if(prev == null) { 16 | prev = current; 17 | current = current.next; 18 | start = current; 19 | prev = null; 20 | continue; 21 | } if(current.next == null) { 22 | prev.next = null; 23 | current = null; 24 | break; 25 | } else { 26 | current = current.next; 27 | prev.next = current; 28 | continue; 29 | } 30 | } 31 | prev = current; 32 | current = current.next; 33 | } 34 | 35 | return start; 36 | } 37 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # CodeSignal Solutions 2 | This is a collection of solutions for the code challanges in CodeFights/CodeSignal in C#. 3 | This repository will contain solutions for every code challange in the "Intro"-mode in the Arcade 4 | however solutions of the other modes will also eventually be added. 5 | 6 | Other topics such as interview preparation will also be added in the not so distant future. 7 | 8 | This repository was created to sharpen developers problem solving skills. 9 | --------------------------------------------------------------------------------