Skip to main content
Caleb-Mitchell

RB109: Practice Examples - Live

·7 mins
# ---------------------------------------- #1 ----------------------------------------
 
# Write a method that takes an array of consecutive (increasing) letters as input and that returns the missing letter in the array.
 
p determine_missing_letter(['a','b','c','d','f']) == 'e'
p determine_missing_letter(['O','Q','R','S']) == 'P'
 
# ---------------------------------------- #2 ----------------------------------------
 
# Write a method that takes two numbers. Return an array containing all primes between the two numbers (include the two given numbers in your answer if they are prime). Don't use Ruby's 'prime' class.

p find_primes(3, 10) == [3, 5, 7]
p find_primes(11, 20) == [11, 13, 17, 19]
p find_primes(100, 101) == [101]
p find_primes(1, 100) == [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97]
p find_primes(1, 2) == [2]
 
# ---------------------------------------- #3 ----------------------------------------
 
# Write a function that maps a string into an array of name, string, and occupation pairs. Return an empty array if given an empty string.

organize("John Mayer, 41, Singer, Emily Blunt, 36, Actor") == [
  {:name=>"John Mayer", :age=>"41", :occupation=>"Singer"},
  {:name=>"Emily Blunt", :age=>"36", :occupation=>"Actor"}
]

organize("Conan O'Brien, 56, Talk Show Host, Anna Wintour, 69, Editor") == [
  {:name=>"Conan O'Brien", :age=>"56", :occupation=>"Talk Show Host"},
  {:name=>"Anna Wintour", :age=>"69", :occupation=>"Editor"}
]

organize("") == []
 
# ---------------------------------------- #4 ----------------------------------------
 
# Given a word, create a hash which stores the indexes of each letter in an array.
# Make sure the letters are the keys.
# Make sure the letters are symbols.
# Make sure the indexes are stored in an array and those arrays are values.

map_letters("froggy") == { :f => [0], :r=>[1], :o=>[2], :g=>[3, 4], :y=>[5] }
map_letters("dodo") == { :d=>[0, 2], :o=>[1, 3] }
map_letters("grapes") == { :g=>[0], :r=>[1], :a=>[2], :p=>[3], :e=>[4], :s=>[5] }
 
# ---------------------------------------- #5 ----------------------------------------
 
# Have the method letter_changes(str) take the str parameter being passed and modify it using the following algorithm:
# Replace every letter in the string with the 3rd letter following it in the alphabet (ie. c becomes f, Z becomes C).
# Then return this modified string.

p letter_changes("this long cake@&") == "wklv orqj fdnh@&"
p letter_changes("Road trip9") == "Urdg wuls9"
p letter_changes("EMAILZ@gmail.com") == "HPDLOC@jpdlo.frp"
p letter_changes('xyz') == ('abc')
 
# ---------------------------------------- #6 ----------------------------------------
 
# Given an array of n positive integers and a positive integers, find the minimal length of a contiguous subarray for which the sum > s.
# If there isn't one, return 0 instead.

p minSubLength([2, 3, 1, 2, 4, 3], 7) == 2
p minSubLength([1, 10, 5, 2, 7), 9) == 1
p minSubLength([1, 11, 100, 1, 0, 200, 3, 2, 1, 250], 280) == 4
p minSubLength([1, 2, 4], 8) == 0
 
# ---------------------------------------- #7 ----------------------------------------
 
# Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target.

# You may assume that each input would have exactly one solution, and you may not use the same element twice.

# You can return the answer in any order.

p two_sum([2, 11, 7, 15], 9) == [0, 2]
p two_sum([3, 2, 4], 6) == [1, 2]
p two_sum([3, 3], 6) == [0, 1]
 
# ---------------------------------------- #8 ----------------------------------------
 
# You will be given an array of numbers.
# You have to sort the odd numbers in ascending order, while leaving the even numbers at their original positions.

p sort_odd([7, 1])  ==  [1, 7]
p sort_odd([5, 8, 6, 3, 4])  ==  [3, 8, 6, 5, 4]
p sort_odd([9, 8, 7, 6, 5, 4, 3, 2, 1, 0])  ==  [1, 8, 3, 6, 5, 4, 7, 2, 9, 0]
 
# ---------------------------------------- #9 ----------------------------------------
 
# Given a non-empty string check if it can be constructed by taking a substring of it
# and appending multiple copies of the substring together. You may assume the given string consists of
# lowercase English letters only.

p repeated_substring_pattern("abab") == true
p repeated_substring_pattern("aba") == false
p repeated_substring_pattern("aabaaba") == false
p repeated_substring_pattern("abaababaab") == true
p repeated_substring_pattern("abcabcabcabc") == true
 
# ---------------------------------------- #10 ----------------------------------------
 
# Given an array of strings made only from lowercase letters,
# return an array of all characters that show up in all strings within the given array (including duplicates).
# For example, if a character occurs 3 times in all strings but not 4 times, you need to include that character
# three times in the final answer.

p common_chars(["bella", "label", "roller"]) == ["e", "l", "l"]
p common_chars(["cool", "lock", "cook"]) == ["c", "o"]
p common_chars(["hello", "goodbye", "booya", "random"]) == ["o"]
p common_chars(["aabbaaaa", "ccdddddd", "eeffee", "ggrrrrr", "yyyzzz"]) == []
 
# ---------------------------------------- #11 ----------------------------------------
 
# You have to create a method that takes a positive integer number
# and returns the next bigger number formed by the same digits.

p next_bigger_num(9) == -1
p next_bigger_num(12) == 21
p next_bigger_num(513) == 531
p next_bigger_num(2017) == 2071
p next_bigger_num(111) == -1
p next_bigger_num(531) == -1
p next_bigger_num(123456789) == 123456798
 
# ---------------------------------------- #12 ----------------------------------------
 
# Find the maximum sum of a contiguous subsequence in an array of integers

p max_sequence([]) == 0
p max_sequence([-2, 1, -3, 4, -1, 2, 1, -5, 4]) == 6
p max_sequence([11]) == 11
p max_sequence([-32]) == 0
p max_sequence([-2, 1, -7, 4, -10, 2, 1, 5, 4]) == 12
 
# ---------------------------------------- #13 ----------------------------------------
 
# Find the longest common prefix string amongst an array of strings
# If there is no common prefix, return an empty string

p common_prefix(["flower", "flow", "flight"]) == "fl"
p common_prefix(["dog, "racecar", "car"]) == ""
p common_prefix(["interspecies, "interstellar", "interstate"]) == "inters"
p common_prefix(["throne", "dungeon"]) == ""
p common_prefix(["throne", "throne"]) == "throne"
 
# ---------------------------------------- #14 ----------------------------------------
 
# Find if there is a substring that appears in both strings
# You will return true if you find one, or false if you do not

p substring_test('Something', 'Home') == true
p substring_test('Something', 'Fun') == false
p substring_test('Something', '') == false
p substring_test('', 'Something') == false
p substring_test('BANANA', 'banana') == true
p substring_test('test', 'lllt') == false
p substring_test('', '') == false
p substring_test('1234567', '541265') == true
p substring_test('supercalifragilisticexpialidocious', 'SoundOfItIsAtrociou') == true
 
# ---------------------------------------- #15 ----------------------------------------
 
# Given two strings, write a method that returns true
# if a portion of the str1 characters can be rearranged to match str2
# otherwise return false

p scramble('javaass', 'jjss') == false
p scramble('rkqodlw', 'world') == true
p scramble('cedewaraaossoqqyt', 'codewars') == true
p scramble('katas', 'steak') == false
p scramble('scriptjava', 'javascript') == true
p scramble('scriptingjava', 'javascript') == true
 
# ---------------------------------------- #16 ----------------------------------------
 
# Find the length of the longest substring in the given string
# that is the same in reverse

p longest_palindrome("a") == 1
p longest_palindrome("aa") == 2
p longest_palindrome("baa") == 2
p longest_palindrome("aab") == 2
p longest_palindrome("baabcd") == 4
p longest_palindrome("baablkj12345432133d") == 9
 
# ---------------------------------------- #17 ----------------------------------------
 
# Find an index where the sum of the integers to the left of the index
# is equal to the sum of the integers to the right of the index
# if none, return -1

p find_even_index([1, 2, 3, 4, 3, 2, 1]) == 3
p find_even_index([1, 100, 50, -51, 1, 1]) == 1
p find_even_index([1, 2, 3, 4, 5, 6]) == -1
p find_even_index([20, 10, 30, 10, 10, 15, 35]) == 3
p find_even_index([20, 10, -80, 10, 10, 15, 35]) == 0
p find_even_index([10, -80, 10, 10, 15, 35, 20]) == 6
p find_even_index([-1, -2, -3, -4, -3, -2, -1]) == 3
 
# ---------------------------------------- #18 ----------------------------------------
 
# Write a method that takes an integer year and returns the century
# return value must be a number that ends with st, nd, rd, or th appropriately

p century(24) == '1st'
p century(104) == '2nd'
p century(299) == '3rd'
p century(1078) == '11th'
p century(1123) == '12th'
p century(1244) == '13th'
p century(2092) == '21st'
p century(2134) == '22nd'
 
# ---------------------------------------- #19 ----------------------------------------
 
# Given a word, compute the Scrabble score for that word
# A, E, I, O, U, L, N, R, S, T = 1
# D, G = 2
# B, C, M, P = 3
# F, H, V, W, Y = 4
# K = 5
# J, X = 8
# Q, Z = 10

p scrabble_score('a') == 1
p scrabble_score('cabbage') == 14
p scrabble_score('Cabbage') == 14
p scrabble_score('zebra') == 16
 
# ---------------------------------------- #20 ----------------------------------------
 
# Write a program that takes a word problem and returns the answer as an integer
 
p solver("What is 15 divided by 3?") == 5
 
# ---------------------------------------- #21 ----------------------------------------
 
# Reverse an array without using the built-in reverse method
 
# ---------------------------------------- #22 ----------------------------------------
 
# Select the element out of the array if its index is a fibonacci number
 
# ---------------------------------------- #23 ----------------------------------------
 
# Write a method to determine if a word is a palindrome, without using the reverse method