Skip to content

Commit

Permalink
Check for non-integer elements
Browse files Browse the repository at this point in the history
  • Loading branch information
JingSyue committed Feb 4, 2024
1 parent e278279 commit 5b5c990
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 35 deletions.
7 changes: 5 additions & 2 deletions filter_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,11 @@ def filter_list(input_list: List[int]) -> List[int]:
if len(input_list) % 10 != 0:
raise ValueError("Input list length must be a multiple of 10. Received length: {}".format(len(input_list)))

# Efficiently filter the list with comprehension, leveraging the fact that
# we only need to include elements not at positions multiple of 2 or 3.
# Check for non-integer elements
for item in input_list:
if not isinstance(item, int):
raise TypeError("All elements in the input list must be integers. Found non-integer element: {}".format(item))

filtered_list = [
item for index, item in enumerate(input_list, start=1)
if index % 2 != 0 and index % 3 != 0
Expand Down
42 changes: 9 additions & 33 deletions test_filter_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,45 +2,21 @@
import time
from filter_list import filter_list

def test_length_not_multiple_of_10():
def test_length_not_multiple_of_10_raises_value_error():
with pytest.raises(ValueError):
filter_list([1, 2, 3])
filter_list([1, 2, 3, 4, 5, 6, 7, 8, 9])

def test_filtering_criteria():
assert filter_list(list(range(1, 11))) == [1, 5, 7]

def test_large_list():
input_list = list(range(1, 101))
expected = [x for x in input_list if x % 2 != 0 and x % 3 != 0]
assert filter_list(input_list) == expected

def test_empty_list():
with pytest.raises(ValueError):
filter_list([])

def test_length_exactly_multiple_of_10():
input_list = list(range(1, 21)) # 20 is a multiple of 10
expected = [1, 5, 7, 11, 13, 17, 19]
def test_basic_filtering_criteria():
input_list = list(range(1, 11)) # 1到10
expected = [1, 5, 7]
assert filter_list(input_list) == expected

def test_length_not_exactly_multiple_of_10_but_more():
with pytest.raises(ValueError):
filter_list(list(range(1, 15))) # Length is 14, more than 10 but not a multiple

def test_negative_and_zero_values():
input_list = [-10, -5, 0, 5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 65, 70, 75, 80, 85]
expected = [0, 5, 25, 35, 55, 65, 85]
assert filter_list(input_list) == expected

def test_duplicate_values():
input_list = [1, 2, 3, 3, 4, 5, 6, 7, 7, 8, 9, 10, 10, 11, 12, 13, 14, 15, 16, 17]
expected = [1, 5, 7, 11, 13, 17]
assert filter_list(input_list) == expected
def test_input_with_non_integer_values_raises_type_error():
with pytest.raises(TypeError):
filter_list([1, 2, 'a', 4, 5, 6, 7, 8, 9, 10])

def test_performance_large_list():
start_time = time.time()
filter_list(list(range(1, 100001))) # 100,000 items
filter_list(list(range(1, 100001))) # 100,000个元素
end_time = time.time()
assert end_time - start_time < 1


0 comments on commit 5b5c990

Please sign in to comment.