-
-
Save vibinnair/e68df45981386a025f8e60ae09ed5bf8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
https://www.hackerrank.com/challenges/fp-filter-positions-in-a-list/problem | |
import Foundation | |
func filterOddElements(_ list: [Int]) -> [Int] { | |
if list.count <= 1 { | |
return [] | |
} | |
let elements = list.prefix(2) | |
let oddElement = elements.last! | |
let newList = Array(list.dropFirst(2)) | |
return [oddElement] + filterOddElements(newList) | |
} | |
print(filterOddElements([5, 3, 4, 6, 7, 9, 8])) | |
print(filterOddElements([2, 5, 3, 4, 6, 7, 9, 8])) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment