Skip to main content

More Arrays

- Learnt from delete_nth kata

- .combination gives the unique permutation.

for example

a = [1, 2, 3, 4]
a.combination(1).to_a  #=> [[1],[2],[3],[4]]
a.combination(2).to_a  #=> [[1,2],[1,3],[1,4],[2,3],[2,4],[3,4]]
a.combination(3).to_a  #=> [[1,2,3],[1,2,4],[1,3,4],[2,3,4]]
a.combination(4).to_a  #=> [[1,2,3,4]]

-.product gives all permutations

a = [4,5,6]
b= [1,2,3]
a.product(b) => [[4, 1], [4, 2], [4, 3], [5, 1], [5, 2], [5, 3], [6, 1], [6, 2], [6, 3]]


- .index tells you the index of a object
a = [1,1,2,3]
a.index(1) => 0
a.index(2) => 2

- Ruby Cheatsheet
https://www.shortcutfoo.com/app/dojos/ruby-arrays/cheatsheet

- Learnt delete_at() and .rindex(n)
rindex basically is the reverse of .index which gets the last index of the value in the array.

- .delete_if and .reject are quite similar

- a = Hash.new(0) => {}
a["good"] = 1 => 1
a => {good => 1}
a["good"] += 1 => 2
a => {good => 2}



Comments