7 kyu
Every nth array element. (Basic)
126 of 1,369Insti
Description:
Create a function (a method in Ruby, an extension method in C#) every
which returns every nth element of an array.
Usage
With one argument,
every(array)
returns every element of thearray
.With two arguments,
every(array, interval)
it returns everyinterval
th element.With three arguments,
every(array, interval, start_index)
returns everyinterval
th element starting at indexstart_index
.
Examples
every([0,1,2,3,4]) -> [0,1,2,3,4]
every([0,1,2,3,4],1) -> [0,1,2,3,4]
every([0,1,2,3,4],2) -> [0,2,4]
every([0,1,2,3,4],3) -> [0,3]
every([0,1,2,3,4],3,1) -> [1,4]
Notes
Test cases:
interval
will always be a positive integer (but may be larger than the size of the array).start_index
will always be within the bounds of the array.
Once you have completed this kata, try the advanced version (http://www.codewars.com/kata/every-nth-array-element-advanced) which allows negative intervals and unbounded start_indexes
Arrays
Fundamentals
Similar Kata:
Stats:
Created | Jun 5, 2016 |
Published | Jun 5, 2016 |
Warriors Trained | 2480 |
Total Skips | 64 |
Total Code Submissions | 4204 |
Total Times Completed | 1369 |
Ruby Completions | 126 |
JavaScript Completions | 633 |
C# Completions | 107 |
Python Completions | 478 |
C Completions | 79 |
Dart Completions | 20 |
Total Stars | 38 |
% of votes with a positive feedback rating | 92% of 312 |
Total "Very Satisfied" Votes | 264 |
Total "Somewhat Satisfied" Votes | 43 |
Total "Not Satisfied" Votes | 5 |