Odd Position Elements

excerpt: This covers the basic building blocks such as Linear Scan, Multiple of a Number and Finding Odd Number. tags: linear-scan multiple-of-a-number finding-odd-number

Write a function that returns the elements on odd positions in a list.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
def odd_positions(a)
  result = []
  
  for i in (0..a.size-1)
    if i % 2 != 0
      result << a[i]
    end
  end
  
  result
end

a = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

p odd_positions(a)

Building Blocks

  • Linear Scan
  • Multiple of a Number
  • Finding Odd Number