├── .rspec ├── Gemfile ├── README.md ├── generator.rb ├── list.csv ├── possible_boards.yml ├── problems ├── base_converter.rb ├── binary_search.rb ├── bubble_sort.rb ├── caesar_cipher.rb ├── deep_dup.rb ├── digital_root.rb ├── doubler.rb ├── dups.rb ├── eight_queens.rb ├── exponent.rb ├── factorials_rec.rb ├── factors.rb ├── fibs_sum.rb ├── first_even_numbers_sum.rb ├── jumble_sort.rb ├── make_better_change.rb ├── median.rb ├── merge_sort.rb ├── my_all.rb ├── my_any.rb ├── my_each.rb ├── my_each_hash.rb ├── my_flatten.rb ├── my_inject.rb ├── my_join.rb ├── my_merge.rb ├── my_reject.rb ├── my_reverse.rb ├── my_rotate.rb ├── my_select.rb ├── my_zip.rb ├── permutations.rb ├── pig_latinify.rb ├── prime_factorization.rb ├── primes.rb ├── quicksort.rb ├── real_words_in_string.rb ├── rec_sum.rb ├── string_include_key.rb ├── subsets.rb ├── symmetric_substrings.rb ├── titleize.rb └── two_sum.rb ├── solutions ├── base_converter_solution.rb ├── binary_search_solution.rb ├── bubble_sort_solution.rb ├── caesar_cipher_solution.rb ├── deep_dup_solution.rb ├── digital_root_solution.rb ├── doubler_solution.rb ├── dups_solution.rb ├── eight_queens_solution.rb ├── exponent_solution.rb ├── factorials_rec_solution.rb ├── factors_solution.rb ├── fibs_sum_solution.rb ├── first_even_numbers_sum_solution.rb ├── jumble_sort_solution.rb ├── make_better_change_solution.rb ├── median_solution.rb ├── merge_sort_solution.rb ├── my_all_solution.rb ├── my_any_solution.rb ├── my_each_hash_solution.rb ├── my_each_solution.rb ├── my_flatten_solution.rb ├── my_inject_solution.rb ├── my_join_solution.rb ├── my_merge_solution.rb ├── my_reject_solution.rb ├── my_reverse_solution.rb ├── my_rotate_solution.rb ├── my_select_solution.rb ├── my_zip_solution.rb ├── permutations_solution.rb ├── pig_latinify_solution.rb ├── prime_factorization_solution.rb ├── primes_solution.rb ├── quicksort_solution.rb ├── real_words_in_string_solution.rb ├── rec_sum_solution.rb ├── string_include_key_solution.rb ├── subsets_solution.rb ├── symmetric_substrings_solution.rb ├── titleize_solution.rb └── two_sum_solution.rb └── specs ├── base_converter_spec.rb ├── binary_search_spec.rb ├── bubble_sort_spec.rb ├── caesar_cipher_spec.rb ├── deep_dup_spec.rb ├── digital_root_spec.rb ├── doubler_spec.rb ├── dups_spec.rb ├── eight_queens_spec.rb ├── exponent_spec.rb ├── factorials_rec_spec.rb ├── factors_spec.rb ├── fibs_sum_spec.rb ├── first_even_numbers_sum_spec.rb ├── jumble_sort_spec.rb ├── make_better_change_spec.rb ├── median_spec.rb ├── merge_sort_spec.rb ├── my_all_spec.rb ├── my_any_spec.rb ├── my_each_hash_spec.rb ├── my_each_spec.rb ├── my_flatten_spec.rb ├── my_inject_spec.rb ├── my_join_spec.rb ├── my_merge_spec.rb ├── my_reject_spec.rb ├── my_reverse_spec.rb ├── my_rotate_spec.rb ├── my_select_spec.rb ├── my_zip_spec.rb ├── permutations_spec.rb ├── pig_latinify_spec.rb ├── prime_factorization_spec.rb ├── primes_spec.rb ├── quicksort_spec.rb ├── real_words_in_string_spec.rb ├── rec_sum_spec.rb ├── string_include_key_spec.rb ├── subsets_spec.rb ├── symmetric_substrings_spec.rb ├── titleize_spec.rb └── two_sum_spec.rb /.rspec: -------------------------------------------------------------------------------- 1 | --format=documentation 2 | --color 3 | -------------------------------------------------------------------------------- /Gemfile: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | # A sample Gemfile 3 | source "https://rubygems.org" 4 | 5 | gem 'rspec' 6 | 7 | gem 'byebug' 8 | 9 | gem 'colorize' 10 | 11 | # gem "rails" 12 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # a/A Practice Test Generator 2 | 3 | I created this simple CLI practice test generator during my first week at App Academy to help myself and other students prepare for our first assessment. I wanted a way to simulate the actual test rather than just solving practice problems individually. The questions, RSpec tests, and solutions were largely pulled from exercises we had encountered during the prepwork and first week of class, or contributed by other students. 4 | 5 | 6 | All of the practice problems are listed and categorized in `list.csv` (categories include: recursion, sorting, enumerable, array, string). When you run `generator.rb` on the command line and provide your desired number of questions from each category, it uses your input and the CSV file to randomly select practice problems. It combines these problems and writes 3 new files inside the repo folder: 7 | * `practice_test.rb` contains the problems to be solved 8 | * `spec.rb` combines the specs for the chosen problems into one file for easy testing 9 | * `solutions.rb` combines the solutions for each problem 10 | 11 | ## How to use this generator 12 | 13 | 1. Clone this repo 14 | 15 | 2. Navigate to the folder in terminal and run 16 | `ruby generator.rb` 17 | 18 | 3. Input your practice test requests in the form `category: # of problems` 19 | 20 | 4. You will now have three new files: `practice_test.rb`, `spec.rb` and `solutions.rb`. Run `bundle exec rspec spec.rb` to test your answers against the spec as you work through `practice_test.rb`. 21 | 22 | 5. Check your solutions against those in `solutions.rb`. 23 | 24 | Note: if you run the generator again in the same folder, it will re-write those three files and erase your previous work. If you wish to save your previous work, you will need to rename the files. 25 | -------------------------------------------------------------------------------- /generator.rb: -------------------------------------------------------------------------------- 1 | require 'csv' 2 | require 'colorize' 3 | 4 | # Instructions 5 | system("clear") 6 | puts "Welcome to Mallory's a/A Practice Assessment Generator".cyan 7 | puts "This generator will create a practice test based on your input. " \ 8 | "You can choose how many problems from each category to include in your test. " 9 | puts "This program will generate 3 files in this folder: practice_test, spec, and solution. " \ 10 | "Complete the practice_test file, running the spec file to check your answers. " \ 11 | "When your time is up (you are timing yourself, right?), compare your answers to the solutions." 12 | puts "Good luck!" 13 | 14 | # read in csv with test info 15 | tests = CSV.read('list.csv', headers: true, header_converters: :symbol, converters: :all) 16 | 17 | # list possible categories 18 | categories = Array.new 19 | tests.each do |test| 20 | categories << test[1] 21 | end 22 | categories = categories.uniq 23 | puts "Possible categories: #{categories.join(", ")}".magenta 24 | puts 25 | 26 | # get user request 27 | puts "Input your requests, separated by commas and spaces please" 28 | puts "Example input: " + "array: 2, recursion: 1, sort: 1".yellow 29 | input = gets.chomp.split(", ") 30 | 31 | categoryrequests = Hash.new(0) 32 | input.each do |request| 33 | req = request.downcase.split(": ") 34 | categoryrequests[req[0]] = req[1].to_i 35 | end 36 | 37 | # make test array for each category 38 | master = Array.new 39 | categories.each do |category| 40 | problems_in_category = Array.new 41 | tests.each do |test| 42 | if category == test[1] 43 | problems_in_category << test 44 | end 45 | end 46 | 47 | # pick tests at random from each category 48 | n = categoryrequests[category] 49 | master = master.concat(problems_in_category.sample(n)) 50 | end 51 | 52 | # create new test, spec and solution files 53 | practice_test = File.open("practice_test.rb", "w") 54 | spec = File.open("spec.rb", "w") 55 | solution = File.open("solution.rb", "w") 56 | 57 | # require rspec and the practice_test in the spec 58 | spec << "require 'rspec'" << "\n" 59 | spec << "require_relative 'practice_test'" << "\n" 60 | 61 | # loop through master tests and add text to the new files 62 | master.each do |test| 63 | practice_test << File.read(test[2]) << "\n" 64 | spec << File.read(test[3]) << "\n" 65 | solution << File.read(test[4]) << "\n" 66 | end 67 | 68 | # close the files that were just created 69 | practice_test.close 70 | spec.close 71 | solution.close 72 | 73 | puts 74 | puts "Done!" 75 | -------------------------------------------------------------------------------- /list.csv: -------------------------------------------------------------------------------- 1 | Name,Category,ProblemFile,SpecFile,SolutionFile 2 | base_converter,recursion,problems/base_converter.rb,specs/base_converter_spec.rb,solutions/base_converter_solution.rb 3 | binary_search,array,problems/binary_search.rb,specs/binary_search_spec.rb,solutions/binary_search_solution.rb 4 | caesar_cipher,string,problems/caesar_cipher.rb,specs/caesar_cipher_spec.rb,solutions/caesar_cipher_solution.rb 5 | deep_dup,recursion,problems/deep_dup.rb,specs/deep_dup_spec.rb,solutions/deep_dup_solution.rb 6 | digital_root,recursion,problems/digital_root.rb,specs/digital_root_spec.rb,solutions/digital_root_solution.rb 7 | doubler,enumerable,problems/doubler.rb,specs/doubler_spec.rb,solutions/doubler_solution.rb 8 | dups,array,problems/dups.rb,specs/dups_spec.rb,solutions/dups_solution.rb 9 | eight_queens,recursion,problems/eight_queens.rb,specs/eight_queens_spec.rb,solutions/eight_queens_solution.rb 10 | exponent,recursion,problems/exponent.rb,specs/exponent_spec.rb,solutions/exponent_solution.rb 11 | factorials_rec,recursion,problems/factorials_rec.rb,specs/factorials_rec_spec.rb,solutions/factorials_rec_solution.rb 12 | factors,array,problems/factors.rb,specs/factors_spec.rb,solutions/factors_solution.rb 13 | fibs_sum,recursion,problems/fibs_sum.rb,specs/fibs_sum_spec.rb,solutions/fibs_sum_solution.rb 14 | first_even_numbers_sum,recursion,problems/first_even_numbers_sum.rb,specs/first_even_numbers_sum_spec.rb,solutions/first_even_numbers_sum_solution.rb 15 | jumble_sort,sort,problems/jumble_sort.rb,specs/jumble_sort_spec.rb,solutions/jumble_sort_solution.rb 16 | make_better_change,recursion,problems/make_better_change.rb,specs/make_better_change_spec.rb,solutions/make_better_change_solution.rb 17 | median,array,problems/median.rb,specs/median_spec.rb,solutions/median_solution.rb 18 | merge_sort,sort,problems/merge_sort.rb,specs/merge_sort_spec.rb,solutions/merge_sort_solution.rb 19 | my_all,enumerable,problems/my_all.rb,specs/my_all_spec.rb,solutions/my_all_solution.rb 20 | my_any,enumerable,problems/my_any.rb,specs/my_any_spec.rb,solutions/my_any_solution.rb 21 | my_each,enumerable,problems/my_each.rb,specs/my_each_spec.rb,solutions/my_each_solution.rb 22 | my_each_hash,enumerable,problems/my_each_hash.rb,specs/my_each_hash_spec.rb,solutions/my_each_hash_solution.rb 23 | my_flatten,array,problems/my_flatten.rb,specs/my_flatten_spec.rb,solutions/my_flatten_solution.rb 24 | my_inject,enumerable,problems/my_inject.rb,specs/my_inject_spec.rb,solutions/my_inject_solution.rb 25 | my_join,array,problems/my_join.rb,specs/my_join_spec.rb,solutions/my_join_solution.rb 26 | my_merge,array,problems/my_merge.rb,specs/my_merge_spec.rb,solutions/my_merge_solution.rb 27 | my_reject,enumerable,problems/my_reject.rb,specs/my_reject_spec.rb,solutions/my_reject_solution.rb 28 | my_reverse,array,problems/my_reverse.rb,specs/my_reverse_spec.rb,solutions/my_reverse_solution.rb 29 | my_rotate,array,problems/my_rotate.rb,specs/my_rotate_spec.rb,solutions/my_rotate_solution.rb 30 | my_select,enumerable,problems/my_select.rb,specs/my_select_spec.rb,solutions/my_select_solution.rb 31 | my_zip,enumerable,problems/my_zip.rb,specs/my_zip_spec.rb,solutions/my_zip_solution.rb 32 | prime_factorization,recursion,problems/prime_factorization.rb,specs/prime_factorization_spec.rb,solutions/prime_factorization_solution.rb 33 | primes,array,problems/primes.rb,specs/primes_spec.rb,solutions/primes_solution.rb 34 | quicksort,sort,problems/quicksort.rb,specs/quicksort_spec.rb,solutions/quicksort_solution.rb 35 | real_words_in_string,string,problems/real_words_in_string.rb,specs/real_words_in_string_spec.rb,solutions/real_words_in_string_solution.rb 36 | string_include_key,recursion,problems/string_include_key.rb,specs/string_include_key_spec.rb,solutions/string_include_key_solution.rb 37 | subsets,recursion,problems/subsets.rb,specs/subsets_spec.rb,solutions/subsets_solution.rb 38 | symmetric_substrings,string,problems/symmetric_substrings.rb,specs/symmetric_substrings_spec.rb,solutions/symmetric_substrings_solution.rb 39 | two_sum,array,problems/two_sum.rb,specs/two_sum_spec.rb,solutions/two_sum_solution.rb 40 | rec_sum,recursion,problems/rec_sum.rb,specs/rec_sum_spec.rb,solutions/rec_sum_solution.rb 41 | permutations,recursion,problems/permutations.rb,specs/permutations_spec.rb,solutions/permutations_solution.rb 42 | bubble_sort,sort,problems/bubble_sort.rb,specs/bubble_sort_spec.rb,solutions/bubble_sort_solution.rb 43 | pig_latinify,string,problems/pig_latinify.rb,specs/pig_latinify_spec.rb,solutions/pig_latinify_solution.rb 44 | titleize,string,problems/titleize.rb,specs/titleize_spec.rb,solutions/titleize_solution.rb -------------------------------------------------------------------------------- /problems/base_converter.rb: -------------------------------------------------------------------------------- 1 | # Write a recursive method that takes in a base 10 number n and 2 | # converts it to a base b number. Return the new number as a string 3 | # 4 | # E.g. base_converter(5, 2) == "101" 5 | # base_converter(31, 16) == "1f" 6 | 7 | def base_converter(num, b) 8 | 9 | end 10 | -------------------------------------------------------------------------------- /problems/binary_search.rb: -------------------------------------------------------------------------------- 1 | class Array 2 | 3 | # Write a monkey patch of binary search: 4 | # E.g. [1, 2, 3, 4, 5, 7].my_bsearch(5) => 4 5 | def my_bsearch(target, &prc) 6 | 7 | end 8 | 9 | end 10 | -------------------------------------------------------------------------------- /problems/bubble_sort.rb: -------------------------------------------------------------------------------- 1 | class Array 2 | def bubble_sort!(&prc) 3 | end 4 | 5 | def bubble_sort(&prc) 6 | end 7 | end 8 | -------------------------------------------------------------------------------- /problems/caesar_cipher.rb: -------------------------------------------------------------------------------- 1 | # Back in the good old days, you used to be able to write a darn near 2 | # uncrackable code by simply taking each letter of a message and incrementing it 3 | # by a fixed number, so "abc" by 2 would look like "cde", wrapping around back 4 | # to "a" when you pass "z". Write a function, `caesar_cipher(str, shift)` which 5 | # will take a message and an increment amount and outputs the encoded message. 6 | # Assume lowercase and no punctuation. Preserve spaces. 7 | # 8 | # To get an array of letters "a" to "z", you may use `("a".."z").to_a`. To find 9 | # the position of a letter in the array, you may use `Array#find_index`. 10 | 11 | def caesar_cipher(str, shift) 12 | 13 | end 14 | -------------------------------------------------------------------------------- /problems/deep_dup.rb: -------------------------------------------------------------------------------- 1 | # Using recursion and the is_a? method, 2 | # write an Array#deep_dup method that will perform a "deep" duplication of the interior arrays. 3 | 4 | def deep_dup(arr) 5 | 6 | end 7 | -------------------------------------------------------------------------------- /problems/digital_root.rb: -------------------------------------------------------------------------------- 1 | # Write a method, `digital_root(num)`. It should Sum the digits of a positive 2 | # integer. If it is greater than 10, sum the digits of the resulting number. 3 | # Keep repeating until there is only one digit in the result, called the 4 | # "digital root". **Do not use string conversion within your method.** 5 | # 6 | # You may wish to use a helper function, `digital_root_step(num)` which performs 7 | # one step of the process. 8 | 9 | def digital_root(num) 10 | 11 | end 12 | -------------------------------------------------------------------------------- /problems/doubler.rb: -------------------------------------------------------------------------------- 1 | # Write a method that doubles each element in an array 2 | def doubler(array) 3 | end 4 | -------------------------------------------------------------------------------- /problems/dups.rb: -------------------------------------------------------------------------------- 1 | class Array 2 | 3 | # Write an Array#dups method that will return a hash containing the indices of all 4 | # duplicate elements. The keys are the duplicate elements; the values are 5 | # arrays of their indices in ascending order, e.g. 6 | # [1, 3, 4, 3, 0, 3, 0].dups => { 3 => [1, 3, 5], 0 => [4, 6] } 7 | 8 | def dups 9 | end 10 | end 11 | -------------------------------------------------------------------------------- /problems/eight_queens.rb: -------------------------------------------------------------------------------- 1 | # CHALLENGE: Eight queens puzzle precursor 2 | # 3 | # Write a recursive method that generates all 8! possible unique ways to 4 | # place eight queens on a chess board such that no two queens are in 5 | # the same board row or column (the same diagonal is OK). 6 | # 7 | # Each of the 8! elements in the return array should be an array of positions: 8 | # E.g. [[0,0], [1,1], [2,2], [3,3], [4,4], [5,5], [6,6], [7,7]] 9 | # 10 | # My solution used 3 method parameters: current_row, taken_columns, and 11 | # positions so far 12 | def eight_queens_possibilities(current_row, taken_columns, positions) 13 | 14 | end 15 | -------------------------------------------------------------------------------- /problems/exponent.rb: -------------------------------------------------------------------------------- 1 | # return b^n recursively. Your solution should accept negative values 2 | # for n 3 | def exponent(b, n) 4 | 5 | end 6 | -------------------------------------------------------------------------------- /problems/factorials_rec.rb: -------------------------------------------------------------------------------- 1 | # Write a recursive method that returns the first "num" factorial numbers. 2 | # Note that the 1st factorial number is 0!, which equals 1. The 2nd factorial 3 | # is 1!, the 3rd factorial is 2!, etc. 4 | 5 | def factorials_rec(num) 6 | end 7 | -------------------------------------------------------------------------------- /problems/factors.rb: -------------------------------------------------------------------------------- 1 | # Write a method that returns the factors of a number in ascending order. 2 | 3 | def factors(num) 4 | 5 | end 6 | -------------------------------------------------------------------------------- /problems/fibs_sum.rb: -------------------------------------------------------------------------------- 1 | # Implement a method that finds the sum of the first n 2 | # fibonacci numbers recursively. Assume n > 0 3 | def fibs_sum(n) 4 | 5 | end 6 | -------------------------------------------------------------------------------- /problems/first_even_numbers_sum.rb: -------------------------------------------------------------------------------- 1 | # return the sum of the first n even numbers recursively. Assume n > 0 2 | def first_even_numbers_sum(n) 3 | 4 | end 5 | -------------------------------------------------------------------------------- /problems/jumble_sort.rb: -------------------------------------------------------------------------------- 1 | # Jumble sort takes a string and an alphabet. It returns a copy of the string 2 | # with the letters re-ordered according to their positions in the alphabet. If 3 | # no alphabet is passed in, it defaults to normal alphabetical order (a-z). 4 | 5 | # Example: 6 | # jumble_sort("hello") => "ehllo" 7 | # jumble_sort("hello", ['o', 'l', 'h', 'e']) => 'ollhe' 8 | 9 | def jumble_sort(str, alphabet = nil) 10 | 11 | end 12 | -------------------------------------------------------------------------------- /problems/make_better_change.rb: -------------------------------------------------------------------------------- 1 | # make better change problem from class 2 | # make_better_change(24, [10,7,1]) should return [10,7,7] 3 | # make change with the fewest number of coins 4 | 5 | # To make_better_change, we only take one coin at a time and 6 | # never rule out denominations that we've already used. 7 | # This allows each coin to be available each time we get a new remainder. 8 | # By iterating over the denominations and continuing to search 9 | # for the best change, we assure that we test for 'non-greedy' uses 10 | # of each denomination. 11 | 12 | def make_better_change(value, coins) 13 | 14 | end 15 | -------------------------------------------------------------------------------- /problems/median.rb: -------------------------------------------------------------------------------- 1 | # Write a method that returns the median of elements in an array 2 | # If the length is even, return the average of the middle two elements 3 | class Array 4 | def median 5 | end 6 | end 7 | -------------------------------------------------------------------------------- /problems/merge_sort.rb: -------------------------------------------------------------------------------- 1 | class Array 2 | 3 | # Write an Array#merge_sort method; it should not modify the original array. 4 | 5 | def merge_sort(&prc) 6 | end 7 | 8 | private 9 | def self.merge(left, right, &prc) 10 | end 11 | end 12 | -------------------------------------------------------------------------------- /problems/my_all.rb: -------------------------------------------------------------------------------- 1 | class Array 2 | 3 | def my_all?(&prc) 4 | 5 | end 6 | 7 | end 8 | -------------------------------------------------------------------------------- /problems/my_any.rb: -------------------------------------------------------------------------------- 1 | class Array 2 | 3 | def my_any?(&prc) 4 | 5 | end 6 | 7 | end 8 | -------------------------------------------------------------------------------- /problems/my_each.rb: -------------------------------------------------------------------------------- 1 | class Array 2 | 3 | def my_each(&prc) 4 | 5 | end 6 | 7 | def my_each_with_index(&prc) 8 | 9 | end 10 | 11 | end 12 | -------------------------------------------------------------------------------- /problems/my_each_hash.rb: -------------------------------------------------------------------------------- 1 | class Hash 2 | 3 | # Write a version of my each that calls a proc on each key, value pair 4 | def my_each(&prc) 5 | 6 | end 7 | 8 | end 9 | -------------------------------------------------------------------------------- /problems/my_flatten.rb: -------------------------------------------------------------------------------- 1 | class Array 2 | 3 | # Takes a multi-dimentional array and returns a single array of all the elements 4 | # [1,[2,3], [4,[5]]].my_controlled_flatten(1) => [1,2,3,4,5] 5 | def my_flatten 6 | 7 | end 8 | 9 | # Write a version of flatten that only flattens n levels of an array. 10 | # E.g. If you have an array with 3 levels of nested arrays, and run 11 | # my_flatten(1), you should return an array with 2 levels of nested 12 | # arrays 13 | # 14 | # [1,[2,3], [4,[5]]].my_controlled_flatten(1) => [1,2,3,4,[5]] 15 | def my_controlled_flatten(n) 16 | 17 | end 18 | end 19 | -------------------------------------------------------------------------------- /problems/my_inject.rb: -------------------------------------------------------------------------------- 1 | class Array 2 | 3 | # Monkey patch the Array class and add a my_inject method. If my_inject receives 4 | # no argument, then use the first element of the array as the default accumulator. 5 | 6 | def my_inject(accumulator = nil) 7 | end 8 | end 9 | -------------------------------------------------------------------------------- /problems/my_join.rb: -------------------------------------------------------------------------------- 1 | class Array 2 | 3 | def my_join(str = "") 4 | 5 | end 6 | 7 | end 8 | -------------------------------------------------------------------------------- /problems/my_merge.rb: -------------------------------------------------------------------------------- 1 | class Hash 2 | 3 | # Write a version of merge. This should NOT modify the original hash 4 | def my_merge(hash2) 5 | 6 | end 7 | 8 | end 9 | -------------------------------------------------------------------------------- /problems/my_reject.rb: -------------------------------------------------------------------------------- 1 | class Array 2 | 3 | def my_reject(&prc) 4 | 5 | end 6 | 7 | end 8 | -------------------------------------------------------------------------------- /problems/my_reverse.rb: -------------------------------------------------------------------------------- 1 | class Array 2 | 3 | def my_reverse 4 | 5 | end 6 | 7 | end 8 | -------------------------------------------------------------------------------- /problems/my_rotate.rb: -------------------------------------------------------------------------------- 1 | class Array 2 | 3 | def my_rotate(num) 4 | 5 | end 6 | 7 | end 8 | -------------------------------------------------------------------------------- /problems/my_select.rb: -------------------------------------------------------------------------------- 1 | class Array 2 | 3 | def my_select(&prc) 4 | 5 | end 6 | 7 | end 8 | -------------------------------------------------------------------------------- /problems/my_zip.rb: -------------------------------------------------------------------------------- 1 | class Array 2 | 3 | def my_zip(*arrs) 4 | 5 | end 6 | 7 | end 8 | -------------------------------------------------------------------------------- /problems/permutations.rb: -------------------------------------------------------------------------------- 1 | # Write a recursive method that returns all of the permutations of an array 2 | def permutations(array) 3 | end 4 | -------------------------------------------------------------------------------- /problems/pig_latinify.rb: -------------------------------------------------------------------------------- 1 | # Write a method that translates a sentence into pig latin. You may want a helper method. 2 | # 'apple' => 'appleay' 3 | # 'pearl' => 'earlpay' 4 | # 'quick' => 'ickquay' 5 | def pig_latinify(sentence) 6 | end 7 | -------------------------------------------------------------------------------- /problems/prime_factorization.rb: -------------------------------------------------------------------------------- 1 | # Write a recursive function that returns the prime factorization of 2 | # a given number. Assume num > 1 3 | # 4 | # prime_factorization(12) => [2,2,3] 5 | def prime_factorization(num) 6 | 7 | end 8 | 9 | def is_prime?(num) 10 | 11 | end 12 | -------------------------------------------------------------------------------- /problems/primes.rb: -------------------------------------------------------------------------------- 1 | # primes(num) returns an array of the first "num" primes. 2 | # You may wish to use an is_prime? helper method. 3 | 4 | def is_prime?(num) 5 | end 6 | 7 | def primes(num) 8 | end 9 | -------------------------------------------------------------------------------- /problems/quicksort.rb: -------------------------------------------------------------------------------- 1 | class Array 2 | 3 | #Write a monkey patch of quick sort that accepts a block 4 | def my_quick_sort(&prc) 5 | 6 | end 7 | 8 | end 9 | -------------------------------------------------------------------------------- /problems/real_words_in_string.rb: -------------------------------------------------------------------------------- 1 | class String 2 | # Returns an array of all the subwords of the string that appear in the 3 | # dictionary argument. The method does NOT return any duplicates. 4 | 5 | def real_words_in_string(dictionary) 6 | 7 | end 8 | end 9 | -------------------------------------------------------------------------------- /problems/rec_sum.rb: -------------------------------------------------------------------------------- 1 | # Write a recursive method that returns the sum of all elements in an array 2 | def rec_sum(nums) 3 | end 4 | -------------------------------------------------------------------------------- /problems/string_include_key.rb: -------------------------------------------------------------------------------- 1 | # Write a recursive method that takes in a string to search and a key string. 2 | # Return true if the string contains all of the characters in the key 3 | # in the same order that they appear in the key. 4 | # 5 | # string_include_key?("cadbpc", "abc") => true 6 | # string_include_key("cba", "abc") => false 7 | def string_include_key?(string, key) 8 | 9 | end 10 | -------------------------------------------------------------------------------- /problems/subsets.rb: -------------------------------------------------------------------------------- 1 | #returns all subsets of an array 2 | def subsets(array) 3 | 4 | end 5 | -------------------------------------------------------------------------------- /problems/symmetric_substrings.rb: -------------------------------------------------------------------------------- 1 | class String 2 | 3 | # Write a String#symmetric_substrings method that returns an array of substrings 4 | # that are palindromes, e.g. "cool".symmetric_substrings => ["oo"] 5 | # Only include substrings of length > 1. 6 | 7 | def symmetric_substrings 8 | end 9 | end 10 | -------------------------------------------------------------------------------- /problems/titleize.rb: -------------------------------------------------------------------------------- 1 | # Write a method that capitalizes each word in a string like a book title 2 | # Do not capitalize words like 'a', 'and', 'of', 'over' or 'the' 3 | def titleize(title) 4 | end 5 | -------------------------------------------------------------------------------- /problems/two_sum.rb: -------------------------------------------------------------------------------- 1 | class Array 2 | # Write a method, `Array#two_sum`, that finds all pairs of positions where the 3 | # elements at those positions sum to zero. 4 | 5 | # NB: ordering matters. I want each of the pairs to be sorted smaller index 6 | # before bigger index. I want the array of pairs to be sorted 7 | # "dictionary-wise": 8 | # [0, 2] before [1, 2] (smaller first elements come first) 9 | # [0, 1] before [0, 2] (then smaller second elements come first) 10 | 11 | def two_sum 12 | 13 | end 14 | end 15 | -------------------------------------------------------------------------------- /solutions/base_converter_solution.rb: -------------------------------------------------------------------------------- 1 | def base_converter(num, b) 2 | return "" if num == 0 3 | 4 | digits = %w(0 1 2 3 4 5 6 7 8 9 a b c d e f) 5 | base_converter(num/b, b) + digits[num % b] 6 | end 7 | -------------------------------------------------------------------------------- /solutions/binary_search_solution.rb: -------------------------------------------------------------------------------- 1 | class Array 2 | 3 | def my_bsearch(target) 4 | return nil if size == 0 5 | mid = size/2 6 | 7 | case self[mid] <=> target 8 | when 0 9 | return mid 10 | when 1 11 | return self.dup.take(mid).my_bsearch(target) 12 | else 13 | search_res = self.dup.drop(mid+1).my_bsearch(target) 14 | search_res.nil? ? nil : mid + 1 + search_res 15 | end 16 | end 17 | 18 | end 19 | -------------------------------------------------------------------------------- /solutions/bubble_sort_solution.rb: -------------------------------------------------------------------------------- 1 | class Array 2 | def bubble_sort! 3 | # Without a proc 4 | sorted = false 5 | until sorted 6 | sorted = true 7 | 8 | each_index do |i| 9 | next if i + 1 == self.length 10 | j = i + 1 11 | if self[i] > self[j] 12 | sorted = false 13 | self[i], self[j] = self[j], self[i] 14 | end 15 | end 16 | end 17 | 18 | self 19 | end 20 | 21 | def bubble_sort!(&prc) 22 | # With a proc 23 | prc ||= Proc.new { |x, y| x <=> y } 24 | 25 | sorted = false 26 | until sorted 27 | sorted = true 28 | 29 | each_index do |i| 30 | next if i + 1 == self.length 31 | j = i + 1 32 | if prc.call(self[i], self[j]) == 1 33 | sorted = false 34 | self[i], self[j] = self[j], self[i] 35 | end 36 | end 37 | end 38 | 39 | self 40 | end 41 | 42 | def bubble_sort(&prc) 43 | self.dup.bubble_sort!(&prc) 44 | end 45 | end 46 | -------------------------------------------------------------------------------- /solutions/caesar_cipher_solution.rb: -------------------------------------------------------------------------------- 1 | def caesar_cipher(str, shift) 2 | letters = ("a".."z").to_a 3 | 4 | encoded_str = "" 5 | str.each_char do |char| 6 | if char == " " 7 | encoded_str << " " 8 | next 9 | end 10 | 11 | old_idx = letters.find_index(char) 12 | new_idx = (old_idx + shift) % letters.count 13 | 14 | encoded_str << letters[new_idx] 15 | end 16 | 17 | encoded_str 18 | end 19 | -------------------------------------------------------------------------------- /solutions/deep_dup_solution.rb: -------------------------------------------------------------------------------- 1 | def deep_dup(arr) 2 | arr.map{|el| el.is_a?(Array) ? deep_dup(el) : el} 3 | end 4 | -------------------------------------------------------------------------------- /solutions/digital_root_solution.rb: -------------------------------------------------------------------------------- 1 | 2 | def digital_root(num) 3 | while num > 10 4 | num = digital_root_step(num) 5 | end 6 | 7 | num 8 | end 9 | 10 | def digital_root_step(num) 11 | root = 0 12 | while num > 0 13 | root += (num % 10) 14 | 15 | num /= 10 16 | end 17 | 18 | root 19 | end 20 | 21 | # Alternate Solution 22 | # def digital_root(num) 23 | # digits = [] 24 | # 25 | # while num > 0 26 | # digits << num % 10 27 | # num /= 10 28 | # end 29 | # 30 | # digit_sum = digits.inject(&:+) 31 | # 32 | # digit_sum > 10 ? digital_root(digit_sum) : digit_sum 33 | # end 34 | -------------------------------------------------------------------------------- /solutions/doubler_solution.rb: -------------------------------------------------------------------------------- 1 | def doubler(array) 2 | array.map { |num| num * 2 } 3 | end 4 | -------------------------------------------------------------------------------- /solutions/dups_solution.rb: -------------------------------------------------------------------------------- 1 | class Array 2 | def dups 3 | positions = Hash.new { |h, k| h[k] = [] } 4 | 5 | each_with_index do |item, index| 6 | positions[item] << index 7 | end 8 | 9 | positions.select { |key, val| val.count > 1 } 10 | end 11 | end 12 | -------------------------------------------------------------------------------- /solutions/eight_queens_solution.rb: -------------------------------------------------------------------------------- 1 | def eight_queens_possibilities(row_num, taken_columns, positions) 2 | positions ||= [] 3 | return [positions] if row_num == 8 4 | 5 | all_positions = [] 6 | columns_left = (0..7).to_a - taken_columns 7 | 8 | columns_left.each do |col| 9 | positions_dup = positions.dup 10 | positions_dup << [row_num, col] 11 | 12 | all_positions += eight_queens_possibilities(row_num + 1, 13 | taken_columns + [col], positions_dup) 14 | end 15 | 16 | all_positions 17 | end 18 | -------------------------------------------------------------------------------- /solutions/exponent_solution.rb: -------------------------------------------------------------------------------- 1 | def exponent(b, n) 2 | return 1 if n == 0 3 | 4 | if n > 0 5 | b * exponent(b, n - 1) 6 | else 7 | 1.0/b * exponent(b, n + 1) 8 | end 9 | end 10 | -------------------------------------------------------------------------------- /solutions/factorials_rec_solution.rb: -------------------------------------------------------------------------------- 1 | # The "calls itself recursively" spec may say that there is no method 2 | # named "and_call_original" if you are using an older version of 3 | # rspec. You may ignore this failure. 4 | # Also, be aware that the first factorial number is 0!, which is defined 5 | # to equal 1. So the 2nd factorial is 1!, the 3rd factorial is 2!, etc. 6 | def factorials_rec(num) 7 | if num == 1 8 | [1] 9 | else 10 | facs = factorials_rec(num - 1) 11 | facs << facs.last * (num - 1) 12 | facs 13 | end 14 | end 15 | -------------------------------------------------------------------------------- /solutions/factors_solution.rb: -------------------------------------------------------------------------------- 1 | 2 | def factors(num) 3 | (1..num).select { |i| (num % i) == 0 } 4 | end 5 | -------------------------------------------------------------------------------- /solutions/fibs_sum_solution.rb: -------------------------------------------------------------------------------- 1 | def fibs_sum(n) 2 | return 0 if n == 0 3 | return 1 if n == 1 4 | 5 | fibs_sum(n-1) + fibs_sum(n-2) + 1 6 | end 7 | -------------------------------------------------------------------------------- /solutions/first_even_numbers_sum_solution.rb: -------------------------------------------------------------------------------- 1 | def first_even_numbers_sum(n) 2 | return 2 if n == 1 3 | 4 | 2 * n + first_even_numbers_sum(n-1) 5 | end 6 | -------------------------------------------------------------------------------- /solutions/jumble_sort_solution.rb: -------------------------------------------------------------------------------- 1 | 2 | def jumble_sort(str, alphabet = nil) 3 | alphabet ||= ('a'..'z').to_a 4 | 5 | sorted = false 6 | until sorted 7 | sorted = true 8 | 9 | str.length.times do |i| 10 | break if i == (str.length - 1) 11 | if alphabet.index(str[i]) > alphabet.index(str[i + 1]) 12 | str[i], str[i + 1] = str[i + 1], str[i] 13 | sorted = false 14 | end 15 | end 16 | end 17 | 18 | str 19 | end 20 | 21 | # Alternately: 22 | # 23 | # def jumble_sort(str, alphabet = nil) 24 | # alphabet ||= ('a'..'z').to_a 25 | # 26 | # sorted_chars = str.chars.sort do |chr1, chr2| 27 | # alphabet.index(chr1) <=> alphabet.index(chr2) 28 | # end 29 | # 30 | # sorted_chars.join 31 | # end 32 | -------------------------------------------------------------------------------- /solutions/make_better_change_solution.rb: -------------------------------------------------------------------------------- 1 | def make_better_change(value, coins) 2 | coins_to_check = coins.select{|coin| coin <= value} 3 | return nil if coins_to_check.empty? 4 | 5 | solutions = [] 6 | 7 | coins_to_check.sort.reverse.each do |coin| 8 | remainder = value - coin 9 | 10 | if remainder > 0 11 | remainder_solution = make_better_change(remainder, coins_to_check) 12 | solutions << [coin] + remainder_solution unless remainder_solution.nil? 13 | else 14 | solutions << [coin] 15 | end 16 | end 17 | 18 | solutions.sort_by!{|arr| arr.size}.first 19 | end 20 | -------------------------------------------------------------------------------- /solutions/median_solution.rb: -------------------------------------------------------------------------------- 1 | def median 2 | return nil if empty? 3 | sorted = self.sort 4 | if length.odd? 5 | sorted[length / 2] 6 | else 7 | (sorted[length / 2] + sorted[length / 2 - 1]).fdiv(2) 8 | end 9 | end 10 | -------------------------------------------------------------------------------- /solutions/merge_sort_solution.rb: -------------------------------------------------------------------------------- 1 | class Array 2 | def merge_sort(&prc) 3 | # See how I create a Proc if no block was given; this eliminates 4 | # having to later have two branches of logic, one for a block and 5 | # one for no block. 6 | prc ||= Proc.new { |x, y| x <=> y } 7 | 8 | return self if count <= 1 9 | 10 | midpoint = count / 2 11 | sorted_left = self.take(midpoint).merge_sort(&prc) 12 | sorted_right = self.drop(midpoint).merge_sort(&prc) 13 | 14 | Array.merge(sorted_left, sorted_right, &prc) 15 | 16 | end 17 | 18 | private 19 | def self.merge(left, right, &prc) 20 | merged = [] 21 | 22 | until left.empty? || right.empty? 23 | case prc.call(left.first, right.first) 24 | when -1 25 | merged << left.shift 26 | when 0 27 | merged << left.shift 28 | when 1 29 | merged << right.shift 30 | end 31 | end 32 | 33 | merged.concat(left) 34 | merged.concat(right) 35 | 36 | merged 37 | end 38 | end 39 | -------------------------------------------------------------------------------- /solutions/my_all_solution.rb: -------------------------------------------------------------------------------- 1 | class Array 2 | 3 | def my_all?(&prc) 4 | my_each{|el| return false unless prc.call(el)} 5 | true 6 | end 7 | 8 | end 9 | -------------------------------------------------------------------------------- /solutions/my_any_solution.rb: -------------------------------------------------------------------------------- 1 | class Array 2 | 3 | def my_any?(&prc) 4 | my_each{|el| return true if prc.call(el)} 5 | false 6 | end 7 | 8 | end 9 | -------------------------------------------------------------------------------- /solutions/my_each_hash_solution.rb: -------------------------------------------------------------------------------- 1 | class Hash 2 | 3 | def my_each(&prc) 4 | keys.each do |k| 5 | prc.call(k, self[k]) 6 | end 7 | end 8 | 9 | end 10 | -------------------------------------------------------------------------------- /solutions/my_each_solution.rb: -------------------------------------------------------------------------------- 1 | class Array 2 | 3 | def my_each(&prc) 4 | (0...size).each do |i| 5 | prc.call(self[i]) 6 | end 7 | 8 | self 9 | end 10 | 11 | def my_each_with_index(&prc) 12 | (0...size).each do |idx| 13 | prc.call(self[idx], idx) 14 | end 15 | 16 | self 17 | end 18 | end 19 | -------------------------------------------------------------------------------- /solutions/my_flatten_solution.rb: -------------------------------------------------------------------------------- 1 | class Array 2 | 3 | def my_flatten 4 | flattened = [] 5 | self.my_each do |el| 6 | el.is_a?(Array) ? flattened += el.my_flatten : flattened << el 7 | end 8 | flattened 9 | end 10 | 11 | def my_controlled_flatten(n) 12 | return self if n < 1 13 | result = [] 14 | 15 | each do |el| 16 | if el.is_a?(Array) 17 | result += el.my_controlled_flatten(n-1) 18 | else 19 | result << el 20 | end 21 | end 22 | 23 | result 24 | end 25 | 26 | end 27 | -------------------------------------------------------------------------------- /solutions/my_inject_solution.rb: -------------------------------------------------------------------------------- 1 | class Array 2 | def my_inject(accumulator = nil, &block) 3 | i = 0 4 | 5 | if accumulator.nil? 6 | accumulator = self.first 7 | i += 1 8 | end 9 | 10 | while i < length 11 | accumulator = block.call(accumulator, self[i]) 12 | i += 1 13 | end 14 | 15 | accumulator 16 | end 17 | end 18 | -------------------------------------------------------------------------------- /solutions/my_join_solution.rb: -------------------------------------------------------------------------------- 1 | class Array 2 | 3 | def my_join(str = "") 4 | res = "" 5 | my_each_with_index do |el, i| 6 | res << el.to_s 7 | res << str unless i == size - 1 8 | end 9 | 10 | res 11 | end 12 | 13 | end 14 | -------------------------------------------------------------------------------- /solutions/my_merge_solution.rb: -------------------------------------------------------------------------------- 1 | class Hash 2 | 3 | def my_merge(hash2) 4 | self_dup = dup 5 | 6 | hash2.each do |k, v| 7 | self_dup[k] = v 8 | end 9 | 10 | self_dup 11 | end 12 | 13 | end 14 | -------------------------------------------------------------------------------- /solutions/my_reject_solution.rb: -------------------------------------------------------------------------------- 1 | class Array 2 | 3 | def my_reject(&prc) 4 | self.dup - my_select(&prc) 5 | end 6 | 7 | end 8 | -------------------------------------------------------------------------------- /solutions/my_reverse_solution.rb: -------------------------------------------------------------------------------- 1 | class Array 2 | 3 | def my_reverse 4 | reversed = [] 5 | my_each{|el| reversed.unshift(el)} 6 | reversed 7 | end 8 | 9 | end 10 | -------------------------------------------------------------------------------- /solutions/my_rotate_solution.rb: -------------------------------------------------------------------------------- 1 | class Array 2 | 3 | def my_rotate(num=1) 4 | rotations = num % size 5 | rotated_arr = self.dup 6 | 7 | rotations.times do 8 | rotated_arr << rotated_arr.shift 9 | end 10 | 11 | rotated_arr 12 | end 13 | 14 | end 15 | -------------------------------------------------------------------------------- /solutions/my_select_solution.rb: -------------------------------------------------------------------------------- 1 | class Array 2 | 3 | def my_select(&prc) 4 | select = [] 5 | 6 | self.my_each{|el| select << el if prc.call(el)} 7 | select 8 | end 9 | 10 | end 11 | -------------------------------------------------------------------------------- /solutions/my_zip_solution.rb: -------------------------------------------------------------------------------- 1 | class Array 2 | 3 | def my_zip(*arrs) 4 | result = [] 5 | (0...size).each do |idx| 6 | result << [self[idx]] 7 | arrs.each do |arr| 8 | result[idx] << arr[idx] 9 | end 10 | end 11 | 12 | result 13 | end 14 | 15 | end 16 | -------------------------------------------------------------------------------- /solutions/permutations_solution.rb: -------------------------------------------------------------------------------- 1 | def permutations(array) 2 | return [array] if array.length <= 1 3 | 4 | 5 | # Similar to the subsets problem, we observe that to get the permutations 6 | # of [1, 2, 3] we can look at the permutations of [1, 2] which are 7 | # [1, 2] and [2, 1] and add the last element to every possible index getting 8 | # [3, 1, 2], [1, 3, 2], [1, 2, 3], [3, 2, 1], [2, 3, 1] 9 | 10 | # pop off the last element 11 | first = array.shift 12 | # make the recursive call 13 | perms = permutations(array) 14 | # we will need an array to store all our different permutations 15 | total_permutations = [] 16 | 17 | 18 | # Now we iterate over the result of our recusive call say [[1, 2], [2, 1]] 19 | # and for each permutation add first into every index. This new subarray 20 | # gets added to total_permutations. 21 | perms.each do |perm| 22 | (0..perm.length).each do |i| 23 | total_permutations << perm[0 ... i] + [first] + perm[i .. -1] 24 | end 25 | end 26 | total_permutations.sort 27 | end 28 | -------------------------------------------------------------------------------- /solutions/pig_latinify_solution.rb: -------------------------------------------------------------------------------- 1 | def pig_latinify(sentence) 2 | translated_words = sentence.split(" ").map do |word| 3 | translate_word(word) 4 | end 5 | translated_words.join(" ") 6 | end 7 | 8 | def translate_word(word) 9 | vowels = %w(a e i o u) 10 | if vowels.include?(word[0]) 11 | "#{word}ay" 12 | else 13 | phoneme_end = 0 14 | until vowels.include?(word[phoneme_end]) 15 | phoneme_end += 1 16 | end 17 | phoneme_end += 1 if word[phoneme_end - 1] == "q" 18 | "#{word[phoneme_end..-1]}#{word[0...phoneme_end]}ay" 19 | end 20 | end 21 | -------------------------------------------------------------------------------- /solutions/prime_factorization_solution.rb: -------------------------------------------------------------------------------- 1 | def prime_factorization(num) 2 | return [] if num == 1 3 | 4 | (2..Math.sqrt(num).ceil).each do |i| 5 | if num % i == 0 6 | return [i] + prime_factorization(num/i) 7 | end 8 | end 9 | 10 | return [num] 11 | end 12 | -------------------------------------------------------------------------------- /solutions/primes_solution.rb: -------------------------------------------------------------------------------- 1 | def is_prime?(num) 2 | (2...num).none? { |factor| num % factor == 0 } 3 | end 4 | 5 | def primes(count) 6 | primes = [] 7 | 8 | i = 2 9 | until primes.count >= count 10 | primes << i if is_prime?(i) 11 | 12 | i += 1 13 | end 14 | 15 | primes 16 | end 17 | -------------------------------------------------------------------------------- /solutions/quicksort_solution.rb: -------------------------------------------------------------------------------- 1 | class Array 2 | 3 | def my_quick_sort(&prc) 4 | prc ||= proc {|a, b| a<=>b} 5 | return self if size < 2 6 | 7 | pivot = first 8 | left = self[1..-1].select{|el| prc.call(el, pivot) == -1} 9 | right = self[1..-1].select{|el| prc.call(el, pivot) != -1} 10 | 11 | left.my_quick_sort(&prc) + [pivot] + right.my_quick_sort(&prc) 12 | end 13 | 14 | end 15 | -------------------------------------------------------------------------------- /solutions/real_words_in_string_solution.rb: -------------------------------------------------------------------------------- 1 | 2 | class String 3 | def real_words_in_string(dictionary) 4 | real_words = [] 5 | (1...self.length - 1).each do |first| 6 | (first + 1...self.length).each do |last| 7 | word = self[first...last] 8 | if dictionary.include?(word) 9 | real_words << word unless real_words.include?(word) 10 | end 11 | end 12 | end 13 | real_words 14 | end 15 | end 16 | -------------------------------------------------------------------------------- /solutions/rec_sum_solution.rb: -------------------------------------------------------------------------------- 1 | def rec_sum(nums) 2 | return 0 if nums.empty? 3 | nums[0] + rec_sum(nums.drop(1)) 4 | end 5 | -------------------------------------------------------------------------------- /solutions/string_include_key_solution.rb: -------------------------------------------------------------------------------- 1 | def string_include_key?(string, key) 2 | return true if key.length == 0 3 | 4 | next_key_char = key.chars.first 5 | key_index = string.index(next_key_char) 6 | 7 | return false if key_index.nil? 8 | string_include_key?(string[key_index+1..-1], key[1..-1]) 9 | end 10 | -------------------------------------------------------------------------------- /solutions/subsets_solution.rb: -------------------------------------------------------------------------------- 1 | def subsets(arr) 2 | return [[]] if arr.empty? 3 | 4 | subs = subsets(arr[0..-2]) 5 | subs.concat(subs.map{|el| el += [arr.last]}) 6 | end 7 | -------------------------------------------------------------------------------- /solutions/symmetric_substrings_solution.rb: -------------------------------------------------------------------------------- 1 | class String 2 | def symmetric_substrings 3 | symm_subs = [] 4 | 5 | length.times do |start_pos| 6 | (2..(length - start_pos)).each do |len| 7 | substr = self[start_pos...(start_pos + len)] 8 | symm_subs << substr if substr == substr.reverse 9 | end 10 | end 11 | 12 | symm_subs 13 | end 14 | end 15 | -------------------------------------------------------------------------------- /solutions/titleize_solution.rb: -------------------------------------------------------------------------------- 1 | LITTLE_WORDS = [ 2 | "and", 3 | "the", 4 | "over" 5 | ] 6 | 7 | def titleize(title) 8 | words = title.split(" ") 9 | titleized_words = words.map.with_index do |word, i| 10 | if i != 0 && LITTLE_WORDS.include?(word) 11 | word.downcase 12 | else 13 | word.capitalize 14 | end 15 | end 16 | 17 | titleized_words.join(" ") 18 | end 19 | -------------------------------------------------------------------------------- /solutions/two_sum_solution.rb: -------------------------------------------------------------------------------- 1 | 2 | class Array 3 | def two_sum 4 | pairs = [] 5 | (0...length).each do |i| 6 | ((i + 1)...length).each do |j| 7 | pairs << [i, j] if self[i] + self[j] == 0 8 | end 9 | end 10 | 11 | pairs 12 | end 13 | end 14 | -------------------------------------------------------------------------------- /specs/base_converter_spec.rb: -------------------------------------------------------------------------------- 1 | describe "#base_converter" do 2 | it "converts a small number in binary" do 3 | expect(base_converter(5, 2)).to eq("101") 4 | end 5 | 6 | it "converts a large number into base 16" do 7 | expect(base_converter(1239449, 16)).to eq("12e999" || "12E999" ) 8 | end 9 | end 10 | -------------------------------------------------------------------------------- /specs/binary_search_spec.rb: -------------------------------------------------------------------------------- 1 | describe "my_bsearch" do 2 | 3 | it "finds the first element in the array" do 4 | expect([1, 2, 3].my_bsearch(1)).to eq(0) 5 | end 6 | 7 | it "finds an element for an array with an even number of elements" do 8 | expect([2, 3, 4, 5].my_bsearch(3)).to eq(1) 9 | end 10 | 11 | it "finds an element for an array with an odd number of elements" do 12 | expect([2, 4, 6, 8, 10].my_bsearch(6)).to eq(2) 13 | end 14 | 15 | it "finds an element in the second half of the array (even)" do 16 | expect([1, 3, 4, 5, 9].my_bsearch(5)).to eq(3) 17 | end 18 | 19 | it "finds an element in the second half of the array (odd)" do 20 | expect([1, 2, 3, 4, 5, 6].my_bsearch(6)).to eq(5) 21 | end 22 | 23 | it "Returns nil if the element is not in the array (smaller)" do 24 | expect([1, 2, 3, 4, 5, 6].my_bsearch(0)).to eq(nil) 25 | end 26 | 27 | it "Returns nil if the element is not in the array (bigger)" do 28 | expect([1, 2, 3, 4, 5, 7].my_bsearch(6)).to eq(nil) 29 | end 30 | 31 | end 32 | -------------------------------------------------------------------------------- /specs/bubble_sort_spec.rb: -------------------------------------------------------------------------------- 1 | describe "#bubble_sort!" do 2 | let(:array) { [1, 2, 3, 4, 5].shuffle } 3 | 4 | it "works with an empty array" do 5 | expect([].bubble_sort!).to eq([]) 6 | end 7 | 8 | it "works with an array of one item" do 9 | expect([1].bubble_sort!).to eq([1]) 10 | end 11 | 12 | it "sorts numbers" do 13 | expect(array.bubble_sort!).to eq(array.sort) 14 | end 15 | 16 | it "modifies the original array" do 17 | duped_array = array.dup 18 | array.bubble_sort! 19 | expect(duped_array).not_to eq(array) 20 | end 21 | 22 | it "will use a block if given" do 23 | sorted = array.bubble_sort! do |num1, num2| 24 | # order numbers based on descending sort of their squares 25 | num2**2 <=> num1**2 26 | end 27 | 28 | expect(sorted).to eq([5, 4, 3, 2, 1]) 29 | end 30 | end 31 | 32 | describe "#bubble_sort" do 33 | let(:array) { [1, 2, 3, 4, 5].shuffle } 34 | 35 | it "delegates to #bubble_sort!" do 36 | expect_any_instance_of(Array).to receive(:bubble_sort!) 37 | 38 | array.bubble_sort 39 | end 40 | 41 | it "does not modify the original array" do 42 | duped_array = array.dup 43 | array.bubble_sort 44 | expect(duped_array).to eq(array) 45 | end 46 | end 47 | -------------------------------------------------------------------------------- /specs/caesar_cipher_spec.rb: -------------------------------------------------------------------------------- 1 | describe "#caesar_cipher" do 2 | it "encodes a simple word" do 3 | expect(caesar_cipher("aaa", 11)).to eq("lll") 4 | end 5 | 6 | it "wraps around the alphabet" do 7 | expect(caesar_cipher("zzz", 1)).to eq("aaa") 8 | end 9 | 10 | it "encodes multiple words" do 11 | expect(caesar_cipher("catz hatz", 2)).to eq("ecvb jcvb") 12 | end 13 | end 14 | -------------------------------------------------------------------------------- /specs/deep_dup_spec.rb: -------------------------------------------------------------------------------- 1 | describe "deep_dup" do 2 | robot_parts = [ 3 | ["nuts", "bolts", "washers"], 4 | ["capacitors", "resistors", "inductors"] 5 | ] 6 | 7 | copy = deep_dup(robot_parts) 8 | 9 | it "makes a copy of the original array" do 10 | expect(copy).to eq(robot_parts) 11 | end 12 | 13 | it "deeply copies arrays" do 14 | copy[1] << "LEDs" 15 | expect(robot_parts[1]).to eq(["capacitors", "resistors", "inductors"]) 16 | end 17 | end 18 | -------------------------------------------------------------------------------- /specs/digital_root_spec.rb: -------------------------------------------------------------------------------- 1 | describe "#digital_root" do 2 | it "calculates the digital root of a single-digit number" do 3 | expect(digital_root(9)).to eq(9) 4 | end 5 | 6 | it "calculates the digital root of a larger number" do 7 | expect(digital_root(4322)).to eq(2) 8 | end 9 | 10 | it "does not call #to_s on the argument" do 11 | expect_any_instance_of(Fixnum).to_not receive(:to_s) 12 | digital_root(4322) 13 | end 14 | end 15 | -------------------------------------------------------------------------------- /specs/doubler_spec.rb: -------------------------------------------------------------------------------- 1 | describe "#doubler" do 2 | let(:array) { [1, 2, 3] } 3 | 4 | it "doubles the elements of the array" do 5 | expect(doubler(array)).to eq([2, 4, 6]) 6 | end 7 | 8 | it "does not modify the original array" do 9 | duped_array = array.dup 10 | 11 | doubler(array) 12 | 13 | expect(array).to eq(duped_array) 14 | end 15 | end 16 | -------------------------------------------------------------------------------- /specs/dups_spec.rb: -------------------------------------------------------------------------------- 1 | describe "#dups" do 2 | it "solves a simple example" do 3 | expect([1, 3, 0, 1].dups).to eq({ 1 => [0, 3] }) 4 | end 5 | 6 | it "finds two dups" do 7 | expect([1, 3, 0, 3, 1].dups).to eq({ 1 => [0, 4], 3 => [1, 3] }) 8 | end 9 | 10 | it "finds multi-dups" do 11 | expect([1, 3, 4, 3, 0, 3].dups).to eq({ 3 => [1, 3, 5] }) 12 | end 13 | 14 | it "returns {} when no dups found" do 15 | expect([1, 3, 4, 5].dups).to eq({}) 16 | end 17 | end 18 | -------------------------------------------------------------------------------- /specs/eight_queens_spec.rb: -------------------------------------------------------------------------------- 1 | require 'yaml' 2 | 3 | describe "eight_queens_possibilities" do 4 | sol = YAML.load_file("./possible_boards.yml").sort 5 | pos = eight_queens_possibilities(0, [], nil) 6 | 7 | it "Returns an array of the correct_size" do 8 | expect(pos.length).to eq(sol.length) 9 | end 10 | 11 | it "Returns the correct positions" do 12 | expect(pos.sort).to eq(sol) 13 | end 14 | end 15 | -------------------------------------------------------------------------------- /specs/exponent_spec.rb: -------------------------------------------------------------------------------- 1 | describe "exponent" do 2 | it "correctly handles positive powers" do 3 | expect(exponent(5,3)).to eq(125) 4 | end 5 | 6 | it "correctly handles negative powers" do 7 | expect(exponent(2, -3)).to eq(1/8.0) 8 | end 9 | 10 | it "correctly handles 0" do 11 | expect(exponent(2, 0)).to eq(1) 12 | end 13 | end 14 | -------------------------------------------------------------------------------- /specs/factorials_rec_spec.rb: -------------------------------------------------------------------------------- 1 | 2 | describe "#factorials_rec" do 3 | it "returns first factorial number" do 4 | expect(factorials_rec(1)).to eq([1]) 5 | end 6 | 7 | it "returns first two factorial numbers" do 8 | expect(factorials_rec(2)).to eq([1, 1]) # = [0!, 1!] 9 | end 10 | 11 | it "returns many factorials numbers" do 12 | expect(factorials_rec(6)).to eq([1, 1, 2, 6, 24, 120]) 13 | # == [0!, 1!, 2!, 3!, 4!, 5!] 14 | end 15 | 16 | it "calls itself recursively" do 17 | # this should enforce you calling your method recursively. 18 | 19 | expect(self).to receive(:factorials_rec).at_least(:twice).and_call_original 20 | factorials_rec(6) 21 | end 22 | end 23 | -------------------------------------------------------------------------------- /specs/factors_spec.rb: -------------------------------------------------------------------------------- 1 | describe "#factors" do 2 | it "returns the factors of 10 in order" do 3 | expect(factors(10)).to eq([1, 2, 5, 10]) 4 | end 5 | 6 | it "returns just two factors for primes" do 7 | expect(factors(13)).to eq([1, 13]) 8 | end 9 | end 10 | -------------------------------------------------------------------------------- /specs/fibs_sum_spec.rb: -------------------------------------------------------------------------------- 1 | describe 'fibs_sum' do 2 | 3 | it 'It correctly gets the answer for the 1st fibonacci number' do 4 | expect(fibs_sum(1)).to eq(1) 5 | end 6 | 7 | it 'It correctly gets the answer for the first 2 fibonacci numbers' do 8 | expect(fibs_sum(2)).to eq(2) 9 | end 10 | 11 | it 'It correctly gets the answer for the first 6 fibonacci numbers' do 12 | expect(fibs_sum(6)).to eq(20) 13 | end 14 | 15 | end 16 | -------------------------------------------------------------------------------- /specs/first_even_numbers_sum_spec.rb: -------------------------------------------------------------------------------- 1 | describe 'first_even_numbers_sum' do 2 | 3 | it "Correctly returns the sum of the first even number" do 4 | expect(first_even_numbers_sum(1)).to eq(2) 5 | end 6 | 7 | it "Returns the sum of the first n even numbers" do 8 | expect(first_even_numbers_sum(6)).to eq(42) 9 | end 10 | 11 | end 12 | -------------------------------------------------------------------------------- /specs/jumble_sort_spec.rb: -------------------------------------------------------------------------------- 1 | describe "#jumble_sort" do 2 | it "defaults to alphabetical order" do 3 | expect(jumble_sort("hello")).to eq("ehllo") 4 | end 5 | 6 | it "takes an alphabet array and sorts by that order" do 7 | alph = ("a".."z").to_a 8 | hello = "hello".chars.uniq 9 | alph -= hello 10 | alphabet = (hello += alph) 11 | 12 | expect(jumble_sort("hello", alphabet)).to eq("hello") 13 | end 14 | 15 | it "sorts by a reversed alphabet" do 16 | reverse = ("a".."z").to_a.reverse 17 | expect(jumble_sort("hello", reverse)).to eq("ollhe") 18 | end 19 | end 20 | -------------------------------------------------------------------------------- /specs/make_better_change_spec.rb: -------------------------------------------------------------------------------- 1 | describe "Make better change" do 2 | it "Returns the smallest possible array of coins: case 1" do 3 | expect(make_better_change(24, [10,7,1])).to match_array([10,7,7]) 4 | end 5 | 6 | it "Returns the smallest possible array of coins: case 2" do 7 | expect(make_better_change(25, [10,7,1])).to match_array([10,7,7,1]) 8 | end 9 | 10 | it "Returns the smallest possible array of coins: case 3" do 11 | expect(make_better_change(25, [10,8,7,1])).to match_array([10,8,7]) 12 | end 13 | end 14 | -------------------------------------------------------------------------------- /specs/median_spec.rb: -------------------------------------------------------------------------------- 1 | describe "#median" do 2 | let(:even_array) { [3, 2, 6, 7] } 3 | let(:odd_array) { [3, 2, 6, 7, 1] } 4 | 5 | it "returns nil for the empty array" do 6 | expect([].median).to be_nil 7 | end 8 | 9 | it "returns the element for an array of length 1" do 10 | expect([1].median).to eq(1) 11 | end 12 | 13 | it "returns the median of an odd-length array" do 14 | expect(odd_array.median).to eq(3) 15 | end 16 | 17 | it "returns the median of an even-length array" do 18 | expect(even_array.median).to eq(4.5) 19 | end 20 | end 21 | -------------------------------------------------------------------------------- /specs/merge_sort_spec.rb: -------------------------------------------------------------------------------- 1 | describe "#merge_sort" do 2 | let(:array) { [1, 2, 3, 4, 5].shuffle } 3 | 4 | it "works with an empty array" do 5 | expect([].merge_sort).to eq([]) 6 | end 7 | 8 | it "works with an array of one item" do 9 | expect([1].merge_sort).to eq([1]) 10 | end 11 | 12 | it "sorts numbers" do 13 | expect(array.merge_sort).to eq(array.sort) 14 | end 15 | 16 | it "will use block if given" do 17 | reversed = array.merge_sort do |num1, num2| 18 | # reverse order 19 | num2 <=> num1 20 | end 21 | expect(reversed).to eq([5, 4, 3, 2, 1]) 22 | end 23 | 24 | it "does not modify original" do 25 | duped_array = array.dup 26 | duped_array.merge_sort 27 | expect(duped_array).to eq(array) 28 | end 29 | 30 | it "calls the merge helper method" do 31 | expect(Array).to receive(:merge).at_least(:once).and_call_original 32 | array.merge_sort 33 | end 34 | end 35 | -------------------------------------------------------------------------------- /specs/my_all_spec.rb: -------------------------------------------------------------------------------- 1 | describe 'my_all' do 2 | a= [1,2,3] 3 | 4 | it "returns true if all elements match the block" do 5 | expect(a.my_all? { |num| num > 0 }).to eq(true) 6 | end 7 | 8 | it "returns false if not all elementes match the block" do 9 | expect(a.my_all? { |num| num > 1 }).to eq(false) 10 | end 11 | end 12 | -------------------------------------------------------------------------------- /specs/my_any_spec.rb: -------------------------------------------------------------------------------- 1 | describe 'my_any' do 2 | a= [1,2,3] 3 | it "returns true if any number matches the block" do 4 | expect(a.my_any? { |num| num > 1 }).to eq(true) 5 | end 6 | 7 | it "returns false if no elementes match the block" do 8 | expect(a.my_any? { |num| num == 4 }).to eq(false) 9 | end 10 | end 11 | -------------------------------------------------------------------------------- /specs/my_each_hash_spec.rb: -------------------------------------------------------------------------------- 1 | describe "my_each" do 2 | a = {"a"=> 1, "b" => 2, "c" => 3} 3 | res = "" 4 | a.my_each{|key, v| v.times{res << key}} 5 | 6 | it "Calls the proc on each key value pair" do 7 | expect(res.chars.sort).to eq(["a","b","b","c","c","c"]) 8 | end 9 | end 10 | -------------------------------------------------------------------------------- /specs/my_each_spec.rb: -------------------------------------------------------------------------------- 1 | describe "my_each" do 2 | res = [] 3 | [1,2,3].my_each{|el| res << 2*el} 4 | 5 | it "It works for blocks" do 6 | expect(res).to eq([2,4,6]) 7 | end 8 | end 9 | 10 | describe "my_each_with_index" do 11 | res = [] 12 | [1,2,3].my_each_with_index{|el, i| res << 2*el + i} 13 | 14 | it "It works for blocks that use both the index and element" do 15 | expect(res).to eq([2,5,8]) 16 | end 17 | end 18 | -------------------------------------------------------------------------------- /specs/my_flatten_spec.rb: -------------------------------------------------------------------------------- 1 | describe "my_flatten" do 2 | it 'Flattens arrays correctly' do 3 | expect([1, 2, 3, [4, [5, 6]], [[[7]], 8]].my_flatten).to eq([1, 2, 3, 4, 5, 6, 7, 8]) 4 | end 5 | end 6 | 7 | describe "my_controlled_flatten" do 8 | it "Flattens an array the specified number of levels" do 9 | expect([1,[2,3], [4,[5]], [[6,[7]]]].my_controlled_flatten(1)).to eq([1,2,3,4,[5], [6, [7]]]) 10 | end 11 | end 12 | -------------------------------------------------------------------------------- /specs/my_inject_spec.rb: -------------------------------------------------------------------------------- 1 | describe 'Array#my_inject' do 2 | 3 | it 'calls the block passed to it' do 4 | expect do |block| 5 | ["test array"].my_inject(:dummy, &block) 6 | end.to yield_control.once 7 | end 8 | 9 | it 'makes the first element the accumulator if no default is given' do 10 | expect do |block| 11 | ["el1", "el2", "el3"].my_inject(&block) 12 | end.to yield_successive_args(["el1", "el2"], [nil, "el3"]) 13 | end 14 | 15 | it 'yields the accumulator and each element to the block' do 16 | expect do |block| 17 | [1, 2, 3].my_inject(100, &block) 18 | end.to yield_successive_args([100, 1], [nil, 2], [nil, 3]) 19 | end 20 | 21 | it 'does NOT call the built in Array#inject or Array#reduce method' do 22 | original_array = ["original array"] 23 | expect(original_array).not_to receive(:inject) 24 | expect(original_array).not_to receive(:reduce) 25 | original_array.my_inject {} 26 | end 27 | 28 | it 'with accumulator, it correctly injects and returns answer' do 29 | expect([1, 2, 3].my_inject(1) { |acc, x| acc + x }).to eq(7) 30 | expect([3, 3].my_inject(3) { |acc, x| acc * x }).to eq(27) 31 | end 32 | 33 | it 'without accumulator, it correctly injects and returns answer' do 34 | expect([1, 2, 3].my_inject { |acc, x| acc + x }).to eq(6) 35 | expect([3, 3].my_inject { |acc, x| acc * x }).to eq(9) 36 | end 37 | end 38 | -------------------------------------------------------------------------------- /specs/my_join_spec.rb: -------------------------------------------------------------------------------- 1 | describe "my_join" do 2 | a = [ "a", "b", "c", "d" ] 3 | 4 | it "Joins an array if no argument is passed" do 5 | expect(a.my_join).to eq("abcd") 6 | end 7 | 8 | it "Joins an array if an argument is passed" do 9 | expect(a.my_join("$")).to eq("a$b$c$d") 10 | end 11 | end 12 | -------------------------------------------------------------------------------- /specs/my_merge_spec.rb: -------------------------------------------------------------------------------- 1 | describe "my_merge" do 2 | a = {a: 1, b: 2, c: 3} 3 | b = {d: 4, e: 5} 4 | c = {c: 33, d: 4, e: 5} 5 | 6 | it "Merges 2 hashes and returns a hash" do 7 | expect(a.my_merge(b)).to eq(a.merge(b)) 8 | end 9 | 10 | it "Priortizes values from the hash being merged in" do 11 | expect(a.my_merge(c)).to eq(a.merge(c)) 12 | end 13 | end 14 | -------------------------------------------------------------------------------- /specs/my_reject_spec.rb: -------------------------------------------------------------------------------- 1 | describe 'my_reject' do 2 | 3 | a = [1, 2, 3] 4 | 5 | it 'It correctly selects elements that do not match the passed in block' do 6 | expect(a.my_reject { |num| num > 1 }).to eq([1]) 7 | end 8 | 9 | it 'It returns all elements if no elements match the block' do 10 | expect(a.my_reject { |num| num == 4 }).to eq([1,2,3]) 11 | end 12 | 13 | end 14 | -------------------------------------------------------------------------------- /specs/my_reverse_spec.rb: -------------------------------------------------------------------------------- 1 | describe "my_reverse" do 2 | a = [ "a", "b", "c", "d" ] 3 | 4 | it "Reverses an array" do 5 | expect(a.my_reverse).to eq(a.reverse) 6 | end 7 | end 8 | -------------------------------------------------------------------------------- /specs/my_rotate_spec.rb: -------------------------------------------------------------------------------- 1 | describe "my_rotate" do 2 | a = [ "a", "b", "c", "d" ] 3 | 4 | it "Rotates the elements 1 position if no argument is passed in" do 5 | expect(a.my_rotate).to eq(["b", "c", "d", "a"]) 6 | end 7 | 8 | it "Rotates the elements correctly if an argument is passed in" do 9 | expect(a.my_rotate(2)).to eq(["c", "d", "a", "b"]) 10 | end 11 | 12 | it "Rotates the elements correctly if a negative argument is passed in" do 13 | expect(a.my_rotate(-3)).to eq(["b", "c", "d", "a"]) 14 | end 15 | 16 | it "Rotates the elements correctly for a large argument" do 17 | expect(a.my_rotate(15)).to eq(["d", "a", "b", "c"]) 18 | end 19 | 20 | end 21 | -------------------------------------------------------------------------------- /specs/my_select_spec.rb: -------------------------------------------------------------------------------- 1 | describe 'my_select' do 2 | 3 | a = [1, 2, 3] 4 | 5 | it 'It correctly selects elements according to the passed in block' do 6 | expect(a.my_select { |num| num > 1 }).to eq([2, 3]) 7 | end 8 | 9 | it 'It returns an empty array if there are no matches' do 10 | expect(a.my_select { |num| num == 4 }).to eq([]) 11 | end 12 | 13 | end 14 | -------------------------------------------------------------------------------- /specs/my_zip_spec.rb: -------------------------------------------------------------------------------- 1 | describe "my_zip" do 2 | a = [ 4, 5, 6 ] 3 | b = [ 7, 8, 9 ] 4 | 5 | it 'Zips arrays of the same size' do 6 | expect([1, 2, 3].my_zip(a, b)).to eq([[1, 4, 7], [2, 5, 8], [3, 6, 9]]) 7 | end 8 | 9 | it 'Zips arrays of differnet sizes and adds nil appropriately' do 10 | expect(a.my_zip([1,2], [8])).to eq([[4, 1, 8], [5, 2, nil], [6, nil, nil]]) 11 | end 12 | 13 | c = [10, 11, 12] 14 | d = [13, 14, 15] 15 | 16 | it "Zips arrays with more elements than the original" do 17 | expect([1, 2].my_zip(a, b, c, d)).to eq([[1, 4, 7, 10, 13], [2, 5, 8, 11, 14]]) 18 | end 19 | 20 | end 21 | -------------------------------------------------------------------------------- /specs/permutations_spec.rb: -------------------------------------------------------------------------------- 1 | describe "#permutations" do 2 | it "returns all permutations of an array" do 3 | expect(permutations([1, 2, 3])).to match_array([[1, 2, 3], [1, 3, 2], [2, 1, 3], [2, 3, 1], [3, 1, 2], [3, 2, 1]]) 4 | end 5 | end 6 | -------------------------------------------------------------------------------- /specs/pig_latinify_spec.rb: -------------------------------------------------------------------------------- 1 | describe "#pig_latinify" do 2 | it "translates a word beginning with a vowel" do 3 | s = pig_latinify("apple") 4 | expect(s).to eq("appleay") 5 | end 6 | 7 | it "translates a word beginning with a consonant" do 8 | s = pig_latinify("banana") 9 | expect(s).to eq("ananabay") 10 | end 11 | 12 | it "translates a word beginning with two consonants" do 13 | s = pig_latinify("cherry") 14 | expect(s).to eq("errychay") 15 | end 16 | 17 | it "translates two words" do 18 | s = pig_latinify("eat pie") 19 | expect(s).to eq("eatay iepay") 20 | end 21 | 22 | it "translates a word beginning with three consonants" do 23 | expect(pig_latinify("three")).to eq("eethray") 24 | end 25 | 26 | it "counts 'sch' as a single phoneme" do 27 | s = pig_latinify("school") 28 | expect(s).to eq("oolschay") 29 | end 30 | 31 | it "counts 'qu' as a single phoneme" do 32 | s = pig_latinify("quiet") 33 | expect(s).to eq("ietquay") 34 | end 35 | 36 | it "counts 'qu' as a consonant even when it's preceded by a consonant" do 37 | s = pig_latinify("square") 38 | expect(s).to eq("aresquay") 39 | end 40 | 41 | it "translates many words" do 42 | s = pig_latinify("the quick brown fox") 43 | expect(s).to eq("ethay ickquay ownbray oxfay") 44 | end 45 | end 46 | -------------------------------------------------------------------------------- /specs/prime_factorization_spec.rb: -------------------------------------------------------------------------------- 1 | describe "prime_factorization" do 2 | it "handles an input of 2" do 3 | expect(prime_factorization(2)).to eq([2]) 4 | end 5 | 6 | it "Test case: 12" do 7 | expect(prime_factorization(12).sort).to eq([2,2,3]) 8 | end 9 | 10 | it "Test case: 600851475143" do 11 | expect(prime_factorization(600851475143).sort).to eq([71,839,1471,6857]) 12 | end 13 | end 14 | -------------------------------------------------------------------------------- /specs/primes_spec.rb: -------------------------------------------------------------------------------- 1 | 2 | describe "#primes" do 3 | 4 | it "returns first five primes in order" do 5 | expect(primes(5)).to eq([2, 3, 5, 7, 11]) 6 | end 7 | 8 | it "returns an empty array when asked for zero primes" do 9 | expect(primes(0)).to eq([]) 10 | end 11 | end 12 | -------------------------------------------------------------------------------- /specs/quicksort_spec.rb: -------------------------------------------------------------------------------- 1 | describe "my_quick_sort" do 2 | 3 | a = (0..8).to_a 4 | 5 | it "Sorts an array of numbers with no duplicates" do 6 | expect(a.shuffle.my_quick_sort).to eq(a) 7 | end 8 | 9 | it "Sorts an array of numbers with duplicates" do 10 | expect([1,2,3,3,9,10,10,17,432].shuffle.my_quick_sort).to eq([1,2,3,3,9,10,10,17,432]) 11 | end 12 | 13 | it "Sorts according to the block passed in" do 14 | expect(a.shuffle.my_quick_sort{|a,b| b<=>a}).to eq(a.reverse) 15 | end 16 | end 17 | -------------------------------------------------------------------------------- /specs/real_words_in_string_spec.rb: -------------------------------------------------------------------------------- 1 | describe "real_words_in_string" do 2 | it "finds a simple word" do 3 | words = "asdfcatqwer".real_words_in_string(["cat", "car"]) 4 | expect(words).to eq(["cat"]) 5 | end 6 | 7 | it "doesn't find words not in the dictionary" do 8 | words = "batcabtarbrat".real_words_in_string(["cat", "car"]) 9 | expect(words).to be_empty 10 | end 11 | 12 | it "finds words within words" do 13 | dictionary = ["bears", "ear", "a", "army"] 14 | words = "erbearsweatmyajs".real_words_in_string(dictionary) 15 | expect(words).to match_array(["bears", "ear", "a"]) 16 | end 17 | end 18 | -------------------------------------------------------------------------------- /specs/rec_sum_spec.rb: -------------------------------------------------------------------------------- 1 | describe "#rec_sum" do 2 | it "returns the sums of all elements in an array" do 3 | arr = [1,2,3,4] 4 | expect(rec_sum(arr)).to eq(10) 5 | end 6 | 7 | it "returns 0 if the array is empty" do 8 | expect(rec_sum([])).to eq(0) 9 | end 10 | end 11 | -------------------------------------------------------------------------------- /specs/string_include_key_spec.rb: -------------------------------------------------------------------------------- 1 | describe "string_include_key" do 2 | it "returns true for the same string" do 3 | expect(string_include_key?("adblfci", "abc")).to eq(true) 4 | end 5 | 6 | it "handles keys with duplicate characters: case 1" do 7 | expect(string_include_key?("adbblfci", "abbc")).to eq(true) 8 | end 9 | 10 | it "handles keys with duplicate characters: case 2" do 11 | expect(string_include_key?("adbclfci", "abbc")).to eq(false) 12 | end 13 | 14 | it "returns false if the key characters are in the wrong order" do 15 | expect(string_include_key?("dblfcia", "abc")).to eq(false) 16 | end 17 | 18 | it "returns false if the string doesn't contain the key" do 19 | expect(string_include_key?("db", "abc")).to eq(false) 20 | end 21 | 22 | end 23 | -------------------------------------------------------------------------------- /specs/subsets_spec.rb: -------------------------------------------------------------------------------- 1 | describe 'subsets' do 2 | 3 | it "Correctly returns all subsets of an array" do 4 | expect(subsets([1, 2, 3])).to match_array([[], [1], [2], [1, 2], [3], [1, 3], [2, 3], [1, 2, 3]]) 5 | end 6 | 7 | end 8 | -------------------------------------------------------------------------------- /specs/symmetric_substrings_spec.rb: -------------------------------------------------------------------------------- 1 | describe "#symmetric_substrings" do 2 | 3 | it "handles a simple example" do 4 | expect("aba".symmetric_substrings).to match_array(["aba"]) 5 | end 6 | 7 | it "handles two substrings" do 8 | expect("aba1cdc".symmetric_substrings).to match_array(["aba", "cdc"]) 9 | end 10 | 11 | it "handles nested substrings" do 12 | expect("xabax".symmetric_substrings).to match_array(["aba", "xabax"]) 13 | end 14 | end 15 | -------------------------------------------------------------------------------- /specs/titleize_spec.rb: -------------------------------------------------------------------------------- 1 | describe "titleize" do 2 | it "capitalizes a word" do 3 | expect(titleize("jaws")).to eq("Jaws") 4 | end 5 | 6 | it "capitalizes every word (aka title case)" do 7 | expect(titleize("david copperfield")).to eq("David Copperfield") 8 | end 9 | 10 | it "doesn't capitalize 'little words' in a title" do 11 | expect(titleize("war and peace")).to eq("War and Peace") 12 | end 13 | 14 | it "does capitalize 'little words' at the start of a title" do 15 | expect(titleize("the bridge over the river kwai")).to eq("The Bridge over the River Kwai") 16 | end 17 | end 18 | -------------------------------------------------------------------------------- /specs/two_sum_spec.rb: -------------------------------------------------------------------------------- 1 | describe "#two_sum" do 2 | it "returns positions of pairs of numbers that add to zero" do 3 | expect([5, 1, -7, -5].two_sum).to eq([[0, 3]]) 4 | end 5 | 6 | it "finds multiple pairs" do 7 | expect([5, -1, -5, 1].two_sum).to eq([[0, 2], [1, 3]]) 8 | end 9 | 10 | it "finds pairs with same element" do 11 | expect([5, -5, -5].two_sum).to eq([[0, 1], [0, 2]]) 12 | end 13 | 14 | it "returns [] when no pair is found" do 15 | expect([5, 5, 3, 1].two_sum).to eq([]) 16 | end 17 | 18 | it "won't find spurious zero pairs" do 19 | expect([0, 1, 2, 3].two_sum).to eq([]) 20 | end 21 | 22 | it "will find real zero pairs" do 23 | expect([0, 1, 2, 0].two_sum).to eq([[0, 3]]) 24 | end 25 | end 26 | --------------------------------------------------------------------------------