# Filter Positions in a List

# Filter Positions in a List

QuomoZ + 0 comments I've been solving these challenges in Haskell without using higher order functions.

`f (_:x:xs) = x : f xs f _ = []`

jeffbarge + 0 comments The default solution input for Scala is broken. I had to delete the entire

`object Solution { ...`

and replace it with`def f()...`

koskinasnapoleon + 3 comments In Scala:

def f(arr:List[Int]):List[Int] = arr.zipWithIndex.filter(_._2 %2 == 1).map(_._1)

So, zipWithIndex produces pairs like (28,0) (32,1) (5,2) ...

,where the second element is the index and the first the value in the list. Hold only those pairs for which the index is odd(filter), and return the first element of the pair which is the value(map) by applying the function which just describes holding only the first element of the pair.

Vel0city + 4 comments Yup, very close to my solution which is a bit faster:

def f(arr:List[Int]):List[Int] = arr.view.zipWithIndex.filter { _._2 % 2 != 0 }.map { _._1}.force.toList

koskinasnapoleon + 0 comments I am new to Scala, I will learn to be more effective by time.

stsatlantis + 2 comments You can use collect instead of filter and map.

`.collect{case (a,b) if b % 2 == 1 => a}`

utsavjha + 0 comments You can also use the following.. I like to follow KISS for hacker rank quite often.

def f(arr:List[Int]):List[Int] = { val inputArraySize = arr.size - 1 val indicesToDrop: Range = 1 to inputArraySize by 2 return indicesToDrop.map((idx: Int) => arr(idx)).toList }

bennetryan + 1 comment Hi, I'm new to Scala, could you please explain the meaning of _._2 and _._1 in your code. I don't see mto follow.

Vel0city + 0 comments The

**_**at the start is just a quick way to express the argument in the function you're making._._2 % 2 != 0

could be done like so:

tp => tp._2 % 2 != 0

Now, we are working with tuples here, that's what zipWithIndex gives you. The values themselves, each paired up with their index. So,

**_**in this case is a tuple (read up on them, they're basically batched variables like I described above), which is why I use the accessors**_1**and**_2**to refer to its first and second elements (that's just a Scala thing). For this, the second element is the index and the first its associated value in`arr`

.

kishoreraja240 + 0 comments It can be done like this too : Range(1,arr.length,2).map(x=>arr(x)).toList

asatna13 + 2 comments I ended up with the following haskell solution:

f (x:xs) = (head xs) : f (tail xs)

Obviously, I am a beginner at haskell. Any comments?

pgcosta + 0 comments I think it's great! very simple!

ddevine1 + 1 comment I had the same answer.

Though it throws an error on stderr because it doesn't explicity match the the case where (x:xs) = []. To fix it, add this to the line before it:

f [] = []

roman_balzer + 0 comments This also fails for me with list with an odd amount of numbers.

so i used

f [] = [] f [x] = [] f (x:xs) = (head xs) : f (tail xs)

foru_fy + 0 comments f (x:y:ys) = y: (f ys)

f _ = []

balintant + 4 comments Haskell

f lst = map ((!!) lst) [1,3..length lst - 1]

hololeap + 0 comments The instructions say that it needs to be the first item, the third item, etc. So it should read as

f lst = map (lst !!) [0,2..length lst - 1]

The instructions need to be fixed.

daniel_florez + 0 comments this is my answer.. :3

f [] = [] f lst = head (tail lst):f (tail (tail lst))

dim1tri + 0 comments another option

f lst = [j | (i,j) <- zip [1..] lst, i `mod` 2 == 0]

but not sure how efficient it is, I'm a beginner

byronhambly + 1 comment Easy peezy in Elixir using Enum.drop_every

IO.stream(:stdio, :line) |> Enum.map(&String.trim/1) |> Enum.map(&String.to_integer/1) |> Enum.drop_every(2) |> Enum.each(&IO.puts(&1))

BHesseldieck + 1 comment Or like this:

IO.read(:all) |> String.split("\n") |> Enum.drop_every(2) |> Enum.join("\n") |> IO.puts

byronhambly + 0 comments very nice!

scratchme + 0 comments var k = arr.zipWithIndex.filter(_._2 %2==1).map(x => x._1)/

m0smith + 0 comments Clojure solution:

(fn [lst] (map second (partition 2 lst)))

Sort 109 Discussions, By:

Please Login in order to post a comment