0
0
Rubyprogramming~10 mins

Array comparison and set operations 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 check if two arrays are equal.

Ruby
arr1 = [1, 2, 3]
arr2 = [1, 2, 3]
puts arr1 [1] arr2
Drag options to blanks, or click blank then click option'
A==
B!=
C===
Dequal?
Attempts:
3 left
💡 Hint
Common Mistakes
Using equal? which checks object identity, not content equality.
Using != which checks inequality.
2fill in blank
medium

Complete the code to find the intersection of two arrays.

Ruby
arr1 = [1, 2, 3, 4]
arr2 = [3, 4, 5, 6]
common = arr1 [1] arr2
puts common.inspect
Drag options to blanks, or click blank then click option'
A&
B-
C|
D^
Attempts:
3 left
💡 Hint
Common Mistakes
Using | which returns union, not intersection.
Using - which returns difference.
3fill in blank
hard

Fix the error in the code to get the union of two arrays.

Ruby
arr1 = [1, 2]
arr2 = [2, 3]
union = arr1 [1] arr2
puts union.inspect
Drag options to blanks, or click blank then click option'
A+
B|
C&
D-
Attempts:
3 left
💡 Hint
Common Mistakes
Using + which concatenates but does not remove duplicates.
Using & which returns intersection.
4fill in blank
hard

Fill both blanks to create a hash with array elements as keys and their lengths as values, only for words longer than 3 letters.

Ruby
words = ["cat", "lion", "tiger", "dog"]
lengths = {word[1] for word in words if word[2] 3}
puts lengths.inspect
Drag options to blanks, or click blank then click option'
A => word.length
B >
C <
D ==
Attempts:
3 left
💡 Hint
Common Mistakes
Using < or == which filter wrong words.
Forgetting the hash rocket => in the hash comprehension.
5fill in blank
hard

Fill all three blanks to create a hash with uppercase words as keys, their lengths as values, only for words longer than 3 letters.

Ruby
words = ["apple", "bat", "carrot", "dog"]
result = {word[1]: [2] for word in words if word.length [3] 3}
puts result.inspect
Drag options to blanks, or click blank then click option'
A.upcase()
Blen(word)
C>
Dword.length
Attempts:
3 left
💡 Hint
Common Mistakes
Using len(word) which is Python syntax, not Ruby.
Using wrong comparison operators.