# Filter Positions in a List

# Filter Positions in a List

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()...`

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

`f (_:x:xs) = x : f xs 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 + 3 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 + 1 comment You can use collect instead of filter and map.

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

pavelsau + 0 comments I think your solution with partial function seems more clear

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

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

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?

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

andreigasp1 + 0 comments Clojure solution:

(defn solve[x] (take-nth 2 (rest x)))

byronhambly + 0 comments 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))

Sort 95 Discussions, By:

Please Login in order to post a comment