Created
February 21, 2023 13:49
-
-
Save domhnall/361c634562a732c34830e0be4e375927 to your computer and use it in GitHub Desktop.
Short quiz to test how well you know ruby array methods. See the [blog post](https://www.vector-logic.com/blog/posts/test-yourself-on-ruby-array-operations) to have a go at the quiz
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# To test yourself on the actual quiz, see | |
# https://www.vector-logic.com/blog/posts/test-yourself-on-ruby-array-operations | |
def print_question_number(n) | |
puts ["="*20, n, "="*20].join(" ") | |
end | |
# Array indexing options | |
print_question_number(1) | |
arr = ['a', 'b', 'c', 'd', 'e'] | |
puts arr[1] | |
puts arr.at(1) | |
begin | |
puts arr.fetch('b') # Raises error | |
rescue StandardError; end | |
puts arr.find{|i| i=='b' } | |
# Array selection options | |
print_question_number(2) | |
arr = ['a', 'b', 'c', 'd', 'e'] | |
puts arr.select{|i| i=='b' }.map(&:upcase) | |
begin | |
puts arr.find{|i| i=='b' }.map(&:upcase) # Raises error | |
rescue StandardError; end | |
puts arr.take(2).last(1).map(&:upcase) | |
puts arr.sample(3).map(&:upcase) | |
# Bang operators | |
print_question_number(3) | |
arr = ['a', 'b', 'c', 'd', 'e'] | |
arr.map(&:upcase).reject!{|i| i=='B' } | |
puts arr | |
# Array initialization options | |
print_question_number(4) | |
puts Array.new(4, "a") | |
puts Array.new(4){|i| "a" } | |
puts ["a"]*4 | |
puts "aaaa".split | |
# Array equality operator | |
print_question_number(5) | |
puts ["a", 5, "6"]==["a", 5.0, "6"] | |
puts ["a", nil, 5, "6"].compact==["a", 5.0, "6", nil].compact | |
puts ["a", 5, "6"].map(&:to_s)==["a", 5.0, 6].map(&:to_s) | |
puts ["a", 5, "6"]==["a", 5.0, 6] | |
puts ["a", 5, "6"]==["a", 5.0, "6", nil] | |
# Array map, sum, join and split | |
print_question_number(6) | |
puts (1...5).map(&:to_s).join('') | |
puts 12345.to_s.split.sum('') | |
puts "54321".reverse | |
puts "54321".split.reverse.join | |
# Array select, find, join and split | |
print_question_number(7) | |
nums = [8, 13, 27, 61, 50].shuffle | |
puts nums.select{|n| n.even? } | |
puts nums.find{|n| n.even? } | |
puts nums.filter{|n| n.even? } | |
puts [nums[0], nums[-1]] | |
# Array set operations | |
print_question_number(8) | |
puts ['a', 'e', 'f'] | ['b'] & arr # Outputs ['a', 'e', 'f', 'b'] | |
# Array mutation operations | |
print_question_number(9) | |
foo = ['a', 'b', 'c'] | |
foo << "x" # Appended to end ['a', 'b', 'c', 'x'] | |
foo.shift # Remove from start of array ['b', 'c', 'x'] | |
foo.append(['d', 'e', 'f']) # Appends full array to last element ['b', 'c', 'x', ['d', 'e', 'f']] | |
foo.unshift("y") # Pushes y to front of array ['y', 'b', 'c', 'x', ['d', 'e', 'f']] | |
foo.pop # Removes the array we appended earlier: ['y', 'b', 'c', 'x' ] | |
puts foo | |
# Array zip and sort | |
print_question_number(10) | |
names = [ "Aodhan", "Domhnall", "Caoimhe", "Blaithin"] | |
scores = [ 68, 23, 88, 71 ] | |
puts names.zip(scores).sort_by{|el| el[1] }.last[0] # Should return the person associated with the highest score, i.e. Siobhan |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment