array Flashcards

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

Doubler
Write a method doubler(numbers) that takes an array of numbers and returns a new array
where every element of the original array is multiplied by 2.

A
def doubler(numbers)
  doubled_nums = []
  i = 0
  while i < numbers.length
    doubled_nums << numbers[i] * 2
    i += 1 
  end
  return doubled_nums

end

print doubler([1, 2, 3, 4]) # => [2, 4, 6, 8]
puts
print doubler([7, 1, 8])    # => [14, 2, 16]
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Yell
Write a method yell(words) that takes in an array of words and returns a
new array where every word from the original array has an exclamation point after it.

A
def yell(words)
  yelled = []
  i = 0
  while i < words.length
    yelled << words[i] + "!"
    i += 1
  end
end
print yell(["hello", "world"]) # => ["hello!", "world!"]
puts
print yell(["code", "is", "cool"]) # => ["code!", "is!", "cool!"]
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Element Times Index
Write a method element_times_index(nums) that takes in an array of numbers and returns a new array containing every number of the original array multiplied with its index.

A
def element_times_index(numbers)
  ele_times_i = []
  i = 0
  while i < numbers.length
    ele_times_i << numbers[i] * i
    i += 1 
  end
  return ele_times_i

end

print element_times_index([4, 7, 6, 5])       # => [0, 7, 12, 15]
puts
print element_times_index([1, 1, 1, 1, 1, 1]) # => [0, 1, 2, 3, 4, 5]
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Even Nums

Write a method even_nums(max) that takes in a number max and returns an array containing all even numbers from 0 to max

A

return even numbers from 0 to max

def even_nums(max)
  evens = []
  i = 0
  while i <= max
    if i % 2 == 0
      evens << i 
    end
    i += 1
  end
  return evens

end

print even_nums(10) # => [0, 2, 4, 6, 8, 10]
puts
print even_nums(5)  # => [0, 2, 4]
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Range
Write a method range(min, max) that takes in two numbers min and max. The method should return an array containing all numbers from min to max inclusive.

A
def range(min, max)
  range_arr = []
  i = min 
  while i <= max
    range_arr << i
    i += 1
  end
  return range_arr

end

print range(2, 7)   # => [2, 3, 4, 5, 6, 7]
puts
print range(13, 20) # => [13, 14, 15, 16, 17, 18, 19, 20]
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Odd Range
Write a method odd_range(min, max) that takes in two numbers min and max. The method should return an array containing all odd numbers from min to max (inclusive).

A
def odd_range(min, max)
  odds = []
  i = min
  while i <= max
    if i % 2 == 1
      odds << i
    end
    i += 1 
  end
  return odds

end

print odd_range(11, 18) # => [11, 13, 15, 17]
puts
print odd_range(3, 7)   # => [3, 5, 7]
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Reverse Range
Write a method reverse_range(min, max) that takes in two numbers min and max. The function should return an array containing all numbers from min to max in reverse order. The min and max should be excluded from the array

A

previous solution consisted of starting from max to min

def reverse_range(min, max)
  reverse = []
  i = min + 1
  while i < max
    reverse << i
    i += 1
  end
  return reverse.reverse

end

print reverse_range(10, 17) # => [16, 15, 14, 13, 12, 11]
puts
print reverse_range(1, 7)   # => [6, 5, 4, 3, 2]
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

First Half
Write a method first_half(array) that takes in an array and returns a new array containing the first half of the elements in the array. If there is an odd number of elements, return the first half including the middle element.

A
def first_half(array)
  half = []
  i = 0
  while i < (array.length / 2.0)
    half << array[i]
    i += 1 
  end
  return half

end

print first_half(["Brian", "Abby", "David", "Ommi"]) # => ["Brian", "Abby"]
puts
print first_half(["a", "b", "c", "d", "e"])          # => ["a", "b", "c"]
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Factors Of
Write a method factors_of(num) that takes in a num and returns an array of all positive numbers less than or equal to num that can divide num.

A
def factors_of(num)
  factors = []
  i = 1
  while i <=num
    if num % i == 0
      factors << i
    end
    i += 1 
  end
  return factors

end

print factors_of(3)   # => [1, 3]
puts
print factors_of(4)   # => [1, 2, 4]
puts
print factors_of(8)   # => [1, 2, 4, 8]
puts
print factors_of(9)   # => [1, 3, 9]
puts
print factors_of(16)  # => [1, 2, 4, 8, 16]
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Select Odds
Write a method select_odds(numbers) that takes in an array of numbers and returns a new array containing the odd numbers of the original array.

A

odds in an array of numbers.

def select_odds(numbers)
  odds = []
  i = 0
  while  i < numbers.length
     if numbers[i] % 2 == 1
    odds << numbers[i] 
     end
      i += 1
  end
  return odds

end

print select_odds([13, 4, 3, 7, 6, 11]) # => [13, 3, 7, 11]
puts
print select_odds([2, 4, 6])            # => []
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Select Long Words
Write a method select_long_words(words) that takes in an array of words and returns a new array containing all of the words of the original array that are longer than 4 characters.

A

return an array with all words/length > 4

def select_long_words(words)
  long_words = []
  i = 0
  while i < words.length
    if words[i].length > 4
      long_words << words[i]
    end
    i += 1 
  end
  return long_words

end

print select_long_words(["what", "are", "we", "eating", "for", "dinner"]) # => ["eating", "dinner"]
puts
print select_long_words(["keep", "coding"])                               # => ["coding"]
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Sum Elements
Write a method sum_elements(arr1, arr2) that takes in two arrays. The method should return a new array containing the results of adding together corresponding elements of the original arrays. You can assume the arrays have the same length.

A

sum of 2 arrays into one array based on index

def sum_elements(arr1, arr2)
  sums = []
  i = 0
  while i < arr1.length
    sums << arr1[i] + arr2[i]
    i += 1
  end
return sums end
print sum_elements([7, 4, 4], [3, 2, 11])                            # => [10, 6, 15]
puts
print sum_elements(["cat", "pizza", "boot"], ["dog", "pie", "camp"]) # => ["catdog", "pizzapie", "bootcamp"]
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Fizz Buzz
Write a method fizz_buzz(max) that takes in a number max and returns an array containing all numbers greater than 0 and less than max that are divisible by either 4 or 6, but not both.

A

return an array wit hall nums > 0 and that are divisable by 4 or 6 but not both

def fizz_buzz(max)
  buzzes = []
  i = 1
  while i < max
    if (i % 4 == 0 || i % 6 == 0) &amp;&amp; !(i % 4 == 0 &amp;&amp; i % 6 == 0)
      buzzes << i 
    end
    i += 1
  end
return buzzes end
print fizz_buzz(20) # => [4, 6, 8, 16, 18]
puts
print fizz_buzz(15) # => [4, 6, 8]
How well did you know this?
1
Not at all
2
3
4
5
Perfectly