Skip to content

Instantly share code, notes, and snippets.

@pschanely
Created March 1, 2024 19:17
Show Gist options
  • Save pschanely/ef252680363cb19df359894b96711ae0 to your computer and use it in GitHub Desktop.
Save pschanely/ef252680363cb19df359894b96711ae0 to your computer and use it in GitHub Desktop.
Shared via CrossHair Playground
from typing import List
def remove_duplicates(numbers: List[int]) -> List[int]:
"""
Removes duplicate numbers from the given list while preserving the order of the remaining elements.
"""
# Precondition: List must not be empty
assert len(numbers) > 0, "List must not be empty"
result = []
seen = set()
for number in numbers:
if number not in seen:
seen.add(number)
if number > 10 and number % 2 == 0:
result.append(number)
# Postconditions:
# 1. The result list should not have duplicates
assert len(result) == len(set(result)), "Result list contains duplicates"
# 2. The result list should be a subset of the original list
assert all(item in numbers for item in result), "Result list contains elements not in original list"
# 3. Every element in the original list that is not a duplicate should be in the result list
assert all(numbers.count(item) == 1 or item in result for item in numbers), "An element from the original list is missing in the result list"
return result
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment