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 the array.

  • With two arguments, every(array, interval) it returns every intervalth element.

  • With three arguments, every(array, interval, start_index) returns every intervalth element starting at index start_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

Stats:

CreatedJun 5, 2016
PublishedJun 5, 2016
Warriors Trained2480
Total Skips64
Total Code Submissions4204
Total Times Completed1369
Ruby Completions126
JavaScript Completions633
C# Completions107
Python Completions478
C Completions79
Dart Completions20
Total Stars38
% of votes with a positive feedback rating92% of 312
Total "Very Satisfied" Votes264
Total "Somewhat Satisfied" Votes43
Total "Not Satisfied" Votes5
Ad
Contributors
  • Insti Avatar
  • ZozoFouchtra Avatar
  • EricDunaway Avatar
  • GiacomoSorbi Avatar
  • mojodev Avatar
  • Verall Avatar
  • donaldsebleung Avatar
  • lilsweetcaligula Avatar
  • FArekkusu Avatar
  • monadius Avatar
  • user9644768 Avatar
  • hobovsky Avatar
  • farhanaditya Avatar
  • saudiGuy Avatar
Ad