Skip to content

Instantly share code, notes, and snippets.

@ZechCodes
Created June 10, 2020 23:51
Show Gist options
  • Save ZechCodes/1f1d259e1baeefa7ce8539e7537dc418 to your computer and use it in GitHub Desktop.
Save ZechCodes/1f1d259e1baeefa7ce8539e7537dc418 to your computer and use it in GitHub Desktop.
Challenge #24 - Remove Extras

Challenge #24 - Remove Extras

Create a function that takes two arguments: a list and a number. In the list (the first argument), if an element occurs more than N times (the second argument), remove the extra occurrences and return the result.

Examples

delete_occurrences([1, 1, 1, 1], 2) ➞ [1, 1]

delete_occurrences([13, True, 13, None], 1) ➞ [13, True, None]

delete_occurrences([True, True, True], 3) ➞ [True, True, True]

Notes

  • Do not alter the order of the original list.
import unittest
from typing import Any, List
def delete_occurrences(items: List[Any], max_occurrences: int) -> List[Any]:
return [] # Put your code here
class TestRemoveExtras(unittest.TestCase):
def test_1(self):
self.assertEqual(delete_occurrences([1, 1, 1, 1], 2), [1, 1])
def test_2(self):
self.assertEqual(delete_occurrences([True, True, True], 3), [True, True, True])
def test_3(self):
self.assertEqual(delete_occurrences([13, True, 13, None], 1), [13, True, None])
def test_4(self):
self.assertEqual(delete_occurrences([], 100), [])
def test_5(self):
self.assertEqual(delete_occurrences(["John", "John", "Marry", "Marry"], 1), ["John", "Marry"])
def test_6(self):
self.assertEqual(delete_occurrences(["Marry", "John", None, "John", False, "John", 0, "John", "Marry", "Marry", "John"], 3), ["Marry", "John", None, "John", False, "John", 0, "Marry", "Marry"])
def test_7(self):
self.assertEqual(delete_occurrences([20, 37, 20, 21], 1), [20, 37, 21])
def test_8(self):
self.assertEqual(delete_occurrences([1, 1, 3, 3, 7, 2, 2, 2, 2], 3), [1, 1, 3, 3, 7, 2, 2, 2])
def test_9(self):
self.assertEqual(delete_occurrences([1, 2, 3, 1, 1, 2, 1, 2, 3, 3, 2, 4, 5, 3, 1],3), [1, 2, 3, 1, 1, 2, 2, 3, 3, 4, 5])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment