Skip to content

Instantly share code, notes, and snippets.

@tyler-austin
Created June 21, 2017 18:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tyler-austin/58a9420d6368296423150abe2f61bf0c to your computer and use it in GitHub Desktop.
Save tyler-austin/58a9420d6368296423150abe2f61bf0c to your computer and use it in GitHub Desktop.
Code Fights - extractEachKth

Given array of integers, remove each kth element from it.

Example

For inputArray = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] and k = 3, the output should be
extractEachKth(inputArray, k) = [1, 2, 4, 5, 7, 8, 10].

Input/Output

  • [time limit] 4000ms (py3)

  • [input] array.integer inputArray

    Guaranteed constraints:

    5 ≤ inputArray.length ≤ 15,
    -20 ≤ inputArray[i] ≤ 20.
    
  • [input] integer k

    Guaranteed constraints:

    1 ≤ k ≤ 10.
    
  • [output] array.integer

    inputArray without elements k - 1, 2k - 1, 3k - 1 etc.

def extract_each_kth(input_array: list, k: int):
result = []
for i, e in enumerate(input_array):
if (i + 1) % k != 0:
result.append(e)
return result
import unittest
from extract_each_kth import extract_each_kth
class TestExtractEachKth(unittest.TestCase):
def test_1(self):
input_array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
k = 3
solution = [1, 2, 4, 5, 7, 8, 10]
result = extract_each_kth(input_array, k)
self.assertEqual(result, solution)
def test_2(self):
input_array = [1, 1, 1, 1, 1]
k = 1
solution = []
result = extract_each_kth(input_array, k)
self.assertEqual(result, solution)
def test_3(self):
input_array = [1, 2, 1, 2, 1, 2, 1, 2]
k = 2
solution = [1, 1, 1, 1]
result = extract_each_kth(input_array, k)
self.assertEqual(result, solution)
if __name__ == '__main__':
unittest.main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment