Ruby 1 - Practice Flashcards
Write a method that returns b^n recursively. Your solution should accept
negative values for n.
def exponent(b, n) return 1 if n == 0 if n > 0 b * exponent(b, n-1) else 1.0 / (b * exponent(b, n.abs-1)) end end
Write a function anagrams(str1, str2)
that takes in two words and returns a boolean indicating whether or not the words are anagrams. Anagrams are words that contain the same characters but not necessarily in the same order.
Solve this without using Array#sort
or Array#sort_by
.
- Create a hash of letters with initial values of 0
- iterate through string_1 and create each character as a key and then INCREMENT the value
- Iterate through string_2 and do the same but DECREMENT the value
- If the character appears in both strings, it’ll cancel each other to zero
- then check if all the values in the hash using .all? equal zeros.
Write a method, pow(base, exponent), that takes in two numbers. The method should calculate the base raised to the exponent power. You can assume the exponent is always positive.
Solve this recursively!
def pow(base, exponent) return 1 if exponent == 0 return base if exponent == 1
base * pow(base, exponent-1)
end
How can you change the puts output?
Puts changes everything to a string. So you can monkey patch and override to “to_s” method name.
Write a hash_my_each that takes in a proc and does not use hash#each, hash#map or hash#each_with_index.
- Use a while loop to iterate through keys
- Proc calls on a hash require both the key and value so need to pass in both.
def my_each(&prc) i = 0 key_arr = self.keys while i < key_arr.length prc.call(key_arr[i], self[key_arr[i]]) i += 1 end self end
Write an Array#my_inject
method. If my_inject receives no argument, then use the first element of the array as the default accumulator.
**Do NOT use the built-in Array#inject
orArray#reduce
methods in your implementation.
- If the accumulator is not given, then it’s set to the first element and the arr drops the first element.
- Iterate through the array and reassign the accumulator as the result of running through the proc each time.
def my_inject(accumulator = nil, &prc)
arr = self.dup if accumulator == nil accumulator = arr[0] arr.shift end
arr.each do |e| debugger accumulator = prc.call(accumulator, e) end accumulator
end
end
Define a method Array#my_zip(*arrays) that merges elements from the receiver with the corresponding elements from each provided argument. You CANNOT use Ruby’s built-in Array#zip
method
- Merge the arrays together
- find the column by finding the max length of each subarray
- find rows by counting the number of subarrays so length of merged
- Double iteration of merged to assign the inverted version of the index pair to result
def my_zip(*arrays) merged = [self, *arrays] col = merged.map(&:length).max row = merged.length
result = Array.new(col) { Array.new(row) } (0...merged.length).each do |i1| (0...merged[0].length).each do |i2| result[i2][i1] = merged[i1][i2] end end result end
Write an Array#my_controlled_flatten(n) method that only flattens n levels of an array. For example, if you have an array with 3 levels of nested arrays, and run arr.my_flatten(1), you should return an array with 1 level of nested arrays flattened.
def my_controlled_flatten(n) result = []
self.each do |e| if e.is_a?(Array) && n != 0 result += e.my_controlled_flatten(n-1) else result << e end end
result end end
Find all the fibonacci numbers up to and including the nth using recursive
What about with memoization? What does memoization do?
- Leverages the RETURN of the recursive method to influence the output of the next.
def all_fibs(n) return [ ] if n == 0 return [0] if n == 1 return [0, 1] if n == 2 all_fibs(n-1) << all_fibs(n-1)[-1] + all_fibs(n-1)[-2] end
_________________
With memoization, calls on the method less and runs way faster
def all_fibs(n) return [ ] if n == 0 return [0] if n == 1 return [0, 1] if n == 2 prev_call = all_fibs(n-1) prev_call << prev_call[-1] + prev_call[-2] end