Nested Loops & 2D arrays Flashcards

You may prefer our related Brainscape-certified flashcards:
1
Q

Array Translate
Write a method array_translate that takes in an array whose elements alternate between words and numbers. The method should return a string where each word is repeated the number of times that immediately follows in the array.

A
def array_translate(array)
  str = ""
  i = 0
  while i < array.length
    ele = array[i]
    num = array[i + 1]
    num.times { str += ele }
i += 2   end

return str
end

print array_translate(["Cat", 2, "Dog", 3, "Mouse", 1]); # => "CatCatDogDogDogMouse"
puts
print array_translate(["red", 3, "blue", 1]); # => "redredredblue"
puts
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Combinations
Write a method combinations that takes in an array of unique elements, the method should return a 2D array representing all possible combinations of 2 elements of the array.

A
def combinations(arr)
  combo = []

arr.each_with_index do |ele1, idx1|
arr.each_with_index do |ele2, idx2|
if idx2 > idx1
combo &laquo_space;[ ele1 , ele2 ]
end
end
end
return combo
end

print combinations(["a", "b", "c"]); # => [ [ "a", "b" ], [ "a", "c" ], [ "b", "c" ] ]
puts
print combinations([0, 1, 2, 3]); # => [ [ 0, 1 ], [ 0, 2 ], [ 0, 3 ], [ 1, 2 ], [ 1, 3 ], [ 2, 3 ] ]
puts
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Opposite Count
Write a method opposite_count that takes in an array of unique numbers. The method should return the number of pairs of elements that sum to 0.

A
def opposite_count(nums)
  count = 0
pairs = []
  pair = []
  nums.each_with_index do |num1, idx1|
    nums.each_with_index do |num2, idx2|
      if idx2 > idx1 &amp;&amp; num1 + num2 == 0
        count += 1 
        pairs.push( [num1, num2] )
      end
    end
  end
  print pairs
  puts
  return count
end
puts opposite_count([ 2, 5, 11, -5, -2, 7 ]) # => 2
puts opposite_count([ 21, -23, 24 -12, 23 ]) # => 1
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Two D Sum

Write a method two_d_Sum that takes in a two dimensional array and returns the sum of all elements in the array.

A

iterate through each subarry and add each ele to the sum

def two_d_sum(arr)
  sum = 0
  sum_list = []

arr.each do |subarr|
subarr.each do |ele2|
sum = sum + ele2
sum_list &laquo_space;[sum]
end
end
print sum_list
puts
return sum
end

array_1 = [
  [4, 5],
  [1, 3, 7, 1]
]
puts two_d_sum(array_1)    # => 21
array_2 = [
  [3, 3],
  [2],
  [2, 5]
]
puts two_d_sum(array_2)    # => 15
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Two D Translate
Write a method two_d_translate that takes in a 2 dimensional array and translates it into a 1 dimensional array. You can assume that the inner arrays always have 2 elements. See the examples.

A
def two_d_translate(arr)
  new_arr = []

arr.each do |subArray|
ele = subArray[0]
num = subArray[1]

num.times { new_arr << ele }   end

return new_arr

end

arr_1 = [
  ['boot', 3],
  ['camp', 2],
  ['program', 0]
]
print two_d_translate(arr_1) # => [ 'boot', 'boot', 'boot', 'camp', 'camp' ]
puts

arr_2 = [
[‘red’, 1],
[‘blue’, 4]
]

print two_d_translate(arr_2) # => [ 'red', 'blue', 'blue', 'blue', 'blue' ]
puts
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Pig Latin Word

Write a method pig_latin_word that takes in a word string and translates the word into pig latin.

A
# Pig latin translation uses the following rules:
# - for words that start with a vowel, add 'yay' to the end
# - for words that start with a nonvowel, move all letters before the first vowel to the end of the word and add 'ay'
def pig_latin_word(word)
  vowels = "AEIOUaeiou"

if vowels.include?(word[0])
return word + “yay”
end

  word.each_char.with_index do |char, i|
    if vowels.include?(char)
      return word[i..word.length] + word[0...i] + "ay"
    end
  end
end
puts pig_latin_word("apple")   # => "appleyay"
puts pig_latin_word("eat")     # => "eatyay"
puts pig_latin_word("banana")  # => "ananabay"
puts pig_latin_word("trash")   # => "ashtray"
How well did you know this?
1
Not at all
2
3
4
5
Perfectly