w1d1 Flashcards
(35 cards)
creating arrays
empty_array = Array.new empty_array = []
accessing item by index (array)
some_fruits = [“pear”, “apple”]
some_fruits[0]
some_fruits[-1] from end
array ‘range’ example
some_fruits[0..1], some_fruits[0…2]
.. => start to finish
… => start to, but not including finish
accessing elements in order
array.each do |thing|
you should never modify array during iteration
true
setting and adding elements (array)
array[1] = “something else”
array «_space;“something”
array.push “something”
array + array
creates a new array that hold both values
add two arrays without ‘+’
array.concat(array_other)
array size example
array.count
check if array is empty
array.empty?
push pop array
array «_space;“something”
array.pop
array as queue example
array «_space;elements
array.shift
(first in, first out)
push element in front of array
array.unshift
interpolation
execute code in #{} inside string
finding elements in array
array.include?(something)
finding index of element in array
array.index(something)
sorting an array
array. sort (returns new array)
array. sort! (modifies array)
randomize order of array
array.shuffle
access first and last element of array
array. first
array. last
select element at random in array
array.sample
arrays should always contain one type of something
true
TQ: remove duplicates from array
enter
TQ: sum of two positions in array that equal zero
enter
TQ: Tower of Hanoi
enter