# Filter Positions in a List

# Filter Positions in a List

QuomoZ + 0 comments I've 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 + 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 + 2 comments You can use collect instead of filter and map.

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

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 + 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))

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

pinto_jennyfer + 1 comment I don't understand why my code doesn't work when there are duplicates in the list

def f ( arr: List[Int] ) : List [Int] = { arr.filter (arr.indexOf ( _ : Int) % 2 != 0) }

f ( List(1,2,3,4,4,5) )

`scala List[Int] = List(2, 4, 4, 5)`

--> this is the resultpetr_zoldos + 0 comments When there are more items with the same value in the list, the method indexOf doesn't care about it. It just returns index of the first item with the specified value that it founds.

Sort 104 Discussions, By:

Please Login in order to post a comment