├── .gitignore ├── .tool-versions ├── JavaScript ├── lib │ ├── caesar_cipher.js │ ├── common_substrings.js │ ├── decimal_to_binary.js │ ├── dictionary.js │ ├── digital_root.js │ ├── fast_intersection.js │ ├── fibonacci.js │ ├── find_duplicate.js │ ├── find_missing_letter.js │ ├── find_non_duplicate_character.js │ ├── folding_cipher.js │ ├── hash_table.js │ ├── is_palindrome.js │ ├── lcs.js │ ├── max_region_sum.js │ ├── pair_sum.js │ ├── recursive_sum.js │ ├── silly_years.js │ ├── stack.js │ ├── unique_substrings.js │ └── valid_ip.js ├── package-lock.json ├── package.json └── test │ ├── caesar_cipher.test.js │ ├── common_substrings.test.js │ ├── decimal_to_binary.test.js │ ├── dictionary.test.js │ ├── digital_root.test.js │ ├── fast_intersection.test.js │ ├── fibonacci.test.js │ ├── find_duplicate.test.js │ ├── find_missing_letter.test.js │ ├── find_non_duplicate_character.test.js │ ├── folding_cipher.test.js │ ├── hash_table.test.js │ ├── is_palindrome.test.js │ ├── lcs.test.js │ ├── max_region_sum.test.js │ ├── pair_sum.test.js │ ├── recursive_sum.test.js │ ├── silly_years.test.js │ ├── stack.test.js │ ├── unique_substrings.test.js │ └── valid_ip.test.js ├── Python ├── lib │ ├── caesar_cipher.py │ ├── character_count.py │ ├── common_substrings.py │ ├── digital_root.py │ ├── fibonacci.py │ ├── fibs.py │ ├── folding_cipher.py │ ├── is_palindrome.py │ ├── recursive_sum.py │ ├── reverse.py │ ├── subsets.py │ ├── unique_substrings.py │ └── valid_ip.py └── test │ ├── test_caesar_cipher.py │ ├── test_character_count.py │ ├── test_common_substrings.py │ ├── test_digital_root.py │ ├── test_fibonacci.py │ ├── test_fibs.py │ ├── test_folding_cipher.py │ ├── test_is_palindrome.py │ ├── test_recursive_sum.py │ ├── test_reverse.py │ ├── test_subsets.py │ ├── test_unique_substrings.py │ └── test_valid_ip.py ├── README.md └── Ruby ├── .rspec ├── Gemfile ├── Gemfile.lock ├── lib ├── add_until_100.rb ├── binary_search.rb ├── caesar_cipher.rb ├── can_win.rb ├── character_count.rb ├── common_subsets.rb ├── common_substrings.rb ├── decimal_to_binary.rb ├── digital_root.rb ├── fast_intersection.rb ├── fibonacci.rb ├── file_list.rb ├── find_missing_number.rb ├── folding_cipher.rb ├── golomb.rb ├── in_words.rb ├── index_of_x.rb ├── is_palindrome.rb ├── is_shuffle.rb ├── iterative_factorial.rb ├── lcs.rb ├── longest_palindrome.rb ├── look_and_say.rb ├── matrix_region_sum.rb ├── max_stack.rb ├── merge_sort.rb ├── min_max_stack.rb ├── min_max_stack_queue.rb ├── minimum_coin_change.rb ├── move_zeros.rb ├── pair_sum.rb ├── permutations.rb ├── productify.rb ├── queue.rb ├── recursive_factorial.rb ├── recursive_print.rb ├── recursive_sum.rb ├── reverse.rb ├── select_even.rb ├── silly_years.rb ├── stack.rb ├── stack_queue.rb ├── subsets.rb ├── sum_upon_sums.rb ├── triangle_number.rb ├── unique_paths.rb ├── unique_substrings.rb ├── valid_ip.rb ├── weighted_random_index.rb └── windowed_max_range.rb └── spec ├── add_until_100_spec.rb ├── binary_search_spec.rb ├── caesar_cipher_spec.rb ├── can_win_spec.rb ├── character_count_spec.rb ├── common_subsets_spec.rb ├── common_substrings_spec.rb ├── decimal_to_binary_spec.rb ├── digital_root_spec.rb ├── fast_intersection_spec.rb ├── fibonacci_spec.rb ├── file_list_spec.rb ├── find_missing_number_spec.rb ├── folding_cipher_spec.rb ├── golomb_spec.rb ├── in_words_spec.rb ├── index_of_x_spec.rb ├── is_palindrome_spec.rb ├── is_shuffle_spec.rb ├── iterative_factorial_spec.rb ├── lcs_spec.rb ├── longest_palindrome_spec.rb ├── look_and_say_spec.rb ├── matrix_region_sum_spec.rb ├── max_stack_spec.rb ├── merge_sort_spec.rb ├── min_max_stack_queue_spec.rb ├── min_max_stack_spec.rb ├── minimum_coin_change_spec.rb ├── move_zeros_spec.rb ├── pair_sum_spec.rb ├── permutations_spec.rb ├── productify_spec.rb ├── queue_spec.rb ├── recursive_factorial_spec.rb ├── recursive_print_spec.rb ├── recursive_sum_spec.rb ├── reverse_spec.rb ├── select_even_spec.rb ├── silly_years_spec.rb ├── spec_helper.rb ├── stack_queue_spec.rb ├── stack_spec.rb ├── subsets_spec.rb ├── sum_upon_sums_spec.rb ├── triangle_number_spec.rb ├── uniq_substrings_spec.rb ├── unique_paths_spec.rb ├── valid_ip_spec.rb ├── weighted_random_index_spec.rb └── windowed_max_range_spec.rb /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | .DS_Store 3 | __pycache__ -------------------------------------------------------------------------------- /.tool-versions: -------------------------------------------------------------------------------- 1 | nodejs 19.8.1 2 | python 3.9.6 3 | ruby 3.1.3 4 | -------------------------------------------------------------------------------- /JavaScript/lib/caesar_cipher.js: -------------------------------------------------------------------------------- 1 | // Write a function that takes a message and an increment amount and outputs the same letters shifted by that amount in the alphabet. 2 | // Assume lowercase and no punctuation. 3 | // Preserve spaces. 4 | function caesarCipher(plainText, shift) { 5 | 6 | } 7 | module.exports = caesarCipher; 8 | -------------------------------------------------------------------------------- /JavaScript/lib/common_substrings.js: -------------------------------------------------------------------------------- 1 | // Write a function that takes two strings and returns the length of the longest common substring. 2 | function commonSubstrings(stringOne, stringTwo) { 3 | 4 | } 5 | module.exports = commonSubstrings; 6 | -------------------------------------------------------------------------------- /JavaScript/lib/decimal_to_binary.js: -------------------------------------------------------------------------------- 1 | const Stack = require('../lib/stack'); 2 | // Write a function that takes an integer and returns it in binary form. 3 | // Consider using the stack data structure. 4 | function decimalToBinary(integer) { 5 | } 6 | module.exports = decimalToBinary; 7 | -------------------------------------------------------------------------------- /JavaScript/lib/dictionary.js: -------------------------------------------------------------------------------- 1 | // Implement a dictionary. 2 | class Dictionary { 3 | constructor() { 4 | this.items = {}; 5 | } 6 | clear() {} 7 | delete() {} 8 | has() {} 9 | keys() {} 10 | set() {} 11 | size() {} 12 | values() {} 13 | } 14 | module.exports = Dictionary; 15 | -------------------------------------------------------------------------------- /JavaScript/lib/digital_root.js: -------------------------------------------------------------------------------- 1 | // Write a method, digitalRoot(number). 2 | // It should sum the digits of a positive integer. 3 | // If it is greater than or equal to 10, sum the digits of the resulting number. 4 | // Keep repeating until there is only one digit in the result, called the "digital root". 5 | // Do not use string conversion within your method. 6 | function digitalRoot(number) { 7 | 8 | } 9 | module.exports = digitalRoot; 10 | -------------------------------------------------------------------------------- /JavaScript/lib/fast_intersection.js: -------------------------------------------------------------------------------- 1 | // Given two arrays, find the intersection of both sets. 2 | // It should be trivial to write an O(n**2) solution. 3 | // Use sorting to solve in O(nlog(n)). 4 | // Next, improve this to O(n) time (maybe use a non-array datastructure). 5 | function fastIntersection(array1, array2) { 6 | 7 | } 8 | module.exports = fastIntersection; 9 | -------------------------------------------------------------------------------- /JavaScript/lib/fibonacci.js: -------------------------------------------------------------------------------- 1 | // Write a function which returns the first n elements from the Fibonacci sequence, given n. 2 | function fibonacci(number) { 3 | 4 | } 5 | module.exports = fibonacci; 6 | -------------------------------------------------------------------------------- /JavaScript/lib/find_duplicate.js: -------------------------------------------------------------------------------- 1 | // Given an array of strings, find the duplicate string. 2 | // Aim for linear time complexity. 3 | function findDuplicate(strings) { 4 | 5 | } 6 | module.exports = findDuplicate; 7 | -------------------------------------------------------------------------------- /JavaScript/lib/find_missing_letter.js: -------------------------------------------------------------------------------- 1 | // Implement a function that takes a string. 2 | // The string will contain all the letters of the alphabet, except for one. 3 | // Return the missing letter. 4 | // Ignore spaces. 5 | // Linear time complexity is achievable. 6 | function findMissingLetter(string) { 7 | 8 | } 9 | module.exports = findMissingLetter; 10 | -------------------------------------------------------------------------------- /JavaScript/lib/find_non_duplicate_character.js: -------------------------------------------------------------------------------- 1 | // Implement a function that takes a string. 2 | // Return the first character that isn't duplicated within the string. 3 | // Linear time complexity is achievable. 4 | function findNonDuplicateCharacter(string) { 5 | 6 | } 7 | module.exports = findNonDuplicateCharacter; 8 | -------------------------------------------------------------------------------- /JavaScript/lib/folding_cipher.js: -------------------------------------------------------------------------------- 1 | // Implement the Folding Cipher. 2 | // It folds the alphabet in half and uses the adjacent letter. 3 | // a <=> z, b <=> y, c <=> x, m <=> n. 4 | function foldingCipher(plainText) { 5 | 6 | } 7 | module.exports = foldingCipher; 8 | -------------------------------------------------------------------------------- /JavaScript/lib/hash_table.js: -------------------------------------------------------------------------------- 1 | // Implement a hash table (also called a hash map). 2 | // Compute hash codes using the given lose lose hash function. 3 | class HashTable { 4 | constructor() { 5 | this.table = []; 6 | } 7 | get() {} 8 | loseloseHashCode(key) { 9 | let hash = 0; 10 | for (let i = 0; i < key.length; i++) { hash += key.charCodeAt(i); } 11 | return hash % 37; 12 | } 13 | put() {} 14 | remove() {} 15 | } 16 | module.exports = HashTable; -------------------------------------------------------------------------------- /JavaScript/lib/is_palindrome.js: -------------------------------------------------------------------------------- 1 | // Write a function that takes a string and returns true if it's a palindrome, false if it's not. 2 | // Your solution should take less time and memory than rebuilding the string backward and comparing the two. 3 | function isPalindrome(number) { 4 | 5 | } 6 | module.exports = isPalindrome; 7 | -------------------------------------------------------------------------------- /JavaScript/lib/lcs.js: -------------------------------------------------------------------------------- 1 | // Given an array of integers (positive and negative) find the largest contiguous subsum (sum of a subarray). 2 | // You can solve this trivially in O(n**2) time by considering all subarrays. 3 | // Try to solve it in O(n) time with O(1) memory. 4 | function lcs(numbers) { 5 | 6 | } 7 | module.exports = lcs; 8 | -------------------------------------------------------------------------------- /JavaScript/lib/max_region_sum.js: -------------------------------------------------------------------------------- 1 | // Given a matrix of integers and coordinates of a rectangular region within the matrix. 2 | // Find the sum of numbers falling inside the rectangle. 3 | // Time complexity: O(number of rows * number of columns). 4 | function maxRegionSum(matrix, topLeftCoords, bottomRightCoords) { 5 | 6 | } 7 | module.exports = maxRegionSum; 8 | -------------------------------------------------------------------------------- /JavaScript/lib/pair_sum.js: -------------------------------------------------------------------------------- 1 | // Given an array of integers, return all pairs that sum up to a specified value k. 2 | // List the pairs in [min, max] order. 3 | // Time complexity: O(n). 4 | // Return an array. 5 | function pairSum(numbers, k) { 6 | 7 | } 8 | module.exports = pairSum; 9 | -------------------------------------------------------------------------------- /JavaScript/lib/recursive_sum.js: -------------------------------------------------------------------------------- 1 | // Write a function that takes an array of integers and returns their sum. 2 | // Use recursion. 3 | function recursiveSum(numbers) { 4 | 5 | } 6 | module.exports = recursiveSum; 7 | -------------------------------------------------------------------------------- /JavaScript/lib/silly_years.js: -------------------------------------------------------------------------------- 1 | // Write a function that takes a year (four digit integer) and returns an array with the 10 closest subsequent years that meet the following condition: 2 | // the first two digits summed with the last two digits are equal to the middle two digits. 3 | function sillyYears(years) { 4 | 5 | } 6 | module.exports = sillyYears; 7 | -------------------------------------------------------------------------------- /JavaScript/lib/stack.js: -------------------------------------------------------------------------------- 1 | // Implement the stack data structure. 2 | class Stack { 3 | constructor() { 4 | this.items = []; 5 | } 6 | clear() {} 7 | isEmpty() {} 8 | peek() {} 9 | pop() {} 10 | push() {} 11 | size() {} 12 | toString() { return this.items.toString() } 13 | } 14 | module.exports = Stack; -------------------------------------------------------------------------------- /JavaScript/lib/unique_substrings.js: -------------------------------------------------------------------------------- 1 | // Write a method that finds all the unique substrings for a word. 2 | function uniqueSubstrings(string) { 3 | 4 | } 5 | module.exports = uniqueSubstrings; 6 | -------------------------------------------------------------------------------- /JavaScript/lib/valid_ip.js: -------------------------------------------------------------------------------- 1 | // Write a method that takes a string as input. 2 | // It should return true if the input is a valid IPv4 address. 3 | // Valid IPs are anything between '0.0.0.0' and '255.255.255.255'. 4 | function validIp(string) { 5 | 6 | } 7 | module.exports = validIp; 8 | -------------------------------------------------------------------------------- /JavaScript/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "devDependencies": { 3 | "jest": "^27" 4 | }, 5 | "scripts": { 6 | "test": "jest" 7 | }, 8 | "dependencies": { 9 | "ansi-regex": "^5.0.1", 10 | "braces": "^2.3.1", 11 | "glob-parent": "^5.1.2", 12 | "merge": "^2.1.1", 13 | "node-notifier": "^8.0.1", 14 | "set-value": "^4.0.1", 15 | "yargs-parser": "^13.1.2" 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /JavaScript/test/caesar_cipher.test.js: -------------------------------------------------------------------------------- 1 | const caesarCipher = require('../lib/caesar_cipher'); 2 | 3 | test("'hello' should return 'lipps'", () => { 4 | expect(caesarCipher('hello', 4)).toBe('lipps'); 5 | }); 6 | 7 | test("'abc' should return 'abc'", () => { 8 | expect(caesarCipher('abc', 26)).toBe('abc'); 9 | }); 10 | -------------------------------------------------------------------------------- /JavaScript/test/common_substrings.test.js: -------------------------------------------------------------------------------- 1 | const commonSubstrings = require('../lib/common_substrings'); 2 | 3 | test("'Hello' and 'Hello World' should return 'Hello'", () => { 4 | expect(commonSubstrings('Hello', 'Hello World')).toBe(5); 5 | }); 6 | 7 | test("'ABABC' and 'BABCA' should return 'BABC'", () => { 8 | expect(commonSubstrings('ABABC', 'BABCA')).toBe(4); 9 | }); 10 | -------------------------------------------------------------------------------- /JavaScript/test/decimal_to_binary.test.js: -------------------------------------------------------------------------------- 1 | const decimalToBinary = require('../lib/decimal_to_binary'); 2 | 3 | test('0 should return 0', () => { 4 | expect(decimalToBinary(0)).toBe("0"); 5 | }); 6 | 7 | test('1 should return 1', () => { 8 | expect(decimalToBinary(1)).toBe("1"); 9 | }); 10 | 11 | test('17 should return 10001 ', () => { 12 | expect(decimalToBinary(17)).toBe("10001"); 13 | }); 14 | -------------------------------------------------------------------------------- /JavaScript/test/dictionary.test.js: -------------------------------------------------------------------------------- 1 | const Dictionary = require('../lib/dictionary'); 2 | 3 | require('../lib/dictionary'); 4 | 5 | test("is implemented using a JavaScript object", () => { 6 | const dictionary = new Dictionary(); 7 | expect(typeof dictionary.items).toBe("object"); 8 | }); 9 | 10 | test("set", () => { 11 | const dictionary = new Dictionary(); 12 | dictionary.set("Foo", "Bar"); 13 | expect(dictionary.size()).toBe(1) 14 | }); 15 | 16 | test("delete", () => { 17 | const dictionary = new Dictionary(); 18 | dictionary.set("Foo", "Bar"); 19 | expect(dictionary.has("Foo")).toBe(true); 20 | dictionary.delete("Foo"); 21 | expect(dictionary.get("Foo")).toBe(undefined); 22 | }); 23 | 24 | test("has", () => { 25 | const dictionary = new Dictionary(); 26 | expect(dictionary.has("Luna")).toBe(false); 27 | dictionary.set("Luna", "Moon"); 28 | expect(dictionary.has("Luna")).toBe(true); 29 | }); 30 | 31 | test("get", () => { 32 | const dictionary = new Dictionary(); 33 | dictionary.set("Luna", "Moon"); 34 | expect(dictionary.get("Luna")).toBe("Moon"); 35 | }); 36 | 37 | test("clear", () => { 38 | const dictionary = new Dictionary(); 39 | dictionary.set("1", "2"); 40 | dictionary.set("2", "4"); 41 | dictionary.set("4", "8"); 42 | dictionary.clear(); 43 | expect(dictionary.size()).toBe(0); 44 | }); 45 | 46 | test("size", () => { 47 | const dictionary = new Dictionary(); 48 | dictionary.set("1", "2"); 49 | dictionary.set("2", "4"); 50 | dictionary.set("4", "8"); 51 | expect(dictionary.size()).toBe(3); 52 | }); 53 | 54 | test("keys", () => { 55 | const dictionary = new Dictionary(); 56 | dictionary.set("1", "2"); 57 | dictionary.set("2", "4"); 58 | dictionary.set("4", "8"); 59 | expect(dictionary.keys()).toBe(["1", "2", "4"]); 60 | }); 61 | 62 | test("values", () => { 63 | const dictionary = new Dictionary(); 64 | dictionary.set("1", "2"); 65 | dictionary.set("2", "4"); 66 | dictionary.set("4", "8"); 67 | expect(dictionary.values()).toBe(["2", "4", "8"]); 68 | }); 69 | -------------------------------------------------------------------------------- /JavaScript/test/digital_root.test.js: -------------------------------------------------------------------------------- 1 | const digitalRoot = require('../lib/digital_root'); 2 | 3 | test('65,536 should return 7', () => { 4 | expect(digitalRoot(65536)).toBe(7); 5 | }); 6 | 7 | test('1,853 should return 8', () => { 8 | expect(digitalRoot(1853)).toBe(8); 9 | }); 10 | -------------------------------------------------------------------------------- /JavaScript/test/fast_intersection.test.js: -------------------------------------------------------------------------------- 1 | const fastIntersection = require('../lib/fast_intersection'); 2 | 3 | test('{1, 2, 3, 4, 5} ∩ {2, 3, 4} = {2, 3, 4}', () => { 4 | const arrayOne = [1, 2, 3, 4, 5]; 5 | const arrayTwo = [2, 3, 4]; 6 | const intersection = [2, 3, 4]; 7 | 8 | expect(fastIntersection(arrayOne, arrayTwo).sort()).toEqual(intersection.sort()); 9 | }); 10 | 11 | test('{1, 3, 5, 7, 9} ∩ {2, 4, 5, 6, 8} = {5}', () => { 12 | const arrayOne = [1, 3, 5, 7, 9]; 13 | const arrayTwo = [2, 4, 5, 6, 8]; 14 | const intersection = [5]; 15 | 16 | expect(fastIntersection(arrayOne, arrayTwo).sort()).toEqual(intersection.sort()); 17 | }); 18 | 19 | -------------------------------------------------------------------------------- /JavaScript/test/fibonacci.test.js: -------------------------------------------------------------------------------- 1 | const fibonacci = require('../lib/fibonacci'); 2 | 3 | test("3 should return [0, 1, 1]", () => { 4 | expect(fibonacci(3)).toEqual([0, 1, 1]); 5 | }); 6 | 7 | test("5 should return [0, 1, 1, 2, 3]", () => { 8 | expect(fibonacci(5)).toEqual([0, 1, 1, 2, 3]); 9 | }); 10 | -------------------------------------------------------------------------------- /JavaScript/test/find_duplicate.test.js: -------------------------------------------------------------------------------- 1 | const findDuplicate = require('../lib/find_duplicate'); 2 | 3 | test("returns the duplicated character", () => { 4 | const characters = ["a", "b", "c", "d", "d", "e"]; 5 | 6 | expect(findDuplicate(characters)).toEqual("d"); 7 | }); 8 | -------------------------------------------------------------------------------- /JavaScript/test/find_missing_letter.test.js: -------------------------------------------------------------------------------- 1 | const findMissingLetter = require('../lib/find_missing_letter'); 2 | 3 | test("returns the missing letter", () => { 4 | const string = "the quick brown box jumps over a lazy dog"; 5 | expect(findMissingLetter(string)).toEqual("f"); 6 | }); 7 | -------------------------------------------------------------------------------- /JavaScript/test/find_non_duplicate_character.test.js: -------------------------------------------------------------------------------- 1 | const findNonDuplicateCharacter = require('../lib/find_non_duplicate_character'); 2 | 3 | test("returns the non-duplicate character", () => { 4 | const string = "minimum"; 5 | expect(findNonDuplicateCharacter(string)).toEqual("n"); 6 | }); 7 | -------------------------------------------------------------------------------- /JavaScript/test/folding_cipher.test.js: -------------------------------------------------------------------------------- 1 | const foldingCipher = require('../lib/folding_cipher'); 2 | 3 | test("'abcm' should return 'zyxn'", () => { 4 | expect(foldingCipher('abcm')).toBe('zyxn'); 5 | }); 6 | 7 | test("'zyxn' should return 'abcm'", () => { 8 | expect(foldingCipher('zyxn')).toBe('abcm'); 9 | }); 10 | -------------------------------------------------------------------------------- /JavaScript/test/hash_table.test.js: -------------------------------------------------------------------------------- 1 | const HashTable = require('../lib/hash_table'); 2 | 3 | test("is implemented using a JavaScript array", () => { 4 | const hashTable = new HashTable(); 5 | expect(Array.isArray(hashTable.table)).toBe(true); 6 | }); 7 | 8 | test("preserves the given lose lose hash function", () => { 9 | const hashTable = new HashTable(); 10 | expect(hashTable.loseloseHashCode("foo")).toBe(28); 11 | }); 12 | 13 | test("get", () => { 14 | let hashTable = new HashTable(); 15 | hashTable.put("foo", "bar"); 16 | expect(hashTable.get("foo")).toBe("bar"); 17 | }); 18 | 19 | test("remove", () => { 20 | let hashTable = new HashTable(); 21 | hashTable.put("foo", "bar"); 22 | expect(hashTable.get("foo")).toBe("bar"); 23 | hashTable.remove("foo") 24 | expect(hashTable.get("foo")).toBe(undefined); 25 | }); 26 | 27 | 28 | -------------------------------------------------------------------------------- /JavaScript/test/is_palindrome.test.js: -------------------------------------------------------------------------------- 1 | const isPalindrome = require('../lib/is_palindrome'); 2 | 3 | test("'ricercar' should return false", () => { 4 | expect(isPalindrome('ricercar')).toBe(false); 5 | }); 6 | 7 | test("'racecar' should return true", () => { 8 | expect(isPalindrome('racecar')).toBe(true); 9 | }); 10 | -------------------------------------------------------------------------------- /JavaScript/test/lcs.test.js: -------------------------------------------------------------------------------- 1 | const lcs = require('../lib/lcs'); 2 | 3 | test("should return the largest subsum", () => { 4 | let array = [4, -1, 5, 6, -13, 2]; 5 | expect(lcs(array)).toBe(14); 6 | }); 7 | 8 | test("should return the largest subsum", () => { 9 | let array = [-2, 1, -3, 4, -1, 2, 1, -5, 4]; 10 | expect(lcs(array)).toBe(6); 11 | }); 12 | -------------------------------------------------------------------------------- /JavaScript/test/max_region_sum.test.js: -------------------------------------------------------------------------------- 1 | const maxRegionSum = require('../lib/max_region_sum'); 2 | 3 | test("should return the sum of the elements within the coordinates", () => { 4 | let matrix = [[1, 2, 3], [2, 3, 4], [3, 4, 5]]; 5 | let topLeftCoords = [0, 0]; 6 | let bottomRightCoords = [1, 1]; 7 | expect(matrixRegionSum(matrix, topLeftCoords, bottomRightCoords)).toBe(8); 8 | }); 9 | 10 | test("should return the sum of the elements within the coordinates", () => { 11 | let matrix = [[2, 3, 4], [3, 4, 5], [4, 5, 6]]; 12 | let topLeftCoords = [0, 0]; 13 | let bottomRightCoords = [2, 2]; 14 | expect(matrixRegionSum(matrix, topLeftCoords, bottomRightCoords)).toBe(36); 15 | }); 16 | -------------------------------------------------------------------------------- /JavaScript/test/pair_sum.test.js: -------------------------------------------------------------------------------- 1 | const pairSum = require('../lib/pair_sum'); 2 | 3 | test("should return all the pairs that sum to 0", () => { 4 | let array = [[-1, 1]]; 5 | expect(pairSum([1, 2, -1], 0)).toEqual(array); 6 | }); 7 | 8 | test("should return all the pairs that sum to 1", () => { 9 | let array = [[-1, 2]]; 10 | expect(pairSum([1, 2, -1, -1, -2], 1)).toEqual(array); 11 | }); 12 | -------------------------------------------------------------------------------- /JavaScript/test/recursive_sum.test.js: -------------------------------------------------------------------------------- 1 | const recursiveSum = require('../lib/recursive_sum'); 2 | 3 | test("[1, 2, 3] should return 6", () => { 4 | expect(recursiveSum([1, 2, 3])).toBe(6); 5 | }); 6 | 7 | test("[99, 66, 33] should return 198", () => { 8 | expect(recursiveSum([99, 66, 33])).toBe(198); 9 | }); 10 | -------------------------------------------------------------------------------- /JavaScript/test/silly_years.test.js: -------------------------------------------------------------------------------- 1 | const sillyYears = require('../lib/silly_years'); 2 | 3 | test("should return the ten subsequent silly years", () => { 4 | let array = [2307, 2417, 2527, 2637, 2747, 2857, 2967, 3406, 3516, 3626]; 5 | expect(sillyYears(1978)).toEqual(array); 6 | }); 7 | 8 | test("should return the ten subsequent silly years", () => { 9 | let array = [2417, 2527, 2637, 2747, 2857, 2967, 3406, 3516, 3626, 3736]; 10 | expect(sillyYears(2307)).toEqual(array); 11 | }); 12 | -------------------------------------------------------------------------------- /JavaScript/test/stack.test.js: -------------------------------------------------------------------------------- 1 | const Stack = require('../lib/stack'); 2 | 3 | test("clear", () => { 4 | const stack = new Stack(); 5 | stack.push(1); 6 | stack.push(2); 7 | stack.push(3); 8 | stack.clear() 9 | expect(stack.toString()).toBe(""); 10 | }); 11 | 12 | test("isEmpty", () => { 13 | const stack = new Stack(); 14 | stack.push(1); 15 | expect(stack.isEmpty()).toBe(false); 16 | stack.pop(); 17 | expect(stack.isEmpty()).toBe(true); 18 | }); 19 | 20 | test("peek", () => { 21 | const stack = new Stack(); 22 | stack.push(1); 23 | stack.push(2); 24 | expect(stack.peek()).toBe(2) 25 | }); 26 | 27 | test("pop", () => { 28 | const stack = new Stack(); 29 | stack.push(1); 30 | stack.push(2); 31 | stack.push(3); 32 | expect(stack.pop()).toBe(3) 33 | expect(stack.toString()).toBe("1,2"); 34 | }); 35 | 36 | test("push", () => { 37 | const stack = new Stack(); 38 | stack.push(1); 39 | expect(stack.toString()).toBe("1"); 40 | }); 41 | 42 | test("size", () => { 43 | const stack = new Stack(); 44 | expect(stack.size()).toBe(0) 45 | stack.push("foo"); 46 | stack.push("bar"); 47 | stack.push("baz"); 48 | expect(stack.size()).toBe(3); 49 | }); 50 | 51 | 52 | 53 | -------------------------------------------------------------------------------- /JavaScript/test/unique_substrings.test.js: -------------------------------------------------------------------------------- 1 | const uniqueSubstrings = require('../lib/unique_substrings'); 2 | 3 | test("should return all the substrings", () => { 4 | let array = ["a", "b", "c", "d", "ab", "bc", "cd", "abc", "bcd", "abcd"]; 5 | expect(uniqueSubstrings('abcd').sort()).toEqual(array.sort()); 6 | }); 7 | 8 | it("should not return repeats", function() { 9 | let array = ["d", "du", "dud", "dude", "u", "ud", "ude", "de", "e"]; 10 | expect(uniqueSubstrings('dude').sort()).toEqual(array.sort()); 11 | }); 12 | -------------------------------------------------------------------------------- /JavaScript/test/valid_ip.test.js: -------------------------------------------------------------------------------- 1 | const valid_ip = require('../lib/valid_ip'); 2 | 3 | test("1.1.1.1", () => { 4 | expect(valid_ip("1.1.1.1")).toBe(true); 5 | }); 6 | 7 | test("256.2.2.2", () => { 8 | expect(valid_ip("256.2.2.2")).toBe(false); 9 | }); 10 | 11 | test("1.1.1.1.1", () => { 12 | expect(valid_ip("1.1.1.1.1")).toBe(false); 13 | }); 14 | -------------------------------------------------------------------------------- /Python/lib/caesar_cipher.py: -------------------------------------------------------------------------------- 1 | # Write a function that takes a message and an increment amount. 2 | # Output the same letters shifted by that amount in the alphabet. 3 | # Assume lowercase and no punctuation. 4 | # Preserve spaces. 5 | def caesar_cipher(string, shift): 6 | return "" 7 | -------------------------------------------------------------------------------- /Python/lib/character_count.py: -------------------------------------------------------------------------------- 1 | # Take an array of strings 2 | # Return the total number of characters 3 | # Use recursion 4 | def character_count(array): 5 | return 0 6 | -------------------------------------------------------------------------------- /Python/lib/common_substrings.py: -------------------------------------------------------------------------------- 1 | # Write a function that takes two strings. 2 | # Return the longest common substring. 3 | def common_substrings(string_one, string_two): 4 | return "" 5 | -------------------------------------------------------------------------------- /Python/lib/digital_root.py: -------------------------------------------------------------------------------- 1 | # Write a method that will sum the digits of a positive integer. 2 | # If it is greater than or equal to 10, sum the digits of the resulting number. 3 | # Keep repeating until there is only one digit in the result. 4 | # The result is called a 'digital root'. 5 | # Do not use string conversion within your method. 6 | def digital_root(number): 7 | return 0 8 | -------------------------------------------------------------------------------- /Python/lib/fibonacci.py: -------------------------------------------------------------------------------- 1 | # Write a function that takes n, the length of the sequence. 2 | # Return the first n elements from the Fibonacci sequence as an array. 3 | 4 | def fibonacci(n): 5 | return 0 -------------------------------------------------------------------------------- /Python/lib/fibs.py: -------------------------------------------------------------------------------- 1 | # Write a function that takes n, the length of the sequence. 2 | # Return the first n elements from the Fibonacci sequence as an array. 3 | def fibs(n): 4 | return [] 5 | -------------------------------------------------------------------------------- /Python/lib/folding_cipher.py: -------------------------------------------------------------------------------- 1 | # Implement the Folding Cipher. 2 | # It folds the alphabet in half and uses the adjacent letter. 3 | # a -> z, b -> y, c -> x, m -> n, etc... 4 | def folding_cipher(string): 5 | return "" 6 | -------------------------------------------------------------------------------- /Python/lib/is_palindrome.py: -------------------------------------------------------------------------------- 1 | # Write a function that takes a string. 2 | # Return true if the string is a palindrome, otherwise return false. 3 | # It should take less time and memory than reversing the string. 4 | def is_palindrome(s): 5 | return True 6 | -------------------------------------------------------------------------------- /Python/lib/recursive_sum.py: -------------------------------------------------------------------------------- 1 | # Write a function that takes an array of integers and returns their sum. 2 | # Use recursion. 3 | def recursive_sum(lst): 4 | return 0 5 | -------------------------------------------------------------------------------- /Python/lib/reverse.py: -------------------------------------------------------------------------------- 1 | # Write a method that takes a string 2 | # Return a new string with all the characters in reverse order 3 | # Consider using a stack 4 | def reverse(string): 5 | return "" 6 | -------------------------------------------------------------------------------- /Python/lib/subsets.py: -------------------------------------------------------------------------------- 1 | # Write a function that takes an array and returns all of its subsets. 2 | def subsets(array): 3 | return [] 4 | -------------------------------------------------------------------------------- /Python/lib/unique_substrings.py: -------------------------------------------------------------------------------- 1 | # Write a method that finds all the unique substrings for a word. 2 | def unique_substrings(string): 3 | return "" 4 | -------------------------------------------------------------------------------- /Python/lib/valid_ip.py: -------------------------------------------------------------------------------- 1 | # Write a method that takes a string as input. 2 | # It should return true if the input is a valid IPv4 address. 3 | # Valid IPs are anything between '0.0.0.0' and '255.255.255.255'. 4 | def valid_ip(string): 5 | return True 6 | -------------------------------------------------------------------------------- /Python/test/test_caesar_cipher.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | from lib.caesar_cipher import caesar_cipher 3 | 4 | class TestCaesarCipher(unittest.TestCase): 5 | def test_caesar_cipher(self): 6 | self.assertEqual(caesar_cipher("hello", 4), "lipps") 7 | self.assertEqual(caesar_cipher("abc", 0), "abc") 8 | self.assertEqual(caesar_cipher("asdf asdf", 13), "nfqs nfqs") 9 | 10 | if __name__ == '__main__': 11 | unittest.main() 12 | -------------------------------------------------------------------------------- /Python/test/test_character_count.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | from lib.character_count import character_count 3 | 4 | class TestCharacterCount(unittest.TestCase): 5 | def test_character_count(self): 6 | self.assertEqual(character_count(["a", "bc", "def"]), 6) 7 | 8 | if __name__ == '__main__': 9 | unittest.main() 10 | -------------------------------------------------------------------------------- /Python/test/test_common_substrings.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | from lib.common_substrings import common_substrings 3 | 4 | class TestCommonSubstrings(unittest.TestCase): 5 | def test_common_substrings(self): 6 | self.assertEqual(common_substrings("Hello", "Hellow World"), "Hello") 7 | self.assertEqual(common_substrings("ABABC", "BABCA"), "BABC") 8 | 9 | if __name__ == '__main__': 10 | unittest.main() 11 | -------------------------------------------------------------------------------- /Python/test/test_digital_root.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | from lib.digital_root import digital_root 3 | 4 | class TestDigitalRoot(unittest.TestCase): 5 | def test_digital_root(self): 6 | self.assertEqual(digital_root(65536), 7) 7 | self.assertEqual(digital_root(1853), 8) 8 | 9 | if __name__ == '__main__': 10 | unittest.main() 11 | -------------------------------------------------------------------------------- /Python/test/test_fibonacci.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | from lib.fibonacci import fibonacci 3 | 4 | class TestFibonacci(unittest.TestCase): 5 | def test_fibonacci(self): 6 | self.assertEqual(fibonacci(3), [0, 1, 1]) 7 | self.assertEqual(fibonacci(5), [0, 1, 1, 2, 3]) 8 | 9 | if __name__ == '__main__': 10 | unittest.main() -------------------------------------------------------------------------------- /Python/test/test_fibs.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | from lib.fibs import fibs 3 | 4 | class TestFibs(unittest.TestCase): 5 | def test_fibs(self): 6 | self.assertEqual(fibs(3), [0, 1, 1]) 7 | self.assertEqual(fibs(5), [0, 1, 1, 2, 3]) 8 | 9 | if __name__ == '__main__': 10 | unittest.main() 11 | -------------------------------------------------------------------------------- /Python/test/test_folding_cipher.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | from lib.folding_cipher import folding_cipher 3 | 4 | class TestFoldingCipher(unittest.TestCase): 5 | def test_folding_cipher(self): 6 | self.assertEqual(folding_cipher("abcm"), "zyxn") 7 | self.assertEqual(folding_cipher("zyxn"), "abcm") 8 | 9 | if __name__ == '__main__': 10 | unittest.main() 11 | -------------------------------------------------------------------------------- /Python/test/test_is_palindrome.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | from lib.is_palindrome import is_palindrome 3 | 4 | class TestIsPalindrome(unittest.TestCase): 5 | def test_is_palindrome(self): 6 | self.assertFalse(is_palindrome('ricercar')) 7 | self.assertTrue(is_palindrome('racecar')) 8 | 9 | if __name__ == '__main__': 10 | unittest.main() 11 | -------------------------------------------------------------------------------- /Python/test/test_recursive_sum.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | from lib.recursive_sum import recursive_sum 3 | 4 | class TestRecursiveSum(unittest.TestCase): 5 | def test_recursive_sum(self): 6 | self.assertEqual(recursive_sum([1, 2, 3]), 6) 7 | self.assertEqual(recursive_sum([99, 66, 33]), 198) 8 | 9 | if __name__ == '__main__': 10 | unittest.main() 11 | -------------------------------------------------------------------------------- /Python/test/test_reverse.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | from lib.reverse import reverse 3 | 4 | class TestReverse(unittest.TestCase): 5 | def test_reverse(self): 6 | self.assertEqual(reverse("abcd"), "dcba") 7 | 8 | if __name__ == '__main__': 9 | unittest.main() 10 | -------------------------------------------------------------------------------- /Python/test/test_subsets.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | from lib.subsets import subsets 3 | 4 | class TestSubsets(unittest.TestCase): 5 | def test_subsets(self): 6 | self.assertEqual(sorted(map(tuple, subsets([]))), sorted(map(tuple, [[]]))) 7 | self.assertEqual( 8 | sorted(map(tuple, subsets([1, 2, 3]))), 9 | sorted(map(tuple, [[], [1], [2], [3], [1, 2], [1, 3], [2, 3], [1, 2, 3]])) 10 | ) 11 | 12 | if __name__ == '__main__': 13 | unittest.main() 14 | -------------------------------------------------------------------------------- /Python/test/test_unique_substrings.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | from lib.unique_substrings import unique_substrings 3 | 4 | class TestUniqueSubstrings(unittest.TestCase): 5 | def test_uniq_substrings(self): 6 | array = ['a', 'b', 'c', 'd', 'ab', 'bc', 'cd', 'abc', 'bcd', 'abcd'] 7 | self.assertEqual(set(unique_substrings("abcd")), set(array)) 8 | 9 | def test_no_repeats(self): 10 | array = ['d', 'du', 'dud', 'dude', 'u', 'ud', 'ude', 'de', 'e'] 11 | self.assertEqual(set(unique_substrings("dude")), set(array)) 12 | 13 | if __name__ == '__main__': 14 | unittest.main() 15 | -------------------------------------------------------------------------------- /Python/test/test_valid_ip.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | from lib.valid_ip import valid_ip 3 | 4 | class TestValidIp(unittest.TestCase): 5 | def test_valid_ip(self): 6 | self.assertTrue(valid_ip("1.1.1.1")) 7 | self.assertFalse(valid_ip("256.2.2.2")) 8 | self.assertFalse(valid_ip("1.1.1.1.1")) 9 | 10 | if __name__ == '__main__': 11 | unittest.main() 12 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | This repository includes a series of tests that can be used for practicing algorithms in the _Javascript_, _Python_, and _Ruby_ programming languages. 2 | To get started, clone this repo locally: 3 | ``` 4 | git clone https://github.com/jaysonvirissimo/practice-thy-algorithms.git 5 | ``` 6 | 7 | ## JavaScript Instructions 8 | Make sure you have the [npm](https://www.npmjs.com/) package manager installed. 9 | Navigate to the `JavaScript` directory and run `npm install`. 10 | The problem statements are inside the appropriately named files in `lib`. 11 | This is also where you write your own code. 12 | Run `npm test` when you are ready for some feedback on your attempt. 13 | 14 | ## Python Instructions 15 | Ensure Python is installed. 16 | Navigate to the `Python` directory. 17 | Add your solutions in the `lib` directory. 18 | Run the tests with `python -m unittest discover -s test`. 19 | 20 | ## Ruby Instructions 21 | Make sure you have `bundler` installed. 22 | Navigate to the `Ruby` directory and run `bundle install`. 23 | The problem statements are inside the appropriately named file in `lib`. 24 | This is also where you write your own code. 25 | Run `rspec` when you are ready for some feedback on your attempt. 26 | 27 | ## Problems 28 | | Name | JavaScript | Ruby | Python | 29 | |:----------------------------:|:----------:|:----:|:------:| 30 | | Digital Root | x | x | x | 31 | | Caesar Cipher | x | x | x | 32 | | Common Substrings | x | x | x | 33 | | Recursive Sum | x | x | x | 34 | | Fibonacci Sequence | x | x | x | 35 | | Palindrome | x | x | x | 36 | | Valid IP | x | x | x | 37 | | Folding Cipher | x | x | x | 38 | | Unique Substrings | x | x | x | 39 | | Largest Contiguous Subsum | x | x | | 40 | | Silly Years | x | x | | 41 | | Pair Sum | x | x | | 42 | | Matrix Region Sum | x | x | | 43 | | Merge Sort | | x | | 44 | | Binary Search | | x | | 45 | | Productify | | x | | 46 | | Subsets | | x | x | 47 | | Longest Palindrome | | x | | 48 | | Fast Intersection | x | x | | 49 | | Common Subsets | | x | | 50 | | Can Win | | x | | 51 | | Weighted Random Index | | x | | 52 | | Move Zeros | | x | | 53 | | Look and Say | | x | | 54 | | Sum Upon Sums | | x | | 55 | | Max Stack | | x | | 56 | | Stack Queue | | x | | 57 | | Windowed Max Range | | x | | 58 | | File List | | x | | 59 | | Find Missing Number | | x | | 60 | | Is Shuffle? | | x | | 61 | | Decimal to Binary | x | x | | 62 | | Recursive Factorial | | x | | 63 | | Iterative Factorial | | x | | 64 | | Permutations | | x | | 65 | | Dictionary | x | | | 66 | | Hash Table | x | | | 67 | | Minimum Coin Change | | x | | 68 | | Stack | x | x | | 69 | | Find Duplicate | x | | | 70 | | Find Missing Letter | x | | | 71 | | Find Non-Duplicate Character | x | | | 72 | | Queue | | x | | 73 | | Reverse String | | x | x | 74 | | Recursive Print | | x | | 75 | | Recursive Character Count | | x | x | 76 | | Select Even | | x | | 77 | | Triangle Number | | x | | 78 | | Index of X | | x | | 79 | | Uniques Paths | | x | | 80 | | Add Until 100 | | x | | 81 | | Golomb Sequence | | x | | 82 | -------------------------------------------------------------------------------- /Ruby/.rspec: -------------------------------------------------------------------------------- 1 | --color 2 | --format documentation 3 | -------------------------------------------------------------------------------- /Ruby/Gemfile: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | source "https://rubygems.org" 4 | 5 | git_source(:github) { |repo_name| "https://github.com/#{repo_name}" } 6 | 7 | ruby "3.1.3" 8 | 9 | gem "rspec" 10 | gem "rspec-benchmark" 11 | -------------------------------------------------------------------------------- /Ruby/Gemfile.lock: -------------------------------------------------------------------------------- 1 | GEM 2 | remote: https://rubygems.org/ 3 | specs: 4 | benchmark-malloc (0.2.0) 5 | benchmark-perf (0.6.0) 6 | benchmark-trend (0.4.0) 7 | diff-lcs (1.5.0) 8 | rspec (3.12.0) 9 | rspec-core (~> 3.12.0) 10 | rspec-expectations (~> 3.12.0) 11 | rspec-mocks (~> 3.12.0) 12 | rspec-benchmark (0.6.0) 13 | benchmark-malloc (~> 0.2) 14 | benchmark-perf (~> 0.6) 15 | benchmark-trend (~> 0.4) 16 | rspec (>= 3.0) 17 | rspec-core (3.12.1) 18 | rspec-support (~> 3.12.0) 19 | rspec-expectations (3.12.2) 20 | diff-lcs (>= 1.2.0, < 2.0) 21 | rspec-support (~> 3.12.0) 22 | rspec-mocks (3.12.3) 23 | diff-lcs (>= 1.2.0, < 2.0) 24 | rspec-support (~> 3.12.0) 25 | rspec-support (3.12.0) 26 | 27 | PLATFORMS 28 | ruby 29 | 30 | DEPENDENCIES 31 | rspec 32 | rspec-benchmark 33 | 34 | RUBY VERSION 35 | ruby 3.1.3p185 36 | 37 | BUNDLED WITH 38 | 2.4.7 39 | -------------------------------------------------------------------------------- /Ruby/lib/add_until_100.rb: -------------------------------------------------------------------------------- 1 | # Write a method that takes an array of numbers. 2 | # Sum the numbers in sequence, but skip a number if it would result in a sum over 100. 3 | # Use recursion. 4 | # Eliminate unecessary recursive calls. 5 | def add_until_100(array) 6 | end -------------------------------------------------------------------------------- /Ruby/lib/binary_search.rb: -------------------------------------------------------------------------------- 1 | # Implement binary search. 2 | # Return nil if the target isn't found. 3 | def binary_search(array, target) 4 | end 5 | -------------------------------------------------------------------------------- /Ruby/lib/caesar_cipher.rb: -------------------------------------------------------------------------------- 1 | # Write a function that takes a message and an increment amount. 2 | # Output the same letters shifted by that amount in the alphabet. 3 | # Assume lowercase and no punctuation. 4 | # Preserve spaces. 5 | def caesar_cipher(string, shift) 6 | end 7 | -------------------------------------------------------------------------------- /Ruby/lib/can_win.rb: -------------------------------------------------------------------------------- 1 | # You are given an array and index. 2 | # Find if it's possible to reach 0 by starting at the index. 3 | # You can only move left or right by the distance found at array[index]. 4 | def can_win?(array, index) 5 | end 6 | -------------------------------------------------------------------------------- /Ruby/lib/character_count.rb: -------------------------------------------------------------------------------- 1 | # Take an array of strings 2 | # Return the total number of characters 3 | # Use recursion 4 | def character_count(array) 5 | end 6 | -------------------------------------------------------------------------------- /Ruby/lib/common_subsets.rb: -------------------------------------------------------------------------------- 1 | # Write a function that takes two arrays of integers. 2 | # Returns an array with all the subsets commmon to both arrays. 3 | # Don't generate all subsets of both arrays, which would be exponential time. 4 | # Instead, directly generate the subsets of both. 5 | def common_subsets(array_one, array_two) 6 | end 7 | -------------------------------------------------------------------------------- /Ruby/lib/common_substrings.rb: -------------------------------------------------------------------------------- 1 | # Write a function that takes two strings. 2 | # Return the longest common substring. 3 | def common_substrings(string_one, string_two) 4 | end 5 | -------------------------------------------------------------------------------- /Ruby/lib/decimal_to_binary.rb: -------------------------------------------------------------------------------- 1 | # Write a function that takes an integer and returns it in binary form. 2 | # Consider using the stack data structure. 3 | def decimal_to_binary(integer) 4 | end 5 | -------------------------------------------------------------------------------- /Ruby/lib/digital_root.rb: -------------------------------------------------------------------------------- 1 | # Write a method that will sum the digits of a positive integer. 2 | # If it is greater than or equal to 10, sum the digits of the resulting number. 3 | # Keep repeating until there is only one digit in the result. 4 | # The result is called a 'digital root'. 5 | # Do not use string conversion within your method. 6 | def digital_root(number) 7 | 8 | end 9 | -------------------------------------------------------------------------------- /Ruby/lib/fast_intersection.rb: -------------------------------------------------------------------------------- 1 | # Given two arrays, find the intersection of both sets. 2 | # It should be trivial to write an O(n**2) solution. 3 | # Use sorting to solve in O(nlog(n)). 4 | # Next, improve this to O(n) time (maybe use a non-array datastructure). 5 | def fast_intersection(array_one, array_two) 6 | end 7 | -------------------------------------------------------------------------------- /Ruby/lib/fibonacci.rb: -------------------------------------------------------------------------------- 1 | # Write a function that takes n, the length of the sequence. 2 | # Return the first n elements from the Fibonacci sequence as an array. 3 | def fibonacci(n) 4 | end 5 | -------------------------------------------------------------------------------- /Ruby/lib/file_list.rb: -------------------------------------------------------------------------------- 1 | # Suppose a hash representing a directory. 2 | # All keys are strings with names for either folders or files. 3 | # Keys that are folders point to nested hashes. 4 | # Keys that are files point to "true". 5 | # Write a function that takes such a hash. 6 | # Return an array of strings with the path to each file in the hash. 7 | def file_list(hash) 8 | end 9 | -------------------------------------------------------------------------------- /Ruby/lib/find_missing_number.rb: -------------------------------------------------------------------------------- 1 | # Assume an array of non-negative integers. 2 | # A second array is made by shuffling the first and deleting a random element. 3 | # Given these two arrays, find which element is missing in the second array. 4 | # Do this in linear time with constant memory use. 5 | def find_missing_number(array_one, array_two) 6 | end 7 | -------------------------------------------------------------------------------- /Ruby/lib/folding_cipher.rb: -------------------------------------------------------------------------------- 1 | # Implement the Folding Cipher. 2 | # It folds the alphabet in half and uses the adjacent letter. 3 | # a -> z, b -> y, c -> x, m -> n, etc... 4 | def folding_cipher(string) 5 | end 6 | -------------------------------------------------------------------------------- /Ruby/lib/golomb.rb: -------------------------------------------------------------------------------- 1 | # Write a method that returns the Nth number in the Golomb sequence. 2 | # Use recursion. 3 | # Avoid unecessary method calls. 4 | # Consider memoization. 5 | def golomb(n) 6 | end -------------------------------------------------------------------------------- /Ruby/lib/in_words.rb: -------------------------------------------------------------------------------- 1 | # Write a method that takes an integer. 2 | # Return a string representation of the number using English words. 3 | # For example, 132 should return "one hundred and thirty two" 4 | def in_words(number) 5 | end -------------------------------------------------------------------------------- /Ruby/lib/index_of_x.rb: -------------------------------------------------------------------------------- 1 | # Write a method that takes a string. 2 | # Return the index of the first occurance of the letter x in the string. 3 | # All strings will have at least one letter x. 4 | # Use recursion. 5 | def index_of_x(string) 6 | 7 | end 8 | -------------------------------------------------------------------------------- /Ruby/lib/is_palindrome.rb: -------------------------------------------------------------------------------- 1 | # Write a function that takes a string. 2 | # Return true if the string is a palindrome, otherwise return false. 3 | # It should take less time and memory than reversing the string. 4 | def is_palindrome?(string) 5 | end 6 | -------------------------------------------------------------------------------- /Ruby/lib/is_shuffle.rb: -------------------------------------------------------------------------------- 1 | # Create a function that takes three strings. 2 | # Return whether the third is an interleaving of the first two. 3 | # Interleaving means it contains the same characters and preserves their order. 4 | def is_shuffle?(string_one, string_two, string_three) 5 | end 6 | -------------------------------------------------------------------------------- /Ruby/lib/iterative_factorial.rb: -------------------------------------------------------------------------------- 1 | # Write an iterative function that takes a number and returns its factorial. 2 | def iterative_factorial(number) 3 | end 4 | -------------------------------------------------------------------------------- /Ruby/lib/lcs.rb: -------------------------------------------------------------------------------- 1 | # Given an array of integers find the largest contiguous subsum. 2 | # You can solve this trivially in O(n**2) time by considering all subarrays. 3 | # Try to solve it in O(n) time with O(1) memory. 4 | def lcs(array) 5 | end 6 | -------------------------------------------------------------------------------- /Ruby/lib/longest_palindrome.rb: -------------------------------------------------------------------------------- 1 | # Return the indices of the start/end of the longest palindrome in the string. 2 | # You could reverse the string and compare it to the original, but that is slow. 3 | # Instead, you should be able to solve the problem with O(1) memory. 4 | def longest_palindrome(string) 5 | end 6 | -------------------------------------------------------------------------------- /Ruby/lib/look_and_say.rb: -------------------------------------------------------------------------------- 1 | # Implement the 'look and say' function. 2 | # 'Look and say' takes an input array and outputs an array. 3 | # The output describes the count of the elements in the input. 4 | def look_and_say(array) 5 | end 6 | -------------------------------------------------------------------------------- /Ruby/lib/matrix_region_sum.rb: -------------------------------------------------------------------------------- 1 | # Take a matrix of integers and coordinates. 2 | # The coordinates represent a rectangular region within the matrix 3 | # Find the sum of numbers falling inside the rectangle. 4 | # Time complexity: O(number of rows * number of columns). 5 | def matrix_region_sum(matrix, top_left_coords, bottom_right_coords) 6 | end 7 | -------------------------------------------------------------------------------- /Ruby/lib/max_stack.rb: -------------------------------------------------------------------------------- 1 | # Implement a stack with a max method that returns the maximum value. 2 | # It should run in O(1) time. 3 | class MaxStack 4 | end 5 | -------------------------------------------------------------------------------- /Ruby/lib/merge_sort.rb: -------------------------------------------------------------------------------- 1 | # Implement Merge Sort 2 | # Hint: This typically involves a helper function. 3 | def merge_sort(array) 4 | end 5 | 6 | def merge(left, right) 7 | end 8 | -------------------------------------------------------------------------------- /Ruby/lib/min_max_stack.rb: -------------------------------------------------------------------------------- 1 | # Write a MinMaxStack to track both the min and the max in a stack. 2 | class MinMaxStack 3 | end 4 | -------------------------------------------------------------------------------- /Ruby/lib/min_max_stack_queue.rb: -------------------------------------------------------------------------------- 1 | # Write a MinMaxStackQueue which tracks both the min and max. 2 | class MinMaxStackQueue 3 | end 4 | -------------------------------------------------------------------------------- /Ruby/lib/minimum_coin_change.rb: -------------------------------------------------------------------------------- 1 | # Implement MinimumCoinChange#make_change 2 | # When given an amount, it should return a collection of coins that: 3 | # 1. Add up to that amount 4 | # 2. Contains no more coins that are absolutely needed to sum to the amount 5 | class MinimumCoinChange 6 | def initialize(coins: AMERICAN_COINS) 7 | @coins = coins 8 | end 9 | 10 | def make_change(amount) 11 | end 12 | 13 | private 14 | 15 | attr_reader :coins 16 | 17 | AMERICAN_COINS = [1, 5, 10, 25].freeze 18 | end -------------------------------------------------------------------------------- /Ruby/lib/move_zeros.rb: -------------------------------------------------------------------------------- 1 | # Given an array, move all zeros to the end. 2 | # The order of non-zero elements does not matter. 3 | # Try to accomplish this in O(n) time and O(1) space. 4 | def move_zeros(array) 5 | end 6 | -------------------------------------------------------------------------------- /Ruby/lib/pair_sum.rb: -------------------------------------------------------------------------------- 1 | # Take an array of integers, and integer k. 2 | # Return all pairs that sum to k exactly. 3 | # List the pairs in [min, max] order. 4 | # Time complexity: O(n). 5 | # Return a set. 6 | def pair_sum(array, k) 7 | end 8 | -------------------------------------------------------------------------------- /Ruby/lib/permutations.rb: -------------------------------------------------------------------------------- 1 | # Write a method that takes an array and returns all its permutations. 2 | def permutations(array) 3 | end 4 | -------------------------------------------------------------------------------- /Ruby/lib/productify.rb: -------------------------------------------------------------------------------- 1 | # You are given a list of numbers in an array. 2 | # Replace all the numbers with the product of all other numbers. 3 | # Do this in O(n) time without using division. 4 | def productify(array) 5 | end 6 | -------------------------------------------------------------------------------- /Ruby/lib/queue.rb: -------------------------------------------------------------------------------- 1 | class Queue 2 | def initialize 3 | @items = [] 4 | end 5 | 6 | def clear 7 | end 8 | 9 | def empty? 10 | end 11 | 12 | def dequeue 13 | end 14 | 15 | def enqueue(item) 16 | end 17 | 18 | def peek 19 | end 20 | 21 | def size 22 | end 23 | 24 | def to_s 25 | items.to_s 26 | end 27 | 28 | private 29 | 30 | attr_reader :items 31 | end 32 | -------------------------------------------------------------------------------- /Ruby/lib/recursive_factorial.rb: -------------------------------------------------------------------------------- 1 | # Write a recursive function that takes a number and returns its factorial. 2 | def recursive_factorial(number) 3 | 4 | end 5 | -------------------------------------------------------------------------------- /Ruby/lib/recursive_print.rb: -------------------------------------------------------------------------------- 1 | # Take an array of numbers or arrays 2 | # The arrays can contain more numbers and other arrays, etc... 3 | # Print the numbers in order 4 | # Don't use flatten 5 | # Consider using recursion 6 | def recursive_print(array) 7 | 8 | end 9 | -------------------------------------------------------------------------------- /Ruby/lib/recursive_sum.rb: -------------------------------------------------------------------------------- 1 | # Write a function that takes an array of integers and returns their sum. 2 | # Use recursion. 3 | def recursive_sum(numbers) 4 | 5 | end 6 | -------------------------------------------------------------------------------- /Ruby/lib/reverse.rb: -------------------------------------------------------------------------------- 1 | require "stack" 2 | # Write a method that takes a string 3 | # Return a new string with all the characters in reverse order 4 | # Consider using a stack 5 | def reverse(string) 6 | 7 | end 8 | -------------------------------------------------------------------------------- /Ruby/lib/select_even.rb: -------------------------------------------------------------------------------- 1 | # Write a method that take an array of integers 2 | # Return a new array of only the even numbers from the input 3 | # Use recursion 4 | def select_even(integers) 5 | 6 | end 7 | -------------------------------------------------------------------------------- /Ruby/lib/silly_years.rb: -------------------------------------------------------------------------------- 1 | # Write a function that takes a year as a four digit integer. 2 | # Returns an array of the 10 closest subsequent silly years. 3 | # A silly year's first two digits plus the last two digits equal the middle two. 4 | def silly_years(year) 5 | end 6 | -------------------------------------------------------------------------------- /Ruby/lib/stack.rb: -------------------------------------------------------------------------------- 1 | # Implement the LIFO data structure 2 | class Stack 3 | def initialize 4 | @items = [] 5 | end 6 | 7 | def clear 8 | end 9 | 10 | def empty? 11 | end 12 | 13 | def peek 14 | end 15 | 16 | def pop 17 | end 18 | 19 | def push(item) 20 | end 21 | 22 | def size 23 | end 24 | 25 | def to_s 26 | items.to_s 27 | end 28 | 29 | private 30 | 31 | attr_reader :items 32 | end 33 | -------------------------------------------------------------------------------- /Ruby/lib/stack_queue.rb: -------------------------------------------------------------------------------- 1 | # Implement a queue using stacks. 2 | # That is, write enqueue and dequeue using only push and pop operations. 3 | # In terms of performance, enqueue should be O(1). 4 | # Dequeue may be worst-case O(n). 5 | # In terms of ammortized time, dequeue should be O(1). 6 | # Prove that your solution accomplishes this. 7 | class StackQueue 8 | end 9 | -------------------------------------------------------------------------------- /Ruby/lib/subsets.rb: -------------------------------------------------------------------------------- 1 | # Write a function that takes an array and returns all of its subsets. 2 | def subsets(array) 3 | end 4 | -------------------------------------------------------------------------------- /Ruby/lib/sum_upon_sums.rb: -------------------------------------------------------------------------------- 1 | # I give you a scrambled list of n unique integers between 0 and n. 2 | # Tell me what number is missing? 3 | # How could you solve the problem in O(n), and also O(1) space? 4 | def sum_upon_sums(array) 5 | end 6 | -------------------------------------------------------------------------------- /Ruby/lib/triangle_number.rb: -------------------------------------------------------------------------------- 1 | # The sequence of triangle numbers. 2 | # Triangle numbers represent expanding triangular formations. 3 | # The Nth triangle number is N + the previous triangle number. 4 | # The first triangle number is 1. 5 | # The 2nd triangle number is 3, because 2 + 1 = 3. 6 | # The third triangle number is 6 because 3 + 3 (the previous number) = 6, etc... 7 | # Use recursion. 8 | def triangle_number(integer) 9 | 10 | end 11 | -------------------------------------------------------------------------------- /Ruby/lib/unique_paths.rb: -------------------------------------------------------------------------------- 1 | # Write a method that takes a number of rows and columns. 2 | # The rows and columns represent a grid. 3 | # Calculate the number of shortest paths from the upper-left to bottom-right of the grid. 4 | # Each step of a shortest path will always move down or to the right. 5 | # Use recursion. 6 | # Consider memoization. 7 | def unique_paths(rows, columns, memo = {}) 8 | end 9 | -------------------------------------------------------------------------------- /Ruby/lib/unique_substrings.rb: -------------------------------------------------------------------------------- 1 | # Write a method that finds all the unique substrings for a word. 2 | def unique_substrings(string) 3 | end 4 | -------------------------------------------------------------------------------- /Ruby/lib/valid_ip.rb: -------------------------------------------------------------------------------- 1 | # Write a method that takes a string as input. 2 | # It should return true if the input is a valid IPv4 address. 3 | # Valid IPs are anything between '0.0.0.0' and '255.255.255.255'. 4 | def valid_ip?(string) 5 | end 6 | -------------------------------------------------------------------------------- /Ruby/lib/weighted_random_index.rb: -------------------------------------------------------------------------------- 1 | # Given an array, write a function that will return a random index of the array. 2 | # The chance of returning a given index will vary with the value of the element. 3 | # Probability of i should be the ith element divided by the sum of all elements. 4 | def weighted_random_index(array) 5 | end 6 | -------------------------------------------------------------------------------- /Ruby/lib/windowed_max_range.rb: -------------------------------------------------------------------------------- 1 | # Take an array, and a window size w. 2 | # Find the maximum max - min within a range of w elements. 3 | # First solve MaxStack. 4 | # Write a MinMaxStack to track both the min and the max in a stack. 5 | # Next, solve StackQueue. 6 | # Write a MinMaxStackQueue which tracks both the min and max. 7 | # Last, use MinMaxStackQueue to solve the problem. 8 | def windowed_max_range(array, w) 9 | end 10 | -------------------------------------------------------------------------------- /Ruby/spec/add_until_100_spec.rb: -------------------------------------------------------------------------------- 1 | require "add_until_100" 2 | 3 | describe "add_until_100" do 4 | it "calls itself" do 5 | expect(self).to receive(:add_until_100).at_least(:twice).and_call_original 6 | add_until_100([1, 2, 3, 4, 5]) 7 | end 8 | 9 | specify { expect(add_until_100([1, 2, 3, 4, 5])).to eq(15) } 10 | specify { expect(add_until_100([11, 22, 33, 44, 55, 66, 77])).to eq(99) } 11 | end 12 | -------------------------------------------------------------------------------- /Ruby/spec/binary_search_spec.rb: -------------------------------------------------------------------------------- 1 | require "spec_helper" 2 | require "binary_search" 3 | 4 | describe "binary_search" do 5 | it "returns the proper index" do 6 | expect(binary_search([1, 5, 13, 23, 305, 333, 402, 454, 500], 13)).to eq(2) 7 | end 8 | 9 | it "should return nil if the target isn't found" do 10 | array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] 11 | target = 11 12 | expect(binary_search(array, target)).to be_nil 13 | end 14 | 15 | it "performs faster than linear search" do 16 | array = (0..100_000).to_a 17 | target = 64_000 18 | 19 | expect { binary_search(array, target) }.to perform_faster_than { linear_search(array, target) } 20 | end 21 | 22 | def linear_search(array, target) 23 | (0...array.length).each do |index| 24 | return index if array[index] == target 25 | end 26 | 27 | nil 28 | end 29 | end 30 | -------------------------------------------------------------------------------- /Ruby/spec/caesar_cipher_spec.rb: -------------------------------------------------------------------------------- 1 | require "caesar_cipher" 2 | 3 | describe "caesar_cipher" do 4 | specify { expect(caesar_cipher("hello", 4)).to eq("lipps") } 5 | specify { expect(caesar_cipher("abc", 0)).to eq("abc") } 6 | specify { expect(caesar_cipher("asdf asdf", 13)).to eq("nfqs nfqs") } 7 | end 8 | -------------------------------------------------------------------------------- /Ruby/spec/can_win_spec.rb: -------------------------------------------------------------------------------- 1 | require "can_win" 2 | 3 | describe "can_win?" do 4 | it "should return false if can't win" do 5 | array = [1, 2, 3, 4, 5, 0] 6 | expect(can_win?(array, 1)).to be_falsy 7 | end 8 | 9 | it "should return true if can win" do 10 | array = [3, 1, 5, 7, 9, 2, 9, 0] 11 | expect(can_win?(array, 1)).to be_truthy 12 | end 13 | end 14 | -------------------------------------------------------------------------------- /Ruby/spec/character_count_spec.rb: -------------------------------------------------------------------------------- 1 | require "character_count" 2 | 3 | describe "#character_count" do 4 | specify do 5 | expect(character_count(["a", "bc", "def"])).to eq(6) 6 | end 7 | 8 | it "calls itself" do 9 | expect(self).to receive(:character_count) 10 | .at_least(:twice) 11 | .and_call_original 12 | character_count(["a", "bc", "def"]) 13 | end 14 | end 15 | -------------------------------------------------------------------------------- /Ruby/spec/common_subsets_spec.rb: -------------------------------------------------------------------------------- 1 | require "common_subsets" 2 | 3 | describe "common_subsets" do 4 | it "should return the common subsets of two arrays" do 5 | array_one = [1, 2, 3, 4, 5] 6 | array_two = [2, 3, 4] 7 | actual_subsets = common_subsets(array_one, array_two).map(&:sort) 8 | expected_subsets = [[], [4], [3], [3, 4], [2], [2, 4], [2, 3], [2, 3, 4]] 9 | expect(actual_subsets).to match_array(expected_subsets) 10 | end 11 | 12 | it "should return the common subsets of two arrays" do 13 | array_one = [1, 3, 5, 7, 9] 14 | array_two = [2, 4, 5, 6, 8] 15 | actual_subsets = common_subsets(array_one, array_two).map(&:sort) 16 | expected_subsets = [[], [5]] 17 | expect(actual_subsets).to match_array(expected_subsets) 18 | end 19 | end 20 | -------------------------------------------------------------------------------- /Ruby/spec/common_substrings_spec.rb: -------------------------------------------------------------------------------- 1 | require "common_substrings" 2 | 3 | describe "common_substrings" do 4 | specify { expect(common_substrings("Hello", "Hello World")).to eq("Hello") } 5 | specify { expect(common_substrings("ABABC", "BABCA")).to eq("BABC") } 6 | end 7 | -------------------------------------------------------------------------------- /Ruby/spec/decimal_to_binary_spec.rb: -------------------------------------------------------------------------------- 1 | require "decimal_to_binary" 2 | 3 | describe "decimal_to_binary" do 4 | specify { expect(decimal_to_binary(0)).to eq("0") } 5 | specify { expect(decimal_to_binary(5)).to eq("101") } 6 | specify { expect(decimal_to_binary(15)).to eq("1111") } 7 | end 8 | -------------------------------------------------------------------------------- /Ruby/spec/digital_root_spec.rb: -------------------------------------------------------------------------------- 1 | require "digital_root" 2 | 3 | describe "digital_root" do 4 | specify { expect(digital_root(65_536)).to eq(7) } 5 | specify { expect(digital_root(1853)).to eq(8) } 6 | end 7 | -------------------------------------------------------------------------------- /Ruby/spec/fast_intersection_spec.rb: -------------------------------------------------------------------------------- 1 | require "fast_intersection" 2 | 3 | describe "fast_intersection" do 4 | it "should return the intersection of two arrays" do 5 | array_one = [1, 2, 3, 4, 5] 6 | array_two = [2, 3, 4] 7 | intersection = [2, 3, 4] 8 | expect(fast_intersection(array_one, array_two)).to match_array(intersection) 9 | end 10 | 11 | it "should return the intersection of two arrays" do 12 | array_one = [1, 3, 5, 7, 9] 13 | array_two = [2, 4, 5, 6, 8] 14 | intersection = [5] 15 | expect(fast_intersection(array_one, array_two)).to match_array(intersection) 16 | end 17 | end 18 | -------------------------------------------------------------------------------- /Ruby/spec/fibonacci_spec.rb: -------------------------------------------------------------------------------- 1 | require "fibonacci" 2 | 3 | describe "fibonacci" do 4 | specify { expect(fibonacci(3)).to eq([0, 1, 1]) } 5 | specify { expect(fibonacci(5)).to eq([0, 1, 1, 2, 3]) } 6 | end 7 | -------------------------------------------------------------------------------- /Ruby/spec/file_list_spec.rb: -------------------------------------------------------------------------------- 1 | require "file_list" 2 | 3 | describe "file_list" do 4 | let(:files) do 5 | { 6 | "a" => { 7 | "b" => { 8 | "c" => { 9 | "d" => { 10 | "e" => true 11 | }, 12 | 13 | "f" => true 14 | } 15 | } 16 | } 17 | } 18 | end 19 | 20 | specify { expect(file_list(files)).to match_array(["a/b/c/d/e", "a/b/c/f"]) } 21 | end 22 | -------------------------------------------------------------------------------- /Ruby/spec/find_missing_number_spec.rb: -------------------------------------------------------------------------------- 1 | require "find_missing_number" 2 | 3 | describe "find_missing_number" do 4 | let(:integers) { [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] } 5 | let(:shuffled) { [8, 7, 2, 1, 10, 9, 6, 3, 5] } 6 | let(:deleted) { 4 } 7 | 8 | specify { expect(find_missing_number(integers, shuffled)).to eq(deleted) } 9 | end 10 | -------------------------------------------------------------------------------- /Ruby/spec/folding_cipher_spec.rb: -------------------------------------------------------------------------------- 1 | require "folding_cipher" 2 | 3 | describe "folding_cipher" do 4 | specify { expect(folding_cipher("abcm")).to eq("zyxn") } 5 | specify { expect(folding_cipher("zyxn")).to eq("abcm") } 6 | end 7 | -------------------------------------------------------------------------------- /Ruby/spec/golomb_spec.rb: -------------------------------------------------------------------------------- 1 | require "golomb" 2 | 3 | describe "golomb" do 4 | let(:sequence) do 5 | [1, 2, 2, 3, 3, 4, 4, 4, 5, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7] 6 | end 7 | 8 | it "returns the Nth number in the sequence" do 9 | sequence.each_with_index do |n, index| 10 | expect(golomb(index + 1)).to eq(n) 11 | end 12 | end 13 | 14 | it "is recursive" do 15 | expect(self).to receive(:golomb).at_least(:twice).and_call_original 16 | golomb(10) 17 | end 18 | 19 | it "is optimized to avoid unecessary method calls" do 20 | expect(self).to receive(:golomb).at_most(28).times.and_call_original 21 | golomb(10) 22 | end 23 | end 24 | -------------------------------------------------------------------------------- /Ruby/spec/in_words_spec.rb: -------------------------------------------------------------------------------- 1 | require "in_words" 2 | 3 | describe "in_words" do 4 | it { expect(in_words(0)).to eq("zero") } 5 | it { expect(in_words(11)).to eq("eleven") } 6 | it { expect(in_words(23)).to eq("twenty three") } 7 | it { expect(in_words(134)).to eq("one hundred thirty four") } 8 | it { expect(in_words(1456)).to eq("one thousand four hundred fifty six") } 9 | end 10 | -------------------------------------------------------------------------------- /Ruby/spec/index_of_x_spec.rb: -------------------------------------------------------------------------------- 1 | require "index_of_x" 2 | 3 | describe "#index_of_x" do 4 | specify do 5 | alphabet = "abcdefghijklmnopqrstuvwxyz" 6 | expect(index_of_x(alphabet)).to eq(23) 7 | end 8 | 9 | it "calls itself" do 10 | expect(self) 11 | .to receive(:index_of_x) 12 | .at_least(:twice) 13 | .and_call_original 14 | index_of_x("abcdx") 15 | end 16 | end 17 | -------------------------------------------------------------------------------- /Ruby/spec/is_palindrome_spec.rb: -------------------------------------------------------------------------------- 1 | require "is_palindrome" 2 | 3 | describe "is_palindrome?" do 4 | specify { expect(is_palindrome?("ricercar")).to be_falsy } 5 | specify { expect(is_palindrome?("racecar")).to be_truthy } 6 | end 7 | -------------------------------------------------------------------------------- /Ruby/spec/is_shuffle_spec.rb: -------------------------------------------------------------------------------- 1 | require "is_shuffle" 2 | 3 | describe "is_shuffle?" do 4 | let(:first) { "abc" } 5 | let(:second) { "def" } 6 | 7 | specify { expect(is_shuffle?(first, second, "abdecf")).to be_truthy } 8 | specify { expect(is_shuffle?(first, second, "fcedba")).to be_falsy } 9 | end 10 | -------------------------------------------------------------------------------- /Ruby/spec/iterative_factorial_spec.rb: -------------------------------------------------------------------------------- 1 | require "iterative_factorial" 2 | 3 | describe "iterative_factorial" do 4 | specify { expect(iterative_factorial(0)).to eq(1) } 5 | specify { expect(iterative_factorial(1)).to eq(1) } 6 | specify { expect(iterative_factorial(3)).to eq(6) } 7 | specify { expect(iterative_factorial(5)).to eq(120) } 8 | 9 | it "does not call itself" do 10 | allow(self).to receive(:iterative_factorial).at_least(:once).and_call_original 11 | iterative_factorial(10) 12 | 13 | expect(self).to have_received(:iterative_factorial).exactly(:once) 14 | end 15 | end 16 | -------------------------------------------------------------------------------- /Ruby/spec/lcs_spec.rb: -------------------------------------------------------------------------------- 1 | require "lcs" 2 | 3 | describe "lcs" do 4 | specify { expect(lcs([4, -1, 5, 6, -13, 2])).to eq(14) } 5 | specify { expect(lcs([-2, 1, -3, 4, -1, 2, 1, -5, 4])).to eq(6) } 6 | end 7 | -------------------------------------------------------------------------------- /Ruby/spec/longest_palindrome_spec.rb: -------------------------------------------------------------------------------- 1 | require "longest_palindrome" 2 | 3 | describe "longest_palindrome" do 4 | specify { expect(longest_palindrome("asdfdsaqwerqwer")).to eq([0, 6]) } 5 | specify { expect(longest_palindrome("asdfghjklzxcxz")).to eq([9, 13]) } 6 | end 7 | -------------------------------------------------------------------------------- /Ruby/spec/look_and_say_spec.rb: -------------------------------------------------------------------------------- 1 | require "look_and_say" 2 | 3 | describe "look_and_say" do 4 | it "should describe the count of the elements in the array as they appear" do 5 | expect(look_and_say([1])).to match_array([[1, 1]]) 6 | end 7 | 8 | it "should describe the count of the elements in the array as they appear" do 9 | expect(look_and_say([1, 2, 1, 1])).to match_array([[1, 1], [1, 2], [2, 1]]) 10 | end 11 | end 12 | -------------------------------------------------------------------------------- /Ruby/spec/matrix_region_sum_spec.rb: -------------------------------------------------------------------------------- 1 | require "matrix_region_sum" 2 | 3 | describe "matrix_region_sum" do 4 | it "should return the sum of the elements within the coordinates" do 5 | matrix = [[1, 2, 3], [2, 3, 4], [3, 4, 5]] 6 | top_left_coords = [0, 0] 7 | bottom_right_coords = [1, 1] 8 | actual = matrix_region_sum(matrix, top_left_coords, bottom_right_coords) 9 | 10 | expect(actual).to eq(8) 11 | end 12 | 13 | it "should return the sum of the elements within the coordinates" do 14 | matrix = [[2, 3, 4], [3, 4, 5], [4, 5, 6]] 15 | top_left_coords = [0, 0] 16 | bottom_right_coords = [2, 2] 17 | actual = matrix_region_sum(matrix, top_left_coords, bottom_right_coords) 18 | 19 | expect(actual).to eq(36) 20 | end 21 | end 22 | -------------------------------------------------------------------------------- /Ruby/spec/max_stack_spec.rb: -------------------------------------------------------------------------------- 1 | require "max_stack" 2 | 3 | describe MaxStack do 4 | let(:stack) { MaxStack.new } 5 | 6 | it { should respond_to(:push) } 7 | it { should respond_to(:pop) } 8 | it { should respond_to(:max) } 9 | 10 | describe "#max" do 11 | it "should return the largest number added to the stack" do 12 | stack.push(10) 13 | stack.push(21) 14 | stack.push(32) 15 | expect(stack.max).to eq(32) 16 | end 17 | 18 | it "should return the largest number added to the stack" do 19 | stack.push(99) 20 | stack.pop 21 | stack.push(33) 22 | expect(stack.max).to eq(33) 23 | end 24 | end 25 | end 26 | -------------------------------------------------------------------------------- /Ruby/spec/merge_sort_spec.rb: -------------------------------------------------------------------------------- 1 | require "merge_sort" 2 | 3 | describe "merge_sort" do 4 | specify { expect(merge_sort([1452, 23, 1, 5])).to eq([1, 5, 23, 1452]) } 5 | specify { expect(merge_sort([10, 4, 6, 2])).to eq([2, 4, 6, 10]) } 6 | end 7 | -------------------------------------------------------------------------------- /Ruby/spec/min_max_stack_queue_spec.rb: -------------------------------------------------------------------------------- 1 | require "min_max_stack_queue" 2 | 3 | describe MinMaxStackQueue do 4 | it { should respond_to(:enqueue) } 5 | it { should respond_to(:dequeue) } 6 | it { should respond_to(:length) } 7 | it { should respond_to(:max) } 8 | it { should respond_to(:min) } 9 | end 10 | -------------------------------------------------------------------------------- /Ruby/spec/min_max_stack_spec.rb: -------------------------------------------------------------------------------- 1 | require "min_max_stack" 2 | 3 | describe MinMaxStack do 4 | it { should respond_to(:length) } 5 | it { should respond_to(:push) } 6 | it { should respond_to(:pop) } 7 | it { should respond_to(:max) } 8 | it { should respond_to(:min) } 9 | end 10 | -------------------------------------------------------------------------------- /Ruby/spec/minimum_coin_change_spec.rb: -------------------------------------------------------------------------------- 1 | require "minimum_coin_change" 2 | 3 | describe MinimumCoinChange do 4 | describe "#make_change" do 5 | it "uses only 3 coins to make change for 36 cents" do 6 | subject = MinimumCoinChange.new 7 | expect(subject.make_change(36)).to contain_exactly(25, 10, 1) 8 | end 9 | end 10 | end 11 | -------------------------------------------------------------------------------- /Ruby/spec/move_zeros_spec.rb: -------------------------------------------------------------------------------- 1 | require "move_zeros" 2 | 3 | describe "move_zeros" do 4 | it "should return an array with the same elements" do 5 | array = [1, 2, 0, 3, 4, 0, 5, 6, 0] 6 | expect(move_zeros(array).sort).to match_array(array.sort) 7 | end 8 | 9 | it "should move all the zeros to the end of the array" do 10 | array = [1, 2, 0, 3, 4, 0, 5, 6, 0] 11 | expect(move_zeros(array).drop(6)).to match_array([0, 0, 0]) 12 | end 13 | end 14 | -------------------------------------------------------------------------------- /Ruby/spec/pair_sum_spec.rb: -------------------------------------------------------------------------------- 1 | require "pair_sum" 2 | require "set" 3 | 4 | describe "pair_sum" do 5 | let(:set) { Set.new } 6 | 7 | it "should return all the pairs that sum to 0" do 8 | set.add([-1, 1]) 9 | expect(pair_sum([1, 2, -1], 0)).to eq(set) 10 | end 11 | 12 | it "should return all the pairs that sum to 1" do 13 | set.add([-1, 2]) 14 | expect(pair_sum([1, 2, -1, -1, -2], 1)).to eq(set) 15 | end 16 | end 17 | -------------------------------------------------------------------------------- /Ruby/spec/permutations_spec.rb: -------------------------------------------------------------------------------- 1 | require "permutations" 2 | 3 | describe "permutations" do 4 | specify { expect(permutations([1])).to match_array([[1]]) } 5 | specify { expect(permutations([1, 2])).to match_array([[1, 2], [2, 1]]) } 6 | end 7 | -------------------------------------------------------------------------------- /Ruby/spec/productify_spec.rb: -------------------------------------------------------------------------------- 1 | require "productify" 2 | 3 | describe "productify" do 4 | specify { expect(productify([2, 3, 5])).to match_array([15, 10, 6]) } 5 | specify { expect(productify([4, 2, 5, 7])).to match_array([70, 140, 56, 40]) } 6 | end 7 | -------------------------------------------------------------------------------- /Ruby/spec/queue_spec.rb: -------------------------------------------------------------------------------- 1 | require "queue" 2 | 3 | describe Queue do 4 | subject { described_class.new } 5 | 6 | describe "#clear" do 7 | it "empties out the collection" do 8 | subject.enqueue(1); 9 | subject.enqueue(2); 10 | subject.enqueue(3); 11 | subject.clear 12 | expect(subject.to_s).to eq("[]") 13 | end 14 | end 15 | 16 | describe "#empty?" do 17 | specify do 18 | expect(subject).to be_empty 19 | subject.enqueue(1) 20 | expect(subject).not_to be_empty 21 | end 22 | end 23 | 24 | describe "#peek" do 25 | it "exposes the front of the queue" do 26 | subject.enqueue(1) 27 | subject.enqueue(2) 28 | expect(subject.peek).to eq(1) 29 | end 30 | end 31 | 32 | describe "#dequeue" do 33 | it "removes the item at the front of the queue" do 34 | subject.enqueue(1) 35 | subject.enqueue(2) 36 | subject.enqueue(3) 37 | expect(subject.dequeue).to eq(1) 38 | expect(subject.to_s).to eq("[2, 3]") 39 | end 40 | end 41 | 42 | describe "#enqueue" do 43 | it "adds the item to the front of the queue" do 44 | subject.enqueue(1) 45 | subject.enqueue(2) 46 | expect(subject.to_s).to eq("[1, 2]") 47 | end 48 | end 49 | 50 | describe "#size" do 51 | it "tracks the number of items in the queue" do 52 | subject.enqueue(1) 53 | subject.enqueue(2) 54 | expect(subject.size).to eq(2) 55 | end 56 | end 57 | end 58 | -------------------------------------------------------------------------------- /Ruby/spec/recursive_factorial_spec.rb: -------------------------------------------------------------------------------- 1 | require "recursive_factorial" 2 | 3 | describe "recursive_factorial" do 4 | specify { expect(recursive_factorial(0)).to eq(1) } 5 | specify { expect(recursive_factorial(1)).to eq(1) } 6 | specify { expect(recursive_factorial(3)).to eq(6) } 7 | specify { expect(recursive_factorial(5)).to eq(120) } 8 | 9 | it "calls itself" do 10 | expect(self).to receive(:recursive_factorial) 11 | .at_least(:twice).and_call_original 12 | recursive_factorial(10) 13 | end 14 | end 15 | -------------------------------------------------------------------------------- /Ruby/spec/recursive_print_spec.rb: -------------------------------------------------------------------------------- 1 | require "recursive_print" 2 | 3 | describe "#recursive_print" do 4 | specify do 5 | input = [1, [2, 3], [4, 5]] 6 | 7 | expect { recursive_print(input) }.to output(/1 2 3 4 5/).to_stdout 8 | end 9 | end 10 | -------------------------------------------------------------------------------- /Ruby/spec/recursive_sum_spec.rb: -------------------------------------------------------------------------------- 1 | require "recursive_sum" 2 | 3 | describe "recursive_sum" do 4 | specify { expect(recursive_sum([1, 2, 3])).to eq(6) } 5 | specify { expect(recursive_sum([99, 66, 33])).to eq(198) } 6 | 7 | it "calls itself" do 8 | expect(self).to receive(:recursive_sum).at_least(:twice).and_call_original 9 | recursive_sum([10, 20, 30]) 10 | end 11 | end 12 | -------------------------------------------------------------------------------- /Ruby/spec/reverse_spec.rb: -------------------------------------------------------------------------------- 1 | require "reverse" 2 | 3 | describe "reverse" do 4 | specify do 5 | expect(reverse("abcde")).to eq("edcba") 6 | end 7 | end 8 | -------------------------------------------------------------------------------- /Ruby/spec/select_even_spec.rb: -------------------------------------------------------------------------------- 1 | require "select_even" 2 | 3 | describe "#select_even" do 4 | specify do 5 | expect(select_even([1, 2, 3, 4, 5, 6])).to match_array([2, 4, 6]) 6 | end 7 | 8 | it "calls itself" do 9 | expect(self).to receive(:select_even) 10 | .at_least(:twice) 11 | .and_call_original 12 | select_even([1, 2]) 13 | end 14 | end 15 | -------------------------------------------------------------------------------- /Ruby/spec/silly_years_spec.rb: -------------------------------------------------------------------------------- 1 | require "silly_years" 2 | 3 | describe "silly_years" do 4 | it "should return the ten subsequent silly years" do 5 | array = [2307, 2417, 2527, 2637, 2747, 2857, 2967, 3406, 3516, 3626] 6 | expect(silly_years(1978)).to match_array(array) 7 | end 8 | 9 | it "should return the ten subsequent silly years" do 10 | array = [2417, 2527, 2637, 2747, 2857, 2967, 3406, 3516, 3626, 3736] 11 | expect(silly_years(2307)).to match_array(array) 12 | end 13 | end 14 | -------------------------------------------------------------------------------- /Ruby/spec/spec_helper.rb: -------------------------------------------------------------------------------- 1 | require 'rspec-benchmark' 2 | # This file was generated by the `rspec --init` command. Conventionally, all 3 | # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`. 4 | # The generated `.rspec` file contains `--require spec_helper` which will cause 5 | # this file to always be loaded, without a need to explicitly require it in any 6 | # files. 7 | # 8 | # Given that it is always loaded, you are encouraged to keep this file as 9 | # light-weight as possible. Requiring heavyweight dependencies from this file 10 | # will add to the boot time of your test suite on EVERY test run, even for an 11 | # individual file that may not need all of that loaded. Instead, consider making 12 | # a separate helper file that requires the additional dependencies and performs 13 | # the additional setup, and require it from the spec files that actually need 14 | # it. 15 | # 16 | # See https://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration 17 | RSpec.configure do |config| 18 | config.include RSpec::Benchmark::Matchers 19 | # rspec-expectations config goes here. You can use an alternate 20 | # assertion/expectation library such as wrong or the stdlib/minitest 21 | # assertions if you prefer. 22 | config.expect_with :rspec do |expectations| 23 | # This option will default to `true` in RSpec 4. It makes the `description` 24 | # and `failure_message` of custom matchers include text for helper methods 25 | # defined using `chain`, e.g.: 26 | # be_bigger_than(2).and_smaller_than(4).description 27 | # # => "be bigger than 2 and smaller than 4" 28 | # ...rather than: 29 | # # => "be bigger than 2" 30 | expectations.include_chain_clauses_in_custom_matcher_descriptions = true 31 | end 32 | 33 | # rspec-mocks config goes here. You can use an alternate test double 34 | # library (such as bogus or mocha) by changing the `mock_with` option here. 35 | config.mock_with :rspec do |mocks| 36 | # Prevents you from mocking or stubbing a method that does not exist on 37 | # a real object. This is generally recommended, and will default to 38 | # `true` in RSpec 4. 39 | mocks.verify_partial_doubles = true 40 | end 41 | 42 | # This option will default to `:apply_to_host_groups` in RSpec 4 (and will 43 | # have no way to turn it off -- the option exists only for backwards 44 | # compatibility in RSpec 3). It causes shared context metadata to be 45 | # inherited by the metadata hash of host groups and examples, rather than 46 | # triggering implicit auto-inclusion in groups with matching metadata. 47 | config.shared_context_metadata_behavior = :apply_to_host_groups 48 | 49 | # The settings below are suggested to provide a good initial experience 50 | # with RSpec, but feel free to customize to your heart's content. 51 | =begin 52 | # This allows you to limit a spec run to individual examples or groups 53 | # you care about by tagging them with `:focus` metadata. When nothing 54 | # is tagged with `:focus`, all examples get run. RSpec also provides 55 | # aliases for `it`, `describe`, and `context` that include `:focus` 56 | # metadata: `fit`, `fdescribe` and `fcontext`, respectively. 57 | config.filter_run_when_matching :focus 58 | 59 | # Allows RSpec to persist some state between runs in order to support 60 | # the `--only-failures` and `--next-failure` CLI options. We recommend 61 | # you configure your source control system to ignore this file. 62 | config.example_status_persistence_file_path = "spec/examples.txt" 63 | 64 | # Limits the available syntax to the non-monkey patched syntax that is 65 | # recommended. For more details, see: 66 | # https://relishapp.com/rspec/rspec-core/docs/configuration/zero-monkey-patching-mode 67 | config.disable_monkey_patching! 68 | 69 | # This setting enables warnings. It's recommended, but in some cases may 70 | # be too noisy due to issues in dependencies. 71 | config.warnings = true 72 | 73 | # Many RSpec users commonly either run the entire suite or an individual 74 | # file, and it's useful to allow more verbose output when running an 75 | # individual spec file. 76 | if config.files_to_run.one? 77 | # Use the documentation formatter for detailed output, 78 | # unless a formatter has already been configured 79 | # (e.g. via a command-line flag). 80 | config.default_formatter = "doc" 81 | end 82 | 83 | # Print the 10 slowest examples and example groups at the 84 | # end of the spec run, to help surface which specs are running 85 | # particularly slow. 86 | config.profile_examples = 10 87 | 88 | # Run specs in random order to surface order dependencies. If you find an 89 | # order dependency and want to debug it, you can fix the order by providing 90 | # the seed, which is printed after each run. 91 | # --seed 1234 92 | config.order = :random 93 | 94 | # Seed global randomization in this process using the `--seed` CLI option. 95 | # Setting this allows you to use `--seed` to deterministically reproduce 96 | # test failures related to randomization by passing the same `--seed` value 97 | # as the one that triggered the failure. 98 | Kernel.srand config.seed 99 | =end 100 | end 101 | -------------------------------------------------------------------------------- /Ruby/spec/stack_queue_spec.rb: -------------------------------------------------------------------------------- 1 | require "stack_queue" 2 | 3 | describe StackQueue do 4 | let(:stack) { StackQueue.new } 5 | 6 | it { should respond_to(:enqueue) } 7 | it { should respond_to(:dequeue) } 8 | 9 | describe "#dequeue" do 10 | it "should dequeue elements from the stack" do 11 | stack.enqueue(10) 12 | stack.enqueue(15) 13 | stack.enqueue(20) 14 | expect(stack.dequeue).to eq(10) 15 | expect(stack.dequeue).to eq(15) 16 | expect(stack.dequeue).to eq(20) 17 | end 18 | end 19 | end 20 | -------------------------------------------------------------------------------- /Ruby/spec/stack_spec.rb: -------------------------------------------------------------------------------- 1 | require "stack" 2 | 3 | describe Stack do 4 | subject { Stack.new } 5 | 6 | describe "#clear" do 7 | it "empties out the collection" do 8 | subject.push(1); 9 | subject.push(2); 10 | subject.push(3); 11 | subject.clear 12 | expect(subject.to_s).to eq("[]"); 13 | end 14 | end 15 | 16 | describe "#empty?" do 17 | specify do 18 | expect(subject).to be_empty 19 | subject.push(1) 20 | expect(subject).not_to be_empty 21 | end 22 | end 23 | 24 | describe "#peek" do 25 | it "exposes the top of the stack" do 26 | subject.push(1) 27 | subject.push(2) 28 | expect(subject.peek).to eq(2) 29 | end 30 | end 31 | 32 | describe "#pop" do 33 | it "removes the item at the top of the stack" do 34 | subject.push(1) 35 | subject.push(2) 36 | subject.push(3) 37 | expect(subject.pop).to eq(3) 38 | expect(subject.to_s).to eq("[1, 2]") 39 | end 40 | end 41 | 42 | describe "#push" do 43 | it "adds the item to the top of the stack" do 44 | subject.push(1) 45 | subject.push(2) 46 | expect(subject.to_s).to eq("[1, 2]") 47 | end 48 | end 49 | 50 | describe "#size" do 51 | it "tracks the number of items in the stack" do 52 | end 53 | end 54 | end 55 | -------------------------------------------------------------------------------- /Ruby/spec/subsets_spec.rb: -------------------------------------------------------------------------------- 1 | require "subsets" 2 | 3 | describe "subsets" do 4 | it "should return the subsets of a given array" do 5 | array = [[], [1], [2], [3], [1, 2], [1, 3], [2, 3], [1, 2, 3]] 6 | # sort each subset so that differently ordered subsets still pass 7 | expect(subsets([1, 2, 3]).map(&:sort)).to match_array(array) 8 | end 9 | 10 | it "should return a set containing an empty set if given an empty set" do 11 | array = [[]] 12 | expect(subsets([])).to match_array(array) 13 | end 14 | end 15 | -------------------------------------------------------------------------------- /Ruby/spec/sum_upon_sums_spec.rb: -------------------------------------------------------------------------------- 1 | require "sum_upon_sums" 2 | 3 | describe "sum_upon_sums" do 4 | it "should return the missing number" do 5 | array = [0, 3, 6, 4, 10, 5, 1, 9, 2, 8] 6 | expect(sum_upon_sums(array)).to eq(7) 7 | end 8 | 9 | it "should return the missing number" do 10 | array = [5, 1, 10, 3, 0, 4, 2, 7, 6, 8] 11 | expect(sum_upon_sums(array)).to eq(9) 12 | end 13 | end 14 | -------------------------------------------------------------------------------- /Ruby/spec/triangle_number_spec.rb: -------------------------------------------------------------------------------- 1 | require "triangle_number" 2 | 3 | describe "#triangle_number" do 4 | specify { expect(triangle_number(1)).to eq(1) } 5 | specify { expect(triangle_number(2)).to eq(3) } 6 | specify { expect(triangle_number(3)).to eq(6) } 7 | specify { expect(triangle_number(4)).to eq(10) } 8 | specify { expect(triangle_number(5)).to eq(15) } 9 | specify { expect(triangle_number(6)).to eq(21) } 10 | specify { expect(triangle_number(7)).to eq(28) } 11 | 12 | it "calls itself" do 13 | expect(self).to receive(:triangle_number) 14 | .at_least(:twice).and_call_original 15 | triangle_number(7) 16 | end 17 | end 18 | -------------------------------------------------------------------------------- /Ruby/spec/uniq_substrings_spec.rb: -------------------------------------------------------------------------------- 1 | require "uniq_subs" 2 | 3 | describe "unique_substrings" do 4 | it "should return all the substrings" do 5 | array = %w[a b c d ab bc cd abc bcd abcd] 6 | expect(unique_substrings("abcd")).to match_array(array) 7 | end 8 | 9 | it "shouldn't have repeats" do 10 | array = %w[d du dud dude u ud ude de e] 11 | expect(unique_substrings("dude")).to match_array(array) 12 | end 13 | end 14 | -------------------------------------------------------------------------------- /Ruby/spec/unique_paths_spec.rb: -------------------------------------------------------------------------------- 1 | require "unique_paths" 2 | 3 | describe "#unique_paths" do 4 | specify { expect(unique_paths(3, 7)).to eq(28) } 5 | specify { expect(unique_paths(4, 12)).to eq(364) } 6 | specify { expect(unique_paths(5, 15)).to eq(3060) } 7 | 8 | it "is recursive" do 9 | expect(self).to receive(:unique_paths).at_least(:twice).and_call_original 10 | 11 | unique_paths(3, 3) 12 | end 13 | 14 | it "is efficient" do 15 | expect(self).to receive(:unique_paths) 16 | .at_most(9) 17 | .times 18 | .and_call_original 19 | 20 | unique_paths(3, 3) 21 | end 22 | end 23 | -------------------------------------------------------------------------------- /Ruby/spec/valid_ip_spec.rb: -------------------------------------------------------------------------------- 1 | require "valid_ip" 2 | 3 | describe "valid_ip?" do 4 | specify { expect(valid_ip?("1.1.1.1")).to be_truthy } 5 | specify { expect(valid_ip?("256.2.2.2")).to be_falsy } 6 | specify { expect(valid_ip?("1.1.1.1.1")).to be_falsey } 7 | end 8 | -------------------------------------------------------------------------------- /Ruby/spec/weighted_random_index_spec.rb: -------------------------------------------------------------------------------- 1 | require "weighted_random_index" 2 | 3 | describe "weighted_random_index" do 4 | it "should not return an index out of range" do 5 | array = [4, 6, 8] 6 | expect(weighted_random_index(array)).to be_between(0, 2) 7 | end 8 | end 9 | -------------------------------------------------------------------------------- /Ruby/spec/windowed_max_range_spec.rb: -------------------------------------------------------------------------------- 1 | require "min_max_stack_queue" 2 | require "windowed_max_range" 3 | 4 | describe "windowed_max_range" do 5 | let(:queue) { MinMaxStackQueue.new } 6 | 7 | specify { expect(windowed_max_range([1, 0, 2, 5, 4, 8], 2)).to eq(4) } 8 | specify { expect(windowed_max_range([1, 0, 2, 5, 4, 8], 3)).to eq(5) } 9 | specify { expect(windowed_max_range([1, 0, 2, 5, 4, 8], 4)).to eq(6) } 10 | specify { expect(windowed_max_range([1, 3, 2, 5, 4, 8], 5)).to eq(6) } 11 | 12 | it "makes use of MinMaxStackQueue in the solution" do 13 | allow(MinMaxStackQueue).to receive(:new).and_return(queue) 14 | windowed_max_range([1, 0, 2, 5, 4, 8], 2) 15 | 16 | expect(MinMaxStackQueue).to have_received(:new) 17 | end 18 | end 19 | --------------------------------------------------------------------------------