0
0
Rubyprogramming~10 mins

Group_by for categorization in Ruby - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to group numbers by their parity (even or odd).

Ruby
numbers = [1, 2, 3, 4, 5]
groups = numbers.group_by { |n| n.[1] }
puts groups
Drag options to blanks, or click blank then click option'
Ato_s
Bodd?
Cabs
Deven?
Attempts:
3 left
💡 Hint
Common Mistakes
Using odd? groups by odd numbers, which is not what the question asks.
Using to_s or abs does not categorize by parity.
2fill in blank
medium

Complete the code to group words by their first letter.

Ruby
words = ['apple', 'banana', 'apricot', 'blueberry']
groups = words.group_by { |word| word.[1] }
puts groups
Drag options to blanks, or click blank then click option'
Afirst
Blength
Cupcase
Dreverse
Attempts:
3 left
💡 Hint
Common Mistakes
Using length groups by word length, not first letter.
Using upcase returns the whole word in uppercase, not the first letter.
3fill in blank
hard

Fix the error in the code to group numbers by their remainder when divided by 3.

Ruby
numbers = [1, 2, 3, 4, 5, 6]
groups = numbers.group_by { |n| n [1] 3 }
puts groups
Drag options to blanks, or click blank then click option'
A/
B%
C*
D-
Attempts:
3 left
💡 Hint
Common Mistakes
Using / divides numbers but does not give remainder.
Using * or - does not relate to remainder.
4fill in blank
hard

Fill both blanks to group words by their length and select only words longer than 3 letters.

Ruby
words = ['cat', 'dog', 'elephant', 'bee', 'ant']
groups = words.group_by { |word| word.[1] }
filtered = groups.select { |length, words| length [2] 3 }
puts filtered
Drag options to blanks, or click blank then click option'
Alength
B>
C<
Dsize
Attempts:
3 left
💡 Hint
Common Mistakes
Using size is also valid for length but only one correct answer is accepted here.
Using < selects shorter words, not longer.
5fill in blank
hard

Fill all three blanks to group numbers by their sign and select only positive numbers.

Ruby
numbers = [-2, -1, 0, 1, 2]
groups = numbers.group_by { |n| n.[1] }
positive = groups.select { |sign, nums| sign == [2] }
puts positive[[3]]
Drag options to blanks, or click blank then click option'
Anegative?
Btrue
Cfalse
Dpositive?
Attempts:
3 left
💡 Hint
Common Mistakes
Using negative? groups negative numbers, not positive.
Using false selects non-positive numbers.