├── .gitignore ├── 0-easy ├── age-distribution │ ├── input.txt │ ├── main.cpp │ └── main.py ├── armstrong-numbers │ ├── ArmstrongNumbers.java │ ├── input.txt │ └── main.cpp ├── beautiful-strings │ ├── BeautifulStrings.java │ └── input.txt ├── big-digits │ ├── input.txt │ └── main.py ├── bit-positions │ ├── BitPositions.java │ └── input.txt ├── black-card │ ├── input-large.txt │ ├── input-small.txt │ └── main.py ├── calculate-distance │ ├── CalculateDistance.java │ └── input.txt ├── capitalize-words │ ├── CapitalizeWords.java │ └── input.txt ├── chardonnay-or-cabernet │ ├── input-large.txt │ ├── input-small.txt │ └── main.py ├── clean-up-the-words │ ├── input-large.txt │ ├── input-small.txt │ └── main.py ├── compare-points │ ├── input.txt │ └── main.py ├── compressed-sequence │ ├── CompressedSequence.java │ └── input.txt ├── data-recovery │ ├── input.txt │ └── main.py ├── delta-time │ ├── input.txt │ └── main.py ├── details │ ├── input-large.txt │ ├── input-small.txt │ └── main.py ├── even-numbers │ ├── EvenNumbers.java │ ├── input.txt │ └── main.cpp ├── fibonacci-series │ ├── FibonacciSeries.java │ ├── input.txt │ └── main.py ├── file-size │ ├── FileSize.java │ ├── input.txt │ └── main.cpp ├── find-a-writer │ ├── FindAWriter.java │ └── input.txt ├── find-the-highest-score │ ├── input.txt │ └── main.py ├── fizz-buzz │ ├── FizzBuzz.java │ └── input.txt ├── football │ ├── input.txt │ └── main.py ├── happy-numbers │ ├── HappyNumbers.java │ ├── input.txt │ └── main.cpp ├── hex-to-decimal │ ├── HexToDecimal.java │ ├── input.txt │ ├── main.cpp │ └── main.py ├── hidden-digits │ ├── HiddenDigits.java │ └── input.txt ├── json-menu-ids │ ├── JsonMenuIds.java │ └── input.txt ├── juggling-with-zeros │ ├── input.txt │ └── main.py ├── knight-moves │ ├── input.txt │ └── main.py ├── lettercase-percentage-ratio │ ├── input.txt │ └── main.py ├── longest-word │ ├── LongestWord.java │ └── input.txt ├── lowercase │ ├── Lowercase.java │ ├── input.txt │ └── main.cpp ├── lowest-unique-number │ ├── LowestUniqueNumber.java │ └── input.txt ├── matrix-rotation │ ├── input.txt │ └── main.py ├── max-range-sum │ ├── input.txt │ └── main.py ├── mersenne-prime │ ├── input.txt │ ├── main.cpp │ └── main.py ├── minimum-distance │ ├── input.txt │ └── main.py ├── mixed-content │ ├── MixedContent.java │ └── input.txt ├── morse-code │ ├── MorseCode.java │ └── input.txt ├── multiples-of-a-number │ ├── MultiplesOfANumber.java │ └── input.txt ├── multiplication-tables │ ├── MultiplicationTables.java │ └── main.cpp ├── multiply-lists │ ├── MultiplyLists.java │ ├── input.txt │ └── main.cpp ├── n-mod-m │ ├── NModM.java │ └── input.txt ├── nice-angles │ ├── input-large.txt │ ├── input-small.txt │ └── main.py ├── not-so-clever │ ├── input.txt │ ├── main.go │ └── main.py ├── odd-numbers │ ├── OddNumbers.java │ └── main.cpp ├── one-zero-two-zeros │ ├── input.txt │ └── main.py ├── penultimate-word │ ├── PenultimateWord.java │ └── input.txt ├── prime-palindrome │ └── PrimePalindrome.java ├── query-board │ ├── QueryBoard.java │ └── input.txt ├── racing-cars │ ├── input.txt │ └── main.py ├── read-more │ ├── input-large.txt │ ├── input-small.txt │ └── main.py ├── real-fake │ ├── input.txt │ └── main.py ├── reverse-words │ ├── ReverseWords.java │ └── input.txt ├── rightmost-char │ ├── RightmostChar.java │ └── input.txt ├── road-trip │ ├── RoadTrip.java │ └── input.txt ├── roller-coaster │ ├── input.txt │ └── main.py ├── roman-numerals │ ├── RomanNumerals.java │ └── input.txt ├── self-describing-numbers │ ├── SelfDescribingNumbers.java │ └── input.txt ├── set-intersection │ ├── SetIntersection.java │ └── input.txt ├── shortest-repetition │ ├── ShortestRepetition.java │ ├── input.txt │ └── main.py ├── simple-or-trump │ ├── input.txt │ └── main.py ├── simple-sorting │ ├── SimpleSorting.java │ ├── input.txt │ ├── main.go │ └── main.py ├── slang-flavor │ ├── input.txt │ └── main.py ├── split-the-number │ ├── SplitTheNumber.java │ ├── input.txt │ └── main.py ├── stepwise-word │ ├── input.txt │ └── main.py ├── string-mask │ ├── input.txt │ └── main.py ├── strings-and-arrows │ ├── input.txt │ └── main.py ├── sum-of-digits │ ├── SumOfDigits.java │ ├── input.txt │ └── main.cpp ├── sum-of-integers-in-a-file │ ├── SumOfIntegersInAFile.java │ ├── input.txt │ └── main.cpp ├── sum-of-primes │ ├── SumOfPrimes.java │ └── main.cpp ├── swap-case │ ├── SwapCase.java │ ├── input.txt │ └── main.py ├── swap-elements │ ├── SwapElements.java │ └── input.txt ├── swap-numbers │ ├── input.txt │ └── main.py ├── testing │ ├── input.txt │ └── main.py ├── the-major-element │ ├── input.txt │ └── main.py ├── time-to-eat │ ├── input.txt │ └── main.py ├── trick-or-treat │ ├── input.txt │ └── main.py ├── truth-or-lie │ ├── input.txt │ └── main.py ├── unique-elements │ ├── UniqueElements.java │ ├── input.txt │ └── main.cpp ├── without-repetitions │ ├── input.txt │ └── main.py ├── word-to-digits │ ├── WordToDigits.java │ └── input.txt └── working-experience │ ├── input.txt │ └── main.py ├── 1-moderate ├── a-pile-of-bricks │ ├── input.txt │ └── main.py ├── alternative-reality │ ├── input.txt │ └── main.py ├── array-absurdity │ ├── ArrayAbsurdity.java │ └── input.txt ├── balanced-smileys │ ├── input.txt │ └── main.py ├── bats-challenge │ ├── input.txt │ └── main.py ├── black-or-white │ ├── input.txt │ └── main.py ├── broken-lcd │ ├── input.txt │ └── main.py ├── builders-team │ ├── input1.txt │ ├── input2.txt │ └── main.py ├── burrows-wheeler-transform │ ├── input.txt │ └── main.py ├── car-race │ ├── input1.txt │ ├── input2.txt │ └── main.py ├── card-number-validation │ ├── input.txt │ └── main.py ├── cash-register │ ├── input.txt │ └── main.py ├── chain-inspection │ ├── input.txt │ └── main.py ├── city-blocks-flyover │ ├── input.txt │ └── main.py ├── cocktail-sort │ ├── input.txt │ └── main.py ├── code-comparison │ ├── input.txt │ └── main.py ├── color-code-converter │ ├── input.txt │ └── main.py ├── column-names │ ├── ColumnNames.java │ ├── input.txt │ ├── main.cpp │ └── main.py ├── comb-sort │ ├── input.txt │ └── main.py ├── consecutive-primes │ ├── input.txt │ ├── main.cpp │ └── main.py ├── counting-primes │ ├── CountingPrimes.java │ └── input.txt ├── decimal-to-binary │ ├── DecimalToBinary.java │ ├── input.txt │ └── main.cpp ├── decode-numbers │ ├── input.txt │ └── main.py ├── detecting-cycles │ ├── DetectingCycles.java │ └── input.txt ├── double-squares │ ├── input.txt │ ├── main.cpp │ └── main.py ├── double-trouble │ ├── input.txt │ └── main.py ├── email-verification │ ├── EmailVerification.java │ └── input.txt ├── endianness │ ├── Endianness.java │ └── main.cpp ├── filename-pattern │ ├── input-large.txt │ ├── input-small.txt │ └── main.py ├── find-square │ ├── FindSquare.java │ └── input.txt ├── first-non-repeated-char │ ├── FirstNonRepeatedChar.java │ └── input.txt ├── flavius-josephus │ ├── FlaviusJosephus.java │ └── input.txt ├── game-of-life │ ├── input-large.txt │ ├── input-small.txt │ └── main.py ├── gray-code │ ├── input.txt │ └── main.py ├── gronsfeld-cipher │ ├── input.txt │ └── main.py ├── guess-the-number │ ├── input.txt │ └── main.py ├── interrupted-bubble-sort │ ├── input-large.txt │ ├── input-small.txt │ └── main.py ├── jolly-jumpers │ ├── JollyJumpers.java │ └── input.txt ├── justify-the-text │ ├── input.txt │ └── main.py ├── locks │ ├── input.txt │ └── main.py ├── longest-lines │ ├── LongestLines.java │ └── input.txt ├── lost-in-translation │ ├── input.txt │ └── main.py ├── lowest-common-ancestor │ ├── LowestCommonAncestor.java │ └── input.txt ├── lucky-tickets │ ├── fast.py │ ├── input.txt │ └── slow.py ├── magic-numbers │ ├── input.txt │ └── main.py ├── minimum-coins │ ├── MinimumCoins.java │ └── input.txt ├── mth-to-last-element │ ├── MthToLastElement.java │ └── input.txt ├── number-of-ones │ ├── NumberOfOnes.java │ ├── input.txt │ └── main.cpp ├── number-operations │ ├── input.txt │ └── main.py ├── number-pairs │ ├── NumberPairs.java │ └── input.txt ├── organizational-hierarchy │ ├── input.txt │ └── main.py ├── overlapping-rectangles │ ├── OverlappingRectangles.java │ ├── input.txt │ └── main.py ├── pangrams │ ├── Pangrams.java │ └── input.txt ├── pascals-triangle │ ├── PascalsTriangle.java │ └── input.txt ├── pass-triangle │ ├── PassTriangle.java │ └── input.txt ├── point-in-circle │ ├── PointInCircle.java │ └── input.txt ├── predict-the-number │ ├── input.txt │ └── main.py ├── prime-numbers │ ├── PrimeNumbers.java │ └── input.txt ├── remove-characters │ ├── RemoveCharacters.java │ ├── input.txt │ └── main.py ├── reverse-and-add │ ├── ReverseAndAdd.java │ ├── input.txt │ └── main.cpp ├── reverse-groups │ ├── ReverseGroups.java │ └── input.txt ├── robo-and-robitta │ ├── input.txt │ └── main.py ├── roman-and-arabic │ ├── input.txt │ └── main.py ├── seek-for-an-intruder │ ├── input.txt │ └── main.py ├── sequence-transformation │ ├── input.txt │ └── main.py ├── simple-calculator │ ├── SimpleCalculator.java │ └── input.txt ├── sort-martix-columns │ ├── input.txt │ └── main.py ├── stack-implementation │ ├── StackImplementation.java │ └── input.txt ├── string-rotation │ ├── StringRotation.java │ └── input.txt ├── sudoku │ ├── Sudoku.java │ └── input.txt ├── suggest-groups │ ├── input.txt │ └── main.py ├── sum-of-integers │ ├── SumOfIntegers.java │ └── input.txt ├── sum-to-zero │ ├── input.txt │ └── main.py ├── the-ministry-of-truth │ ├── input.txt │ └── main.py ├── to-pi-or-not-to-pi │ ├── input.txt │ ├── main.cpp │ ├── main.go │ ├── main1.py │ └── main2.py ├── trailing-string │ ├── TrailingString.java │ └── input.txt ├── try-to-solve-it │ ├── input.txt │ ├── main.cpp │ └── main.py ├── twenty-forty-eight │ ├── input.txt │ └── main.py ├── uri-comparison │ ├── input.txt │ └── main.py ├── valid-parentheses │ ├── ValidParentheses.java │ └── input.txt └── word-chain │ ├── input.txt │ └── main.py ├── 2-hard ├── ascii-decryption │ ├── input.txt │ └── main.py ├── bay-bridges │ ├── input-large.txt │ ├── input-small.txt │ └── main.py ├── climbing-stairs │ ├── input.txt │ └── main.py ├── closest-pair │ ├── ClosestPair.java │ └── input.txt ├── cluster-detection │ ├── input-large.txt │ ├── input-small.txt │ └── main.py ├── commuting-engineer │ ├── CommutingEngineer.java │ ├── README.txt │ ├── input.txt │ ├── main.cpp │ └── main.py ├── computer-terminal │ ├── input-large.txt │ ├── input-small.txt │ ├── main.py │ └── sequences.txt ├── da-vyncy │ ├── input.txt │ └── main.py ├── decryption │ └── main.py ├── digit-statistics │ ├── input.txt │ └── main.py ├── distinct-subsequences │ ├── input.txt │ └── main.py ├── distinct-triangles │ ├── input.txt │ └── main.py ├── efficient-delivery │ ├── input.txt │ └── main.py ├── find-min │ ├── input.txt │ └── main.py ├── following-integer │ ├── input.txt │ └── main.py ├── grid-walk │ └── main.py ├── ip-package │ ├── input-large.txt │ ├── input-small.txt │ └── main.py ├── largest-sub-matrix │ ├── input.txt │ └── main.py ├── longest-common-subsequence │ ├── input.txt │ └── main.py ├── mars-networks │ ├── input-large.txt │ ├── input-small.txt │ └── main.py ├── message-decoding │ ├── input.txt │ └── main.py ├── minesweeper │ ├── input.txt │ └── main.py ├── minimum-path-sum │ ├── input.txt │ └── main.py ├── package-problem │ ├── input-large.txt │ ├── input-micro.txt │ ├── input-small.txt │ └── main.py ├── palindromic-ranges │ ├── input.txt │ └── main.py ├── playing-with-dna │ ├── input.txt │ └── main.py ├── poker-hands │ ├── input.txt │ └── main.py ├── prefix-expressions │ ├── input.txt │ └── main.py ├── ray-of-light │ ├── input.txt │ └── main.py ├── repeated-substring │ ├── RepeatedSubstring.java │ └── input.txt ├── robot-movements │ ├── main.cpp │ └── main.py ├── routing-problem │ ├── input1.txt │ ├── input2.txt │ ├── input3.txt │ └── main.py ├── running-for-president │ ├── input.txt │ └── main.py ├── seat-your-team-members │ ├── input.txt │ └── main.py ├── skyscrapers │ ├── input1.txt │ ├── input2.txt │ └── main.py ├── spiral-printing │ ├── input.txt │ └── main.py ├── string-list │ ├── input.txt │ └── main.py ├── string-permutations │ ├── StringPermutations.java │ └── input.txt ├── string-searching │ ├── input.txt │ └── main.py ├── string-substitution │ ├── input.txt │ └── main.py ├── telephone-words │ ├── input.txt │ └── main.py ├── text-dollar │ ├── input.txt │ └── main.py ├── text-to-number │ ├── input.txt │ └── main.py ├── the-cubes │ ├── input-large.txt │ ├── input-small.txt │ └── main.py ├── the-frequency │ ├── input-large.txt │ ├── input-small.txt │ └── main.py ├── the-labyrinth │ ├── input.txt │ └── main.py ├── type-ahead │ ├── input-large.txt │ ├── input-small.txt │ └── main.py ├── ugly-numbers │ ├── input.txt │ └── main.py ├── visit-to-the-headquarter │ ├── input.txt │ └── main.py └── word-search │ ├── WordSearch.java │ └── input.txt ├── CMakeLists.txt ├── LICENSE.md └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | *.iml 2 | *.class 3 | *.vpj 4 | *.vpw 5 | *.vpwhistu 6 | *.vtg 7 | *.cbp 8 | *.cmake 9 | cmake-build-debug 10 | CMakeFiles 11 | CMakeCache.txt 12 | .idea 13 | .cproject 14 | .project 15 | .c9 16 | a.out 17 | Debug 18 | Makefile 19 | bin 20 | out 21 | -------------------------------------------------------------------------------- /0-easy/age-distribution/input.txt: -------------------------------------------------------------------------------- 1 | 0 2 | 19 3 | -------------------------------------------------------------------------------- /0-easy/age-distribution/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | int main(int argc, char** argv) 6 | { 7 | std::ifstream file(argv[1]); 8 | std::string line; 9 | 10 | while (std::getline(file, line)) 11 | { 12 | int age; 13 | std::stringstream ss(line); 14 | ss >> age; 15 | 16 | // Print line depending on the age. 17 | if (age < 0 || age > 100) { 18 | std::cout << "This program is for humans" << std::endl; 19 | continue; 20 | } 21 | 22 | if (age <= 2) { 23 | std::cout << "Still in Mama's arms" << std::endl; 24 | } else if (age <= 4) { 25 | std::cout << "Preschool Maniac" << std::endl; 26 | } else if (age <= 11) { 27 | std::cout << "Elementary school" << std::endl; 28 | } else if (age <= 14) { 29 | std::cout << "Middle school" << std::endl; 30 | } else if (age <= 18) { 31 | std::cout << "High school" << std::endl; 32 | } else if (age <= 22) { 33 | std::cout << "College" << std::endl; 34 | } else if (age <= 65) { 35 | std::cout << "Working for the man" << std::endl; 36 | } else { 37 | std::cout << "The Golden Years" << std::endl; 38 | } 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /0-easy/age-distribution/main.py: -------------------------------------------------------------------------------- 1 | import sys 2 | 3 | def main(): 4 | test_cases = open(sys.argv[1], 'r') 5 | for test in test_cases: 6 | test = test.strip() 7 | if len(test) == 0: continue 8 | age = int(test) 9 | if age < 0 or age > 100: 10 | print("This program is for humans") 11 | continue 12 | if age <= 2: 13 | print("Still in Mama's arms") 14 | elif age <= 4: 15 | print("Preschool Maniac") 16 | elif age <= 11: 17 | print("Elementary school") 18 | elif age <= 14: 19 | print("Middle school") 20 | elif age <= 18: 21 | print("High school") 22 | elif age <= 22: 23 | print("College") 24 | elif age <= 65: 25 | print("Working for the man") 26 | else: 27 | print("The Golden Years") 28 | test_cases.close() 29 | 30 | if __name__ == '__main__': 31 | main() 32 | -------------------------------------------------------------------------------- /0-easy/armstrong-numbers/input.txt: -------------------------------------------------------------------------------- 1 | 6 2 | 153 3 | 351 4 | -------------------------------------------------------------------------------- /0-easy/armstrong-numbers/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | int digitCount(unsigned int n) 6 | { 7 | unsigned int count = 0; 8 | 9 | while (n > 0) { 10 | count++; 11 | n /= 10; 12 | } 13 | 14 | return count; 15 | } 16 | 17 | bool isArmstrongNumber(unsigned int n) 18 | { 19 | int digits = digitCount(n); 20 | 21 | int sum = 0; 22 | int working = n; 23 | 24 | while (working > 0) { 25 | int digit = working % 10; 26 | int multipliedDigit = digit; 27 | 28 | for (int i = 1; i < digits; i++) { 29 | multipliedDigit *= digit; 30 | } 31 | 32 | sum += multipliedDigit; 33 | working /= 10; 34 | } 35 | 36 | return sum == n; 37 | } 38 | 39 | int main(int argc, char** argv) 40 | { 41 | std::ifstream file(argv[1]); 42 | 43 | std::string line; 44 | while (std::getline(file, line)) 45 | { 46 | unsigned int x; 47 | std::stringstream ss(line); 48 | ss >> x; 49 | 50 | if (isArmstrongNumber(x)) { 51 | std::cout << "True" << std::endl; 52 | } else { 53 | std::cout << "False" << std::endl; 54 | } 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /0-easy/beautiful-strings/input.txt: -------------------------------------------------------------------------------- 1 | ABbCcc 2 | Good luck in the Facebook Hacker Cup this year! 3 | Ignore punctuation, please :) 4 | Sometimes test cases are hard to make up. 5 | So I just go consult Professor Dalves 6 | -------------------------------------------------------------------------------- /0-easy/big-digits/input.txt: -------------------------------------------------------------------------------- 1 | 3.1415926 2 | 1.41421356 3 | 01-01-1970 4 | 2.7182818284 5 | 4 8 15 16 23 42 6 | -------------------------------------------------------------------------------- /0-easy/bit-positions/BitPositions.java: -------------------------------------------------------------------------------- 1 | import java.io.IOException; 2 | import java.io.BufferedReader; 3 | import java.io.File; 4 | import java.io.FileReader; 5 | 6 | public class BitPositions 7 | { 8 | public static void main (String[] args) 9 | throws IOException 10 | { 11 | File file = new File(args[0]); 12 | BufferedReader in = new BufferedReader(new FileReader(file)); 13 | String line; 14 | 15 | while ((line = in.readLine()) != null) { 16 | 17 | line = line.trim(); 18 | 19 | if (line.length() > 0) { 20 | String[] lineArray = line.trim().split(","); 21 | 22 | if (lineArray.length > 0) { 23 | int n = Integer.parseInt(lineArray[0]); 24 | int p1 = Integer.parseInt(lineArray[1]); 25 | int p2 = Integer.parseInt(lineArray[2]); 26 | 27 | System.out.println(isBitAtPositionTheSame(n, p1, p2)); 28 | } 29 | } 30 | } 31 | } 32 | 33 | private static boolean isBitAtPositionTheSame(int n, int p1, int p2) 34 | { 35 | boolean bit1 = (n & (0x1 << (p1-1))) == 0; 36 | boolean bit2 = (n & (0x1 << (p2-1))) == 0; 37 | 38 | return bit1 == bit2; 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /0-easy/bit-positions/input.txt: -------------------------------------------------------------------------------- 1 | 86,2,3 2 | 125,1,2 3 | 3,1,2 4 | 5,1,2 5 | -------------------------------------------------------------------------------- /0-easy/black-card/input-small.txt: -------------------------------------------------------------------------------- 1 | John Sara Tom Susan | 3 2 | John Tom Mary | 5 3 | -------------------------------------------------------------------------------- /0-easy/black-card/main.py: -------------------------------------------------------------------------------- 1 | import sys 2 | 3 | def main(): 4 | test_cases = open(sys.argv[1], 'r') 5 | for test in test_cases: 6 | people = test.split('|')[0].strip().split(' ') 7 | number = int(test.split('|')[1].strip()) 8 | index = 0 9 | counter = 0 10 | while len(people) > 1: 11 | counter += 1 12 | if counter == number: 13 | counter = 0 14 | people.remove(people[index]) # N.B. doesn't work with duplicates. 15 | index = 0 16 | else: 17 | index += 1 18 | index %= len(people) 19 | print(people[0]) 20 | test_cases.close() 21 | 22 | if __name__ == '__main__': 23 | main() 24 | -------------------------------------------------------------------------------- /0-easy/calculate-distance/CalculateDistance.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.regex.Matcher; 3 | import java.util.regex.Pattern; 4 | 5 | public class CalculateDistance 6 | { 7 | public static void main (String[] args) 8 | throws IOException 9 | { 10 | File file = new File(args[0]); 11 | BufferedReader in = new BufferedReader(new FileReader(file)); 12 | String line; 13 | 14 | String patternString = "^\\(([\\d-]+), ([\\d-]+)\\) \\(([\\d-]+), ([\\d-]+)\\)$"; 15 | Pattern pattern = Pattern.compile(patternString); 16 | 17 | while ((line = in.readLine()) != null) { 18 | line = line.trim(); 19 | 20 | Matcher m = pattern.matcher(line); 21 | 22 | if (m.matches()) { 23 | int x1 = Integer.parseInt(m.group(1)); 24 | int y1 = Integer.parseInt(m.group(2)); 25 | int x2 = Integer.parseInt(m.group(3)); 26 | int y2 = Integer.parseInt(m.group(4)); 27 | 28 | int xDiff = x1-x2; 29 | xDiff *= xDiff; 30 | 31 | int yDiff = y1-y2; 32 | yDiff *= yDiff; 33 | 34 | int distance = (int) Math.sqrt(xDiff + yDiff); 35 | 36 | System.out.println(distance); 37 | } 38 | } 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /0-easy/calculate-distance/input.txt: -------------------------------------------------------------------------------- 1 | (25, 4) (1, -6) 2 | (47, 43) (-25, -11) 3 | -------------------------------------------------------------------------------- /0-easy/capitalize-words/CapitalizeWords.java: -------------------------------------------------------------------------------- 1 | import java.io.BufferedReader; 2 | import java.io.File; 3 | import java.io.FileReader; 4 | import java.io.IOException; 5 | 6 | public class CapitalizeWords 7 | { 8 | public static void main (String[] args) 9 | throws IOException 10 | { 11 | File file = new File(args[0]); 12 | BufferedReader in = new BufferedReader(new FileReader(file)); 13 | String line; 14 | 15 | while ((line = in.readLine()) != null) { 16 | line = line.trim(); 17 | System.out.println(capitalizeWordLetters(line)); 18 | } 19 | } 20 | 21 | public static String capitalizeWordLetters(String str) 22 | { 23 | char[] array = str.toCharArray(); 24 | 25 | for (int i = 0; i < array.length; i++) { 26 | char c = array[i]; 27 | 28 | if (i == 0 || Character.isWhitespace(array[i-1])) { 29 | array[i] = Character.toTitleCase(c); 30 | } 31 | } 32 | 33 | return new String(array); 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /0-easy/capitalize-words/input.txt: -------------------------------------------------------------------------------- 1 | Hello world 2 | javaScript language 3 | a letter -------------------------------------------------------------------------------- /0-easy/chardonnay-or-cabernet/input-small.txt: -------------------------------------------------------------------------------- 1 | Cabernet Merlot Noir | ot 2 | Chardonnay Sauvignon | ann 3 | Shiraz Grenache | o 4 | -------------------------------------------------------------------------------- /0-easy/chardonnay-or-cabernet/main.py: -------------------------------------------------------------------------------- 1 | import sys 2 | import copy 3 | 4 | def main(): 5 | test_cases = open(sys.argv[1], 'r') 6 | for test in test_cases: 7 | test = test.strip() 8 | if len(test) == 0:continue 9 | wines = test.split('|')[0].strip().split(' ') 10 | letters = list(test.split('|')[1].strip().lower()) 11 | result = [] 12 | for wine in wines: 13 | original = wine 14 | match = True 15 | for letter in letters: 16 | if letter not in wine.lower(): 17 | match = False 18 | break 19 | wine = wine.replace(letter, '', 1) 20 | if match: 21 | result.append(original) 22 | print('False' if len(result) == 0 else ' '.join(result)) 23 | test_cases.close() 24 | 25 | if __name__ == '__main__': 26 | main() 27 | -------------------------------------------------------------------------------- /0-easy/clean-up-the-words/input-small.txt: -------------------------------------------------------------------------------- 1 | (--9Hello----World...--) 2 | Can 0$9 ---you~ 3 | 13What213are;11you-123+138doing7 4 | -------------------------------------------------------------------------------- /0-easy/clean-up-the-words/main.py: -------------------------------------------------------------------------------- 1 | import sys 2 | import re 3 | 4 | ALPHABET = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ' 5 | 6 | def main(): 7 | test_cases = open(sys.argv[1], 'r') 8 | for test in test_cases: 9 | test = test.strip() 10 | if len(test) == 0: continue 11 | result = [] 12 | for char in test: 13 | result.append(' ' if char not in ALPHABET else char) 14 | print(re.sub(' +', ' ', ''.join(result).lower()).strip()) 15 | test_cases.close() 16 | 17 | if __name__ == '__main__': 18 | main() 19 | -------------------------------------------------------------------------------- /0-easy/compare-points/input.txt: -------------------------------------------------------------------------------- 1 | 0 0 1 5 2 | 12 13 12 13 3 | 0 1 0 5 4 | -------------------------------------------------------------------------------- /0-easy/compressed-sequence/input.txt: -------------------------------------------------------------------------------- 1 | 40 40 40 40 29 29 29 29 29 29 29 29 57 57 92 92 92 92 92 86 86 86 86 86 86 86 86 86 86 2 | 73 73 73 73 41 41 41 41 41 41 41 41 41 41 3 | 1 1 3 3 3 2 2 2 2 14 14 14 11 11 11 2 4 | 7 5 | -------------------------------------------------------------------------------- /0-easy/data-recovery/input.txt: -------------------------------------------------------------------------------- 1 | 2000 and was not However, implemented 1998 it until;9 8 3 4 1 5 7 2 2 | programming first The language;3 2 1 3 | programs Manchester The written ran Mark 1952 1 in Autocode from;6 2 1 7 5 3 11 4 8 9 4 | -------------------------------------------------------------------------------- /0-easy/data-recovery/main.py: -------------------------------------------------------------------------------- 1 | import sys 2 | 3 | test_cases = open(sys.argv[1], 'r') 4 | for test in test_cases: 5 | test = test.strip() 6 | if len(test) == 0: 7 | continue 8 | scramble = test.split(';')[0].split(' ') 9 | indexes = test.split(';')[1].split(' ') 10 | result = {} 11 | count = 0 12 | for i in indexes: 13 | result[i] = scramble[count] 14 | count += 1 15 | output = "" 16 | for i in range(1, len(indexes)+2): 17 | if str(i) in result: 18 | output += result[str(i)] + " " 19 | else: 20 | output += scramble[-1] + " " 21 | print(output.strip()) 22 | 23 | test_cases.close() 24 | -------------------------------------------------------------------------------- /0-easy/delta-time/input.txt: -------------------------------------------------------------------------------- 1 | 14:01:57 12:47:11 2 | 13:09:42 22:16:15 3 | 08:08:06 08:38:28 4 | 23:35:07 02:49:59 5 | 14:31:45 14:46:56 6 | -------------------------------------------------------------------------------- /0-easy/delta-time/main.py: -------------------------------------------------------------------------------- 1 | import sys 2 | from datetime import datetime 3 | 4 | FMT = '%H:%M:%S' 5 | 6 | def main(): 7 | test_cases = open(sys.argv[1], 'r') 8 | for test in test_cases: 9 | test = test.strip() 10 | if len(test) == 0: 11 | continue 12 | test = test.split(' ') 13 | t1 = test[0] 14 | t2 = test[1] 15 | 16 | if t1 > t2: 17 | delta = datetime.strptime(t1, FMT) - datetime.strptime(t2, FMT) 18 | else: 19 | delta = datetime.strptime(t2, FMT) - datetime.strptime(t1, FMT) 20 | 21 | print("%.2d:%.2d:%.2d" % (delta.seconds/3600, (delta.seconds/60)%60, delta.seconds%60)) 22 | 23 | test_cases.close() 24 | 25 | if __name__ == '__main__': 26 | main() 27 | -------------------------------------------------------------------------------- /0-easy/details/input-small.txt: -------------------------------------------------------------------------------- 1 | XX.YY,XXX.Y,X..YY,XX..Y 2 | XX...YY,X....YY,XX..YYY,X..YYYY 3 | XXYY,X..Y,XX.Y 4 | XYY,XXY,XYY 5 | -------------------------------------------------------------------------------- /0-easy/details/main.py: -------------------------------------------------------------------------------- 1 | import sys 2 | 3 | def main(): 4 | test_cases = open(sys.argv[1], 'r') 5 | for test in test_cases: 6 | test = test.strip() 7 | if len(test) == 0: 8 | continue 9 | matrix = test.split(',') 10 | iterations = 0 11 | while True: 12 | finished = False 13 | for col in range(len(matrix)): 14 | row = matrix[col] 15 | if '.Y' in row and 'XY' not in row: 16 | matrix[col] = row.replace('.Y', 'Y') 17 | else: 18 | finished = True 19 | break 20 | if finished: 21 | break 22 | iterations += 1 23 | print(iterations) 24 | test_cases.close() 25 | 26 | if __name__ == '__main__': 27 | main() 28 | -------------------------------------------------------------------------------- /0-easy/even-numbers/EvenNumbers.java: -------------------------------------------------------------------------------- 1 | import java.io.BufferedReader; 2 | import java.io.File; 3 | import java.io.FileReader; 4 | import java.io.IOException; 5 | 6 | public class EvenNumbers 7 | { 8 | public static void main (String[] args) 9 | throws IOException 10 | { 11 | File file = new File(args[0]); 12 | BufferedReader in = new BufferedReader(new FileReader(file)); 13 | String line; 14 | 15 | while ((line = in.readLine()) != null) { 16 | int n = Integer.parseInt(line.trim()); 17 | 18 | if ((n & 0x1) == 0) { 19 | System.out.println(1); 20 | } else { 21 | System.out.println(0); 22 | } 23 | } 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /0-easy/even-numbers/input.txt: -------------------------------------------------------------------------------- 1 | 701 2 | 4123 3 | 2936 4 | -------------------------------------------------------------------------------- /0-easy/even-numbers/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | int main(int argc, char** argv) 6 | { 7 | std::ifstream file(argv[1]); 8 | 9 | std::string line; 10 | while (std::getline(file, line)) 11 | { 12 | int x; 13 | std::stringstream ss(line); 14 | ss >> x; 15 | std::cout << ((x & 0x01) == 0 ? 1 : 0) << std::endl; 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /0-easy/fibonacci-series/input.txt: -------------------------------------------------------------------------------- 1 | 5 2 | 12 3 | -------------------------------------------------------------------------------- /0-easy/fibonacci-series/main.py: -------------------------------------------------------------------------------- 1 | import sys 2 | 3 | fibonacci_cache = {} 4 | def fibonacci_at_position(p): 5 | if p in fibonacci_cache: 6 | return fibonacci_cache[p] 7 | if p == 0: 8 | result = 0 9 | elif p == 1: 10 | result = 1 11 | else: 12 | result = fibonacci_at_position(p-1) + fibonacci_at_position(p-2) 13 | fibonacci_cache[p] = result 14 | return result 15 | 16 | test_cases = open(sys.argv[1], 'r') 17 | for test in test_cases: 18 | test = test.strip() 19 | if len(test) == 0: 20 | continue 21 | print(fibonacci_at_position(int(test))) 22 | 23 | test_cases.close() 24 | -------------------------------------------------------------------------------- /0-easy/file-size/FileSize.java: -------------------------------------------------------------------------------- 1 | import java.io.IOException; 2 | import java.io.File; 3 | 4 | public class FileSize 5 | { 6 | public static void main (String[] args) 7 | throws IOException 8 | { 9 | File file = new File(args[0]); 10 | System.out.println(file.length()); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /0-easy/file-size/input.txt: -------------------------------------------------------------------------------- 1 | 86,2,3 2 | 125,1,2 3 | 3,1,2 4 | 5,1,2 5 | lsdjflkjsd 6 | fsdlkfjs 7 | dfj 8 | sdfkljsd 9 | fskdljfsldfj 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | jksdf 76 | -------------------------------------------------------------------------------- /0-easy/file-size/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main(int argc, char** argv) 5 | { 6 | struct stat st; 7 | stat(argv[1], &st); 8 | std::cout << st.st_size << std::endl; 9 | } 10 | -------------------------------------------------------------------------------- /0-easy/find-a-writer/FindAWriter.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | 3 | public class FindAWriter 4 | { 5 | public static void main (String[] args) 6 | throws IOException 7 | { 8 | File file = new File(args[0]); 9 | BufferedReader in = new BufferedReader(new FileReader(file)); 10 | String line; 11 | 12 | while ((line = in.readLine()) != null) { 13 | line = line.trim(); 14 | 15 | if (line.length() == 0) { 16 | continue; 17 | } 18 | 19 | String[] lineArray = line.split("\\|"); 20 | 21 | String encoded = lineArray[0]; 22 | String keyString = lineArray[1].trim(); 23 | 24 | StringBuilder sb = new StringBuilder(); 25 | 26 | for (String s : keyString.split(" ")) { 27 | int i = Integer.parseInt(s); 28 | sb.append(encoded.charAt(i-1)); 29 | } 30 | 31 | System.out.println(sb.toString()); 32 | } 33 | } 34 | 35 | } 36 | -------------------------------------------------------------------------------- /0-easy/find-a-writer/input.txt: -------------------------------------------------------------------------------- 1 | osSE5Gu0Vi8WRq93UvkYZCjaOKeNJfTyH6tzDQbxFm4M1ndXIPh27wBA rLclpg| 3 35 27 62 51 27 46 57 26 10 46 63 57 45 15 43 53 2 | 3 | 3Kucdq9bfCEgZGF2nwx8UpzQJyHiOm0hoaYP6ST1WM7Nks5XjrR4IltBeDLV vA| 2 26 33 55 34 50 33 61 44 28 46 32 28 30 3 50 34 61 40 7 1 31 4 | -------------------------------------------------------------------------------- /0-easy/find-the-highest-score/input.txt: -------------------------------------------------------------------------------- 1 | 72 64 150 | 100 18 33 | 13 250 -6 2 | 10 25 -30 44 | 5 16 70 8 | 13 1 31 12 3 | 100 6 300 20 10 | 5 200 6 9 500 | 1 10 3 400 143 4 | -------------------------------------------------------------------------------- /0-easy/find-the-highest-score/main.py: -------------------------------------------------------------------------------- 1 | import sys 2 | 3 | def main(): 4 | test_cases = open(sys.argv[1], 'r') 5 | for test in test_cases: 6 | test = test.strip() 7 | if len(test) == 0: continue 8 | table = [[int(r) for r in row.split(' ')] for row in test.split(' | ')] 9 | maxes = [None] * len(table[0]) 10 | for column in range(len(maxes)): 11 | for row in range(len(table)): 12 | v = table[row][column] 13 | maxes[column] = v if maxes[column] is None else max(v, maxes[column]) 14 | print(' '.join([str(m) for m in maxes])) 15 | test_cases.close() 16 | 17 | if __name__ == '__main__': 18 | main() -------------------------------------------------------------------------------- /0-easy/fizz-buzz/input.txt: -------------------------------------------------------------------------------- 1 | 3 5 10 2 | 2 7 15 3 | -------------------------------------------------------------------------------- /0-easy/football/input.txt: -------------------------------------------------------------------------------- 1 | 1 2 3 4 | 3 1 | 4 1 2 | 19 11 | 19 21 23 | 31 39 29 3 | -------------------------------------------------------------------------------- /0-easy/football/main.py: -------------------------------------------------------------------------------- 1 | import sys 2 | import collections 3 | 4 | def main(): 5 | test_cases = open(sys.argv[1], 'r') 6 | for test in test_cases: 7 | test = test.strip() 8 | if len(test) == 0: continue 9 | test = [[int(team) for team in country.strip().split(' ')] for country in test.split('|')] 10 | team_to_fan_countries = collections.defaultdict(list) 11 | for country in range(len(test)): 12 | liked_teams = test[country] 13 | for team in liked_teams: 14 | team_to_fan_countries[team].append(country+1) 15 | result = [] 16 | for team in sorted(team_to_fan_countries.keys()): 17 | result.append(str(team) + ':' + ','.join([str(c) for c in team_to_fan_countries[team]])) 18 | print('; '.join(result) + ';') 19 | test_cases.close() 20 | 21 | if __name__ == '__main__': 22 | main() 23 | -------------------------------------------------------------------------------- /0-easy/happy-numbers/HappyNumbers.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | 4 | public class HappyNumbers 5 | { 6 | public static void main (String[] args) 7 | throws IOException 8 | { 9 | File file = new File(args[0]); 10 | BufferedReader in = new BufferedReader(new FileReader(file)); 11 | String line; 12 | 13 | while ((line = in.readLine()) != null) { 14 | line = line.trim(); 15 | int n = Integer.parseInt(line); 16 | 17 | boolean isHappyNumber = isNumberHappy(n); 18 | 19 | if (isHappyNumber) { 20 | System.out.println(1); 21 | } else { 22 | System.out.println(0); 23 | } 24 | } 25 | } 26 | 27 | public static boolean isNumberHappy(int n) 28 | { 29 | Set visited = new HashSet(); 30 | 31 | while (!visited.contains(n)) { 32 | visited.add(n); 33 | n = sumOfDigitSquares(n); 34 | 35 | if (n == 1) { 36 | return true; 37 | } 38 | } 39 | 40 | return false; 41 | } 42 | 43 | public static int sumOfDigitSquares(int n) 44 | { 45 | int sum = 0; 46 | 47 | while (n > 0) { 48 | int lastDigit = n % 10; 49 | sum += lastDigit*lastDigit; 50 | n /= 10; 51 | } 52 | 53 | return sum; 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /0-easy/happy-numbers/input.txt: -------------------------------------------------------------------------------- 1 | 1 2 | 7 3 | 22 -------------------------------------------------------------------------------- /0-easy/happy-numbers/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | int sumOfDigitSquares(int x) 7 | { 8 | int sum = 0; 9 | while (x > 0) { 10 | int lastDigit = x % 10; 11 | sum += lastDigit * lastDigit; 12 | x /= 10; 13 | } 14 | return sum; 15 | } 16 | 17 | bool isNumberHappy(int n) 18 | { 19 | std::set visited; 20 | 21 | while (visited.find(n) == visited.end()) { 22 | visited.insert(n); 23 | n = sumOfDigitSquares(n); 24 | 25 | if (n == 1) { 26 | return true; 27 | } 28 | } 29 | 30 | return false; 31 | } 32 | 33 | int main(int argc, char** argv) 34 | { 35 | std::ifstream file(argv[1]); 36 | 37 | std::string line; 38 | while (std::getline(file, line)) 39 | { 40 | unsigned int x; 41 | std::stringstream ss(line); 42 | ss >> x; 43 | 44 | std::cout << isNumberHappy(x) << std::endl; 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /0-easy/hex-to-decimal/HexToDecimal.java: -------------------------------------------------------------------------------- 1 | import java.io.IOException; 2 | import java.io.BufferedReader; 3 | import java.io.File; 4 | import java.io.FileReader; 5 | 6 | public class HexToDecimal 7 | { 8 | public static void main (String[] args) 9 | throws IOException 10 | { 11 | File file = new File(args[0]); 12 | BufferedReader in = new BufferedReader(new FileReader(file)); 13 | String line; 14 | 15 | while ((line = in.readLine()) != null) { 16 | String[] lineArray = line.split(" "); 17 | if (lineArray.length > 0) { 18 | String hex = lineArray[0]; 19 | System.out.println(hexToDecimalInteger(hex)); 20 | } 21 | } 22 | } 23 | 24 | private static int hexToDecimalInteger(String hex) 25 | { 26 | return Integer.parseInt(hex, 16); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /0-easy/hex-to-decimal/input.txt: -------------------------------------------------------------------------------- 1 | 9f 2 | 11 3 | -------------------------------------------------------------------------------- /0-easy/hex-to-decimal/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | int main(int argc, char** argv) 6 | { 7 | std::ifstream file(argv[1]); 8 | 9 | std::string line; 10 | while (std::getline(file, line)) 11 | { 12 | unsigned int x; 13 | std::stringstream ss; 14 | ss << std::hex << line; 15 | ss >> x; 16 | 17 | std::cout << x << std::endl; 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /0-easy/hex-to-decimal/main.py: -------------------------------------------------------------------------------- 1 | import sys 2 | 3 | test_cases = open(sys.argv[1], 'r') 4 | for test in test_cases: 5 | test = test.strip() 6 | if len(test) == 0: 7 | continue 8 | print(int(test, 16)) 9 | 10 | test_cases.close() 11 | -------------------------------------------------------------------------------- /0-easy/hidden-digits/input.txt: -------------------------------------------------------------------------------- 1 | abcdefghik 2 | Xa,}A#5N}{xOBwYBHIlH,#W 3 | (ABW>'yy^'M{X-K}q, 4 | 6240488 5 | -------------------------------------------------------------------------------- /0-easy/json-menu-ids/JsonMenuIds.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.regex.Matcher; 3 | import java.util.regex.Pattern; 4 | 5 | /** 6 | * JSON Menu IDs problem. 7 | * 8 | * If I were solving this in real life I'd use a JSON parser library. But for the purposes of this problem, I'm just 9 | * going to find the ID tags and extract those individually. Rolling an entire JSON parse myself seems like a bit much. 10 | */ 11 | public class JsonMenuIds 12 | { 13 | public static void main (String[] args) 14 | throws IOException 15 | { 16 | File file = new File(args[0]); 17 | BufferedReader in = new BufferedReader(new FileReader(file)); 18 | String line; 19 | 20 | while ((line = in.readLine()) != null) { 21 | line = line.trim(); 22 | 23 | if (line.length() == 0) { 24 | continue; 25 | } 26 | 27 | int sum = 0; 28 | 29 | Pattern p = Pattern.compile("\"id\": (\\d+), \"label\""); 30 | Matcher m = p.matcher(line); 31 | while (m.find()) { 32 | sum += Integer.parseInt(m.group(1)); 33 | } 34 | 35 | System.out.println(sum); 36 | } 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /0-easy/json-menu-ids/input.txt: -------------------------------------------------------------------------------- 1 | {"menu": {"header": "menu", "items": [{"id": 27}, {"id": 0, "label": "Label 0"}, null, {"id": 93}, {"id": 85}, {"id": 54}, null, {"id": 46, "label": "Label 46"}]}} 2 | 3 | {"menu": {"header": "menu", "items": [{"id": 81}]}} 4 | 5 | {"menu": {"header": "menu", "items": [{"id": 70, "label": "Label 70"}, {"id": 85, "label": "Label 85"}, {"id": 93, "label": "Label 93"}, {"id": 2}]}} 6 | -------------------------------------------------------------------------------- /0-easy/juggling-with-zeros/input.txt: -------------------------------------------------------------------------------- 1 | 00 0 0 00 00 0 2 | 00 0 3 | 00 0 0 000 00 0000000 0 000 4 | 0 000000000 00 00 5 | -------------------------------------------------------------------------------- /0-easy/juggling-with-zeros/main.py: -------------------------------------------------------------------------------- 1 | import sys 2 | 3 | test_cases = open(sys.argv[1], 'r') 4 | for test in test_cases: 5 | test = test.strip() 6 | if len(test) == 0: 7 | continue 8 | test = test.split(' ') 9 | binary_string = '' 10 | for i in range(0, len(test), 2): 11 | code = test[i] 12 | seq = test[i+1] 13 | if code == '0': 14 | binary_string += seq 15 | else: 16 | binary_string += '1' * len(seq) 17 | print(int(binary_string, 2)) 18 | 19 | test_cases.close() 20 | -------------------------------------------------------------------------------- /0-easy/knight-moves/input.txt: -------------------------------------------------------------------------------- 1 | g2 2 | a1 3 | d6 4 | e5 5 | b1 6 | -------------------------------------------------------------------------------- /0-easy/lettercase-percentage-ratio/input.txt: -------------------------------------------------------------------------------- 1 | thisTHIS 2 | AAbbCCDDEE 3 | N 4 | UkJ 5 | -------------------------------------------------------------------------------- /0-easy/lettercase-percentage-ratio/main.py: -------------------------------------------------------------------------------- 1 | import sys 2 | test_cases = open(sys.argv[1], 'r') 3 | 4 | for test in test_cases: 5 | test = test.strip() 6 | if len(test) == 0: 7 | continue 8 | lower = 0 9 | upper = 0 10 | for character in test: 11 | if character.islower(): 12 | lower += 1 13 | else: 14 | upper += 1 15 | lower = float(lower) / len(test) * 100 16 | upper = float(upper) / len(test) * 100 17 | print("lowercase: %.2f uppercase: %.2f" % (lower, upper)) 18 | 19 | test_cases.close() 20 | -------------------------------------------------------------------------------- /0-easy/longest-word/LongestWord.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | 3 | public class LongestWord 4 | { 5 | public static void main (String[] args) 6 | throws IOException 7 | { 8 | File file = new File(args[0]); 9 | BufferedReader in = new BufferedReader(new FileReader(file)); 10 | String line; 11 | 12 | while ((line = in.readLine()) != null) { 13 | String longestWord = null; 14 | line = line.trim(); 15 | 16 | for (String word : line.split(" ")) { 17 | if (longestWord == null) { 18 | longestWord = word; 19 | } else if (word.length() > longestWord.length()) { 20 | longestWord = word; 21 | } 22 | } 23 | 24 | System.out.println(longestWord); 25 | } 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /0-easy/longest-word/input.txt: -------------------------------------------------------------------------------- 1 | some line with text 2 | another line 3 | -------------------------------------------------------------------------------- /0-easy/lowercase/Lowercase.java: -------------------------------------------------------------------------------- 1 | import java.io.IOException; 2 | import java.io.BufferedReader; 3 | import java.io.File; 4 | import java.io.FileReader; 5 | 6 | public class Lowercase 7 | { 8 | public static void main (String[] args) 9 | throws IOException 10 | { 11 | File file = new File(args[0]); 12 | BufferedReader in = new BufferedReader(new FileReader(file)); 13 | String line; 14 | 15 | while ((line = in.readLine()) != null) { 16 | line = line.trim(); 17 | System.out.println(line.toLowerCase()); 18 | } 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /0-easy/lowercase/input.txt: -------------------------------------------------------------------------------- 1 | HELLO CODEEVAL 2 | This is some text 3 | -------------------------------------------------------------------------------- /0-easy/lowercase/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | int main(int argc, char** argv) 7 | { 8 | std::ifstream file(argv[1]); 9 | 10 | std::string line; 11 | while (std::getline(file, line)) 12 | { 13 | for (int i = 0; i < line.length(); i++) { 14 | putchar(tolower(line[i])); 15 | } 16 | 17 | std::cout << std::endl; 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /0-easy/lowest-unique-number/input.txt: -------------------------------------------------------------------------------- 1 | 3 3 9 1 6 5 8 1 5 3 2 | 9 2 9 9 1 8 8 8 2 1 1 3 | -------------------------------------------------------------------------------- /0-easy/matrix-rotation/input.txt: -------------------------------------------------------------------------------- 1 | a b c d 2 | a b c d e f g h i j k l m n o p 3 | a b c d e f g h i 4 | -------------------------------------------------------------------------------- /0-easy/matrix-rotation/main.py: -------------------------------------------------------------------------------- 1 | import sys 2 | import math 3 | 4 | # N.B. there are lots of good solutions to this problem available on 5 | # http://stackoverflow.com/questions/42519/how-do-you-rotate-a-two-dimensional-array 6 | 7 | def main(): 8 | test_cases = open(sys.argv[1], 'r') 9 | for test in test_cases: 10 | test = test.strip() 11 | if len(test) == 0: continue 12 | test = test.split(' ') 13 | n = int(math.sqrt(len(test))) 14 | matrix = [test[i:i+n] for i in range(0, len(test), n)] 15 | rotated = zip(*matrix[::-1]) 16 | print(' '.join([' '.join(row) for row in rotated])) 17 | test_cases.close() 18 | 19 | if __name__ == '__main__': 20 | main() 21 | -------------------------------------------------------------------------------- /0-easy/max-range-sum/input.txt: -------------------------------------------------------------------------------- 1 | 5;7 -3 -10 4 2 8 -2 4 -5 -2 2 | 6;-4 3 -10 5 3 -7 -3 7 -6 3 3 | 3;-7 0 -45 34 -24 7 4 | -------------------------------------------------------------------------------- /0-easy/max-range-sum/main.py: -------------------------------------------------------------------------------- 1 | import sys 2 | 3 | def main(input_file): 4 | with open(input_file, 'r') as fh: 5 | for line in fh: 6 | line = line.strip() 7 | days = int(line.split(';')[0]) 8 | array = [int(x) for x in line.split(';')[1].split(' ')] 9 | result = 0 10 | for i in range(0, len(array)-days+1): 11 | result = max(sum(array[i:i+days]), result) 12 | print(result) 13 | 14 | if __name__ == '__main__': 15 | if len(sys.argv) != 2: 16 | print("Usage: %s " % sys.argv[0]) 17 | sys.exit(1) 18 | main(sys.argv[1]) 19 | -------------------------------------------------------------------------------- /0-easy/mersenne-prime/input.txt: -------------------------------------------------------------------------------- 1 | 4 2 | 308 3 | 3000 4 | 7 5 | 32 6 | 31 7 | -------------------------------------------------------------------------------- /0-easy/mersenne-prime/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | int main(int argc, char** argv) 7 | { 8 | std::string line; 9 | std::ifstream infile(argv[1]); 10 | 11 | while (std::getline(infile, line)) 12 | { 13 | std::istringstream iss(line); 14 | int a; 15 | iss >> a; 16 | 17 | if (a < 7) { 18 | std::cout << "3" << std::endl; 19 | } else if (a < 31) { 20 | std::cout << "3, 7" << std::endl; 21 | } else if (a < 127) { 22 | std::cout << "3, 7, 31" << std::endl; 23 | } else if (a < 2047) { 24 | std::cout << "3, 7, 31, 127" << std::endl; 25 | } else { 26 | std::cout << "3, 7, 31, 127, 2047" << std::endl; 27 | } 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /0-easy/mersenne-prime/main.py: -------------------------------------------------------------------------------- 1 | import sys 2 | 3 | 4 | mersenne = [3, 7, 31, 127, 2047] 5 | 6 | 7 | def main(): 8 | test_cases = open(sys.argv[1], 'r') 9 | for test in test_cases: 10 | test = test.strip() 11 | if len(test) == 0: 12 | continue 13 | test = int(test) 14 | result = [] 15 | for m in mersenne: 16 | if test > m: 17 | result.append(m) 18 | print(', '.join([str(r) for r in result])) 19 | test_cases.close() 20 | 21 | 22 | if __name__ == '__main__': 23 | main() 24 | -------------------------------------------------------------------------------- /0-easy/minimum-distance/input.txt: -------------------------------------------------------------------------------- 1 | 4 3 3 5 7 2 | 3 20 30 40 3 | -------------------------------------------------------------------------------- /0-easy/minimum-distance/main.py: -------------------------------------------------------------------------------- 1 | import sys 2 | 3 | def compute_difference(houses, house): 4 | difference = 0 5 | for h in houses: 6 | difference += abs(h - house) 7 | return difference 8 | 9 | def main(input_file): 10 | with open(input_file, 'r') as fh: 11 | for line in fh: 12 | houses = sorted([int(x) for x in line.split(' ')[1:]]) 13 | difference = float("inf") 14 | for house in houses: 15 | difference = min(difference, compute_difference(houses, house)) 16 | print(difference) 17 | 18 | if __name__ == '__main__': 19 | if len(sys.argv) != 2: 20 | print("Usage: %s " % sys.argv[0]) 21 | sys.exit(1) 22 | main(sys.argv[1]) 23 | -------------------------------------------------------------------------------- /0-easy/mixed-content/input.txt: -------------------------------------------------------------------------------- 1 | 8,33,21,0,16,50,37,0,melon,7,apricot,peach,pineapple,17,21 2 | 24,13,14,43,41 3 | test,testing 4 | 24,69 5 | -------------------------------------------------------------------------------- /0-easy/morse-code/input.txt: -------------------------------------------------------------------------------- 1 | .- ...- ..--- .-- .... .. . -.-. -..- ....- ..... 2 | -... .... ...-- 3 | -------------------------------------------------------------------------------- /0-easy/multiples-of-a-number/MultiplesOfANumber.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | 3 | public class MultiplesOfANumber 4 | { 5 | public static void main (String[] args) 6 | throws IOException 7 | { 8 | File file = new File(args[0]); 9 | BufferedReader in = new BufferedReader(new FileReader(file)); 10 | String line; 11 | 12 | while ((line = in.readLine()) != null) { 13 | String[] lineArray = line.trim().split(","); 14 | 15 | int x = Integer.parseInt(lineArray[0]); 16 | int n = Integer.parseInt(lineArray[1]); 17 | 18 | System.out.println(findNextMultipleOfNGreaterThanX(x, n)); 19 | } 20 | } 21 | 22 | public static int findNextMultipleOfNGreaterThanX(int x, int n) 23 | { 24 | int multiplier = 1; 25 | 26 | while (true) { 27 | int multiple = n * multiplier; 28 | 29 | if (multiple > x) { 30 | return multiple; 31 | } 32 | 33 | multiplier++; 34 | } 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /0-easy/multiples-of-a-number/input.txt: -------------------------------------------------------------------------------- 1 | 13,8 2 | 17,16 3 | -------------------------------------------------------------------------------- /0-easy/multiplication-tables/MultiplicationTables.java: -------------------------------------------------------------------------------- 1 | public class MultiplicationTables 2 | { 3 | public static void main (String[] args) 4 | { 5 | for (int i = 1; i <= 12; i++) { 6 | for (int j = 1; j <= 12; j++) { 7 | System.out.print(String.format("%4d", i*j)); 8 | } 9 | 10 | System.out.println(); 11 | } 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /0-easy/multiplication-tables/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main(int argc, char** argv) 5 | { 6 | for (int i = 1; i <= 12; i++) { 7 | for (int j = 1; j <= 12; j++) { 8 | std::cout << std::setfill(' ') << std::setw(4) << (i*j); 9 | 10 | } 11 | std::cout << std::endl; 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /0-easy/multiply-lists/MultiplyLists.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | 3 | public class MultiplyLists 4 | { 5 | public static void main (String[] args) 6 | throws IOException 7 | { 8 | File file = new File(args[0]); 9 | BufferedReader in = new BufferedReader(new FileReader(file)); 10 | String line; 11 | 12 | while ((line = in.readLine()) != null) { 13 | line = line.trim(); 14 | String[] lineArray = line.split("\\|"); 15 | 16 | String[] list1 = lineArray[0].trim().split(" "); 17 | String[] list2 = lineArray[1].trim().split(" "); 18 | 19 | assert(list1.length == list2.length); 20 | 21 | for (int i = 0; i < list1.length; i++) { 22 | System.out.print(Integer.parseInt(list1[i]) * Integer.parseInt(list2[i])); 23 | 24 | if (i != list1.length-1) { 25 | System.out.print(" "); 26 | } 27 | } 28 | 29 | System.out.println(); 30 | } 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /0-easy/multiply-lists/input.txt: -------------------------------------------------------------------------------- 1 | 9 0 6 | 15 14 9 2 | 5 | 8 3 | 13 4 15 1 15 5 | 1 4 15 14 8 2 4 | -------------------------------------------------------------------------------- /0-easy/multiply-lists/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | int main(int argc, char** argv) 8 | { 9 | std::ifstream file(argv[1]); 10 | 11 | std::string line; 12 | 13 | while (std::getline(file, line)) 14 | { 15 | std::vector sv; 16 | std::stringstream ss(line); 17 | std::string s; 18 | while (ss >> s) sv.push_back(s); 19 | 20 | std::vector v1; 21 | std::vector v2; 22 | for (int i = 0; i < sv.size()/2; i++) { 23 | v1.push_back(atoi(sv[i].c_str())); 24 | } 25 | for (int i = sv.size()/2 + (sv.size() % 2 == 0 ? 0 : 1); i < sv.size(); i++) { 26 | v2.push_back(atoi(sv[i].c_str())); 27 | } 28 | 29 | for (int i = 0; i < v1.size(); i++) { 30 | std::cout << v1[i] * v2[i]; 31 | if (i+1 != v1.size()) { 32 | std::cout << " "; 33 | } 34 | } 35 | 36 | std::cout << std::endl; 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /0-easy/n-mod-m/NModM.java: -------------------------------------------------------------------------------- 1 | import java.io.IOException; 2 | import java.io.BufferedReader; 3 | import java.io.File; 4 | import java.io.FileReader; 5 | 6 | public class NModM 7 | { 8 | public static void main (String[] args) 9 | throws IOException 10 | { 11 | File file = new File(args[0]); 12 | BufferedReader in = new BufferedReader(new FileReader(file)); 13 | String line; 14 | 15 | while ((line = in.readLine()) != null) { 16 | String[] lineArray = line.split(","); 17 | if (lineArray.length > 0) { 18 | int n = Integer.parseInt(lineArray[0]); 19 | int m = Integer.parseInt(lineArray[1]); 20 | 21 | System.out.println(nModMWithoutModulusOperator(n, m)); 22 | } 23 | } 24 | } 25 | 26 | /** 27 | * Calculate n mod m without using the built-in modulus operator. 28 | * 29 | * N.B. do not assume the integers are positive. 30 | */ 31 | public static int nModMWithoutModulusOperator(int n, int m) 32 | { 33 | int current = n; 34 | 35 | while (Math.abs(current) >= Math.abs(m)) { 36 | current -= m; 37 | } 38 | 39 | return current; 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /0-easy/n-mod-m/input.txt: -------------------------------------------------------------------------------- 1 | 20,6 2 | 2,3 3 | 500,13 4 | 60,11 5 | -3,-5 6 | -2,-1 7 | -4,-2 8 | -------------------------------------------------------------------------------- /0-easy/nice-angles/input-large.txt: -------------------------------------------------------------------------------- 1 | 0.001 2 | 90.0 3 | 10.29833064 4 | 276.38151808 5 | 22.71788306 6 | 222.16346032 7 | 0.5 8 | 180.0 9 | 211.86355409 10 | 205.64046622 11 | 275.46843205 12 | 9.40140340 13 | 286.60760996 14 | 58.45148191 15 | 133.53375920 16 | 0.0 17 | 0.25 18 | 32.85873796 19 | 327.73269080 20 | 17.29485889 21 | -------------------------------------------------------------------------------- /0-easy/nice-angles/input-small.txt: -------------------------------------------------------------------------------- 1 | 330.39991833 2 | 0.001 3 | 14.64530319 4 | 0.25 5 | 254.16991217 6 | -------------------------------------------------------------------------------- /0-easy/nice-angles/main.py: -------------------------------------------------------------------------------- 1 | import sys 2 | 3 | def decimal_degree_to_minutes_seconds(decimal_degree): 4 | degree = int(decimal_degree) 5 | minutes = int((decimal_degree-degree) * 60) 6 | seconds = int((decimal_degree-degree-(minutes/60.0)) * 3600) 7 | return degree, minutes, seconds 8 | 9 | def format_degree_minutes_seconds(minutes_seconds_degree): 10 | return '%i.%02i\'%02i"' % minutes_seconds_degree 11 | 12 | def main(): 13 | test_cases = open(sys.argv[1], 'r') 14 | for test in test_cases: 15 | test = test.strip() 16 | if len(test) == 0: 17 | continue 18 | decimal_degree = float(test) 19 | minutes_seconds_degree = decimal_degree_to_minutes_seconds(decimal_degree) 20 | print(format_degree_minutes_seconds(minutes_seconds_degree)) 21 | test_cases.close() 22 | 23 | if __name__ == '__main__': 24 | main() 25 | -------------------------------------------------------------------------------- /0-easy/not-so-clever/input.txt: -------------------------------------------------------------------------------- 1 | 4 3 2 1 | 1 2 | 5 4 3 2 1 | 2 3 | -------------------------------------------------------------------------------- /0-easy/not-so-clever/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "io/ioutil" 6 | "os" 7 | "strconv" 8 | "strings" 9 | ) 10 | 11 | func stringArrayToIntArray(s []string) (result []int) { 12 | for _, i := range s { 13 | j, _ := strconv.Atoi(i) 14 | result = append(result, j) 15 | } 16 | return 17 | } 18 | 19 | func main() { 20 | 21 | dat, _ := ioutil.ReadFile(os.Args[1]) 22 | for _, line := range strings.Split(strings.TrimSpace(string(dat)), "\n") { 23 | array := stringArrayToIntArray( 24 | strings.Split(strings.TrimSpace(strings.Split(line, "|")[0]), " ")) 25 | 26 | iterations, _ := strconv.Atoi( 27 | strings.TrimSpace(strings.Split(line, "|")[1])) 28 | 29 | for i := 0; i < iterations; i++ { 30 | for j := 0; j < len(array)-1; j++ { 31 | if array[j] > array[j+1] { 32 | tmp := array[j] 33 | array[j] = array[j+1] 34 | array[j+1] = tmp 35 | break 36 | } 37 | } 38 | } 39 | 40 | for i, dat := range array { 41 | if i == len(array)-1 { 42 | fmt.Println(dat) 43 | } else { 44 | fmt.Print(dat, " ") 45 | } 46 | } 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /0-easy/not-so-clever/main.py: -------------------------------------------------------------------------------- 1 | import sys 2 | 3 | def stupid_sort(array, iterations): 4 | for i in range(iterations): 5 | for j in range(len(array)-1): 6 | if array[j] > array[j+1]: 7 | tmp = array[j] 8 | array[j] = array[j+1] 9 | array[j+1] = tmp 10 | break 11 | return array 12 | 13 | def main(input_filename): 14 | test_cases = open(input_filename, 'r') 15 | for test in test_cases: 16 | array, iterations = test.strip().split('|') 17 | array = [int(i) for i in array.split()] 18 | iterations = int(iterations.strip()) 19 | stupid_sort(array, iterations) 20 | print(' '.join([str(s) for s in array])) 21 | test_cases.close() 22 | 23 | if __name__ == '__main__': 24 | main(sys.argv[1]) 25 | -------------------------------------------------------------------------------- /0-easy/odd-numbers/OddNumbers.java: -------------------------------------------------------------------------------- 1 | public class OddNumbers 2 | { 3 | public static void main (String[] args) 4 | { 5 | for (int i = 1; i <= 99; i += 2) { 6 | System.out.println(i); 7 | } 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /0-easy/odd-numbers/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | for (int i = 1; i <= 99; i += 2) 6 | { 7 | std::cout << i << std::endl; 8 | } 9 | 10 | return 0; 11 | } 12 | -------------------------------------------------------------------------------- /0-easy/one-zero-two-zeros/input.txt: -------------------------------------------------------------------------------- 1 | 1 8 2 | 2 4 3 | -------------------------------------------------------------------------------- /0-easy/one-zero-two-zeros/main.py: -------------------------------------------------------------------------------- 1 | import sys 2 | 3 | def main(): 4 | test_cases = open(sys.argv[1], 'r') 5 | for test in test_cases: 6 | test = test.strip() 7 | if len(test) == 0:continue 8 | num_zeros = int(test.split(' ')[0]) 9 | num_test = int(test.split(' ')[1]) 10 | result = 0 11 | for i in range(1, num_test+1): 12 | binary = '{0:b}'.format(i) 13 | zeros = binary.count('0') 14 | if zeros == num_zeros: result += 1 15 | print(result) 16 | test_cases.close() 17 | 18 | if __name__ == '__main__': 19 | main() -------------------------------------------------------------------------------- /0-easy/penultimate-word/PenultimateWord.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | 3 | public class PenultimateWord 4 | { 5 | public static void main (String[] args) 6 | throws IOException 7 | { 8 | File file = new File(args[0]); 9 | BufferedReader in = new BufferedReader(new FileReader(file)); 10 | String line; 11 | 12 | while ((line = in.readLine()) != null) { 13 | line = line.trim(); 14 | String[] lineArray = line.split(" "); 15 | System.out.println(lineArray[lineArray.length-2]); 16 | } 17 | } 18 | 19 | } 20 | -------------------------------------------------------------------------------- /0-easy/penultimate-word/input.txt: -------------------------------------------------------------------------------- 1 | some line with text 2 | another line -------------------------------------------------------------------------------- /0-easy/prime-palindrome/PrimePalindrome.java: -------------------------------------------------------------------------------- 1 | public class PrimePalindrome 2 | { 3 | public static void main (String[] args) 4 | { 5 | int largestPrimePalindrome = 2; 6 | 7 | // 1 is not prime. 8 | for (int i = 3; i < 1000; i++) { 9 | if (isPrime(i) && isPalindrome(i)) { 10 | largestPrimePalindrome = i; 11 | } 12 | } 13 | 14 | System.out.println(largestPrimePalindrome); 15 | } 16 | 17 | private static boolean isPrime(int n) 18 | { 19 | int root = (int) Math.sqrt(n); 20 | 21 | for (int i = 2; i <= root; i++) { 22 | if (n % i == 0) { 23 | return false; 24 | } 25 | } 26 | 27 | return true; 28 | } 29 | 30 | private static boolean isPalindrome(int n) 31 | { 32 | String str = Integer.toString(n); 33 | return isPalindrome(str); 34 | } 35 | 36 | private static boolean isPalindrome(String str) 37 | { 38 | char[] array = str.toCharArray(); 39 | 40 | for (int i = 0; i < array.length/2; i++) { 41 | if (array[i] != array[array.length-i-1]) { 42 | return false; 43 | } 44 | } 45 | 46 | return true; 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /0-easy/query-board/input.txt: -------------------------------------------------------------------------------- 1 | SetCol 32 20 2 | SetRow 15 7 3 | SetRow 16 31 4 | QueryCol 32 5 | SetCol 2 14 6 | QueryRow 10 7 | SetCol 14 0 8 | QueryRow 15 9 | SetRow 10 1 10 | QueryCol 2 -------------------------------------------------------------------------------- /0-easy/racing-cars/input.txt: -------------------------------------------------------------------------------- 1 | #########_## 2 | ########C_## 3 | #######_#### 4 | ######_#C### 5 | #######_C### 6 | #######_#### 7 | ######C#_### 8 | ######C_#### 9 | #######_#### 10 | #######_#### 11 | -------------------------------------------------------------------------------- /0-easy/racing-cars/main.py: -------------------------------------------------------------------------------- 1 | import sys 2 | 3 | # Input logic. 4 | line_reader = open(sys.argv[1], 'r') 5 | lines = [] 6 | for line in line_reader: 7 | line = line.strip() 8 | lines.append(line) 9 | line_reader.close() 10 | 11 | # First line and initialization. 12 | checkpoint = lines[0].find('C') 13 | position = lines[0].find('_') if checkpoint == -1 else checkpoint 14 | temp = list(lines[0]) 15 | temp[position] = '|' 16 | print(''.join(temp)) 17 | previous_position = position 18 | 19 | # All other lines. 20 | for i in range(1, len(lines)): 21 | line = lines[i] 22 | checkpoint = line.find('C') 23 | if checkpoint != -1 and abs(checkpoint - previous_position) <= 1: 24 | position = checkpoint 25 | else: 26 | position = line.find('_') 27 | # Need to modify the python string == annoying. 28 | temp = list(line) 29 | if position < previous_position: 30 | temp[position] = '/' 31 | elif position == previous_position: 32 | temp[position] = '|' 33 | else: 34 | temp[position] = '\\' 35 | previous_position = position 36 | print(''.join(temp)) 37 | -------------------------------------------------------------------------------- /0-easy/read-more/input-small.txt: -------------------------------------------------------------------------------- 1 | Tom exhibited. 2 | Amy Lawrence was proud and glad, and she tried to make Tom see it in her face - but he wouldn't look. 3 | Tom was tugging at a button-hole and looking sheepish. 4 | Two thousand verses is a great many - very, very great many. 5 | Tom's mouth watered for the apple, but he stuck to his work. 6 | -------------------------------------------------------------------------------- /0-easy/read-more/main.py: -------------------------------------------------------------------------------- 1 | import sys 2 | 3 | def trim(line): 4 | if len(line) <= 55: 5 | return line 6 | else: 7 | line = line[0:40] 8 | if line.rfind(' ') != -1: line = line[0:line.rfind(' ')] 9 | return line + "... " 10 | 11 | def main(): 12 | test_cases = open(sys.argv[1], 'r') 13 | for test in test_cases: 14 | test = test.strip() 15 | print(trim(test)) 16 | test_cases.close() 17 | 18 | if __name__ == '__main__': 19 | main() 20 | -------------------------------------------------------------------------------- /0-easy/real-fake/input.txt: -------------------------------------------------------------------------------- 1 | 9999 9999 9999 9999 2 | 9999 9999 9999 9993 3 | 1234 5678 9876 5432 4 | -------------------------------------------------------------------------------- /0-easy/real-fake/main.py: -------------------------------------------------------------------------------- 1 | import sys 2 | 3 | def main(): 4 | test_cases = open(sys.argv[1], 'r') 5 | for test in test_cases: 6 | test = test.strip() 7 | if len(test) == 0: continue 8 | card_number = [int(i) for i in list(test.replace(' ', ''))] 9 | a = 0 10 | for i in range(0, len(card_number), 2): 11 | a += card_number[i] * 2 12 | b = 0 13 | for i in range(1, len(card_number), 2): 14 | b += card_number[i] 15 | print('Real' if (a+b) % 10 == 0 else 'Fake') 16 | test_cases.close() 17 | 18 | if __name__ == '__main__': 19 | main() 20 | -------------------------------------------------------------------------------- /0-easy/reverse-words/ReverseWords.java: -------------------------------------------------------------------------------- 1 | import java.io.IOException; 2 | import java.io.BufferedReader; 3 | import java.io.File; 4 | import java.io.FileReader; 5 | 6 | public class ReverseWords 7 | { 8 | public static void main (String[] args) 9 | throws IOException 10 | { 11 | File file = new File(args[0]); 12 | BufferedReader in = new BufferedReader(new FileReader(file)); 13 | String line; 14 | 15 | while ((line = in.readLine()) != null) { 16 | 17 | line = line.trim(); 18 | 19 | if (line.length() > 0) { 20 | String[] lineArray = line.trim().split(" "); 21 | 22 | if (lineArray.length > 0) { 23 | for (int i = lineArray.length-1; i >= 0; i--) { 24 | System.out.print(lineArray[i]); 25 | 26 | if (i == 0) { 27 | System.out.println(); 28 | } else { 29 | System.out.print(" "); 30 | } 31 | } 32 | } 33 | } 34 | } 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /0-easy/reverse-words/input.txt: -------------------------------------------------------------------------------- 1 | Hello World 2 | 3 | Hello CodeEval 4 | This is a test of the reverse word algorithm 5 | 6 | -------------------------------------------------------------------------------- /0-easy/rightmost-char/RightmostChar.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | 3 | public class RightmostChar 4 | { 5 | public static void main (String[] args) 6 | throws IOException 7 | { 8 | File file = new File(args[0]); 9 | BufferedReader in = new BufferedReader(new FileReader(file)); 10 | String line; 11 | 12 | while ((line = in.readLine()) != null) { 13 | String[] lineArray = line.trim().split(","); 14 | 15 | String word = lineArray[0]; 16 | char c = lineArray[1].toCharArray()[0]; 17 | 18 | System.out.println(word.lastIndexOf(c)); 19 | } 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /0-easy/rightmost-char/input.txt: -------------------------------------------------------------------------------- 1 | Hello World,r 2 | Hello CodeEval,E 3 | Matthew,H 4 | -------------------------------------------------------------------------------- /0-easy/road-trip/RoadTrip.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | 4 | public class RoadTrip 5 | { 6 | public static void main (String[] args) 7 | throws IOException 8 | { 9 | File file = new File(args[0]); 10 | BufferedReader in = new BufferedReader(new FileReader(file)); 11 | String line; 12 | 13 | while ((line = in.readLine()) != null) { 14 | line = line.trim(); 15 | 16 | SortedSet ss = new TreeSet(); 17 | String[] cities = line.split(";"); 18 | 19 | for (String c : cities) { 20 | ss.add(Integer.parseInt(c.split(",")[1])); 21 | } 22 | 23 | Iterator it = ss.iterator(); 24 | int previous = 0; 25 | 26 | while (it.hasNext()) { 27 | int current = it.next(); 28 | System.out.print(current - previous); 29 | previous = current; 30 | 31 | if (it.hasNext()) { 32 | System.out.print(","); 33 | } 34 | } 35 | 36 | System.out.println(); 37 | } 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /0-easy/road-trip/input.txt: -------------------------------------------------------------------------------- 1 | Rkbs,5453; Wdqiz,1245; Rwds,3890; Ujma,5589; Tbzmo,1303; 2 | Vgdfz,70; Mgknxpi,3958; Nsptghk,2626; Wuzp,2559; Jcdwi,3761; 3 | Yvnzjwk,5363; Pkabj,5999; Xznvb,3584; Jfksvx,1240; Inwm,5720; 4 | Ramytdb,2683; Voclqmb,5236; 5 | -------------------------------------------------------------------------------- /0-easy/roller-coaster/input.txt: -------------------------------------------------------------------------------- 1 | To be, or not to be: that is the question. 2 | Whether 'tis nobler in the mind to suffer 3 | The slings and arrows of outrageous fortune, 4 | Or to take arms against a sea of troubles, 5 | And by opposing end them? To die: to sleep. 6 | -------------------------------------------------------------------------------- /0-easy/roller-coaster/main.py: -------------------------------------------------------------------------------- 1 | import sys 2 | 3 | def format_text(text): 4 | result = '' 5 | uppercase = True 6 | for character in text: 7 | if character.isalpha(): 8 | result += character.upper() if uppercase else character.lower() 9 | uppercase = not uppercase 10 | else: 11 | result += character 12 | return result 13 | 14 | test_cases = open(sys.argv[1], 'r') 15 | 16 | for test in test_cases: 17 | test = test.strip() 18 | if len(test) == 0: 19 | continue 20 | print(format_text(test)) 21 | -------------------------------------------------------------------------------- /0-easy/roman-numerals/input.txt: -------------------------------------------------------------------------------- 1 | 159 2 | 296 3 | 3992 4 | -------------------------------------------------------------------------------- /0-easy/self-describing-numbers/SelfDescribingNumbers.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | 3 | public class SelfDescribingNumbers 4 | { 5 | public static void main (String[] args) 6 | throws IOException 7 | { 8 | File file = new File(args[0]); 9 | BufferedReader in = new BufferedReader(new FileReader(file)); 10 | String line; 11 | 12 | while ((line = in.readLine()) != null) { 13 | line = line.trim(); 14 | int n = Integer.parseInt(line); 15 | 16 | if (isIntegerSelfDescribing(n)) { 17 | System.out.println(1); 18 | } else { 19 | System.out.println(0); 20 | } 21 | } 22 | } 23 | 24 | public static boolean isIntegerSelfDescribing(int n) 25 | { 26 | char[] nArray = Integer.toString(n).toCharArray(); 27 | 28 | // Count how many times each digit appears. 29 | int[] digitCounter = new int[10]; 30 | for (char c : nArray) { 31 | int digit = c-48; 32 | digitCounter[digit]++; 33 | } 34 | 35 | for (int i = 0; i < nArray.length; i++) { 36 | if (digitCounter[i] != (nArray[i]-48)) { 37 | return false; 38 | } 39 | } 40 | 41 | return true; 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /0-easy/self-describing-numbers/input.txt: -------------------------------------------------------------------------------- 1 | 2020 2 | 22 3 | 1210 -------------------------------------------------------------------------------- /0-easy/set-intersection/input.txt: -------------------------------------------------------------------------------- 1 | 1,2,3,4;4,5,6 2 | 20,21,22;45,46,47 3 | 7,8,9;8,9,10,11,12 4 | -------------------------------------------------------------------------------- /0-easy/shortest-repetition/ShortestRepetition.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | 3 | public class ShortestRepetition 4 | { 5 | public static void main (String[] args) 6 | throws IOException 7 | { 8 | File file = new File(args[0]); 9 | BufferedReader in = new BufferedReader(new FileReader(file)); 10 | String line; 11 | 12 | while ((line = in.readLine()) != null) { 13 | line = line.trim(); 14 | 15 | for (int i = 1; i <= line.length(); i++) { 16 | String sub = line.substring(0, i); 17 | 18 | String[] lineArray = line.split(sub); 19 | 20 | boolean allSplitElementsEmpty = true; 21 | for (String s : lineArray) { 22 | if (!s.isEmpty()) { 23 | allSplitElementsEmpty = false; 24 | break; 25 | } 26 | } 27 | 28 | if (allSplitElementsEmpty) { 29 | System.out.println(sub.length()); 30 | break; 31 | } 32 | } 33 | } 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /0-easy/shortest-repetition/input.txt: -------------------------------------------------------------------------------- 1 | abcabcabcabc 2 | bcbcbcbcbcbcbcbcbcbcbcbcbcbc 3 | dddddddddddddddddddd 4 | a 5 | hijklmnopqrstuvwx 6 | aaaaaaaaabaaaaaaaaabaaaaaaaaabaaaaaaaaab 7 | aaaaaaaaaaaaaaaaaaaaaaaaab 8 | aaab 9 | ijklmijklm 10 | -------------------------------------------------------------------------------- /0-easy/shortest-repetition/main.py: -------------------------------------------------------------------------------- 1 | import sys 2 | 3 | test_cases = open(sys.argv[1], 'r') 4 | for test in test_cases: 5 | test = test.strip() 6 | if len(test) == 0: 7 | continue 8 | for i in range(1, len(test)+1): 9 | sub_string = test[:i] 10 | rejoined = "".join(test.split(sub_string)) 11 | if len(rejoined) == 0: 12 | print(len(sub_string)) 13 | break 14 | 15 | test_cases.close() 16 | -------------------------------------------------------------------------------- /0-easy/simple-or-trump/input.txt: -------------------------------------------------------------------------------- 1 | AD 2H | H 2 | KD KH | C 3 | JH 10S | C 4 | 7H JD | C 5 | -------------------------------------------------------------------------------- /0-easy/simple-sorting/SimpleSorting.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | 4 | public class SimpleSorting 5 | { 6 | public static void main (String[] args) 7 | throws IOException 8 | { 9 | File file = new File(args[0]); 10 | BufferedReader in = new BufferedReader(new FileReader(file)); 11 | String line; 12 | 13 | while ((line = in.readLine()) != null) { 14 | line = line.trim(); 15 | String[] stringArray = line.split(" "); 16 | double[] doubleArray = new double[stringArray.length]; 17 | 18 | for (int i = 0; i < stringArray.length; i++) { 19 | doubleArray[i] = Double.parseDouble(stringArray[i]); 20 | } 21 | 22 | Arrays.sort(doubleArray); 23 | 24 | for (int i = 0; i < doubleArray.length; i++) { 25 | System.out.format("%.3f", doubleArray[i]); 26 | 27 | if (i == doubleArray.length-1) { 28 | System.out.println(); 29 | } else { 30 | System.out.print(" "); 31 | } 32 | } 33 | } 34 | } 35 | 36 | } 37 | -------------------------------------------------------------------------------- /0-easy/simple-sorting/input.txt: -------------------------------------------------------------------------------- 1 | 70.920 -38.797 14.354 99.323 90.374 7.581 2 | -37.507 -3.263 40.079 27.999 65.213 -55.552 3 | -------------------------------------------------------------------------------- /0-easy/simple-sorting/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "io/ioutil" 6 | "os" 7 | "sort" 8 | "strconv" 9 | "strings" 10 | ) 11 | 12 | func main() { 13 | dat, _ := ioutil.ReadFile(os.Args[1]) 14 | for _, line := range strings.Split(strings.TrimSpace(string(dat)), "\n") { 15 | stringArray := strings.Split(line, " ") 16 | var floatSlice = make([]float64, len(stringArray)) 17 | 18 | for i, s := range stringArray { 19 | f, _ := strconv.ParseFloat(s, 64) 20 | floatSlice[i] = f 21 | } 22 | 23 | sort.Sort(sort.Float64Slice(floatSlice)) 24 | 25 | for i, v := range floatSlice { 26 | fmt.Printf("%.3f", v) 27 | if i+1 != len(floatSlice) { 28 | fmt.Print(" ") 29 | } else { 30 | fmt.Println() 31 | } 32 | } 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /0-easy/simple-sorting/main.py: -------------------------------------------------------------------------------- 1 | import sys 2 | 3 | def main(): 4 | test_cases = open(sys.argv[1], 'r') 5 | for test in test_cases: 6 | print(' '.join(["{0:.3f}".format(x) for x in sorted([float(i) for i in test.strip().split(' ')])])) 7 | test_cases.close() 8 | 9 | if __name__ == '__main__': 10 | main() 11 | -------------------------------------------------------------------------------- /0-easy/slang-flavor/input.txt: -------------------------------------------------------------------------------- 1 | Lorem ipsum dolor sit amet. Mea et habeo doming praesent. Te inani utroque recteque has, sea ne fugit verterem! 2 | Usu ei scripta phaedrum, an sed salutatus definiebas? Qui ut recteque gloriatur reformidans. Qui solum aeque sapientem cu. 3 | Eu nam nusquam quaestio principes. 4 | -------------------------------------------------------------------------------- /0-easy/slang-flavor/main.py: -------------------------------------------------------------------------------- 1 | import sys 2 | import re 3 | 4 | POSTFIXES = [ 5 | ', yeah!', 6 | ', this is crazy, I tell ya.', 7 | ', can U believe this?', 8 | ', eh?', 9 | ', aw yea.', 10 | ', yo.', 11 | '? No way!', 12 | '. Awesome!' 13 | ] 14 | 15 | def main(): 16 | test_cases = open(sys.argv[1], 'r') 17 | index = 0 18 | count = 0 19 | for test in test_cases: 20 | test = test.strip() 21 | if len(test) == 0: 22 | continue 23 | sentences = [x.strip() for x in re.split('[\.!\?]', test) if len(x) > 0] 24 | line = [] 25 | for s in sentences: 26 | if count % 2 != 0: 27 | line.append(s + POSTFIXES[index]) 28 | index = (index + 1) % len(POSTFIXES) 29 | else: 30 | punctuation = test[test.find(s) + len(s)] 31 | line.append(s + punctuation) 32 | count += 1 33 | print(' '.join(line)) 34 | test_cases.close() 35 | 36 | if __name__ == '__main__': 37 | main() 38 | -------------------------------------------------------------------------------- /0-easy/split-the-number/input.txt: -------------------------------------------------------------------------------- 1 | 3413289830 a-bcdefghij 2 | 776 a+bc 3 | 12345 a+bcde 4 | 1232 ab+cd 5 | 84481 abc-de 6 | 52309 a-bcde 7 | 6720479737 abcdefgh+ij 8 | 82150 ab-cde 9 | 8841339250 abcdef+ghij 10 | 20016 abcd+e 11 | 540 ab-c 12 | 10454 ab-cde 13 | 58953 a-bcde 14 | 77024 ab-cde 15 | 7841605148 abcdefghi+j 16 | 24954 abc+de 17 | 20744 abcd-e 18 | 359 ab-c 19 | 17366 ab-cde 20 | 8166639447 abc+defghij 21 | 95419 ab+cde 22 | 46691 a-bcde 23 | 69036 a+bcde 24 | 12345 a+bcde 25 | 49852 ab-cde 26 | 751 a+bc 27 | 59086 abcd+e 28 | 630 ab-c 29 | 51543 a-bcde 30 | 63805 a+bcde 31 | 77529 abcd+e 32 | 7534221290 abcdefgh-ij 33 | 77140 abc-de 34 | 22206 ab-cde 35 | 9332527924 abcdef-ghij 36 | 1111 ab+cd 37 | 512 ab+c 38 | 86699 a+bcde 39 | 69886 abcd-e 40 | 8278093165 a-bcdefghij 41 | 69153 abc-de 42 | 958409723 abcdefgh-i 43 | 233 ab-c 44 | 801 a-bc 45 | 999 abc 46 | 999 a+b+c 47 | -------------------------------------------------------------------------------- /0-easy/split-the-number/main.py: -------------------------------------------------------------------------------- 1 | import sys 2 | import re 3 | 4 | test_cases = open(sys.argv[1], 'r') 5 | 6 | def decode_letter_mapping_list(mapper, values): 7 | result = [] 8 | for v in values: 9 | result.append(decode_letter_mapping(mapper, v)) 10 | return result 11 | 12 | def decode_letter_mapping(mapper, value): 13 | result = "" 14 | for c in value: 15 | index = ord(c) - ord('a') 16 | result += mapper[index] 17 | return int(result) 18 | 19 | for test in test_cases: 20 | test = test.strip() 21 | if len(test) == 0: 22 | continue 23 | 24 | mapper = test.split(' ')[0] 25 | expression = test.split(' ')[1] 26 | 27 | operands = decode_letter_mapping_list( 28 | mapper, 29 | expression.replace('+', ' ').replace('-', ' ').split(' ')) 30 | operators = list(re.sub('[a-z]+', '', expression)) 31 | 32 | result = operands[0] 33 | for i in range(1, len(operands)): 34 | operand = operands[i] 35 | operator = operators[i-1] 36 | 37 | if operator == '+': 38 | result += operand 39 | else: 40 | result -= operand 41 | 42 | print(result) 43 | 44 | test_cases.close() 45 | -------------------------------------------------------------------------------- /0-easy/stepwise-word/input.txt: -------------------------------------------------------------------------------- 1 | cat dog hello 2 | stop football play 3 | music is my life 4 | -------------------------------------------------------------------------------- /0-easy/stepwise-word/main.py: -------------------------------------------------------------------------------- 1 | import sys 2 | 3 | def stepwise(word): 4 | result = [] 5 | for i in range(len(word)): 6 | result.append('*' * i + word[i]) 7 | return ' '.join(result) 8 | 9 | def main(): 10 | test_cases = open(sys.argv[1], 'r') 11 | for test in test_cases: 12 | test = test.strip() 13 | if len(test) == 0: continue 14 | test = test.split(' ') 15 | test.sort(key=lambda item: len(item), reverse=True) 16 | print(stepwise(test[0])) 17 | 18 | test_cases.close() 19 | 20 | if __name__ == '__main__': 21 | main() 22 | -------------------------------------------------------------------------------- /0-easy/string-mask/input.txt: -------------------------------------------------------------------------------- 1 | hello 11001 2 | world 10000 3 | cba 111 4 | ungseeh 1111111 5 | wnwyi 01010 6 | gplgvrzpalvpmzt 110101010101010 7 | world 10000 8 | tmgxxzea 00010110 9 | ddwfxx 110111 10 | xizynzhhjojkyd 11100111101100 11 | flbbyusuymcddtb 110100010101100 12 | biznr 00011 13 | zgwuoizthumewuxorfd 0010101111010111111 14 | mxldqoqamvd 00000010111 15 | egrle 10001 16 | bvjmgjnc 01000110 17 | kqkhqcipqgspsdhmra 010010000101110100 18 | j 0 19 | cba 111 20 | jiu 111 21 | sln 110 22 | abc 000 23 | a 0 24 | irjioyfxfnp 10001110110 25 | qddsxkwsagxtcifmeyab 01011010111111010010 26 | tdjnhqomefemrd 01111001101111 27 | pxsclqikmfjhmrbjk 00000010110011000 28 | hello 11001 29 | frbfcqzknp 0111001001 30 | lfmuncd 1000100 31 | qjwgoae 0010011 32 | r 0 33 | tof 101 34 | lnqsyiymjqhuljkejsu 0101001011010000011 35 | f 1 36 | arr 010 37 | abrqtihjo 111010101 38 | vrhf 0010 39 | tzpnuru 1001110 40 | bwtarznzzfu 01011000011 41 | niblhzwtn 010110001 42 | qqihhv 110101 43 | tqklrcpavhixox 10000010000010 44 | -------------------------------------------------------------------------------- /0-easy/string-mask/main.py: -------------------------------------------------------------------------------- 1 | import sys 2 | 3 | def main(input_filename): 4 | test_cases = open(input_filename, 'r') 5 | for test in test_cases: 6 | test = test.strip() 7 | word = list(test.split(' ')[0]) 8 | mask = list(test.split(' ')[1]) 9 | result = [] 10 | for m, w in zip(mask, word): 11 | if m == '1': 12 | result.append(w.upper()) 13 | else: 14 | result.append(w.lower()) 15 | print(''.join(result)) 16 | test_cases.close() 17 | 18 | if __name__ == '__main__': 19 | main(sys.argv[1]) 20 | -------------------------------------------------------------------------------- /0-easy/strings-and-arrows/input.txt: -------------------------------------------------------------------------------- 1 | <--<<--<< 2 | <<>>--><--<<--<<>>>-->< 3 | <-->> 4 | -------------------------------------------------------------------------------- /0-easy/strings-and-arrows/main.py: -------------------------------------------------------------------------------- 1 | import sys 2 | 3 | def count_substring(string, substring): 4 | result = 0 5 | for i in range(0, len(string)-len(substring)+1): 6 | string_segment = string[i:i+len(substring)] 7 | if substring == string_segment: 8 | result += 1 9 | return result 10 | 11 | def main(): 12 | test_cases = open(sys.argv[1], 'r') 13 | for test in test_cases: 14 | test = test.strip() 15 | if len(test) == 0: continue 16 | print(count_substring(test, '<--<<') + count_substring(test, '>>-->')) 17 | test_cases.close() 18 | 19 | if __name__ == '__main__': 20 | main() 21 | -------------------------------------------------------------------------------- /0-easy/sum-of-digits/SumOfDigits.java: -------------------------------------------------------------------------------- 1 | import java.io.IOException; 2 | import java.io.BufferedReader; 3 | import java.io.File; 4 | import java.io.FileReader; 5 | 6 | public class SumOfDigits 7 | { 8 | public static void main (String[] args) 9 | throws IOException 10 | { 11 | File file = new File(args[0]); 12 | BufferedReader in = new BufferedReader(new FileReader(file)); 13 | String line; 14 | 15 | while ((line = in.readLine()) != null) { 16 | line = line.trim(); 17 | 18 | if (line.length() > 0) { 19 | System.out.println(sumOfDigits(Integer.parseInt(line))); 20 | } 21 | } 22 | } 23 | 24 | public static int sumOfDigits(int n) 25 | { 26 | String nString = Integer.toString(n); 27 | int sum = 0; 28 | 29 | for (int i = 0; i < nString.length(); i++) { 30 | int digit = nString.charAt(i)-48; 31 | sum += digit; 32 | } 33 | 34 | return sum; 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /0-easy/sum-of-digits/input.txt: -------------------------------------------------------------------------------- 1 | 23 2 | 496 3 | -------------------------------------------------------------------------------- /0-easy/sum-of-digits/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | int sumOfDigits(int x) 6 | { 7 | int sum = 0; 8 | while (x > 0) { 9 | sum += x % 10; 10 | x /= 10; 11 | } 12 | return sum; 13 | } 14 | 15 | int main(int argc, char** argv) 16 | { 17 | std::ifstream file(argv[1]); 18 | 19 | std::string line; 20 | while (std::getline(file, line)) 21 | { 22 | unsigned int x; 23 | std::stringstream ss(line); 24 | ss >> x; 25 | 26 | std::cout << sumOfDigits(x) << std::endl; 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /0-easy/sum-of-integers-in-a-file/SumOfIntegersInAFile.java: -------------------------------------------------------------------------------- 1 | import java.io.BufferedReader; 2 | import java.io.File; 3 | import java.io.FileReader; 4 | import java.io.IOException; 5 | 6 | public class SumOfIntegersInAFile 7 | { 8 | public static void main (String[] args) 9 | throws IOException 10 | { 11 | File file = new File(args[0]); 12 | BufferedReader in = new BufferedReader(new FileReader(file)); 13 | String line; 14 | 15 | int sum = 0; 16 | while ((line = in.readLine()) != null) { 17 | line = line.trim(); 18 | sum += Integer.parseInt(line); 19 | } 20 | 21 | System.out.println(sum); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /0-easy/sum-of-integers-in-a-file/input.txt: -------------------------------------------------------------------------------- 1 | 5 2 | 12 -------------------------------------------------------------------------------- /0-easy/sum-of-integers-in-a-file/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | int main(int argc, char** argv) 6 | { 7 | std::ifstream file(argv[1]); 8 | 9 | std::string line; 10 | int sum = 0; 11 | while (std::getline(file, line)) 12 | { 13 | int x; 14 | std::stringstream ss(line); 15 | ss >> x; 16 | sum += x; 17 | } 18 | 19 | std::cout << sum << std::endl; 20 | } 21 | -------------------------------------------------------------------------------- /0-easy/sum-of-primes/SumOfPrimes.java: -------------------------------------------------------------------------------- 1 | public class SumOfPrimes 2 | { 3 | public static void main (String[] args) 4 | { 5 | int sum = 0; 6 | int primesSummed = 0; 7 | int currentNumber = 2; 8 | 9 | while (primesSummed < 1000) { 10 | if (isPrime(currentNumber)) { 11 | sum += currentNumber; 12 | primesSummed++; 13 | } 14 | 15 | currentNumber++; 16 | } 17 | 18 | System.out.println(sum); 19 | } 20 | 21 | public static boolean isPrime(int n) 22 | { 23 | int root = (int) Math.sqrt(n); 24 | 25 | for (int i = 2; i <= root; i++) { 26 | if (n % i == 0) { 27 | return false; 28 | } 29 | } 30 | 31 | return true; 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /0-easy/sum-of-primes/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | bool isPrime(int n) 5 | { 6 | int root = (int) sqrt(n); 7 | 8 | for (int i = 2; i <= root; i++) { 9 | if (n % i == 0) { 10 | return false; 11 | } 12 | } 13 | 14 | return true; 15 | } 16 | 17 | int main(int argc, char** argv) 18 | { 19 | unsigned int sum = 0; 20 | unsigned int primesSummed = 0; 21 | unsigned int currentNumber = 2; 22 | 23 | while (primesSummed < 1000) { 24 | if (isPrime(currentNumber)) { 25 | sum += currentNumber; 26 | primesSummed++; 27 | } 28 | 29 | currentNumber++; 30 | } 31 | 32 | std::cout << sum << std::endl; 33 | } 34 | -------------------------------------------------------------------------------- /0-easy/swap-case/SwapCase.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | 3 | public class SwapCase 4 | { 5 | public static void main (String[] args) 6 | throws IOException 7 | { 8 | File file = new File(args[0]); 9 | BufferedReader in = new BufferedReader(new FileReader(file)); 10 | String line; 11 | 12 | while ((line = in.readLine()) != null) { 13 | line = line.trim(); 14 | System.out.println(swapCase(line)); 15 | } 16 | } 17 | 18 | public static String swapCase(String str) 19 | { 20 | char[] array = str.toCharArray(); 21 | 22 | for (int i = 0; i < array.length; i++) { 23 | char c = array[i]; 24 | 25 | if (Character.isLowerCase(c)) { 26 | array[i] = Character.toTitleCase(c); 27 | } else { 28 | array[i] = Character.toLowerCase(c); 29 | } 30 | } 31 | 32 | return new String(array); 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /0-easy/swap-case/input.txt: -------------------------------------------------------------------------------- 1 | Hello world! 2 | JavaScript language 1.8 3 | A letter -------------------------------------------------------------------------------- /0-easy/swap-case/main.py: -------------------------------------------------------------------------------- 1 | import sys 2 | 3 | test_cases = open(sys.argv[1], 'r') 4 | for test in test_cases: 5 | test = test.strip() 6 | if len(test) == 0: 7 | continue 8 | print(test.swapcase()) 9 | 10 | test_cases.close() 11 | -------------------------------------------------------------------------------- /0-easy/swap-elements/input.txt: -------------------------------------------------------------------------------- 1 | 1 2 3 4 5 6 7 8 9 : 0-8 2 | 1 2 3 4 5 6 7 8 9 10 : 0-1, 1-3 3 | -------------------------------------------------------------------------------- /0-easy/swap-numbers/input.txt: -------------------------------------------------------------------------------- 1 | 4Always0 5look8 4on9 7the2 4bright8 9side7 3of8 5life5 2 | 5Nobody5 7expects3 5the4 6Spanish4 9inquisition0 3 | -------------------------------------------------------------------------------- /0-easy/swap-numbers/main.py: -------------------------------------------------------------------------------- 1 | import sys 2 | 3 | def swap_numbers(word): 4 | return word[-1] + word[1:-1] + word[0] 5 | 6 | def main(): 7 | test_cases = open(sys.argv[1], 'r') 8 | for test in test_cases: 9 | print(' '.join([swap_numbers(word) for word in test.strip().split(' ')])) 10 | test_cases.close() 11 | 12 | if __name__ == '__main__': 13 | main() 14 | -------------------------------------------------------------------------------- /0-easy/testing/input.txt: -------------------------------------------------------------------------------- 1 | Heelo Codevval | Hello Codeeval 2 | hELLO cODEEVAL | Hello Codeeval 3 | Hello Codeeval | Hello Codeeval 4 | -------------------------------------------------------------------------------- /0-easy/testing/main.py: -------------------------------------------------------------------------------- 1 | import sys 2 | 3 | def count_mistakes(case, design): 4 | mistakes = 0 5 | for c, d in zip(case, design): 6 | if c != d: 7 | mistakes += 1 8 | return mistakes + abs(len(case) - len(design)) 9 | 10 | def main(): 11 | test_cases = open(sys.argv[1], 'r') 12 | for test in test_cases: 13 | case, design = [x.strip() for x in test.strip().split('|')] 14 | mistakes = count_mistakes(case, design) 15 | if mistakes == 0: 16 | print('Done') 17 | elif mistakes <= 2: 18 | print('Low') 19 | elif mistakes <= 4: 20 | print('Medium') 21 | elif mistakes <= 6: 22 | print('High') 23 | else: 24 | print('Critical') 25 | test_cases.close() 26 | 27 | if __name__ == '__main__': 28 | main() 29 | -------------------------------------------------------------------------------- /0-easy/the-major-element/input.txt: -------------------------------------------------------------------------------- 1 | 92,19,19,76,19,21,19,85,19,19,19,94,19,19,22,67,83,19,19,54,59,1,19,19 2 | 92,11,30,92,1,11,92,38,92,92,43,92,92,51,92,36,97,92,92,92,43,22,84,92,92 3 | 4,79,89,98,48,42,39,79,55,70,21,39,98,16,96,2,10,24,14,47,0,50,95,20,95,48,50,12,42 4 | -------------------------------------------------------------------------------- /0-easy/the-major-element/main.py: -------------------------------------------------------------------------------- 1 | import sys 2 | test_cases = open(sys.argv[1], 'r') 3 | 4 | for test in test_cases: 5 | test = test.strip() 6 | if len(test) == 0: 7 | continue 8 | elements = (test.split(',')) 9 | elements.sort(key=int) 10 | freq = 0 11 | previous = 0 12 | found = False 13 | for i in elements: 14 | if freq == 0 or i == previous: 15 | freq += 1 16 | else: 17 | freq = 1 18 | previous = i 19 | if freq > (len(elements)/2): 20 | found = True 21 | print(i) 22 | break 23 | if not found: 24 | print('None') 25 | 26 | test_cases.close() 27 | -------------------------------------------------------------------------------- /0-easy/time-to-eat/input.txt: -------------------------------------------------------------------------------- 1 | 02:26:31 14:44:45 09:53:27 2 | 05:33:44 21:25:41 3 | -------------------------------------------------------------------------------- /0-easy/time-to-eat/main.py: -------------------------------------------------------------------------------- 1 | import sys 2 | import datetime 3 | 4 | def main(): 5 | test_cases = open(sys.argv[1], 'r') 6 | for test in test_cases: 7 | test = test.strip() 8 | if len(test) == 0: continue 9 | times = [] 10 | for t in test.split(' '): 11 | times.append((t, datetime.datetime.strptime(t, "%H:%M:%S"))) 12 | print(' '.join([x[0] for x in sorted(times, key=lambda x: x[1], reverse=True)])) 13 | test_cases.close() 14 | 15 | if __name__ == '__main__': 16 | main() -------------------------------------------------------------------------------- /0-easy/trick-or-treat/input.txt: -------------------------------------------------------------------------------- 1 | Vampires: 1, Zombies: 1, Witches: 1, Houses: 1 2 | Vampires: 3, Zombies: 2, Witches: 1, Houses: 10 3 | -------------------------------------------------------------------------------- /0-easy/trick-or-treat/main.py: -------------------------------------------------------------------------------- 1 | import sys 2 | import math 3 | 4 | def main(): 5 | test_cases = open(sys.argv[1], 'r') 6 | for test in test_cases: 7 | vampires, zombies, witches, houses = [int(x.split(':')[1].strip()) for x in test.strip().split(',')] 8 | total_candies = houses * (vampires * 3 + zombies * 4 + witches * 5) 9 | total_kids = vampires + zombies + witches 10 | print(math.floor(float(total_candies) / float(total_kids))) 11 | test_cases.close() 12 | 13 | if __name__ == '__main__': 14 | main() 15 | -------------------------------------------------------------------------------- /0-easy/truth-or-lie/input.txt: -------------------------------------------------------------------------------- 1 | 64 6e 78 | 100101100 11110 2 | 5e 7d 59 | 1101100 10010101 1100111 3 | 93 75 | 1000111 1011010 1100010 4 | -------------------------------------------------------------------------------- /0-easy/truth-or-lie/main.py: -------------------------------------------------------------------------------- 1 | import sys 2 | 3 | def main(): 4 | test_cases = open(sys.argv[1], 'r') 5 | for test in test_cases: 6 | test = test.strip() 7 | if len(test) == 0: continue 8 | virus = sum([int(x, 16) for x in test.split('|')[0].strip().split(' ')]) 9 | anti_virus = sum([int(x, 2) for x in test.split('|')[1].strip().split(' ')]) 10 | # Anti-virus >= Virus, print True. 11 | print('True' if anti_virus >= virus else 'False') 12 | test_cases.close() 13 | 14 | if __name__ == '__main__': 15 | main() 16 | -------------------------------------------------------------------------------- /0-easy/unique-elements/UniqueElements.java: -------------------------------------------------------------------------------- 1 | import java.io.BufferedReader; 2 | import java.io.File; 3 | import java.io.FileReader; 4 | import java.io.IOException; 5 | import java.util.Iterator; 6 | import java.util.LinkedHashSet; 7 | 8 | public class UniqueElements 9 | { 10 | public static void main (String[] args) 11 | throws IOException 12 | { 13 | File file = new File(args[0]); 14 | BufferedReader in = new BufferedReader(new FileReader(file)); 15 | String line; 16 | 17 | 18 | while ((line = in.readLine()) != null) { 19 | LinkedHashSet lhs = new LinkedHashSet(); 20 | 21 | line = line.trim(); 22 | String[] lineArray = line.split(","); 23 | 24 | 25 | for (String s : lineArray) { 26 | int n = Integer.parseInt(s); 27 | 28 | if (!lhs.contains(n)) { 29 | lhs.add(n); 30 | } 31 | } 32 | 33 | Iterator it = lhs.iterator(); 34 | 35 | while (true) { 36 | System.out.print(it.next()); 37 | 38 | if (it.hasNext()) { 39 | System.out.print(","); 40 | } else { 41 | System.out.println(); 42 | break; 43 | } 44 | } 45 | } 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /0-easy/unique-elements/input.txt: -------------------------------------------------------------------------------- 1 | 1,1,1,2,2,3,3,4,4 2 | 2,3,4,5,5 -------------------------------------------------------------------------------- /0-easy/unique-elements/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | template 9 | std::string join(Iter begin, Iter end, std::string const& separator) 10 | { 11 | std::ostringstream result; 12 | 13 | if (begin != end) { 14 | result << *begin++; 15 | } 16 | 17 | while (begin != end) { 18 | result << separator << *begin++; 19 | } 20 | 21 | return result.str(); 22 | } 23 | 24 | int main(int argc, char** argv) 25 | { 26 | std::ifstream file(argv[1]); 27 | 28 | std::string line; 29 | bool first = true; 30 | while (std::getline(file, line)) 31 | { 32 | std::set s; 33 | std::stringstream ss(line); 34 | 35 | int i; 36 | while (ss >> i) 37 | { 38 | s.insert(i); 39 | if (ss.peek() == ',') { 40 | ss.ignore(); 41 | } 42 | } 43 | 44 | std::cout << join(s.begin(), s.end(), ",") << std::endl; 45 | } 46 | 47 | } -------------------------------------------------------------------------------- /0-easy/without-repetitions/input.txt: -------------------------------------------------------------------------------- 1 | But as he spake he drew the good sword from its scabbard, and smote a heathen 2 | knight, Jusssstin of thee Iron Valley. 3 | No matttter whom you choose, she deccccclared, I will abide by your decision. 4 | Wwwhat is your will? 5 | At his magic speech the ground oppened and he began the path of descent. 6 | I should fly away and you would never see me again. 7 | -------------------------------------------------------------------------------- /0-easy/without-repetitions/main.py: -------------------------------------------------------------------------------- 1 | import sys 2 | 3 | def remove_repetitions(text): 4 | if len(text) == 0: return text 5 | result = [text[0]] 6 | for char in text[1:]: 7 | if char != result[-1]: 8 | result.append(char) 9 | return ''.join(result) 10 | 11 | def main(): 12 | test_cases = open(sys.argv[1], 'r') 13 | for test in test_cases: 14 | test = test.strip() 15 | if len(test) == 0: 16 | continue 17 | print(remove_repetitions(test)) 18 | test_cases.close() 19 | 20 | if __name__ == '__main__': 21 | main() 22 | -------------------------------------------------------------------------------- /0-easy/word-to-digits/input.txt: -------------------------------------------------------------------------------- 1 | zero;two;five;seven;eight;four 2 | three;seven;eight;nine;two -------------------------------------------------------------------------------- /0-easy/working-experience/main.py: -------------------------------------------------------------------------------- 1 | import sys 2 | import time 3 | import operator 4 | 5 | def is_range_overlapping(x1, x2, y1, y2): 6 | return x1 <= y2 and y1 <= x2 7 | 8 | test_cases = open(sys.argv[1], 'r') 9 | for test in test_cases: 10 | test = test.strip() 11 | if len(test) == 0: 12 | continue 13 | pattern = '%b %Y' 14 | ranges = [] 15 | for date_range in test.split(';'): 16 | date1 = date_range.strip().split('-')[0] 17 | date2 = date_range.strip().split('-')[1] 18 | epoch1 = int(time.mktime(time.strptime(date1, pattern))) 19 | epoch2 = int(time.mktime(time.strptime(date2, pattern))) 20 | ranges.append([epoch1, True]) 21 | ranges.append([epoch2, False]) 22 | ranges = sorted(ranges, key=operator.itemgetter(0)) 23 | queue = [ranges[0][0]] 24 | result = 0 25 | for i in range(1, len(ranges)): 26 | epoch = ranges[i][0] 27 | starting = ranges[i][1] 28 | if starting: 29 | queue.append(epoch) 30 | else: 31 | if len(queue) == 1: 32 | months_diff = round(((epoch - queue[0]) * 12 / 3.15569e7) + 1) 33 | result += int(months_diff) 34 | queue.pop() 35 | print(int(result / 12)) 36 | 37 | test_cases.close() 38 | -------------------------------------------------------------------------------- /1-moderate/a-pile-of-bricks/input.txt: -------------------------------------------------------------------------------- 1 | [4,3] [3,-3]|(1 [10,9,4] [9,4,2]) 2 | [-1,-5] [5,-2]|(1 [4,7,8] [2,9,0]);(2 [0,7,1] [5,9,8]) 3 | [-4,-5] [-5,-3]|(1 [4,8,6] [0,9,2]);(2 [8,-1,3] [0,5,4]) 4 | -------------------------------------------------------------------------------- /1-moderate/alternative-reality/input.txt: -------------------------------------------------------------------------------- 1 | 100 2 | 4 3 | 17 4 | -------------------------------------------------------------------------------- /1-moderate/alternative-reality/main.py: -------------------------------------------------------------------------------- 1 | import sys 2 | 3 | 4 | def count(value, last_coin=50): 5 | if value == 0: 6 | return 1 7 | result = 0 8 | for coin in [50, 25, 10, 5, 1]: 9 | if coin <= value and coin <= last_coin: 10 | result += count(value - coin, coin) 11 | return result 12 | 13 | 14 | def main(): 15 | test_cases = open(sys.argv[1], 'r') 16 | for test in test_cases: 17 | test = test.strip() 18 | if len(test) == 0: continue 19 | test = int(test) 20 | print(count(test)) 21 | test_cases.close() 22 | 23 | 24 | if __name__ == '__main__': 25 | main() 26 | -------------------------------------------------------------------------------- /1-moderate/array-absurdity/ArrayAbsurdity.java: -------------------------------------------------------------------------------- 1 | import java.io.BufferedReader; 2 | import java.io.File; 3 | import java.io.FileReader; 4 | import java.io.IOException; 5 | 6 | public class ArrayAbsurdity 7 | { 8 | public static void main (String[] args) 9 | throws IOException 10 | { 11 | File file = new File(args[0]); 12 | BufferedReader in = new BufferedReader(new FileReader(file)); 13 | String line; 14 | 15 | while ((line = in.readLine()) != null) { 16 | line = line.trim(); 17 | 18 | if (line.length() == 0) { 19 | continue; 20 | } 21 | 22 | String[] stringArray = line.split(";"); 23 | int N = Integer.parseInt(stringArray[0].trim()); 24 | stringArray = stringArray[1].trim().split(","); 25 | 26 | int sum = 0; 27 | for (String s : stringArray) { 28 | sum += Integer.parseInt(s); 29 | } 30 | 31 | int expectedSumLessDuplicate = (N-1)*(N-2)/2; 32 | System.out.println(sum - expectedSumLessDuplicate); 33 | } 34 | 35 | in.close(); 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /1-moderate/array-absurdity/input.txt: -------------------------------------------------------------------------------- 1 | 5;0,1,2,3,0 2 | 20;0,1,10,3,2,4,5,7,6,8,11,9,15,12,13,4,16,18,17,14 3 | -------------------------------------------------------------------------------- /1-moderate/balanced-smileys/input.txt: -------------------------------------------------------------------------------- 1 | )( aaaabcb)a )b((b()bccba: c):: :aac:b) aacac::)c(( (:aa(c):a((a b::a)(b)b:b(bb(b 2 | (((((ca((a):bcc:( )b()))(b): (:)cc:cacc)a(ba)::(( acba:a(: )aaa)a:(c(:::)c)a)):c)b(c(:()b)cc: :(c 3 | b(b(b::(:)(b:(aa(bca(:())(a:b)(caa)ccb:b)((:(cb)::cc:b:)cab::()ab::ca:::aaccb:cb:b): 4 | ba:a)cba)acba(a(bc(:):ab( (b:b:b)bbca((bb:cc))((b))c(((::::c() ())c: abac:((cc) ( 5 | (()c)c))caa(a(aa:)::bb(: (bcb) ca:(c::()b))a:: c)b((ccb)c:bccc(b(b:)a(((ca)a) aa:b)abacccb 6 | a((()(a(bacb:c(c:(a)acbb)( b:ac:()bc):a: )cac:): :ca)::b:):)((aabb:ab(a))a ))) :cc:c:(:(c 7 | i am sick today (:() 8 | (c:(ccc:(a:((::()c(c:c: cba):bc)aa)(c:(:ab)(cb:)c)a:aaa() :a:)((bcabc:(c ac)cbc(a:(:(::):()b) 9 | (:)c::cb((:(:ca::) ::ab:c(bbb(a:b()(:b(bc:)(acbca):b)baaba)bb(a(b:))bb(cca(:acb(c::a()a)b)bcbc)c) 10 | bbb )::b)acc)b((ab((aab)b()) bca:b((a:b(()b)::c:aac((()bacb(abc()ca()bb )::c)b(:a(c(b 11 | (ab ():(ab(bc)c()::()((ca:bb:)()bc:(c:b(( c:))b:bbc)a:b))cccc(:)):(::cb:c:cca )c:b:(c: 12 | )( 13 | hacker cup: started :):) 14 | c::)(b(cbc(bbbc(:)) )c(bcaccc(:) )c)c(bbb(ab:::()c)(a((c(a(cbbca)b: :(c)cc(a)())ca))b 15 | (bcac)a ca(bc::)(cca (b ((aa(ac)(:))( ba)acbcba)b()c(bb:ac::():(c )a):)()):cbb)bcbc:( 16 | :ca) a):cbac)c(ac(:b)(caaa c cbbbba)b(aaa)c(cba)bc)cbba)ca(a((:((c(ccc:(bc:bb((aacbca:)c(cc(:aa 17 | (:) 18 | (:a)) 19 | c( acbb(::a(b:))c))c(aca::)b):)b:ba)()ac(ca(a:c:c::ab:)c:(c(ba:):b:()bcc)(:)cb:bcaa((ca:)):)b(:cb 20 | :(( -------------------------------------------------------------------------------- /1-moderate/balanced-smileys/main.py: -------------------------------------------------------------------------------- 1 | import sys 2 | import re 3 | 4 | def is_balanced(message): 5 | minOpen = 0 6 | maxOpen = 0 7 | for i in range(len(message)): 8 | if message[i] == '(': 9 | maxOpen += 1 10 | if i == 0 or message[i-1] != ':': 11 | minOpen += 1 12 | elif message[i] == ')': 13 | minOpen = max(0, minOpen-1) 14 | if i == 0 or message[i-1] != ':': 15 | maxOpen -= 1 16 | if maxOpen < 0: break 17 | if maxOpen >= 0 and minOpen == 0: return "YES" 18 | else: return "NO" 19 | 20 | test_cases = open(sys.argv[1], 'r') 21 | for test in test_cases: 22 | test = test.strip() 23 | if len(test) == 0: 24 | continue 25 | print(is_balanced(test)) 26 | 27 | test_cases.close() 28 | -------------------------------------------------------------------------------- /1-moderate/bats-challenge/input.txt: -------------------------------------------------------------------------------- 1 | 22 2 2 9 11 2 | 33 5 0 3 | 16 3 2 6 10 4 | 835 125 1 113 5 | 47 5 0 6 | -------------------------------------------------------------------------------- /1-moderate/bats-challenge/main.py: -------------------------------------------------------------------------------- 1 | import sys 2 | 3 | def use(bat, used, d): 4 | for i in range(max(0, bat-d+1), min(len(used), bat+d)): 5 | used[i] = True 6 | 7 | test_cases = open(sys.argv[1], 'r') 8 | for test in test_cases: 9 | test = test.strip() 10 | if len(test) == 0: 11 | continue 12 | l = int(test.split(' ')[0]) 13 | d = int(test.split(' ')[1]) 14 | n = int(test.split(' ')[2]) 15 | bats = map(int, test.split(' ')[3:]) 16 | used = [False] * (l+1) 17 | for i in range(0, 6): used[i] = True 18 | for i in range(len(used)-6, len(used)): used[i] = True 19 | for bat in bats: use(bat, used, d) 20 | additional_bats = 0 21 | for i in range(0, l): 22 | if not used[i]: 23 | additional_bats += 1 24 | use(i, used, d) 25 | print(additional_bats) 26 | 27 | test_cases.close() 28 | -------------------------------------------------------------------------------- /1-moderate/black-or-white/input.txt: -------------------------------------------------------------------------------- 1 | 11 | 11 2 | 1001 | 0110 | 1001 | 0110 3 | 110 | 101 | 111 4 | 000 | 000 | 000 5 | -------------------------------------------------------------------------------- /1-moderate/black-or-white/main.py: -------------------------------------------------------------------------------- 1 | import sys 2 | 3 | 4 | def find_smallest_sub_matrix_size_with_equal_number_of_ones(grid): 5 | for length in range(1, len(grid)): 6 | previous_one_count = -1 7 | success = True 8 | for i in range(0, len(grid) + 1 - length): 9 | for j in range(0, len(grid) + 1 - length): 10 | one_count = 0 11 | for k in range(0, length): 12 | one_count += sum(grid[i+k][j : j+length]) 13 | if previous_one_count == -1: 14 | previous_one_count = one_count 15 | if previous_one_count != one_count: 16 | success = False 17 | break 18 | if not success: 19 | break 20 | if success: 21 | return length, previous_one_count 22 | return len(grid), sum([sum(row) for row in grid]) 23 | 24 | 25 | def main(): 26 | test_cases = open(sys.argv[1], 'r') 27 | for test in test_cases: 28 | test = test.strip() 29 | if len(test) == 0: continue 30 | grid = [[int(i) for i in list(s.strip())] for s in test.split('|')] 31 | l, s = find_smallest_sub_matrix_size_with_equal_number_of_ones(grid) 32 | print("%ix%i, %i" % (l, l, s)) 33 | test_cases.close() 34 | 35 | 36 | if __name__ == '__main__': 37 | main() 38 | -------------------------------------------------------------------------------- /1-moderate/broken-lcd/input.txt: -------------------------------------------------------------------------------- 1 | 10110001 11111000 11111110 11111111 11111111 11111111 11111111 11101101 11111111 01111111 11110010 10100111;84.525784 2 | 11111111 11110110 11101111 11110111 10111110 11110110 10111011 10100111 11111100 01100100 11111101 01011110;5.57 3 | 11000010 00001111 11111111 10111111 11101011 11110011 01111110 11011111 11111111 11111111 11111001 01101110;857.71284 4 | 11111111 01110111 10111011 11001101 11111011 11101010 11110100 01001101 11011111 11111010 10010110 10111111;66.92 5 | 11111011 10010001 11111011 11111101 10011111 10111110 01111100 11011101 10111001 11111110 11101111 11110110;188.87 6 | 11111111 11111111 11111111 11111111 11111111 11111111 11111111 11111111 11111111 11111111 11111111 11111111;111122223333 7 | 11111111 11111111 11111111 11111111 11111111 11111111 11111111 11111111 11111111 11111111 11111111 11111111;0 8 | 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000;0 9 | -------------------------------------------------------------------------------- /1-moderate/builders-team/input1.txt: -------------------------------------------------------------------------------- 1 | 1 2 | 6 7 | 7 2 | 1 6 | 2 3 2 | 1 2 | 6 7 | 7 2 | 1 6 | 2 3 | 7 8 | 3 8 3 | 1 2 | 1 6 | 6 7 4 | -------------------------------------------------------------------------------- /1-moderate/burrows-wheeler-transform/input.txt: -------------------------------------------------------------------------------- 1 | oooooooo$ ffffffff ffffffffuuuuuuuuaaaaaaaallllllllbbBbbBBb| 2 | edarddddddddddntensr$ ehhhhhhhhhhhJ aeaaaaaaaaaaalhtf thmbfe tcwohiahoJ eeec t e | 3 | ooooio,io$Nnssshhhjo ee o nnkkkkkkii | 4 | -------------------------------------------------------------------------------- /1-moderate/burrows-wheeler-transform/main.py: -------------------------------------------------------------------------------- 1 | import sys 2 | 3 | 4 | def sort_lexicographic(s): 5 | return sorted(s) 6 | 7 | 8 | def reverse_burrows_wheeler_transform(s): 9 | array = sort_lexicographic(list(s)) 10 | for i in range(len(s)-1): 11 | for j in range(len(s)): 12 | array[j] = s[j] + array[j] 13 | array = sort_lexicographic(array) 14 | for a in array: 15 | if a[-1] == '$': 16 | return a 17 | return None 18 | 19 | 20 | def main(input_filename): 21 | test_cases = open(input_filename, 'r') 22 | for test in test_cases: 23 | test = test.strip().strip('|') 24 | if len(test) == 0: 25 | continue 26 | print(reverse_burrows_wheeler_transform(test)) 27 | test_cases.close() 28 | 29 | 30 | if __name__ == '__main__': 31 | if len(sys.argv) != 2: 32 | print("Usage: python %s " % sys.argv[0]) 33 | sys.exit(1) 34 | main(sys.argv[1]) 35 | -------------------------------------------------------------------------------- /1-moderate/car-race/input1.txt: -------------------------------------------------------------------------------- 1 | 1.029 115 1.122 125 1.185 100 0.53 110 0.751 95 1.242 85 0.533 85 1.003 120 0.465 110 0.546 125 0.446 90 0.582 70 0.878 45 0.49 30 1.016 130 1.047 140 1.146 75 0.496 85 0.857 125 0.971 0 2 | 1 266 8.1 1.4 3 | 2 178 8.7 4.8 4 | 3 251 8.0 3.4 5 | 4 215 6.8 3.8 6 | 5 220 5.9 3.3 7 | 6 262 4.5 1.5 8 | 7 267 5.4 2.6 9 | 8 268 7.8 3.8 10 | 9 225 4.7 1.8 11 | 10 266 4.0 1.9 12 | -------------------------------------------------------------------------------- /1-moderate/car-race/input2.txt: -------------------------------------------------------------------------------- 1 | 1.222 130 0.784 95 0.721 25 0.654 90 0.779 30 0.781 20 0.555 25 1.157 55 0.712 40 1.227 40 0.648 135 0.834 60 1.087 25 0.804 75 0.78 70 0.648 75 0.82 40 1.108 115 0.459 30 0.73 0 2 | 1 256 8.9 4.0 3 | 2 206 4.7 2.1 4 | 3 272 4.3 1.0 5 | 4 245 7.6 4.8 6 | 5 171 6.6 3.3 7 | 6 228 7.7 3.4 8 | 7 263 6.1 1.9 9 | 8 179 7.1 3.2 10 | 9 157 6.7 3.1 11 | 10 191 8.1 3.7 12 | 11 195 4.5 2.3 13 | 12 183 6.0 2.2 14 | 13 173 5.3 1.8 15 | 14 185 6.0 1.5 16 | 15 174 5.3 2.8 17 | 16 284 7.0 4.6 18 | 17 208 4.0 1.2 19 | 18 157 4.6 1.2 20 | 19 238 5.4 1.1 21 | 20 170 6.8 2.2 22 | -------------------------------------------------------------------------------- /1-moderate/card-number-validation/input.txt: -------------------------------------------------------------------------------- 1 | 6011 5940 0319 9511 2 | 5537 0213 6797 6815 3 | 5574 8363 8022 9735 4 | 3044 8507 9391 30 5 | 6370 1675 9034 6211 774 6 | -------------------------------------------------------------------------------- /1-moderate/card-number-validation/main.py: -------------------------------------------------------------------------------- 1 | import sys 2 | 3 | def double_and_sum(digit): 4 | digit = int(digit) 5 | digit *= 2 6 | digit = str(digit) 7 | if len(digit) == 1: 8 | return int(digit) 9 | else: 10 | return int(digit[0]) + int(digit[1]) 11 | 12 | def is_valid(card_number): 13 | card_number = card_number[::-1] 14 | even = False 15 | total = 0 16 | for digit in card_number: 17 | digit = int(digit) 18 | if even: 19 | digit = double_and_sum(digit) 20 | even = not even 21 | total += digit 22 | return total % 10 == 0 23 | 24 | def main(): 25 | test_cases = open(sys.argv[1], 'r') 26 | for test in test_cases: 27 | test = test.strip() 28 | if len(test) == 0: 29 | continue 30 | test = test.replace(' ', '') 31 | print('1' if is_valid(test) else '0') 32 | 33 | test_cases.close() 34 | 35 | if __name__ == '__main__': 36 | main() 37 | -------------------------------------------------------------------------------- /1-moderate/cash-register/input.txt: -------------------------------------------------------------------------------- 1 | 15.94;16.00 2 | 17;16 3 | 35;35 4 | 45;50 5 | .5;1 6 | 99.90;110 7 | -------------------------------------------------------------------------------- /1-moderate/cash-register/main.py: -------------------------------------------------------------------------------- 1 | import sys 2 | 3 | discrete_options = [10000, 5000, 2000, 1000, 500, 200, 100, 50, 25, 10, 5, 1] 4 | 5 | discrete_options_mapping = { 6 | 1: 'PENNY', 7 | 5: 'NICKEL', 8 | 10: 'DIME', 9 | 25: 'QUARTER', 10 | 50: 'HALF DOLLAR', 11 | 100: 'ONE', 12 | 200: 'TWO', 13 | 500: 'FIVE', 14 | 1000: 'TEN', 15 | 2000: 'TWENTY', 16 | 5000: 'FIFTY', 17 | 10000: 'ONE HUNDRED' 18 | } 19 | 20 | def main(): 21 | test_cases = open(sys.argv[1], 'r') 22 | for test in test_cases: 23 | test = test.strip() 24 | if len(test) == 0: continue 25 | total = int(float(test.split(";")[0]) * 100) 26 | given = int(float(test.split(";")[1]) * 100) 27 | change = given - total 28 | if change == 0: 29 | print('ZERO') 30 | continue 31 | if change < 0: 32 | print('ERROR') 33 | continue 34 | result = [] 35 | for option in discrete_options: 36 | while change >= option: 37 | change = change - option 38 | result.append(discrete_options_mapping[option]) 39 | print(','.join(result)) 40 | test_cases.close() 41 | 42 | if __name__ == '__main__': 43 | main() 44 | -------------------------------------------------------------------------------- /1-moderate/chain-inspection/input.txt: -------------------------------------------------------------------------------- 1 | BEGIN-3;4-2;3-4;2-END 2 | 77-END;BEGIN-8;8-11;11-77 3 | BEGIN-3;4-3;3-4;2-END 4 | 77-END;BEGIN-8;8-77;11-11 5 | -------------------------------------------------------------------------------- /1-moderate/city-blocks-flyover/input.txt: -------------------------------------------------------------------------------- 1 | (0,2,4,8,10,13,14,18,22,23,24,33,40,42,44,47,49,53,55,63,66,81,87,91) (0,147,220) 2 | (0,1,2,4) (0,1,3,4,5) 3 | (0,1,3,4,6) (0,1,2,4) 4 | (0,1,2) (0,1,2) 5 | -------------------------------------------------------------------------------- /1-moderate/city-blocks-flyover/main.py: -------------------------------------------------------------------------------- 1 | import sys 2 | 3 | test_cases = open(sys.argv[1], 'r') 4 | 5 | def is_overlapping(x1, x2, y1, y2): 6 | return max(x1, y1) < min(x2, y2) 7 | 8 | for test in test_cases: 9 | test = test.strip() 10 | if len(test) == 0: 11 | continue 12 | 13 | streets = list(map(int, test.split(' ')[0] 14 | .replace('(', '').replace(')', '') 15 | .split(','))) 16 | avenues = list(map(int, test.split(' ')[1] 17 | .replace('(', '').replace(')', '') 18 | .split(','))) 19 | 20 | max_street = streets[-1] # run 21 | max_avenue = avenues[-1] # rise 22 | 23 | slope = float(max_avenue) / float(max_street) 24 | count = 0 25 | 26 | for s in range(0, len(streets)-1): 27 | for a in range(0, len(avenues)-1): 28 | x1 = streets[s] 29 | x2 = streets[s+1] 30 | y1 = avenues[a] 31 | y2 = avenues[a+1] 32 | 33 | if is_overlapping(x1, x2, y1/slope, y2/slope): 34 | count += 1 35 | 36 | print(count) 37 | 38 | test_cases.close() 39 | -------------------------------------------------------------------------------- /1-moderate/cocktail-sort/input.txt: -------------------------------------------------------------------------------- 1 | 5 4 9 10 7 3 2 1 6 | 1 2 | 9 8 7 6 5 4 3 2 1 | 3 3 | -------------------------------------------------------------------------------- /1-moderate/cocktail-sort/main.py: -------------------------------------------------------------------------------- 1 | import sys 2 | 3 | 4 | def cocktail_sort(l, iterations): 5 | for i in range(iterations): 6 | for j in range(i, len(l)-i-1): 7 | if l[j] > l[j+1]: 8 | l[j], l[j+1] = l[j+1], l[j] 9 | for j in range(len(l)-i-2, i-1, -1): 10 | if l[j] > l[j+1]: 11 | l[j], l[j+1] = l[j+1], l[j] 12 | return l 13 | 14 | 15 | def main(input_filename): 16 | test_cases = open(input_filename, 'r') 17 | for test in test_cases: 18 | test = test.strip() 19 | if len(test) == 0: 20 | continue 21 | l = [int(x) for x in test.split('|')[0].strip().split(' ')] 22 | iterations = int(test.split('|')[1].strip()) 23 | print(' '.join([str(x) for x in cocktail_sort(l, iterations)])) 24 | test_cases.close() 25 | 26 | 27 | if __name__ == '__main__': 28 | if len(sys.argv) != 2: 29 | print("Usage: python %s " % sys.argv[0]) 30 | main(sys.argv[1]) 31 | -------------------------------------------------------------------------------- /1-moderate/code-comparison/input.txt: -------------------------------------------------------------------------------- 1 | **** | *co* | *de* | **** 2 | codx | decx 3 | co | dx 4 | -------------------------------------------------------------------------------- /1-moderate/code-comparison/main.py: -------------------------------------------------------------------------------- 1 | import sys 2 | 3 | 4 | class LetterMatrix(object): 5 | def __init__(self, array): 6 | self.array = array 7 | self.rows = len(array) 8 | self.cols = len(array[0]) 9 | 10 | def count_code_blocks(self): 11 | """ 12 | Count the number of 2x2 blocks that contain the letters 'code'. 13 | """ 14 | result = 0 15 | for i in range(self.rows-1): 16 | for j in range(self.cols-1): 17 | letters = [self.array[i][j], 18 | self.array[i+1][j], 19 | self.array[i][j+1], 20 | self.array[i+1][j+1]] 21 | if 'c' in letters and 'o' in letters and 'd' in letters and 'e' in letters: 22 | result += 1 23 | return result 24 | 25 | def main(): 26 | test_cases = open(sys.argv[1], 'r') 27 | for test in test_cases: 28 | test = test.strip() 29 | if len(test) == 0: continue 30 | letter_matrix = LetterMatrix([list(s.strip()) for s in test.split('|')]) 31 | print(letter_matrix.count_code_blocks()) 32 | test_cases.close() 33 | 34 | 35 | if __name__ == '__main__': 36 | main() 37 | -------------------------------------------------------------------------------- /1-moderate/color-code-converter/input.txt: -------------------------------------------------------------------------------- 1 | (0.56,0.94,0.21,0.02) 2 | HSL(359,0,0) 3 | HSV(276,33,7) 4 | #cfa9c4 5 | -------------------------------------------------------------------------------- /1-moderate/column-names/ColumnNames.java: -------------------------------------------------------------------------------- 1 | import java.io.BufferedReader; 2 | import java.io.File; 3 | import java.io.FileReader; 4 | import java.util.Iterator; 5 | import java.util.LinkedList; 6 | 7 | class ColumnNames 8 | { 9 | public static void main(String[] args) throws Exception 10 | { 11 | File file = new File(args[0]); 12 | BufferedReader in = new BufferedReader(new FileReader(file)); 13 | String line; 14 | 15 | while ((line = in.readLine()) != null) { 16 | int number = Integer.parseInt(line.trim()); 17 | System.out.println(convertToExcelColumnName(number)); 18 | } 19 | 20 | in.close(); 21 | } 22 | 23 | private static final int OFFSET = (int) 'A'; 24 | 25 | private static String convertToExcelColumnName(int number) 26 | { 27 | number--; 28 | LinkedList result = new LinkedList<>(); 29 | 30 | while (number >= 0) { 31 | int quotient = number / 26; 32 | int remainder = number % 26; 33 | result.add((char) (OFFSET + remainder)); 34 | number = quotient - 1; 35 | } 36 | 37 | StringBuilder sb = new StringBuilder(); 38 | 39 | Iterator it = result.descendingIterator(); 40 | while(it.hasNext()){ 41 | sb.append(it.next()); 42 | } 43 | 44 | return sb.toString(); 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /1-moderate/column-names/input.txt: -------------------------------------------------------------------------------- 1 | 25 2 | 26 3 | 27 4 | 52 5 | 3702 6 | -------------------------------------------------------------------------------- /1-moderate/column-names/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | int OFFSET = (int) 'A'; 7 | 8 | std::string convertToExcelColumnName(int number) 9 | { 10 | number--; 11 | std::vector result; 12 | 13 | while (number >= 0) { 14 | int quotient = number / 26; 15 | int remainder = number % 26; 16 | result.push_back((char) (OFFSET + remainder)); 17 | number = quotient - 1; 18 | } 19 | 20 | std::stringstream ss; 21 | 22 | for(std::vector::reverse_iterator it = result.rbegin(); it != result.rend(); ++it) { 23 | ss << *it; 24 | } 25 | 26 | return ss.str(); 27 | } 28 | 29 | int main(int argc, char** argv) 30 | { 31 | std::ifstream file(argv[1]); 32 | 33 | std::string line; 34 | bool first = true; 35 | while (std::getline(file, line)) 36 | { 37 | std::istringstream reader(line); 38 | int val; 39 | reader >> val; 40 | 41 | std::cout << convertToExcelColumnName(val) << std::endl; 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /1-moderate/column-names/main.py: -------------------------------------------------------------------------------- 1 | import sys 2 | import math 3 | 4 | OFFSET = ord('A') 5 | ALPHABET_LENGTH = 26 6 | 7 | def convert_to_excel_column_name(number): 8 | number -= 1 9 | 10 | result = [] 11 | while number >= 0: 12 | quotient, remainder = divmod(number, ALPHABET_LENGTH) 13 | result.append(chr(OFFSET + remainder)) 14 | number = quotient - 1 15 | return ''.join(reversed(result)) 16 | 17 | def main(input_filename): 18 | test_cases = open(input_filename, 'r') 19 | for test in test_cases: 20 | test = test.strip() 21 | if len(test) == 0: 22 | continue 23 | number = int(test) 24 | print(convert_to_excel_column_name(number)) 25 | test_cases.close() 26 | 27 | if __name__ == '__main__': 28 | if len(sys.argv) != 2: 29 | print("Usage: python %s " % sys.argv[0]) 30 | sys.exit(1) 31 | main(sys.argv[1]) 32 | -------------------------------------------------------------------------------- /1-moderate/comb-sort/input.txt: -------------------------------------------------------------------------------- 1 | 3 1 2 2 | 5 4 3 2 1 3 | -------------------------------------------------------------------------------- /1-moderate/comb-sort/main.py: -------------------------------------------------------------------------------- 1 | import sys 2 | 3 | 4 | def compute_comb_sort_iterations(array): 5 | 6 | gap = len(array) 7 | shrink = 1.25 8 | is_sorted = False 9 | iterations = 0 10 | 11 | while not is_sorted: 12 | gap = int(gap / shrink) 13 | iterations += 1 14 | if gap > 1: 15 | is_sorted = False 16 | else: 17 | gap = 1 18 | is_sorted = True 19 | i = 0 20 | while i + gap < len(array): 21 | if array[i] > array[i+gap]: 22 | array[i], array[i+gap] = array[i+gap], array[i] 23 | is_sorted = False 24 | i += 1 25 | return iterations-1 26 | 27 | 28 | def main(input_filename): 29 | test_cases = open(input_filename, 'r') 30 | for test in test_cases: 31 | test = test.strip() 32 | if len(test) == 0: 33 | continue 34 | array = [int(x) for x in test.strip().split(' ')] 35 | print(compute_comb_sort_iterations(array)) 36 | test_cases.close() 37 | 38 | 39 | if __name__ == '__main__': 40 | if len(sys.argv) != 2: 41 | print("Usage: python %s " % sys.argv[0]) 42 | main(sys.argv[1]) 43 | -------------------------------------------------------------------------------- /1-moderate/consecutive-primes/input.txt: -------------------------------------------------------------------------------- 1 | 2 2 | 4 3 | 5 4 | 10 5 | 12 6 | 16 7 | 18 8 | -------------------------------------------------------------------------------- /1-moderate/consecutive-primes/main.py: -------------------------------------------------------------------------------- 1 | import sys 2 | 3 | PRIMES = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31} 4 | 5 | 6 | def count_possible_necklace_arrangements(beads, pos): 7 | if len(beads) == pos: 8 | return 1 if 1 + beads[- 1] in PRIMES else 0 9 | result = 0 10 | if beads[pos] + beads[pos-1] in PRIMES: 11 | result += count_possible_necklace_arrangements(beads, pos + 1) 12 | for i in range(pos+2, len(beads), 2): 13 | if beads[pos-1] + beads[i] in PRIMES: 14 | beads[i], beads[pos] = beads[pos], beads[i] 15 | result += count_possible_necklace_arrangements(beads, pos + 1) 16 | beads[i], beads[pos] = beads[pos], beads[i] 17 | return result 18 | 19 | 20 | def main(input_filename): 21 | test_cases = open(input_filename, 'r') 22 | for test in test_cases: 23 | test = test.strip().strip('|') 24 | if len(test) == 0: 25 | continue 26 | print(count_possible_necklace_arrangements(list(range(1, int(test)+1)), 1)) 27 | test_cases.close() 28 | 29 | 30 | if __name__ == '__main__': 31 | if len(sys.argv) != 2: 32 | print("Usage: python %s " % sys.argv[0]) 33 | sys.exit(1) 34 | main(sys.argv[1]) 35 | -------------------------------------------------------------------------------- /1-moderate/counting-primes/input.txt: -------------------------------------------------------------------------------- 1 | 2,10 2 | 20,30 3 | -------------------------------------------------------------------------------- /1-moderate/decimal-to-binary/DecimalToBinary.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | 3 | public class DecimalToBinary 4 | { 5 | public static void main (String[] args) 6 | throws IOException 7 | { 8 | File file = new File(args[0]); 9 | BufferedReader in = new BufferedReader(new FileReader(file)); 10 | String line; 11 | 12 | while ((line = in.readLine()) != null) { 13 | line = line.trim(); 14 | 15 | if (line.length() == 0) { 16 | continue; 17 | } 18 | 19 | int n = Integer.parseInt(line); 20 | System.out.println(Integer.toBinaryString(n)); 21 | } 22 | 23 | in.close(); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /1-moderate/decimal-to-binary/input.txt: -------------------------------------------------------------------------------- 1 | 2 2 | 10 3 | 67 4 | -------------------------------------------------------------------------------- /1-moderate/decimal-to-binary/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | std::string decToBin(unsigned int number) 7 | { 8 | if (number == 0) return "0"; 9 | if (number == 1) return "1"; 10 | 11 | if (number % 2 == 0) { 12 | return decToBin(number / 2) + "0"; 13 | } else { 14 | return decToBin(number / 2) + "1"; 15 | } 16 | } 17 | 18 | int main(int argc, char** argv) 19 | { 20 | std::ifstream file(argv[1]); 21 | 22 | std::string line; 23 | while (std::getline(file, line)) 24 | { 25 | unsigned int x; 26 | std::stringstream ss(line); 27 | ss >> x; 28 | 29 | std::cout << decToBin(x) << std::endl; 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /1-moderate/decode-numbers/input.txt: -------------------------------------------------------------------------------- 1 | 12 2 | 123 3 | -------------------------------------------------------------------------------- /1-moderate/decode-numbers/main.py: -------------------------------------------------------------------------------- 1 | import sys 2 | 3 | def count_possible_decoding(message): 4 | if len(message) == 1 or len(message) == 0: 5 | return 1 6 | possibilities = 0 7 | num_chars = 1 8 | while True: 9 | chars = message[0:num_chars] 10 | if len(chars) != num_chars: break 11 | if int(chars) > 26: break 12 | possibilities += count_possible_decoding(message[num_chars:]) 13 | num_chars += 1 14 | return possibilities 15 | 16 | test_cases = open(sys.argv[1], 'r') 17 | for test in test_cases: 18 | test = test.strip() 19 | if len(test) == 0: 20 | continue 21 | print(count_possible_decoding(test)) 22 | 23 | test_cases.close() 24 | -------------------------------------------------------------------------------- /1-moderate/double-squares/input.txt: -------------------------------------------------------------------------------- 1 | 5 2 | 10 3 | 25 4 | 3 5 | 0 6 | 1 7 | 2116000000 8 | 2147483647 9 | 50 10 | 48612265 11 | -------------------------------------------------------------------------------- /1-moderate/double-squares/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | unsigned int countDoubleSquares(unsigned int x) 7 | { 8 | if (x == 0) { 9 | return 1; 10 | } 11 | 12 | unsigned int result = 0; 13 | 14 | for (unsigned int b = 0; b <= pow(x, 0.5); b++) { 15 | unsigned int a = pow(x - pow(b, 2), 0.5); 16 | 17 | if (a < b) { 18 | break; 19 | } 20 | 21 | if (x == (pow(a ,2) + pow(b, 2))) { 22 | result++; 23 | } 24 | } 25 | 26 | return result; 27 | } 28 | 29 | int main(int argc, char** argv) 30 | { 31 | std::ifstream file(argv[1]); 32 | 33 | std::string line; 34 | bool first = true; 35 | while (std::getline(file, line)) 36 | { 37 | unsigned int x; 38 | std::stringstream ss(line); 39 | ss >> x; 40 | 41 | if (first) { 42 | // Skip the number of lines count. 43 | first = false; 44 | continue; 45 | } 46 | 47 | std::cout << countDoubleSquares(x) << std::endl; 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /1-moderate/double-squares/main.py: -------------------------------------------------------------------------------- 1 | import sys 2 | 3 | def count_double_squares(x): 4 | if x == 0: return 1 5 | result = 0 6 | for b in range(0, int(x ** 0.5)+1): 7 | a = int((x - (b ** 2)) ** 0.5) 8 | if a < b: break 9 | if x == a ** 2 + b ** 2: result += 1 10 | return result 11 | 12 | test_cases = open(sys.argv[1], 'r') 13 | first = True 14 | 15 | for test in test_cases: 16 | test = test.strip() 17 | if len(test) == 0: continue 18 | if first: 19 | first = False 20 | continue 21 | x = int(test) 22 | print(count_double_squares(x)) 23 | 24 | test_cases.close() 25 | -------------------------------------------------------------------------------- /1-moderate/double-trouble/main.py: -------------------------------------------------------------------------------- 1 | import sys 2 | 3 | 4 | def to_hex_repr(s): 5 | result = [] 6 | for c in s: 7 | result.append({ 8 | 'A': 0x1, 9 | 'B': 0x2, 10 | '*': 0x0 11 | }[c]) 12 | return result 13 | 14 | 15 | def compute_permutations(test): 16 | x = to_hex_repr(test[1:len(test)//2]) 17 | y = to_hex_repr(test[len(test)//2:]) 18 | result = 1 19 | for a, b in zip(x, y): 20 | if a ^ b == 3: 21 | return 0 22 | if a == 0 and b == 0: 23 | result *= 2 24 | return result 25 | 26 | 27 | def main(): 28 | test_cases = open(sys.argv[1], 'r') 29 | for test in test_cases: 30 | test = test.strip() 31 | if len(test) == 0: continue 32 | print(compute_permutations(test)) 33 | test_cases.close() 34 | 35 | 36 | if __name__ == '__main__': 37 | main() 38 | -------------------------------------------------------------------------------- /1-moderate/email-verification/EmailVerification.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.regex.Pattern; 3 | 4 | public class EmailVerification 5 | { 6 | public static void main (String[] args) 7 | throws IOException 8 | { 9 | File file = new File(args[0]); 10 | BufferedReader in = new BufferedReader(new FileReader(file)); 11 | String line; 12 | 13 | String patternString = 14 | "^[_A-Za-z0-9-\\.\\+]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$|" + 15 | "^\"[_A-Za-z0-9-\\.\\+@]+(\\.[_A-Za-z0-9-]+)*\"@[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$"; 16 | 17 | Pattern pattern = Pattern.compile(patternString); 18 | 19 | while ((line = in.readLine()) != null) { 20 | System.out.println(pattern.matcher(line.trim()).matches()); 21 | } 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /1-moderate/email-verification/input.txt: -------------------------------------------------------------------------------- 1 | "very.unusual.@.unusual.com"@example.com 2 | foo@bar.com 3 | good123@bad.com 4 | test+test@example.com 5 | disposable.style.email.with+156@example.com 6 | disposable.style.email.with+symbol@example.com 7 | admin#codeeval.com 8 | this is not an email id 9 | a"b(c)d,e:f;gi[j\k]l@example.com 10 | A@b@c@example.com 11 | just"not"right@example.com 12 | -------------------------------------------------------------------------------- /1-moderate/endianness/Endianness.java: -------------------------------------------------------------------------------- 1 | import java.nio.ByteOrder; 2 | 3 | public class Endianness 4 | { 5 | public static void main (String[] args) 6 | { 7 | if (ByteOrder.nativeOrder().equals(ByteOrder.BIG_ENDIAN)) { 8 | System.out.println("BigEndian"); 9 | } else { 10 | System.out.println("LittleEndian"); 11 | } 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /1-moderate/endianness/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | enum 5 | { 6 | O32_LITTLE_ENDIAN = 0x03020100ul, 7 | O32_BIG_ENDIAN = 0x00010203ul 8 | }; 9 | 10 | int main() 11 | { 12 | static const union { 13 | unsigned char bytes[4]; 14 | uint32_t value; 15 | } o32_host_order = { { 0, 1, 2, 3 } }; 16 | 17 | if (o32_host_order.value == O32_LITTLE_ENDIAN) { 18 | std::cout << "LittleEndian" << std::endl; 19 | } else { 20 | std::cout << "BigEndian" << std::endl; 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /1-moderate/filename-pattern/input-small.txt: -------------------------------------------------------------------------------- 1 | *7* johab.py gen_probe.ko palmtx.h macpath.py tzp dm-dirty-log.h bh1770.h pktloc faillog.8.gz zconf.gperf 2 | *[0123456789]*[auoei]* IBM1008_420.so zfgrep limits.conf.5.gz tc.h ilogb.3.gz limits.conf CyrAsia-TerminusBold28x14.psf.gz nf_conntrack_sip.ko DistUpgradeViewNonInteractive.pyc NFKDQC 3 | *.??? max_user_watches arptables.h net_namespace Kannada.pl menu_no_no.utf-8.vim shtags.1 unistd_32_ia32.h gettext-tools.mo ntpdate.md5sums linkat.2.gz 4 | *.pdf OldItali.pl term.log plymouth-upstart-bridge rand.so libipw.ko jisfreq.pyc impedance-analyzer xmon.h 1.5.0.3.txt bank 5 | g*.* 56b8a0b6.0 sl.vim digctl.h groff-base.conffiles python-software-properties.md5sums CountryInformation.py use_zero_page session-noninteractive d2i_RSAPublicKey.3ssl.gz container-detect.log.4.gz 6 | *[0123456789]* keyboard.h machinecheck 46b2fd3b.0 libip6t_frag.so timer_defs.h nano-menu.xpm NI vim-keys.conf setjmp.h memcg 7 | -------------------------------------------------------------------------------- /1-moderate/filename-pattern/main.py: -------------------------------------------------------------------------------- 1 | import sys 2 | import re 3 | 4 | def matches(pattern, case): 5 | pattern = re.sub('\.', '\.', pattern) 6 | pattern = re.sub('\*', '.*', pattern) 7 | pattern = re.sub('\?', '.', pattern) 8 | return re.match('^' + pattern + '$', case) is not None 9 | 10 | def main(): 11 | test_cases = open(sys.argv[1], 'r') 12 | for test in test_cases: 13 | test = test.strip() 14 | if len(test) == 0: continue 15 | test = test.split() 16 | pattern = test[0] 17 | cases = test[1:] 18 | results = [] 19 | for case in cases: 20 | if matches(pattern, case): 21 | results.append(case) 22 | print('-' if len(results) == 0 else ' '.join(results)) 23 | test_cases.close() 24 | 25 | if __name__ == '__main__': 26 | main() 27 | -------------------------------------------------------------------------------- /1-moderate/find-square/input.txt: -------------------------------------------------------------------------------- 1 | (1,6), (6,7), (2,7), (9,1) 2 | (4,1), (3,4), (0,5), (1,2) 3 | (4,6), (5,5), (5,6), (4,5) 4 | -------------------------------------------------------------------------------- /1-moderate/first-non-repeated-char/input.txt: -------------------------------------------------------------------------------- 1 | yellow 2 | tooth 3 | -------------------------------------------------------------------------------- /1-moderate/flavius-josephus/FlaviusJosephus.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | 3 | public class FlaviusJosephus 4 | { 5 | public static void main (String[] args) 6 | throws IOException 7 | { 8 | File file = new File(args[0]); 9 | BufferedReader in = new BufferedReader(new FileReader(file)); 10 | String line; 11 | 12 | while ((line = in.readLine()) != null) { 13 | line = line.trim(); 14 | String[] split = line.split(","); 15 | 16 | int total = Integer.parseInt(split[0]); 17 | int next = Integer.parseInt(split[1]); 18 | 19 | boolean[] killed = new boolean[total]; 20 | 21 | int current = 0; 22 | for (int i = 0; i < total; i++) { 23 | int count = 0; 24 | 25 | while (true) { 26 | if (!killed[current]) { 27 | count++; 28 | 29 | if (count == next) { 30 | break; 31 | } 32 | } 33 | 34 | current++; 35 | current %= total; 36 | } 37 | 38 | killed[current] = true; 39 | System.out.print(current); 40 | 41 | if (i+1 != total) { 42 | System.out.print(" "); 43 | } 44 | } 45 | 46 | System.out.println(); 47 | } 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /1-moderate/flavius-josephus/input.txt: -------------------------------------------------------------------------------- 1 | 10,3 2 | 5,2 3 | -------------------------------------------------------------------------------- /1-moderate/game-of-life/input-small.txt: -------------------------------------------------------------------------------- 1 | .........* 2 | .*.*...*.. 3 | .......... 4 | ..*.*....* 5 | .*..*...*. 6 | .........* 7 | .......... 8 | .....*..*. 9 | .*....*... 10 | .....**... 11 | -------------------------------------------------------------------------------- /1-moderate/gray-code/input.txt: -------------------------------------------------------------------------------- 1 | 1111 | 1110 2 | 10 | 1100001 | 101 -------------------------------------------------------------------------------- /1-moderate/gray-code/main.py: -------------------------------------------------------------------------------- 1 | import sys 2 | 3 | 4 | def gray_to_binary(num): 5 | result = num 6 | num >>= 1 7 | while num != 0: 8 | result ^= num 9 | num >>= 1 10 | return result 11 | 12 | 13 | def main(input_filename): 14 | test_cases = open(input_filename, 'r') 15 | for test in test_cases: 16 | test = test.strip() 17 | if len(test) == 0: 18 | continue 19 | print(' | '.join([str(gray_to_binary(int(x.strip(), 2))) for x in test.split('|')])) 20 | test_cases.close() 21 | 22 | 23 | if __name__ == '__main__': 24 | if len(sys.argv) != 2: 25 | print("Usage: python %s " % sys.argv[0]) 26 | main(sys.argv[1]) 27 | -------------------------------------------------------------------------------- /1-moderate/gronsfeld-cipher/input.txt: -------------------------------------------------------------------------------- 1 | 31415;HYEMYDUMPS 2 | 45162;M%muxi%dncpqftiix" 3 | 14586214;Uix!&kotvx3 -------------------------------------------------------------------------------- /1-moderate/gronsfeld-cipher/main.py: -------------------------------------------------------------------------------- 1 | import sys 2 | 3 | VOCAB = ' !"#$%&\'()*+,-./0123456789:<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz' 4 | 5 | def main(): 6 | test_cases = open(sys.argv[1], 'r') 7 | for test in test_cases: 8 | test = test.strip() 9 | if len(test) == 0: continue 10 | shifts = [int(x) for x in list(test.split(';')[0])] 11 | enciphered = test.split(';')[1] 12 | result = [] 13 | for i in range(0, len(enciphered)): 14 | shift = shifts[i % len(shifts)] 15 | position = VOCAB.index(enciphered[i]) 16 | char = VOCAB[(position - shift) % len(VOCAB)] 17 | result.append(char) 18 | print(''.join(result)) 19 | test_cases.close() 20 | 21 | if __name__ == '__main__': 22 | main() 23 | -------------------------------------------------------------------------------- /1-moderate/guess-the-number/input.txt: -------------------------------------------------------------------------------- 1 | 100 Lower Lower Higher Lower Lower Lower Yay! 2 | 948 Higher Lower Yay! 3 | -------------------------------------------------------------------------------- /1-moderate/guess-the-number/main.py: -------------------------------------------------------------------------------- 1 | import sys 2 | import math 3 | 4 | class NumberGuesser: 5 | def __init__(self, lower_bound, upper_bound): 6 | self.lower_bound = lower_bound 7 | self.upper_bound = upper_bound 8 | def get_guess(self): 9 | return int(math.ceil((self.lower_bound + self.upper_bound) / 2.0)) 10 | def has_solutions(self): 11 | return self.lower_bound == self.upper_bound 12 | def lower(self): 13 | self.upper_bound = self.get_guess() - 1 14 | def higher(self): 15 | self.lower_bound = self.get_guess() + 1 16 | 17 | def main(): 18 | test_cases = open(sys.argv[1], 'r') 19 | for test in test_cases: 20 | test = test.strip() 21 | if len(test) == 0: continue 22 | upper_bound = int(test.split(' ' )[0]) 23 | number_guesser = NumberGuesser(0, upper_bound) 24 | guesses = test.split(' ')[1:-1] 25 | for guess in guesses: 26 | if number_guesser.has_solutions(): raise Exception() 27 | if guess == "Lower": number_guesser.lower() 28 | else: number_guesser.higher() 29 | print(number_guesser.get_guess()) 30 | test_cases.close() 31 | 32 | if __name__ == '__main__': 33 | main() 34 | -------------------------------------------------------------------------------- /1-moderate/interrupted-bubble-sort/input-large.txt: -------------------------------------------------------------------------------- 1 | 1 2 3 | 1000000000000 2 | 87 19 47 86 39 19 | 3 3 | 21 13 8 70 52 5 39 43 87 87 65 | 1 4 | 72 26 30 18 84 77 43 76 81 57 65 4 45 3 93 85 67 | 7 5 | 5 5 5 5 5 5 5 5 5 5 0 | 3 6 | 9 46 6 92 18 45 77 90 81 35 26 33 8 16 | 3 7 | 27 49 23 20 70 | 1 8 | 0 | 100 9 | 0 61 33 6 48 23 30 82 2 65 72 87 71 57 59 68 18 95 79 56 41 91 24 | 5 10 | 52 5 31 33 30 84 79 | 1 11 | 87 45 95 29 88 22 72 76 66 30 78 45 14 5 | 4 12 | 81 77 5 87 95 19 | 1 13 | 91 84 54 1 34 5 13 49 92 | 1 14 | 29 17 30 1 26 98 94 | 3 15 | 4 62 27 87 95 7 6 20 0 21 59 | 1 16 | 84 7 2 56 52 55 38 0 38 46 15 61 50 89 65 64 42 56 82 39 69 34 95 72 48 | 4 17 | 68 33 89 9 43 3 72 39 37 55 73 79 34 40 73 11 23 32 64 47 29 | 9 18 | 2 25 49 37 44 71 29 94 | 2 19 | 28 15 18 2 59 96 95 10 31 69 86 66 73 67 23 69 73 89 83 97 92 64 | 1 20 | 5 5 97 62 76 15 20 80 21 18 70 3 76 58 60 24 74 22 75 84 27 | 5 21 | -------------------------------------------------------------------------------- /1-moderate/interrupted-bubble-sort/input-small.txt: -------------------------------------------------------------------------------- 1 | 36 47 78 28 20 79 87 16 8 45 72 69 81 66 60 8 3 86 90 90 | 1 2 | 40 69 52 42 24 16 66 | 2 3 | 54 46 0 34 15 48 47 53 25 18 50 5 21 76 62 48 74 1 43 74 78 29 | 6 4 | 48 51 5 61 18 | 2 5 | 59 68 55 31 73 4 1 25 26 19 60 0 | 2 6 | -------------------------------------------------------------------------------- /1-moderate/interrupted-bubble-sort/main.py: -------------------------------------------------------------------------------- 1 | import sys 2 | 3 | def do_bubble_sort_n_times(l, n): 4 | n = min(n, len(l)-1) 5 | while n > 0: 6 | n -= 1 7 | for i in range(len(l)-1): 8 | if l[i] > l[i+1]: 9 | temp = l[i] 10 | l[i] = l[i+1] 11 | l[i+1] = temp 12 | return l 13 | 14 | def main(): 15 | test_cases = open(sys.argv[1], 'r') 16 | for test in test_cases: 17 | test = test.strip() 18 | if len(test) == 0: continue 19 | l = [int(x) for x in test.split(' | ')[0].split(' ')] 20 | n = int(test.split(' | ')[1]) 21 | result = do_bubble_sort_n_times(l, n) 22 | print(' '.join([str(x) for x in result])) 23 | test_cases.close() 24 | 25 | if __name__ == '__main__': 26 | main() 27 | -------------------------------------------------------------------------------- /1-moderate/justify-the-text/input.txt: -------------------------------------------------------------------------------- 1 | Hello, World! 2 | A A A A A AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA. 3 | The precise 50-digits value of Pi is 3.14159265358979323846264338327950288419716939937510. 4 | But he who would be a great man ought to regard, not himself or his interests, but what is just, whether the just act be his own or that of another. Next as to habitations. Such is the tradition. 5 | -------------------------------------------------------------------------------- /1-moderate/locks/input.txt: -------------------------------------------------------------------------------- 1 | 3 1 2 | 100 100 3 | -------------------------------------------------------------------------------- /1-moderate/locks/main.py: -------------------------------------------------------------------------------- 1 | import sys 2 | 3 | def do_lock_pass(locked): 4 | for i in range(1, len(locked), 2): 5 | locked[i] = True 6 | 7 | def do_flip_pass(locked): 8 | for i in range(2, len(locked), 3): 9 | locked[i] = not locked[i] 10 | 11 | def count_unlocked(locked): 12 | result = 0 13 | for l in locked: 14 | if not l: result += 1 15 | return result 16 | 17 | def main(): 18 | test_cases = open(sys.argv[1], 'r') 19 | for test in test_cases: 20 | test = test.strip() 21 | num_locks = int(test.split(' ')[0]) 22 | num_iterations = int(test.split(' ')[1]) 23 | locked = [False] * num_locks 24 | for i in range(num_iterations-1): 25 | do_lock_pass(locked) 26 | do_flip_pass(locked) 27 | locked[-1] = not locked[-1] 28 | print(count_unlocked(locked)) 29 | test_cases.close() 30 | 31 | main() 32 | -------------------------------------------------------------------------------- /1-moderate/longest-lines/LongestLines.java: -------------------------------------------------------------------------------- 1 | import java.io.IOException; 2 | import java.io.BufferedReader; 3 | import java.io.File; 4 | import java.io.FileReader; 5 | import java.util.TreeMap; 6 | 7 | public class LongestLines 8 | { 9 | public static void main (String[] args) 10 | throws IOException 11 | { 12 | File file = new File(args[0]); 13 | BufferedReader in = new BufferedReader(new FileReader(file)); 14 | String line; 15 | 16 | TreeMap tm = new TreeMap(); 17 | int n = Integer.parseInt(in.readLine()); 18 | 19 | while ((line = in.readLine()) != null) { 20 | line = line.trim(); 21 | 22 | if (line.length() > 0) { 23 | tm.put(line.length(), line); 24 | } 25 | } 26 | 27 | int counter = 0; 28 | for (Integer i : tm.descendingKeySet()) { 29 | line = tm.get(i); 30 | System.out.println(line); 31 | 32 | counter++; 33 | if (counter == n) { 34 | break; 35 | } 36 | } 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /1-moderate/longest-lines/input.txt: -------------------------------------------------------------------------------- 1 | 3 2 | Hello World 3 | 4 | CodeEval 5 | Quick Fox 6 | A 7 | San Francisco 8 | -------------------------------------------------------------------------------- /1-moderate/lost-in-translation/input.txt: -------------------------------------------------------------------------------- 1 | rbc vjnmkf kd yxyqci na rbc zjkfoscdd ew rbc ujllmcp 2 | tc rbkso rbyr ejp mysljylc kd kxveddknmc re jsicpdrysi 3 | de kr kd eoya kw aej icfkici re zjkr 4 | -------------------------------------------------------------------------------- /1-moderate/lost-in-translation/main.py: -------------------------------------------------------------------------------- 1 | import sys 2 | 3 | # Determined by the input/output samples. 4 | mapping = { 5 | 'a': 'y', 6 | 'b': 'h', 7 | 'c': 'e', 8 | 'd': 's', 9 | 'e': 'o', 10 | 'f': 'c', 11 | 'g': 'v', # guess 12 | 'h': 'x', # guess 13 | 'i': 'd', 14 | 'j': 'u', 15 | 'k': 'i', 16 | 'l': 'g', 17 | 'm': 'l', 18 | 'n': 'b', 19 | 'o': 'k', 20 | 'p': 'r', 21 | 'q': 'z', 22 | 'r': 't', 23 | 's': 'n', 24 | 't': 'w', 25 | 'u': 'j', 26 | 'v': 'p', 27 | 'w': 'f', 28 | 'x': 'm', 29 | 'y': 'a', 30 | 'z': 'q'} 31 | 32 | def get_mapping(char): 33 | if char == ' ': 34 | return ' ' 35 | else: 36 | return mapping[char] 37 | 38 | test_cases = open(sys.argv[1], 'r') 39 | for test in test_cases: 40 | test = test.strip() 41 | if len(test) == 0: 42 | continue 43 | 44 | output = "" 45 | for i in range(0, len(test)): 46 | output += get_mapping(test[i]) 47 | 48 | print(output) 49 | 50 | test_cases.close() 51 | -------------------------------------------------------------------------------- /1-moderate/lowest-common-ancestor/input.txt: -------------------------------------------------------------------------------- 1 | 8 52 2 | 3 29 3 | -------------------------------------------------------------------------------- /1-moderate/lucky-tickets/fast.py: -------------------------------------------------------------------------------- 1 | import sys 2 | import copy 3 | 4 | 5 | cache = {1: {0:1, 1:1, 2:1, 3:1, 4:1, 5:1, 6:1, 7:1, 8:1, 9:1}} 6 | 7 | 8 | def compute_sum_of_digits_map(digits): 9 | if digits in cache: 10 | return cache[digits] 11 | previous = compute_sum_of_digits_map(digits - 1) 12 | result = copy.deepcopy(previous) 13 | for i in range(1, 10): 14 | for key in previous.keys(): 15 | if key+i not in result: 16 | result[key+i] = 0 17 | result[key+i] += previous[key] 18 | cache[digits] = result 19 | return result 20 | 21 | 22 | def main(): 23 | test_cases = open(sys.argv[1], 'r') 24 | for test in test_cases: 25 | test = test.strip() 26 | if len(test) == 0: continue 27 | digits = int(test)//2 28 | sum_of_digits_map = compute_sum_of_digits_map(digits) 29 | print(sum([a*b for a,b in zip(sum_of_digits_map.values(), sum_of_digits_map.values())])) 30 | test_cases.close() 31 | 32 | 33 | if __name__ == '__main__': 34 | main() 35 | -------------------------------------------------------------------------------- /1-moderate/lucky-tickets/input.txt: -------------------------------------------------------------------------------- 1 | 2 2 | 4 3 | 6 4 | 8 5 | -------------------------------------------------------------------------------- /1-moderate/lucky-tickets/slow.py: -------------------------------------------------------------------------------- 1 | import sys 2 | 3 | 4 | def is_lucky(number): 5 | digits = [int(x) for x in list(str(number))] 6 | if len(digits) % 2 != 0: 7 | return False 8 | sum1 = sum(digits[0:len(digits)//2]) 9 | sum2 = sum(digits[len(digits)//2:]) 10 | return sum1 == sum2 11 | 12 | 13 | def main(): 14 | test_cases = open(sys.argv[1], 'r') 15 | for test in test_cases: 16 | test = test.strip() 17 | if len(test) == 0: continue 18 | length = int(test) 19 | result = 0 20 | for i in range(0, 10 ** length): 21 | result += is_lucky('0' * (length - len(str(i))) + str(i)) 22 | print(result) 23 | test_cases.close() 24 | 25 | 26 | if __name__ == '__main__': 27 | main() 28 | -------------------------------------------------------------------------------- /1-moderate/magic-numbers/input.txt: -------------------------------------------------------------------------------- 1 | 10 100 2 | 8382 8841 3 | -------------------------------------------------------------------------------- /1-moderate/minimum-coins/MinimumCoins.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | 3 | public class MinimumCoins 4 | { 5 | public static void main (String[] args) 6 | throws IOException 7 | { 8 | File file = new File(args[0]); 9 | BufferedReader in = new BufferedReader(new FileReader(file)); 10 | String line; 11 | 12 | while ((line = in.readLine()) != null) { 13 | int remaining = Integer.parseInt(line.trim()); 14 | 15 | int fives = remaining / 5; 16 | remaining -= fives * 5; 17 | 18 | int threes = remaining / 3; 19 | remaining -= threes * 3; 20 | 21 | int ones = remaining; 22 | 23 | System.out.println(fives + threes + ones); 24 | } 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /1-moderate/minimum-coins/input.txt: -------------------------------------------------------------------------------- 1 | 11 2 | 20 -------------------------------------------------------------------------------- /1-moderate/mth-to-last-element/input.txt: -------------------------------------------------------------------------------- 1 | m 4 2 | a b c d 4 3 | e f g h 2 4 | m 1 5 | -------------------------------------------------------------------------------- /1-moderate/number-of-ones/NumberOfOnes.java: -------------------------------------------------------------------------------- 1 | import java.io.BufferedReader; 2 | import java.io.File; 3 | import java.io.FileReader; 4 | import java.io.IOException; 5 | 6 | public class NumberOfOnes 7 | { 8 | public static void main (String[] args) 9 | throws IOException 10 | { 11 | File file = new File(args[0]); 12 | BufferedReader in = new BufferedReader(new FileReader(file)); 13 | String line; 14 | 15 | while ((line = in.readLine()) != null) { 16 | line = line.trim(); 17 | 18 | if (line.length() == 0) { 19 | continue; 20 | } 21 | 22 | int n = Integer.parseInt(line); 23 | int numberOfOnes = countNumberOfOneBits(n); 24 | 25 | System.out.println(numberOfOnes); 26 | } 27 | 28 | in.close(); 29 | } 30 | 31 | public static int countNumberOfOneBits(int n) 32 | { 33 | int count = 0; 34 | 35 | while (n != 0) { 36 | int lastBit = n & 0x1; 37 | 38 | if (lastBit != 0) { 39 | count++; 40 | } 41 | 42 | n >>= 1; 43 | } 44 | 45 | return count; 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /1-moderate/number-of-ones/input.txt: -------------------------------------------------------------------------------- 1 | 10 2 | 22 3 | 56 4 | -------------------------------------------------------------------------------- /1-moderate/number-of-ones/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | int countNumberOfOneBits(unsigned int n) 6 | { 7 | unsigned int count = 0; 8 | 9 | while (n != 0) { 10 | unsigned int lastBit = n & 0x1; 11 | 12 | if (lastBit != 0) { 13 | count++; 14 | } 15 | 16 | n >>= 1; 17 | } 18 | 19 | return count; 20 | } 21 | 22 | int main(int argc, char** argv) 23 | { 24 | std::ifstream file(argv[1]); 25 | 26 | std::string line; 27 | while (std::getline(file, line)) 28 | { 29 | int x; 30 | std::stringstream ss(line); 31 | ss >> x; 32 | 33 | std::cout << countNumberOfOneBits(x) << std::endl; 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /1-moderate/number-operations/input.txt: -------------------------------------------------------------------------------- 1 | 60 1 3 5 20 2 | 44 6 1 49 47 3 | 34 1 49 2 21 4 | 31 38 27 51 18 5 | -------------------------------------------------------------------------------- /1-moderate/number-pairs/input.txt: -------------------------------------------------------------------------------- 1 | 1,2,3,4,6;5 2 | 2,4,5,6,9,11,15;20 3 | 1,2,3,4;50 4 | 1,2,3,4;50 5 | 1,2,2,3,4;3 6 | 100,200,300,400,600;800 7 | -------------------------------------------------------------------------------- /1-moderate/organizational-hierarchy/input.txt: -------------------------------------------------------------------------------- 1 | ab | ae | bc 2 | ab | bc | cd | ae | cx | xz 3 | -------------------------------------------------------------------------------- /1-moderate/overlapping-rectangles/input.txt: -------------------------------------------------------------------------------- 1 | -3,3,-1,1,1,-1,3,-3 2 | -3,3,-1,1,-2,4,2,2 3 | 0,1,1,0,-1,0,0,-1 4 | -------------------------------------------------------------------------------- /1-moderate/pangrams/input.txt: -------------------------------------------------------------------------------- 1 | A quick brown fox jumps over the lazy dog 2 | A slow yellow fox crawls under the proactive dog 3 | Test 4 | -------------------------------------------------------------------------------- /1-moderate/pascals-triangle/input.txt: -------------------------------------------------------------------------------- 1 | 6 2 | 1 3 | 4 4 | 2 5 | 0 6 | -------------------------------------------------------------------------------- /1-moderate/point-in-circle/input.txt: -------------------------------------------------------------------------------- 1 | Center: (0, 0); Radius: 10; Point: (5, 0) 2 | Center: (2.12, -3.48); Radius: 17.22; Point: (16.21, -5) 3 | Center: (5.05, -11); Radius: 21.2; Point: (-31, -45) 4 | Center: (-9.86, 1.95); Radius: 47.28; Point: (6.03, -6.42) 5 | -------------------------------------------------------------------------------- /1-moderate/predict-the-number/input.txt: -------------------------------------------------------------------------------- 1 | 0 2 | 5 3 | 101 4 | 25684 5 | -------------------------------------------------------------------------------- /1-moderate/predict-the-number/main.py: -------------------------------------------------------------------------------- 1 | import sys 2 | 3 | test_cases = open(sys.argv[1], 'r') 4 | for test in test_cases: 5 | test = test.strip() 6 | if len(test) == 0: continue 7 | binary = "{0:b}".format(int(test)) 8 | total = 0 9 | for b in binary: 10 | if b == "1": 11 | total += 1 12 | print(total % 3) 13 | 14 | test_cases.close() 15 | -------------------------------------------------------------------------------- /1-moderate/prime-numbers/input.txt: -------------------------------------------------------------------------------- 1 | 10 2 | 20 3 | 100 4 | -------------------------------------------------------------------------------- /1-moderate/remove-characters/RemoveCharacters.java: -------------------------------------------------------------------------------- 1 | import java.io.BufferedReader; 2 | import java.io.File; 3 | import java.io.FileReader; 4 | import java.io.IOException; 5 | 6 | public class RemoveCharacters 7 | { 8 | public static void main (String[] args) 9 | throws IOException 10 | { 11 | File file = new File(args[0]); 12 | BufferedReader in = new BufferedReader(new FileReader(file)); 13 | String line; 14 | 15 | while ((line = in.readLine()) != null) { 16 | line = line.trim(); 17 | 18 | if (line.length() == 0) { 19 | continue; 20 | } 21 | 22 | String[] lineArray = line.split(","); 23 | 24 | String sentence = lineArray[0].trim(); 25 | String charsToRemove = lineArray[1].trim(); 26 | 27 | System.out.println(removeSpecificCharacters(sentence, charsToRemove)); 28 | } 29 | 30 | in.close(); 31 | } 32 | 33 | public static String removeSpecificCharacters(String sentence, String charsToRemove) 34 | { 35 | StringBuilder sb = new StringBuilder(); 36 | 37 | for (char c : sentence.toCharArray()) { 38 | if (!charsToRemove.contains(String.valueOf(c))) { 39 | sb.append(c); 40 | } 41 | } 42 | 43 | return sb.toString(); 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /1-moderate/remove-characters/input.txt: -------------------------------------------------------------------------------- 1 | how are you, abc 2 | hello world, def 3 | -------------------------------------------------------------------------------- /1-moderate/remove-characters/main.py: -------------------------------------------------------------------------------- 1 | import sys 2 | 3 | def main(): 4 | test_cases = open(sys.argv[1], 'r') 5 | for test in test_cases: 6 | test = test.strip() 7 | if len(test) == 0: 8 | continue 9 | words = test.split(',')[0].strip() 10 | chars = test.split(',')[1].strip() 11 | for c in chars: 12 | words = words.replace(c, '') 13 | print(words) 14 | test_cases.close() 15 | 16 | if __name__ == '__main__': 17 | main() 18 | -------------------------------------------------------------------------------- /1-moderate/reverse-and-add/input.txt: -------------------------------------------------------------------------------- 1 | 195 2 | -------------------------------------------------------------------------------- /1-moderate/reverse-and-add/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | unsigned int reverseDigits(unsigned int num) 6 | { 7 | unsigned int rev = 0; 8 | while (num > 0) 9 | { 10 | unsigned int dig = num % 10; 11 | rev = rev * 10 + dig; 12 | num /= 10; 13 | } 14 | 15 | return rev; 16 | } 17 | 18 | int main(int argc, char** argv) 19 | { 20 | std::ifstream file(argv[1]); 21 | 22 | std::string line; 23 | while (std::getline(file, line)) 24 | { 25 | unsigned int x; 26 | std::stringstream ss(line); 27 | ss >> x; 28 | 29 | unsigned int iterations = 0; 30 | while (true) { 31 | unsigned int reverse = reverseDigits(x); 32 | if (reverse == x) { 33 | std::cout << iterations << " " << x << std::endl; 34 | break; 35 | } 36 | 37 | iterations++; 38 | x = x + reverse; 39 | } 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /1-moderate/reverse-groups/input.txt: -------------------------------------------------------------------------------- 1 | 1,2,3,4,5;2 2 | 1,2,3,4,5;3 -------------------------------------------------------------------------------- /1-moderate/robo-and-robitta/input.txt: -------------------------------------------------------------------------------- 1 | 3x2 | 2 1 2 | 4x4 | 3 3 3 | -------------------------------------------------------------------------------- /1-moderate/robo-and-robitta/main.py: -------------------------------------------------------------------------------- 1 | import re 2 | import sys 3 | 4 | 5 | def walk(m, n, x, y): 6 | c = 0 7 | while n > y: 8 | c, m, n, x, y = c + m, n - 1, m, n - y, x 9 | return c + x 10 | 11 | 12 | with open(sys.argv[1], 'r') as test_cases: 13 | for test in test_cases: 14 | print(walk(*map(int, re.findall(r'\d+', test)))) 15 | -------------------------------------------------------------------------------- /1-moderate/roman-and-arabic/input.txt: -------------------------------------------------------------------------------- 1 | 3M1D2C 2 | 3X2I4X 3 | 2I3I2X9V1X 4 | -------------------------------------------------------------------------------- /1-moderate/roman-and-arabic/main.py: -------------------------------------------------------------------------------- 1 | import sys 2 | 3 | def compute_ar_value(ar): 4 | assert len(ar) == 2 5 | a = int(ar[0]) 6 | r = ar[1] 7 | r = { 8 | 'I': 1, 9 | 'V': 5, 10 | 'X': 10, 11 | 'L': 50, 12 | 'C': 100, 13 | 'D': 500, 14 | 'M': 1000, 15 | }[r] 16 | return a * r 17 | 18 | def compute_aromatic_value(aromatic): 19 | values = [] 20 | for ar in aromatic: values.append(compute_ar_value(ar)) 21 | result = 0 22 | for i in range(0, len(aromatic)-1): 23 | if compute_ar_value("1" + aromatic[i][1]) < compute_ar_value("1" + aromatic[i+1][1]): 24 | result -= values[i] 25 | else: 26 | result += values[i] 27 | return result + values[-1] 28 | 29 | test_cases = open(sys.argv[1], 'r') 30 | for test in test_cases: 31 | test = test.strip() 32 | if len(test) == 0: 33 | continue 34 | print(compute_aromatic_value([test[i:i+2] for i in range(0, len(test), 2)])) 35 | 36 | test_cases.close() 37 | -------------------------------------------------------------------------------- /1-moderate/sequence-transformation/main.py: -------------------------------------------------------------------------------- 1 | import sys 2 | 3 | def matches(pattern, match): 4 | if pattern == '0': 5 | return 'B' not in match 6 | if pattern == '1': 7 | a = 'A' in match 8 | b = 'B' in match 9 | return not (a and b) 10 | else: 11 | raise Exception('pattern too long for matching function') 12 | 13 | def check_matching(pattern, match, matching_matrix, row=0, col=0): 14 | # We already have a solution. 15 | if matching_matrix[-1][-1]: return 16 | # Index out of bounds. 17 | if row >= len(matching_matrix) or col >= len(matching_matrix[row]): return 18 | # This element has already been checked. 19 | if matching_matrix[row][col]: return 20 | for c in range(col, len(match)): 21 | matching_matrix[row][c] = False 22 | if matches(pattern[row], match[col:c+1]): 23 | matching_matrix[row][c] = True 24 | check_matching(pattern, match, matching_matrix, row+1, c+1) 25 | 26 | def main(): 27 | test_cases = open(sys.argv[1], 'r') 28 | for test in test_cases: 29 | pattern, match = test.strip().split(' ') 30 | lm, lp = len(match), len(pattern) 31 | matching_matrix = [[None for m in range(lm)] for p in range(lp)] 32 | check_matching(pattern, match, matching_matrix) 33 | print('Yes' if matching_matrix[-1][-1] else 'No') 34 | test_cases.close() 35 | 36 | if __name__ == '__main__': 37 | main() 38 | -------------------------------------------------------------------------------- /1-moderate/sort-martix-columns/input.txt: -------------------------------------------------------------------------------- 1 | -3 29 -3 | -17 69 -17 | 44 3 8 2 | 25 39 -26 -21 | -81 -98 -91 27 | 32 -87 67 98 | -90 -79 18 9 3 | 26 -10 39 | -62 66 97 | 22 85 36 4 | -------------------------------------------------------------------------------- /1-moderate/sort-martix-columns/main.py: -------------------------------------------------------------------------------- 1 | import sys 2 | 3 | 4 | def row_matrix_to_col_matrix(row_matrix): 5 | col_matrix = [] 6 | for c in range(len(row_matrix)): col_matrix.append([]) 7 | for r in range(len(row_matrix)): 8 | for c in range(len(row_matrix)): 9 | col_matrix[c].append(row_matrix[r][c]) 10 | return col_matrix 11 | 12 | 13 | def col_matrix_to_row_matrix(col_matrix): 14 | row_matrix = [] 15 | for r in range(len(col_matrix)): row_matrix.append([]) 16 | for c in range(len(col_matrix)): 17 | for r in range(len(col_matrix)): 18 | row_matrix[r].append(col_matrix[c][r]) 19 | return row_matrix 20 | 21 | 22 | def matrix_compare(a, b): 23 | length = min(len(a), len(b)) 24 | index = 0 25 | while index < length: 26 | if a[index] != b[index]: 27 | return a[index] - b[index] 28 | index += 1 29 | return 0 30 | 31 | 32 | def main(): 33 | test_cases = open(sys.argv[1], 'r') 34 | for test in test_cases: 35 | test = test.strip() 36 | if len(test) == 0: continue 37 | m = row_matrix_to_col_matrix([[int(i) for i in s.strip().split(' ')] for s in test.split('|')]) 38 | print(' | '.join([' '.join([str(e) for e in r]) for r in col_matrix_to_row_matrix(sorted(m, cmp=matrix_compare))])) 39 | test_cases.close() 40 | 41 | 42 | if __name__ == '__main__': 43 | main() 44 | -------------------------------------------------------------------------------- /1-moderate/stack-implementation/input.txt: -------------------------------------------------------------------------------- 1 | 1 2 3 4 2 | 10 -2 3 4 3 | -------------------------------------------------------------------------------- /1-moderate/string-rotation/input.txt: -------------------------------------------------------------------------------- 1 | Hello,lloHe 2 | Hello,olleH 3 | Basefont,tBasefon 4 | -------------------------------------------------------------------------------- /1-moderate/sudoku/input.txt: -------------------------------------------------------------------------------- 1 | 4;1,4,2,3,2,3,1,4,4,2,3,1,3,1,4,2 2 | 4;2,1,3,2,3,2,1,4,1,4,2,3,2,3,4,1 3 | -------------------------------------------------------------------------------- /1-moderate/suggest-groups/input.txt: -------------------------------------------------------------------------------- 1 | Amira:Isaura,Lizzie,Madalyn,Margarito,Shakira,Un:Driving,Mineral collecting 2 | Elliot:Isaura,Madalyn,Margarito,Shakira:Juggling,Mineral collecting 3 | Isaura:Amira,Elliot,Lizzie,Margarito,Verla,Wilford:Juggling 4 | Lizzie:Amira,Isaura,Verla:Driving,Mineral collecting,Rugby 5 | Madalyn:Amira,Elliot,Margarito,Verla:Driving,Mineral collecting,Rugby 6 | Margarito:Amira,Elliot,Isaura,Madalyn,Un,Verla:Mineral collecting 7 | Shakira:Amira,Elliot,Verla,Wilford:Mineral collecting 8 | Un:Amira,Margarito,Wilford: 9 | Verla:Isaura,Lizzie,Madalyn,Margarito,Shakira:Driving,Juggling,Mineral collecting 10 | Wilford:Isaura,Shakira,Un:Driving 11 | -------------------------------------------------------------------------------- /1-moderate/suggest-groups/main.py: -------------------------------------------------------------------------------- 1 | import sys 2 | import collections 3 | 4 | def suggested_groups_for_user(user, user_to_friends, group_to_members): 5 | result = [] 6 | for group in group_to_members.keys(): 7 | count = 0 8 | if user in group_to_members[group]: 9 | continue 10 | for friend in user_to_friends[user]: 11 | if friend in group_to_members[group]: 12 | count += 1 13 | if (count * 2) >= len(user_to_friends[user]): 14 | result.append(group) 15 | return result 16 | 17 | def main(): 18 | test_cases = open(sys.argv[1], 'r') 19 | user_to_friends = {} 20 | group_to_members = collections.defaultdict(list) 21 | for test in test_cases: 22 | test = test.strip().split(':') 23 | user = test[0] 24 | friends = test[1].split(',') 25 | groups = [] if len(test[2]) == 0 else test[2].split(',') 26 | user_to_friends[user] = friends 27 | for group in groups: 28 | group_to_members[group].append(user) 29 | test_cases.close() 30 | 31 | for user in sorted(user_to_friends.keys()): 32 | suggested_groups = suggested_groups_for_user(user, user_to_friends, group_to_members) 33 | if len(suggested_groups) >= 1: 34 | print("%s:%s" % (user, ','.join(sorted(suggested_groups)))) 35 | 36 | if __name__ == '__main__': 37 | main 38 | -------------------------------------------------------------------------------- /1-moderate/sum-of-integers/input.txt: -------------------------------------------------------------------------------- 1 | -10, 2, 3, -2, 0, 5, -15 2 | 2,3,-2,-1,10 3 | -------------------------------------------------------------------------------- /1-moderate/sum-to-zero/input.txt: -------------------------------------------------------------------------------- 1 | -23,-15,66,-91,21,32,-70,17,17,-55,-51,19,-56,-37,59,45,6,58 2 | 22,34,81,-36,90,35,-89,-54,-89,-76,3,4,16,-94,-82,-14,-73,-51,4 3 | -67,89,47,32,-67,-70,95,-74,18,-22,76,26,74,90,-69,2 4 | 94,40,44,10,-92,36,44,39,-14,46,-81,21,94,-47,13 5 | 56,-76,31,-30,-90,65,17,50,-80,-97,70,-58,-100 6 | 4,65,-5,-97,-37,-22,67,44,32,19,39,-39,32,1,-32,96,-38,4 7 | -11,14,24,-64,19,19,-38,70,-50,-31,52,89,-30,-32,39 8 | 97,100,51,-73,-23,89,-64,-50,-6,4,-68,69,-37,31,-61,-89 9 | 42,-19,69,72,-27,26,-15,35,-84,9,66,91,-41,66,-87,-41,-55 10 | 95,-91,47,-1,37,1,86,-87,64,91,-14,66,27 11 | -100,-43,100,92,-97,-11,28,79,92,-40,84,-19,27 12 | -90,-59,-79,24,27,-92,-81,28,-79 13 | -29,99,-62,-9,-65,6,41,-36,-4,-87,-99 14 | 87,-81,-82,88,6,51,-5,-74,-97,-17,5,10,47,-82,91,55,-57 15 | -2,43,70,-85,-60,-38,-39,-93,-42,-20,-18,-21,17,64 16 | -28,-48,41,20,-29,-74,54,4,97,58,-21,-27,100,57 17 | -20,16,46,-2,-53,32,44,-40,53,-78,65,88,-76,5 18 | -9,8,84,66,-26,87,99,74,-99,18,-70,75,15,-19 19 | -40,90,59,72,-60,-31,36,81,86,-45,-15,-87,12,84,-63,-35 20 | -100,26,20,-14,-31,-7,-87,-38,-25,-61,16,-64,43,-7,11,84,-34,-23,-84 21 | 2,3,1,0,-4,-1 22 | 0,-1,3,-2 23 | -------------------------------------------------------------------------------- /1-moderate/sum-to-zero/main.py: -------------------------------------------------------------------------------- 1 | import sys 2 | import itertools 3 | 4 | test_cases = open(sys.argv[1], 'r') 5 | for test in test_cases: 6 | test = test.strip() 7 | if len(test) == 0: 8 | continue 9 | values = map(int, test.split(',')) 10 | combinations = [list(x) for x in itertools.combinations(values, 4)] 11 | result = 0 12 | for c in combinations: 13 | total = 0 14 | for i in c: 15 | total += i 16 | if total == 0: 17 | result += 1 18 | print(result) 19 | 20 | test_cases.close() 21 | -------------------------------------------------------------------------------- /1-moderate/the-ministry-of-truth/main.py: -------------------------------------------------------------------------------- 1 | import sys 2 | import re 3 | 4 | def render_word(a, b): 5 | result = ['_'] * len(b) 6 | found = b.find(a) 7 | for i in range(found, found + len(a)): result[i] = a[i-found] 8 | return "".join(result) 9 | 10 | test_cases = open(sys.argv[1], 'r') 11 | for test in test_cases: 12 | test = test.strip() 13 | if len(test) == 0: 14 | continue 15 | 16 | utterance1 = re.sub(' +', ' ', test.split(';')[1]).split(' ') 17 | utterance2 = re.sub(' +', ' ', test.split(';')[0]).split(' ') 18 | 19 | index = 0 20 | result = [] 21 | success = False 22 | 23 | for a in range(len(utterance1)): 24 | wA = utterance1[a] 25 | 26 | if index == len(utterance2): 27 | success = False 28 | break 29 | 30 | for b in range(index, len(utterance2)): 31 | wB = utterance2[b] 32 | index = b+1 33 | if wA in wB: 34 | success = True 35 | result.append(render_word(wA, wB)) 36 | break 37 | else: 38 | result.append('_' * len(wB)) 39 | 40 | if not success: 41 | print('I cannot fix history') 42 | continue 43 | 44 | result_length = len(result) 45 | if result_length != len(utterance2): 46 | for i in range(result_length, len(utterance2)): 47 | result.append('_' * len(utterance2[i])) 48 | 49 | print(" ".join(result)) 50 | 51 | test_cases.close() 52 | -------------------------------------------------------------------------------- /1-moderate/to-pi-or-not-to-pi/input.txt: -------------------------------------------------------------------------------- 1 | 3 2 | 1 3 | 1654 4 | -------------------------------------------------------------------------------- /1-moderate/to-pi-or-not-to-pi/main1.py: -------------------------------------------------------------------------------- 1 | import sys 2 | 3 | 4 | # http://stackoverflow.com/questions/9004789/1000-digits-of-pi-in-python 5 | def make_pi(): 6 | q, r, t, k, m, x = 1, 0, 1, 1, 3, 3 7 | for j in range(21604): # experimentation to determine the loop value needed for 5k digits. 8 | if 4 * q + r - t < m * t: 9 | yield m 10 | q, r, t, k, m, x = 10 * q, 10 * (r - m * t), t, k, (10 * (3 * q + r)) // t - 10 * m, x 11 | else: 12 | q, r, t, k, m, x = q * k, (2 * q + r) * x, t * x, k + 1, (q * (7 * k + 2) + r * x) // (t * x), x + 2 13 | 14 | 15 | def main(): 16 | test_cases = open(sys.argv[1], 'r') 17 | digits = [] 18 | for i in make_pi(): 19 | digits.append(str(i)) 20 | for test in test_cases: 21 | test = test.strip() 22 | if len(test) == 0: continue 23 | print(digits[int(test)-1]) 24 | test_cases.close() 25 | 26 | 27 | if __name__ == '__main__': 28 | main() 29 | -------------------------------------------------------------------------------- /1-moderate/trailing-string/TrailingString.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | 3 | public class TrailingString 4 | { 5 | public static void main (String[] args) 6 | throws IOException 7 | { 8 | File file = new File(args[0]); 9 | BufferedReader in = new BufferedReader(new FileReader(file)); 10 | String line; 11 | 12 | while ((line = in.readLine()) != null) { 13 | line = line.trim(); 14 | 15 | if (line.length() == 0) { 16 | continue; 17 | } 18 | 19 | String[] stringArray = line.split(","); 20 | 21 | String s1 = stringArray[0].trim(); 22 | String s2 = stringArray[1].trim(); 23 | 24 | if (s1.endsWith(s2)) { 25 | System.out.println(1); 26 | } else { 27 | System.out.println(0); 28 | } 29 | } 30 | 31 | in.close(); 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /1-moderate/trailing-string/input.txt: -------------------------------------------------------------------------------- 1 | Hello World,World 2 | Hello CodeEval,CodeEval 3 | San Francisco,San Jose -------------------------------------------------------------------------------- /1-moderate/try-to-solve-it/input.txt: -------------------------------------------------------------------------------- 1 | mke 2 | mh 3 | lhsby 4 | pm 5 | -------------------------------------------------------------------------------- /1-moderate/try-to-solve-it/main.py: -------------------------------------------------------------------------------- 1 | import sys 2 | 3 | 4 | l1 = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm'] 5 | l2 = ['u', 'v', 'w', 'x', 'y', 'z', 'n', 'o', 'p', 'q', 'r', 's', 't'] 6 | 7 | 8 | def decode(s): 9 | result = [] 10 | for char in s: 11 | if char in l1: 12 | result.append(l2[l1.index(char)]) 13 | else: 14 | result.append(l1[l2.index(char)]) 15 | return ''.join(result) 16 | 17 | 18 | def main(input_filename): 19 | test_cases = open(input_filename, 'r') 20 | for test in test_cases: 21 | test = test.strip() 22 | if len(test) == 0: 23 | continue 24 | print(decode(test)) 25 | test_cases.close() 26 | 27 | 28 | if __name__ == '__main__': 29 | if len(sys.argv) != 2: 30 | print("Usage: python %s " % sys.argv[0]) 31 | sys.exit(1) 32 | main(sys.argv[1]) -------------------------------------------------------------------------------- /1-moderate/twenty-forty-eight/input.txt: -------------------------------------------------------------------------------- 1 | LEFT; 4; 2 2 0 0|0 0 0 0|0 0 0 0|0 0 0 0 2 | LEFT; 4; 2 2 2 2|0 0 0 0|0 0 0 0|0 0 0 0 3 | RIGHT; 4; 4 0 2 0|0 0 0 8|4 0 2 4|2 4 2 2 4 | UP; 4; 2 0 2 0|0 2 0 4|2 8 0 8|0 8 0 16 5 | -------------------------------------------------------------------------------- /1-moderate/uri-comparison/main.py: -------------------------------------------------------------------------------- 1 | import sys 2 | import re 3 | 4 | def normalize(uri): 5 | while True: 6 | match = re.search('%([0-9a-fA-F][0-9a-fA-F])', uri) 7 | 8 | if match is not None: 9 | old_value = match.group(0) 10 | new_value = bytes.fromhex(match.group(1)).decode('utf-8') 11 | uri = uri.replace(old_value, new_value) 12 | else: 13 | break 14 | 15 | return uri 16 | 17 | test_cases = open(sys.argv[1], 'r') 18 | for test in test_cases: 19 | test = test.strip() 20 | if len(test) == 0: 21 | continue 22 | 23 | uri1 = normalize(test.split(';')[0]) 24 | uri2 = normalize(test.split(';')[1]) 25 | 26 | match = "^([a-zA-Z]+)://([a-zA-Z0-9.]+):?([0-9]+)?(.*)" 27 | 28 | m1 = re.match(match, uri1) 29 | m2 = re.match(match, uri2) 30 | 31 | if m1 is None or m2 is None: 32 | print('False') 33 | continue 34 | 35 | scheme1 = m1.group(1).lower() 36 | scheme2 = m2.group(1).lower() 37 | 38 | host1 = m1.group(2).lower() 39 | host2 = m2.group(2).lower() 40 | 41 | port1 = 80 if m1.group(3) is None else int(m1.group(3)) 42 | port2 = 80 if m2.group(3) is None else int(m2.group(3)) 43 | 44 | path1 = m1.group(4).lower() 45 | path2 = m2.group(4).lower() 46 | 47 | if scheme1 == scheme2 and host1 == host2 and port1 == port2 and path1 == path2: 48 | print('True') 49 | else: 50 | print('False') 51 | 52 | test_cases.close() 53 | -------------------------------------------------------------------------------- /1-moderate/valid-parentheses/input.txt: -------------------------------------------------------------------------------- 1 | () 2 | ([)] 3 | -------------------------------------------------------------------------------- /1-moderate/word-chain/input.txt: -------------------------------------------------------------------------------- 1 | soup,sugar,peas,rice 2 | ljhqi,nrtxgiu,jdtphez,wosqm 3 | cjz,tojiv,sgxf,awonm,fcv 4 | ab,bc,cd,da 5 | ab,bc,ca,xx,yy,zz 6 | aa,aa,bb 7 | aa,aa,ab 8 | ab,ba,ab,ba,ca 9 | -------------------------------------------------------------------------------- /2-hard/ascii-decryption/input.txt: -------------------------------------------------------------------------------- 1 | 5 | s | 92 112 109 40 118 109 109 108 123 40 119 110 40 124 112 109 40 117 105 118 129 40 119 125 124 127 109 113 111 112 40 124 112 109 40 118 109 109 108 123 40 119 110 40 124 112 109 40 110 109 127 54 40 53 40 91 120 119 107 115 2 | -------------------------------------------------------------------------------- /2-hard/bay-bridges/input-large.txt: -------------------------------------------------------------------------------- 1 | 1: ([37.440670, -122.341153], [37.658523, -122.104640]) 2 | 2: ([37.668876, -122.323858], [37.804145, -122.345826]) 3 | 3: ([37.812087, -122.068213], [37.771135, -122.187455]) 4 | 4: ([37.668035, -122.094081], [37.448828, -122.223481]) 5 | 5: ([37.445064, -122.087933], [37.694296, -122.023346]) 6 | 6: ([37.730456, -122.230137], [37.626026, -122.230225]) 7 | 7: ([37.534572, -122.267887], [37.423667, -122.216782]) 8 | 8: ([37.692908, -122.355374], [37.620972, -122.338940]) 9 | 9: ([37.732106, -122.165779], [37.700649, -122.083635]) 10 | 10: ([37.453614, -122.193160], [37.498217, -122.163962]) 11 | 11: ([37.643839, -122.280068], [37.776350, -122.269080]) 12 | 12: ([37.709196, -122.185853], [37.572855, -122.054807]) 13 | 13: ([37.712216, -122.162425], [37.821736, -122.209027]) 14 | 14: ([37.621559, -122.261420], [37.604423, -122.133352]) 15 | 15: ([37.440595, -122.233243], [37.693173, -122.068166]) 16 | -------------------------------------------------------------------------------- /2-hard/bay-bridges/input-small.txt: -------------------------------------------------------------------------------- 1 | 1: ([37.788353, -122.387695], [37.829853, -122.294312]) 2 | 2: ([37.429615, -122.087631], [37.487391, -122.018967]) 3 | 3: ([37.474858, -122.131577], [37.529332, -122.056046]) 4 | 4: ([37.532599,-122.218094], [37.615863,-122.097244]) 5 | 5: ([37.516262,-122.198181], [37.653383,-122.151489]) 6 | 6: ([37.504824,-122.181702], [37.633266,-122.121964]) 7 | -------------------------------------------------------------------------------- /2-hard/climbing-stairs/input.txt: -------------------------------------------------------------------------------- 1 | 10 2 | 20 3 | 516 4 | 607 5 | 641 6 | 10 7 | 134 8 | 91 9 | 874 10 | 250 11 | 237 12 | 760 13 | 890 14 | 502 15 | 266 16 | 294 17 | 274 18 | 999 19 | 523 20 | 292 21 | 847 22 | 511 23 | -------------------------------------------------------------------------------- /2-hard/climbing-stairs/main.py: -------------------------------------------------------------------------------- 1 | import sys 2 | 3 | ways = {1: 1, 2: 2} 4 | def count_ways_to_climb(stairs): 5 | if stairs in ways: 6 | return ways[stairs] 7 | result = count_ways_to_climb(stairs-1) + count_ways_to_climb(stairs-2) 8 | ways[stairs] = result 9 | return result 10 | 11 | test_cases = open(sys.argv[1], 'r') 12 | for test in test_cases: 13 | test = test.strip() 14 | if len(test) == 0: 15 | continue 16 | print(count_ways_to_climb(int(test))) 17 | 18 | test_cases.close() 19 | -------------------------------------------------------------------------------- /2-hard/closest-pair/input.txt: -------------------------------------------------------------------------------- 1 | 5 2 | 0 2 3 | 6 67 4 | 43 71 5 | 39 107 6 | 189 140 7 | 0 8 | -------------------------------------------------------------------------------- /2-hard/cluster-detection/input-small.txt: -------------------------------------------------------------------------------- 1 | Thu Dec 11 17:53:01 PST 2008 a@facebook.com b@facebook.com 2 | Thu Dec 11 17:53:02 PST 2008 b@facebook.com a@facebook.com 3 | Thu Dec 11 17:53:03 PST 2008 a@facebook.com c@facebook.com 4 | Thu Dec 11 17:53:04 PST 2008 c@facebook.com a@facebook.com 5 | Thu Dec 11 17:53:05 PST 2008 b@facebook.com c@facebook.com 6 | Thu Dec 11 17:53:06 PST 2008 c@facebook.com b@facebook.com 7 | Thu Dec 11 17:53:07 PST 2008 d@facebook.com e@facebook.com 8 | Thu Dec 11 17:53:08 PST 2008 e@facebook.com d@facebook.com 9 | Thu Dec 11 17:53:09 PST 2008 d@facebook.com f@facebook.com 10 | Thu Dec 11 17:53:10 PST 2008 f@facebook.com d@facebook.com 11 | Thu Dec 11 17:53:11 PST 2008 e@facebook.com f@facebook.com 12 | Thu Dec 11 17:53:12 PST 2008 f@facebook.com e@facebook.com 13 | -------------------------------------------------------------------------------- /2-hard/commuting-engineer/README.txt: -------------------------------------------------------------------------------- 1 | The solution in C++ works best here. 2 | 3 | The others work but are too slow or consume too much memory. 4 | 5 | The algorithm in each implementation is approximately the same. 6 | -------------------------------------------------------------------------------- /2-hard/commuting-engineer/input.txt: -------------------------------------------------------------------------------- 1 | 1 | CodeEval 1355 Market St, SF (37.7768016, -122.4169151) 2 | 2 | Yelp 706 Mission St, SF (37.7860105, -122.4025377) 3 | 3 | Square 110 5th St, SF (37.7821494, -122.4058960) 4 | 4 | Airbnb 99 Rhode Island St, SF (37.7689269, -122.4029053) 5 | 5 | Dropbox 185 Berry St, SF (37.7768800, -122.3911496) 6 | 6 | Zynga 699 8th St, SF (37.7706628, -122.4040139) 7 | -------------------------------------------------------------------------------- /2-hard/computer-terminal/input-large.txt: -------------------------------------------------------------------------------- 1 | ^e^he^dDEp^bwgy 2 | ^i^er^lPwmL^ol^lCc 3 | Mxess^oG^r^i^r^r^60^h^rXP 4 | fB^35^47vxxOj^b^92zO 5 | ^c^r^oN^bY^u^e^oEqN^^ 6 | ^ozU^i^^^oT^12^44W^51Wm^b^l^b^b^10^39 7 | EEll^cgF^^DTUoya^uZcSp 8 | ^bXmD^ujmRtHBQHf^r 9 | R^62^h^hvartW^o^hvZ^i^ufrv^i^l 10 | LEXeF^oH^^MRB^^ 11 | ^h^c 12 | ^04^^ 13 | ^13/ \^d^b / \ 14 | ^u^d^d^l^l^l^l^l^l^l^l^l 15 | ^r^r^l^l^d^l^l^d/^b \ 16 | ^d^r^r^66/^b \ 17 | ^b^d \ / 18 | ^d^l^lv^d^b===========^i^94O12345 19 | 6789^94A=======^u^u^u^u^u^u^l^l\^o^b^r/ 20 | ^h+^d|^d|^d|^d^r^d|^d|^d|^d|^d+^b+^u^l| 21 | ^u|^u|^u|^u^r^u|^u|^u|^u=+^o^l^l=^l^l 22 | =^l^l=^l^l^l=^l^l=^l^l= 23 | ^09^d|^d|^d|^d^d|^d|^d|^d| 24 | -------------------------------------------------------------------------------- /2-hard/computer-terminal/input-small.txt: -------------------------------------------------------------------------------- 1 | ^h^c 2 | ^04^^ 3 | ^13/ \^d^b / \ 4 | ^u^d^d^l^l^l^l^l^l^l^l^l 5 | ^r^r^l^l^d^l^l^d/^b \ 6 | ^d^r^r^66/^b \ 7 | ^b^d \ / 8 | ^d^l^lv^d^b===========^i^94O123456 9 | 789^94A=======^u^u^u^u^u^u^l^l\^o^b^r/ 10 | -------------------------------------------------------------------------------- /2-hard/computer-terminal/sequences.txt: -------------------------------------------------------------------------------- 1 | ^c - clear the entire screen; the cursor row and column do not change. 2 | 3 | ^h - move the cursor to row 0, column 0; the image on the screen is not 4 | changed. 5 | 6 | ^b - move the cursor to the beginning of the current line; the cursor row does 7 | not change. 8 | 9 | ^d - move the cursor down one row if possible; the cursor column does not 10 | change. 11 | 12 | ^u - move the cursor up one row, if possible; the cursor column does not 13 | change. 14 | 15 | ^l - move the cursor left one column, if possible; the cursor row does not 16 | change. 17 | 18 | ^r - move the cursor right one column, if possible; the cursor row does not 19 | change. 20 | 21 | ^e - erase characters to the right of, and including, the cursor column on the 22 | cursor's row; the cursor row and column do not change. 23 | 24 | ^i - enter insert mode. 25 | 26 | ^o - enter overwrite mode. 27 | 28 | ^^ - write a circumflex (^) at the current cursor location, exactly as if it 29 | was not a special character; this is subject to the actions of the 30 | current mode (insert or overwrite). 31 | 32 | ^DD - move the cursor to the row and column specified; each D represents a 33 | decimal digit; the first D represents the new row number, and the second 34 | D represents the new column number. 35 | -------------------------------------------------------------------------------- /2-hard/decryption/main.py: -------------------------------------------------------------------------------- 1 | def main(): 2 | message = '012222 1114142503 0313012513 03141418192102 0113 2419182119021713 06131715070119' 3 | key = 'BHISOECRTMGWYVALUZDNFJKPQX' 4 | count = 0 5 | key_map = {} 6 | for c in key: 7 | key_map[c] = count 8 | count += 1 9 | result = "" 10 | for i in range(0, len(message), 2): 11 | if message[i:i+2] == ' ': result += ' '; continue 12 | result += chr(ord('A') + key_map[chr(ord('A') + int(message[i:i+2]))]) 13 | print(result) 14 | 15 | if __name__ == '__main__': 16 | main() 17 | -------------------------------------------------------------------------------- /2-hard/digit-statistics/input.txt: -------------------------------------------------------------------------------- 1 | 2 5 2 | 3 5 3 | 4 5 4 | 5 5 5 | 6 5 6 | 7 5 7 | 8 5 8 | 9 5 9 | 2 1000000000000 10 | -------------------------------------------------------------------------------- /2-hard/digit-statistics/main.py: -------------------------------------------------------------------------------- 1 | import sys 2 | import collections 3 | 4 | 5 | # This is the trick: take advantage of the multiplicative pattern. 6 | def get_cycle(number): 7 | cycle = [] 8 | current = number 9 | while current not in cycle: 10 | cycle.append(current) 11 | current = (current * number) % 10 12 | return cycle 13 | 14 | 15 | def get_digits(a, n): 16 | cycle = get_cycle(a) 17 | digits = collections.defaultdict(int) 18 | cycle_repeats = n/len(cycle) 19 | remaining = n - cycle_repeats * len(cycle) 20 | for c in cycle: 21 | digits[c] = cycle_repeats 22 | for i in range(1, int(remaining + 1)): 23 | digit = cycle[(i-1)%len(cycle)] 24 | digits[digit] += 1 25 | return digits 26 | 27 | 28 | def print_pretty_default_dict(d): 29 | result = [] 30 | for i in range(0, 10): 31 | result.append("%i: %i" % (i, d[i])) 32 | print(', '.join(result)) 33 | 34 | 35 | def main(): 36 | test_cases = open(sys.argv[1], 'r') 37 | for test in test_cases: 38 | test = test.strip() 39 | if len(test) == 0: 40 | continue 41 | a = int(test.split(' ')[0]) 42 | n = int(test.split(' ')[1]) 43 | digits = get_digits(a, n) 44 | print_pretty_default_dict(digits) 45 | test_cases.close() 46 | 47 | if __name__ == '__main__': 48 | main() 49 | -------------------------------------------------------------------------------- /2-hard/distinct-subsequences/input.txt: -------------------------------------------------------------------------------- 1 | elezeyeweleszlzzzyzwzlzsmlmzmymwmlms,ezm 2 | egsyukpoyhdx,lzg 3 | rabbbit,rabbit 4 | ryrcrcrysyscscsynyncncny,rsn 5 | exdgsplky,ezg 6 | resyvpdo,uozh 7 | xexvxtxixdxsqeqvqtqiqdqspepvptpipdps,xqp 8 | umuzucvmvzvcamazacjmjzjc,uvaj 9 | ubummhvxkf,bpbfbs 10 | imifjhaxflbfn,dpo 11 | cuyrxwk,cyrk 12 | hello,hel 13 | juggernauter,jger 14 | ofhfossqhzqytw,osyt 15 | hifcakpvusk,ik 16 | ngaajnkyolqw,shvr 17 | zcqdcpgw,bympq 18 | raoxbgrw,zrel 19 | bkovpmyjckx,ykmvqn 20 | tckujgn,eono 21 | ynuhgpkzhldvyw,pk 22 | nlubchjx,amz 23 | ziwvmcjl,rtzst 24 | uwfgpny,rdmgch 25 | uaknccc,ikue 26 | fxdihws,xh 27 | pzichedsnp,irr 28 | bmbobkbdbdbsomoookododosbmbobkbdbdbs,bob 29 | guucgyjh,fxfcia 30 | world,wore 31 | babgbag,bag 32 | bzbzbvgzgzgv,bg 33 | irlzsvbfn,rsfn 34 | mrmbmbmmmsmkerebebemesekmrmbmbmmmsmk,mem 35 | eqhjscmbp,ehmb 36 | ylywylykyuyprlrwrlrkrurpslswslsksusp,yrs 37 | whwmwxweqhqmqxqeahamaxaeahamaxae,wqaa 38 | pvpupepkpsplxvxuxexkxsxlzvzuzezkzszl,pxz 39 | plzpjvtcezwpnr,lewn 40 | zepsmgulgluxm,niqtm 41 | babgbag,bag 42 | rabbbit,rabbit 43 | -------------------------------------------------------------------------------- /2-hard/distinct-subsequences/main.py: -------------------------------------------------------------------------------- 1 | import sys 2 | 3 | def count_sub_strings(str1, str2): 4 | if len(str1) < len(str2): 5 | return 0 6 | if str1 == str2 or len(str2) == 0: 7 | return 1 8 | result = 0 9 | if str1[0] == str2[0]: 10 | result += count_sub_strings(str1[1:], str2[1:]) 11 | return result + count_sub_strings(str1[1:], str2) 12 | 13 | test_cases = open(sys.argv[1], 'r') 14 | for test in test_cases: 15 | test = test.strip() 16 | if len(test) == 0: 17 | continue 18 | str1 = test.split(',')[0] 19 | str2 = test.split(',')[1] 20 | print(count_sub_strings(str1, str2)) 21 | 22 | test_cases.close() 23 | -------------------------------------------------------------------------------- /2-hard/distinct-triangles/input.txt: -------------------------------------------------------------------------------- 1 | 4 5;0 2,0 1,1 2,1 3,2 3 2 | 9 3;1 3,1 8,3 8 3 | 9 3;5 6,5 7,6 7 4 | -------------------------------------------------------------------------------- /2-hard/efficient-delivery/input.txt: -------------------------------------------------------------------------------- 1 | (2,5), 12 2 | (6,9,20), 44 3 | (197,8170), 155862 4 | (2,4,8), 8 5 | -------------------------------------------------------------------------------- /2-hard/efficient-delivery/main.py: -------------------------------------------------------------------------------- 1 | import sys 2 | 3 | test_cases = open(sys.argv[1], 'r') 4 | for test in test_cases: 5 | test = test.strip() 6 | if len(test) == 0: 7 | continue 8 | barrels = int(test.split(', ')[1].strip()) 9 | tanker_capacities = [int(x) for x in test.split(', ')[0][1:-1].split(',')] 10 | # TODO 11 | pass 12 | 13 | test_cases.close() 14 | -------------------------------------------------------------------------------- /2-hard/find-min/input.txt: -------------------------------------------------------------------------------- 1 | 78,51,3,5,5,51230 2 | 186,75,68,16,539,312 3 | 137,135,48,17,461,512 4 | 98,22,6,30,524,100 5 | 46,18,7,11,9,46 6 | 243,120,9032,18409,34736,41442 7 | 197,108,32690,43661,26061,45130 8 | 104,50,4900,28358,24035,32690 9 | 251,241,22487,42129,16262,42646 10 | 81,80,7336,12299,9008,39172 11 | 282,172,13730,11004,39026,15579 12 | 189,42,33109,9406,28567,9555 13 | 178,28,40549,23866,7513,974 14 | 252,15,18945,973,39500,43972 15 | 257,208,3634,23188,16405,28570 16 | 211,92,49859,12338,48245,7026 17 | 56,10,12570,41482,10327,11306 18 | 192,177,9129,16078,11127,31342 19 | 50,48,17274,22278,29865,47733 20 | 209,111,972,31671,35015,10569 21 | 78,51,3,5,5,51230 22 | 286,279,40323,3124,18358,11611 23 | 295,94,35743,26317,36026,47224 24 | 110,18,12049,46333,39194,36446 25 | 274,238,36365,1398,29864,21364 26 | -------------------------------------------------------------------------------- /2-hard/find-min/main.py: -------------------------------------------------------------------------------- 1 | import sys 2 | 3 | test_cases = open(sys.argv[1], 'r') 4 | for test in test_cases: 5 | test = test.strip() 6 | if len(test) == 0: 7 | continue 8 | test_split = test.split(',') 9 | n = int(test_split[0]) 10 | k = int(test_split[1]) 11 | a = int(test_split[2]) 12 | b = int(test_split[3]) 13 | c = int(test_split[4]) 14 | r = int(test_split[5]) 15 | m = [a] 16 | for i in range(1, k): 17 | m.append((b * m[i-1] + c) % r) 18 | for i in range(k, n): 19 | s = sorted(m) 20 | found = False 21 | smallest = 0 22 | while not found: 23 | if smallest not in s: 24 | break 25 | else: 26 | smallest += 1 27 | m.append(smallest) 28 | m.pop(0) 29 | print(m[-1]) 30 | test_cases.close() 31 | -------------------------------------------------------------------------------- /2-hard/following-integer/input.txt: -------------------------------------------------------------------------------- 1 | 115 2 | 842 3 | 8000 4 | -------------------------------------------------------------------------------- /2-hard/following-integer/main.py: -------------------------------------------------------------------------------- 1 | import sys 2 | 3 | def get_digits_ignore_zero(x): 4 | digits = {} 5 | for digit in str(x): 6 | if digit == '0': 7 | continue 8 | if digit in digits: 9 | digits[digit] += 1 10 | else: 11 | digits[digit] = 1 12 | return digits 13 | 14 | def following_integer(x): 15 | original_digits = get_digits_ignore_zero(x) 16 | while True: 17 | x += 1 18 | digits = get_digits_ignore_zero(x) 19 | if original_digits == digits: 20 | return x 21 | 22 | test_cases = open(sys.argv[1], 'r') 23 | for test in test_cases: 24 | test = test.strip() 25 | if len(test) == 0: 26 | continue 27 | test = int(test) 28 | print(following_integer(test)) 29 | test_cases.close() 30 | -------------------------------------------------------------------------------- /2-hard/grid-walk/main.py: -------------------------------------------------------------------------------- 1 | import collections 2 | 3 | def sum_digits(n): 4 | s = 0 5 | while n: 6 | s += n % 10 7 | n /= 10 8 | return s 9 | 10 | def can_visit(p): 11 | value = sum_digits(abs(p[0])) + sum_digits(abs(p[1])) 12 | return value <= 19 13 | 14 | def is_visited(p, visited): 15 | return p in visited 16 | 17 | def neighbors(p): 18 | x, y = p 19 | return [(x+1, y), (x, y+1), (x-1, y), (x, y-1)] 20 | 21 | def count_accessible_coordinates(): 22 | visited = {(0, 0)} 23 | queue = collections.deque([(0, 0)]) 24 | while len(queue) > 0: 25 | p = queue.pop() 26 | for n in neighbors(p): 27 | if not is_visited(n, visited) and can_visit(n): 28 | visited.add(n) 29 | queue.append(n) 30 | return len(visited) 31 | 32 | print(count_accessible_coordinates()) 33 | -------------------------------------------------------------------------------- /2-hard/largest-sub-matrix/input.txt: -------------------------------------------------------------------------------- 1 | -1 -4 -5 -4 2 | -5 8 -1 3 3 | -2 1 3 2 4 | 1 5 6 -9 5 | -------------------------------------------------------------------------------- /2-hard/longest-common-subsequence/input.txt: -------------------------------------------------------------------------------- 1 | XMJYAUZ;MZJAWXU 2 | -------------------------------------------------------------------------------- /2-hard/longest-common-subsequence/main.py: -------------------------------------------------------------------------------- 1 | import sys 2 | 3 | def lcs(a, b): 4 | lengths = [[0 for j in range(len(b)+1)] for i in range(len(a)+1)] 5 | # row 0 and column 0 are initialized to 0 already 6 | for i, x in enumerate(a): 7 | for j, y in enumerate(b): 8 | if x == y: 9 | lengths[i+1][j+1] = lengths[i][j] + 1 10 | else: 11 | lengths[i+1][j+1] = \ 12 | max(lengths[i+1][j], lengths[i][j+1]) 13 | # Read the substring out from the matrix. 14 | result = "" 15 | x, y = len(a), len(b) 16 | while x != 0 and y != 0: 17 | if lengths[x][y] == lengths[x-1][y]: 18 | x -= 1 19 | elif lengths[x][y] == lengths[x][y-1]: 20 | y -= 1 21 | else: 22 | assert a[x-1] == b[y-1] 23 | result = a[x-1] + result 24 | x -= 1 25 | y -= 1 26 | return result 27 | 28 | test_cases = open(sys.argv[1], 'r') 29 | for test in test_cases: 30 | test = test.strip() 31 | if len(test) == 0: 32 | continue 33 | xstr = test.split(';')[0] 34 | ystr = test.split(';')[1] 35 | print(lcs(xstr, ystr)) 36 | 37 | test_cases.close() 38 | -------------------------------------------------------------------------------- /2-hard/mars-networks/input-small.txt: -------------------------------------------------------------------------------- 1 | 500,8000 1000,9500 2000,8500 1000,7500 4500,7000 5500,6500 7000,7000 2500,4000 1000,4000 1000,3000 3000,2500 2500,1000 3500,500 9000,6000 8500,4500 7500,4000 9000,3500 10000,3000 2 | 8028,5930 1835,5145 8537,9824 7623,7936 8031,1207 3 | 9349,3367 395,3342 3588,3655 9235,2503 1075,6413 2394,8353 4 | 9013,3937 7791,872 2417,3183 5 | 3416,472 8093,7510 1709,4893 9999,6958 6761,2692 2530,6753 6 | -------------------------------------------------------------------------------- /2-hard/minesweeper/input.txt: -------------------------------------------------------------------------------- 1 | 3,5;**.........*... 2 | 4,4;*........*...... 3 | -------------------------------------------------------------------------------- /2-hard/package-problem/input-micro.txt: -------------------------------------------------------------------------------- 1 | 10 : (1,5,$10) (2,5,$10) (3,9,$20) 2 | -------------------------------------------------------------------------------- /2-hard/package-problem/input-small.txt: -------------------------------------------------------------------------------- 1 | 81 : (1,53.38,$45) (2,88.62,$98) (3,78.48,$3) (4,72.30,$76) (5,30.18,$9) (6,46.34,$48) 2 | 8 : (1,15.3,$34) 3 | 75 : (1,85.31,$29) (2,14.55,$74) (3,3.98,$16) (4,26.24,$55) (5,63.69,$52) (6,76.25,$75) (7,60.02,$74) (8,93.18,$35) (9,89.95,$78) 4 | 56 : (1,90.72,$13) (2,33.80,$40) (3,43.15,$10) (4,37.97,$16) (5,46.81,$36) (6,48.77,$79) (7,81.80,$45) (8,19.36,$79) (9,6.76,$64) 5 | -------------------------------------------------------------------------------- /2-hard/palindromic-ranges/input.txt: -------------------------------------------------------------------------------- 1 | 87 93 2 | 181 194 3 | 100 105 4 | 178 187 5 | 38 46 6 | 150 159 7 | 1 2 8 | 114 122 9 | 130 149 10 | 87 107 11 | 190 212 12 | 293 301 13 | 160 183 14 | 236 257 15 | 184 197 16 | 216 222 17 | 184 206 18 | 170 183 19 | 0 2 20 | 87 90 21 | 1 2 22 | 1 7 23 | 87 88 24 | 12 14 25 | -------------------------------------------------------------------------------- /2-hard/palindromic-ranges/main.py: -------------------------------------------------------------------------------- 1 | import sys 2 | 3 | def is_palindrome(x): 4 | x = str(x) 5 | counter = 0 6 | while counter <= len(x)/2: 7 | left = x[counter] 8 | right = x[len(x) - counter - 1] 9 | if left != right: 10 | return False 11 | counter += 1 12 | return True 13 | 14 | def count_palindromic_ranges(palindrome_array): 15 | result = 0 16 | for width in range(1, len(palindrome_array)+1): 17 | for start in range(len(palindrome_array)): 18 | if start + width > len(palindrome_array): 19 | break 20 | palindrome_count = 0 21 | for i in range(start, start+width): 22 | if palindrome_array[i]: 23 | palindrome_count += 1 24 | if palindrome_count % 2 == 0: 25 | result += 1 26 | return result 27 | 28 | test_cases = open(sys.argv[1], 'r') 29 | for test in test_cases: 30 | test = test.strip() 31 | if len(test) == 0: 32 | continue 33 | L = int(test.split(" ")[0]) 34 | R = int(test.split(" ")[1]) 35 | palindrome_array = [] 36 | for i in range(L, R+1): 37 | palindrome_array.append(is_palindrome(i)) 38 | print(count_palindromic_ranges(palindrome_array)) 39 | 40 | test_cases.close() 41 | -------------------------------------------------------------------------------- /2-hard/prefix-expressions/input.txt: -------------------------------------------------------------------------------- 1 | * + 2 3 4 2 | -------------------------------------------------------------------------------- /2-hard/prefix-expressions/main.py: -------------------------------------------------------------------------------- 1 | import sys 2 | 3 | test_cases = open(sys.argv[1], 'r') 4 | for test in test_cases: 5 | test = test.strip() 6 | if len(test) == 0: 7 | continue 8 | stack = [] 9 | for i in reversed(test.split(' ')): 10 | if i.isdigit(): 11 | stack.append(i) 12 | else: 13 | op1 = float(stack.pop()) 14 | op2 = float(stack.pop()) 15 | if i == '+': 16 | result = op1 + op2 17 | elif i == '*': 18 | result = op1 * op2 19 | else: 20 | result = op1 / op2 21 | stack.append(result) 22 | print(int(stack.pop())) 23 | 24 | test_cases.close() 25 | -------------------------------------------------------------------------------- /2-hard/repeated-substring/input.txt: -------------------------------------------------------------------------------- 1 | banana 2 | mattmatt 3 | amattmatt 4 | aaabbb 5 | 4 4 6 | aa0bb11aa2bb 7 | aabaa 8 | abc 9 | -------------------------------------------------------------------------------- /2-hard/robot-movements/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int countPaths(bool grid[][4], int currentX, int currentY) 4 | { 5 | if (currentX < 0 || currentX > 3 || currentY < 0 || currentY > 3) { 6 | return 0; 7 | } 8 | 9 | if (grid[currentX][currentY]) { 10 | return 0; 11 | } 12 | 13 | if (currentX == 3 && currentY == 3) { 14 | return 1; 15 | } 16 | 17 | grid[currentX][currentY] = true; 18 | 19 | int result = 0; 20 | 21 | result += countPaths(grid, currentX+1, currentY); 22 | result += countPaths(grid, currentX, currentY+1); 23 | result += countPaths(grid, currentX-1, currentY); 24 | result += countPaths(grid, currentX, currentY-1); 25 | 26 | grid[currentX][currentY] = false; 27 | 28 | return result; 29 | } 30 | 31 | int main() 32 | { 33 | bool grid[][4] = { 34 | {false, false, false, false}, 35 | {false, false, false, false}, 36 | {false, false, false, false}, 37 | {false, false, false, false} 38 | }; 39 | 40 | std::cout << countPaths(grid, 0, 0) << std::endl; 41 | return 0; 42 | } 43 | -------------------------------------------------------------------------------- /2-hard/robot-movements/main.py: -------------------------------------------------------------------------------- 1 | grid = [[False, False, False, False], 2 | [False, False, False, False], 3 | [False, False, False, False], 4 | [False, False, False, False]] 5 | 6 | def count_paths(grid, current_x, current_y, end_x, end_y): 7 | if current_x < 0 or current_x > 3 or current_y < 0 or current_y > 3: 8 | return 0 9 | 10 | if grid[current_x][current_y]: 11 | return 0 12 | 13 | if current_x == end_x and current_y == end_y: 14 | return 1 15 | 16 | grid[current_x][current_y] = True 17 | 18 | result = 0 19 | 20 | result += count_paths(grid, current_x+1, current_y, end_x, end_y) 21 | result += count_paths(grid, current_x, current_y+1, end_x, end_y) 22 | result += count_paths(grid, current_x-1, current_y, end_x, end_y) 23 | result += count_paths(grid, current_x, current_y-1, end_x, end_y) 24 | 25 | grid[current_x][current_y] = False 26 | 27 | return result 28 | 29 | print(count_paths(grid, 0, 0, 3, 3)) 30 | -------------------------------------------------------------------------------- /2-hard/routing-problem/input1.txt: -------------------------------------------------------------------------------- 1 | {0: ['179.177.220.225/23', '77.119.202.216/26', '171.251.60.138/25'], 1: ['147.105.101.114/22', '67.239.38.194/23', '50.202.182.215/21'], 2: ['14.125.37.194/23', '128.3.0.158/23'], 3: ['77.119.202.193/26', '164.119.164.209/30'], 4: ['178.102.37.7/25', '117.184.65.161/21', '246.255.201.77/20', '128.160.69.237/30'], 5: ['67.239.38.85/23', '128.3.1.67/23', '19.136.96.193/20', '108.243.129.176/24'], 6: ['103.131.199.252/25', '222.15.10.40/20'], 7: ['194.214.45.144/20', '22.53.223.185/26'], 8: ['147.105.102.57/22', '122.119.95.62/22', '0.111.125.205/30'], 9: ['0.111.125.206/30'], 10: ['117.184.70.222/21'], 11: ['178.102.37.31/25', '19.136.100.54/20', '96.43.12.49/28', '214.240.75.20/23'], 12: ['171.251.60.130/25', '198.240.92.134/22', '242.183.157.69/27', '235.172.192.119/23'], 13: ['122.119.93.84/22'], 14: ['128.160.69.238/30', '22.53.223.131/26', '89.111.130.102/30'], 15: ['50.202.182.148/21', '235.172.192.12/23'], 16: ['179.177.220.149/23', '103.131.199.146/25', '198.240.92.1/22'], 17: ['96.43.12.52/28'], 18: ['164.119.164.210/30', '108.243.129.104/24', '222.15.4.215/20', '89.111.130.101/30', '63.113.177.19/28'], 19: ['14.125.36.203/23', '246.255.204.239/20', '194.214.34.254/20', '214.240.74.123/23', '242.183.157.93/27', '63.113.177.27/28']} 2 | 5 8 3 | 1 7 4 | 2 14 5 | 11 20 6 | 10 14 7 | -------------------------------------------------------------------------------- /2-hard/routing-problem/input2.txt: -------------------------------------------------------------------------------- 1 | {0: ['1.1.1.10/24'], 1: ['1.1.1.11/24', '1.1.3.13/24'], 2: ['1.1.1.12/24', '1.1.2.10/24'], 3: ['1.1.3.11/24'], 4: ['1.1.1.14/24', '1.1.2.12/24'], 5: ['1.1.2.11/24', '1.1.3.10/24'], 7: ['1.1.1.15/24', '1.1.3.12/24']} 2 | 0 3 3 | -------------------------------------------------------------------------------- /2-hard/seat-your-team-members/input.txt: -------------------------------------------------------------------------------- 1 | 4; 1:[1, 3, 2], 2:[1], 3:[4, 3], 4:[4, 3] 2 | 3; 1:[1, 3, 2], 2:[1], 3:[1] 3 | -------------------------------------------------------------------------------- /2-hard/skyscrapers/input2.txt: -------------------------------------------------------------------------------- 1 | (1,10,2);(2,5,3) 2 | (1,10,2);(3,5,4) 3 | -------------------------------------------------------------------------------- /2-hard/skyscrapers/main.py: -------------------------------------------------------------------------------- 1 | import sys 2 | import re 3 | 4 | test_cases = open(sys.argv[1], 'r') 5 | for test in test_cases: 6 | test = test.strip() 7 | if len(test) == 0: 8 | continue 9 | 10 | heights = {} 11 | 12 | def note_height(index, ma): 13 | if index in heights: 14 | heights[index] = max(heights[index], ma) 15 | else: 16 | heights[index] = ma 17 | 18 | for coord in test.split(';'): 19 | m = re.match("\(([0-9]+),([0-9]+),([0-9]+)\)", coord.strip()) 20 | l = int(m.group(1)) 21 | h = int(m.group(2)) 22 | r = int(m.group(3)) 23 | 24 | # Going up! 25 | note_height(l, h) 26 | # Going down! 27 | note_height(r, 0) 28 | 29 | for i in range(l+1, r): 30 | note_height(i, h) 31 | 32 | previous_height = 0 33 | result = [] 34 | 35 | for i in sorted(heights.keys()): 36 | height = heights[i] 37 | 38 | if height != previous_height: 39 | previous_height = height 40 | result.append(str(i)) 41 | result.append(str(height)) 42 | 43 | print(" ".join(result)) 44 | 45 | test_cases.close() 46 | -------------------------------------------------------------------------------- /2-hard/string-list/input.txt: -------------------------------------------------------------------------------- 1 | 1,aa 2 | 2,ab 3 | 3,pop 4 | 2,pld 5 | 5,tvhni 6 | -------------------------------------------------------------------------------- /2-hard/string-list/main.py: -------------------------------------------------------------------------------- 1 | import sys 2 | 3 | def get_all_possible_words(alphabet, length, prefix=""): 4 | if len(prefix) == length: 5 | return [prefix] 6 | 7 | result = [] 8 | for c in alphabet: 9 | r = get_all_possible_words(alphabet, length, prefix + c) 10 | 11 | for i in r: 12 | result.append(i) 13 | 14 | return result 15 | 16 | test_cases = open(sys.argv[1], 'r') 17 | for test in test_cases: 18 | test = test.strip() 19 | if len(test) == 0: 20 | continue 21 | 22 | length = int(test.split(',')[0]) 23 | chars = test.split(',')[1] 24 | 25 | alphabet = set() 26 | for c in chars: 27 | alphabet.add(c) 28 | 29 | print(",".join(sorted(get_all_possible_words(alphabet, length)))) 30 | 31 | test_cases.close() 32 | -------------------------------------------------------------------------------- /2-hard/string-permutations/input.txt: -------------------------------------------------------------------------------- 1 | hat -------------------------------------------------------------------------------- /2-hard/string-searching/input.txt: -------------------------------------------------------------------------------- 1 | Hello,ell 2 | This is good, is 3 | CodeEval,C*Eval 4 | Old,Young 5 | Testing*Test,Testing\*T 6 | PQYARSqAynqv AZ 9Lh2 lOq v2kH4NwX,*2kH4N 7 | g3a0VR GgZryQT4gl84 MoF,gZryQT*4 MoF 8 | -------------------------------------------------------------------------------- /2-hard/string-searching/main.py: -------------------------------------------------------------------------------- 1 | import sys 2 | 3 | def search(to_search, pattern): 4 | to_search = to_search.replace("*", "#") 5 | pattern = pattern.replace("\\*", "#") 6 | 7 | for p in pattern.split("*"): 8 | found_index = to_search.find(p) 9 | if found_index < 0: 10 | return False 11 | 12 | to_search = to_search[found_index+len(p):] 13 | 14 | return True 15 | 16 | test_cases = open(sys.argv[1], 'r') 17 | for test in test_cases: 18 | test = test.strip() 19 | if len(test) == 0: 20 | continue 21 | 22 | to_search = test.split(",")[0] 23 | pattern = test.split(",")[1] 24 | 25 | print('true' if search(to_search, pattern) else 'false') 26 | 27 | test_cases.close() 28 | -------------------------------------------------------------------------------- /2-hard/string-substitution/input.txt: -------------------------------------------------------------------------------- 1 | 10011011001;0110,1001,1001,0,10,11 2 | -------------------------------------------------------------------------------- /2-hard/string-substitution/main.py: -------------------------------------------------------------------------------- 1 | import sys 2 | 3 | class SearchString: 4 | def __init__(self, raw): 5 | self.array = [] 6 | for c in raw: 7 | self.array.append(c) 8 | 9 | def is_match(self, s, index): 10 | if index + len(s) > len(self.array): 11 | return False 12 | 13 | match = True 14 | for i in range(len(s)): 15 | if self.array[index + i] != s[i]: 16 | match = False 17 | break 18 | 19 | return match 20 | 21 | def find_and_replace(self, s, r): 22 | for i in range(len(self.array)): 23 | if self.is_match(s, i): 24 | self.array[i] = "x" + r 25 | 26 | for j in range(1, len(s)): 27 | self.array[i+j] = "" 28 | 29 | def render(self): 30 | return "".join(self.array).replace("x", "") 31 | 32 | test_cases = open(sys.argv[1], 'r') 33 | for test in test_cases: 34 | test = test.strip() 35 | if len(test) == 0: 36 | continue 37 | 38 | s = SearchString(test.split(';')[0]) 39 | subs = test.split(';')[1].split(',') 40 | 41 | for i in range(0, len(subs), 2): 42 | s.find_and_replace(subs[i], subs[i+1]) 43 | 44 | print(s.render()) 45 | 46 | test_cases.close() 47 | -------------------------------------------------------------------------------- /2-hard/telephone-words/input.txt: -------------------------------------------------------------------------------- 1 | 4155230 2 | -------------------------------------------------------------------------------- /2-hard/telephone-words/main.py: -------------------------------------------------------------------------------- 1 | import sys 2 | 3 | def get_telephone_words(number, prefix, index): 4 | if index == len(number): 5 | return [prefix] 6 | 7 | options = { 8 | '0': '0', 9 | '1': '1', 10 | '2': 'abc', 11 | '3': 'def', 12 | '4': 'ghi', 13 | '5': 'jkl', 14 | '6': 'mno', 15 | '7': 'pqrs', 16 | '8': 'tuv', 17 | '9': 'wxyz' 18 | }[number[index]] 19 | 20 | result = [] 21 | 22 | for o in options: 23 | result.extend(get_telephone_words(number, prefix + o, index+1)) 24 | 25 | return result 26 | 27 | test_cases = open(sys.argv[1], 'r') 28 | for test in test_cases: 29 | test = test.strip() 30 | if len(test) == 0: 31 | continue 32 | 33 | print(",".join(sorted(get_telephone_words(test, "", 0)))) 34 | 35 | test_cases.close() 36 | -------------------------------------------------------------------------------- /2-hard/text-dollar/input.txt: -------------------------------------------------------------------------------- 1 | 3 2 | 10 3 | 21 4 | 466 5 | 1234 6 | 79686 7 | 678047179 8 | 624 9 | 59491 10 | 225 11 | 236 12 | 495465599 13 | 923 14 | 899 15 | 66856 16 | 86884 17 | 943873380 18 | 794233806 19 | 589 20 | 525627324 21 | 743524791 22 | 437089177 23 | 1 24 | 100000000 25 | 89859 26 | 8785 27 | 25831 28 | 123456789 29 | 466 30 | 74831 31 | 207 32 | 84000 33 | 509324860 34 | 739 35 | 25109 36 | 257 37 | 653 38 | 172 39 | 31535 40 | 218546558 41 | 948825159 42 | 82494 43 | 590 44 | 565 45 | 286514165 46 | -------------------------------------------------------------------------------- /2-hard/the-cubes/input-small.txt: -------------------------------------------------------------------------------- 1 | 5;****** ** **** *** ******** o**o* ** * ************ ** * **o* ************ o **** ** ******** ***o ****o**o ****** 2 | 5;****** * ** * ** *** ********o*o** *o** ************ **o* ** *o************ **** ** ******** *** o ****o** o****** 3 | 7;******** ** *** ** * * ** * * ** **** *********** * ** *** ** *o**o***o**oo **************** oo ** * * ** * * ** * * ** *o o**************** ** * ****o* * ** *** **o **************** ** *** ** ****** ** o o**************** *oo ** *** **o * ** *** **o *********** **** * ** * * ** * *o** ***o** ******** 4 | -------------------------------------------------------------------------------- /2-hard/type-ahead/input-large.txt: -------------------------------------------------------------------------------- 1 | 2,a 2 | 2,was 3 | 2,appear 4 | 2,went 5 | 4,It made the 6 | 2,was 7 | 2,Mary 8 | 2,so 9 | 4,waited patiently about 10 | 3,children laugh 11 | 3,the lamb 12 | 3,patiently about 13 | 2,it 14 | 4,everywhere that Mary 15 | 3,children laugh 16 | 3,which was 17 | 4,still it lingered 18 | 3,day which 19 | 4,rule It made 20 | 3,so the 21 | 3,It made 22 | 3,a lamb 23 | 3,rule It 24 | 3,Mary had 25 | 4,It followed her 26 | 2,that 27 | 2,out 28 | 4,followed her to 29 | 3,one day 30 | 2,snow 31 | 3,does the 32 | 2,the 33 | 3,her to 34 | 4,play to see 35 | 2,did 36 | 3,a lamb 37 | 2,the 38 | 4,And everywhere that 39 | 4,which was against 40 | 4,sure to go 41 | -------------------------------------------------------------------------------- /2-hard/type-ahead/input-small.txt: -------------------------------------------------------------------------------- 1 | 2,the 2 | -------------------------------------------------------------------------------- /2-hard/ugly-numbers/input.txt: -------------------------------------------------------------------------------- 1 | 1 2 | 9 3 | 09 4 | 011 5 | 12345 6 | 1067 7 | 0 8 | 3703 9 | 011 10 | 776 11 | 09641 12 | 0181 13 | 844100 14 | 381320 15 | 15943589 16 | 08 17 | 9445038 18 | 9 19 | 0000000000277 20 | 1 21 | 626 22 | 8687623768 23 | 13 24 | 6000 25 | 47255 26 | -------------------------------------------------------------------------------- /2-hard/visit-to-the-headquarter/input.txt: -------------------------------------------------------------------------------- 1 | A 09:00:00 0203 5 0210 10 0305 5 0604 10 0605 10 0901 10 0908 10 2 | B 09:00:25 0205 10 0404 5 0501 5 0602 5 0703 5 0807 5 3 | C 09:00:45 0109 10 0110 5 0207 5 0208 10 0401 10 0510 5 4 | D 09:01:15 0310 5 0404 5 0503 10 0603 5 0604 5 0704 10 0708 5 0910 5 1005 10 5 | -------------------------------------------------------------------------------- /2-hard/visit-to-the-headquarter/main.py: -------------------------------------------------------------------------------- 1 | import sys 2 | 3 | # TODO finish. 4 | 5 | def string_to_time(s): 6 | s_split = s.split(':') 7 | return int(s_split[0]) * 3600 + int(s_split[1]) * 60 + int(s_split[2]) 8 | 9 | def time_to_string(t): 10 | h = (t / 3600) 11 | m = (t / 60 ) % 60 12 | s = t % 3600 % 60 13 | return "%02d:%02d:%02d" % (h, m, s) 14 | 15 | def main(): 16 | test_cases = open(sys.argv[1], 'r') 17 | for test in test_cases: 18 | test = test.strip() 19 | if len(test) == 0: 20 | continue 21 | 22 | test_split = test.split(' ') 23 | 24 | agent = ord(test_split[0]) 25 | start_time = string_to_time(test_split[1]) 26 | rooms_and_times = [] 27 | 28 | for i in range(2, len(test_split), 2): 29 | rooms_and_times.append([test_split[i], int(test_split[i+1])]) 30 | 31 | # TODO remove this. 32 | print(agent) 33 | print(start_time) 34 | print(rooms_and_times) 35 | 36 | test_cases.close() 37 | 38 | if __name__ == "__main__": 39 | main() 40 | -------------------------------------------------------------------------------- /2-hard/word-search/input.txt: -------------------------------------------------------------------------------- 1 | ASADB 2 | ABCCED 3 | ABCF 4 | ADCBE 5 | ECBFDASA 6 | BCE 7 | ASADECF 8 | CCA 9 | SEE 10 | ASABFCEESE 11 | SADFBCESE 12 | ESE 13 | CSEECA 14 | EDEABCSASE 15 | CFSD 16 | ESFDEEACAC 17 | ECCFSA 18 | ECC 19 | FSABCCEDA 20 | ESFBADES 21 | AESFBEDAS 22 | SCCE 23 | ASABFDECC 24 | ABCESCFSADEE 25 | ECFSASDEB 26 | CSE 27 | CBFC 28 | BASADFCCES 29 | AFECBEC 30 | ESEEDFSAB 31 | ASFDE 32 | CBFDEES 33 | SCCFEBDEEA 34 | BFCEE 35 | BCFS 36 | DEC 37 | EECFDA 38 | CESEED 39 | EEC 40 | SABFCSE 41 | FBCESCEDA 42 | SEECFBAS 43 | ABFCCB 44 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | This is free and unencumbered software released into the public domain. 2 | 3 | Anyone is free to copy, modify, publish, use, compile, sell, or distribute this software, either in source code form or as a compiled binary, for any purpose, commercial or non-commercial, and by any means. 4 | 5 | In jurisdictions that recognize copyright laws, the author or authors of this software dedicate any and all copyright interest in the software to the public domain. We make this dedication for the benefit of the public at large and to the detriment of our heirs and successors. We intend this dedication to be an overt act of relinquishment in perpetuity of all present and future rights to this software under copyright law. 6 | 7 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 8 | 9 | For more information, please refer to http://unlicense.org 10 | 11 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # CodeEval 2 | 3 | Selected CodeEval solutions. Sadly, CodeEval is no longer operational. It was fun while it lasted! 4 | 5 | ## Development Notes 6 | 7 | Versions unless otherwise stated: 8 | 9 | * Golang: 1.5 10 | * G++: 4.7 (C++ 11) 11 | * Java: 1.7 12 | * Python: 3.3 13 | 14 | ## Favorites 15 | 16 | * [The labyrinth problem](2-hard/the-labyrinth/main.py) ([Dijkstra's algorithm](https://en.wikipedia.org/wiki/Dijkstra%27s_algorithm)) 17 | * [Cluster detection in graphs](2-hard/cluster-detection/main.py) 18 | * [Counting the number of distrinct triangles in a graph](2-hard/distinct-triangles/main.py) 19 | * [Counting the number of times a noisy signal crosses zero](2-hard/the-frequency/main.py) 20 | --------------------------------------------------------------------------------