Skip to content

Instantly share code, notes, and snippets.

@marcintustin
Created September 29, 2021 14:28
Show Gist options
  • Save marcintustin/e16ab1e64fc587541383d5ee32f5f5fd to your computer and use it in GitHub Desktop.
Save marcintustin/e16ab1e64fc587541383d5ee32f5f5fd to your computer and use it in GitHub Desktop.
Find duplicates python exercise
"""
Write functions that:
a. detects if an array/slice has duplicates
b. detects what the duplicates are
c. counts the duplicates
You may tackle these in any order
You may alter the signatures of the skeletons to meet your needs
"""
from typing import List, Mapping, TypeVar, Iterable
import pytest
V = TypeVar('V')
def has_duplicates(input: List[V]) -> bool:
"""
a. detects if an array/slice has duplicates
"""
pass
def get_duplicates(input: List[V]) -> Iterable:
"""
b. detects what the duplicates are
"""
pass
def get_duplicate_counts(input: List[V]) -> Mapping[V, int]:
"""
c. counts the duplicates
"""
pass
@pytest.mark.parametrize(
"a,expected",
[
pytest.param(
[0,1], False
),
pytest.param(
[1, 0], False
),
pytest.param(
[1, 0, 0], True
),
],
)
def test_has_duplicates(a, expected):
assert has_duplicates(a) == expected
def test_get_duplicates_with_dups():
assert get_duplicates([1,1,2]) == [1]
[tox]
envlist = py38
skipsdist = True
[src]
files = duplicates.py
[testenv]
deps = mypy >= v0.900
pytest
pylint
# -r requirements.txt
commands =
python -m mypy --ignore-missing-imports --show-error-codes --warn-unreachable --config-file=tox.ini {[src]files}
# python -m pylint {[src]files}
pytest {[src]files}
[mypy]
[mypy-google.*]
ignore_missing_imports = True
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment