Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add mergesort and two more replace stubs #7

Merged
merged 2 commits into from
Aug 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/listwiz/inspect.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,5 @@ def longest_decreasing_streak(l):


def most_common_element(l):
# Find the mmost common element in the list
# Find the most common element in the list
raise NotImplementedError
40 changes: 40 additions & 0 deletions src/listwiz/replace.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@

def replaced_elements(l, mapping):
# See issue #12
raise NotImplementedError


def replace_sublist(l, sublist, replacement):
"""Naive function to replace a sublist with a replacement list.

Parameters
----------
l : list
List to replace all occurances of sublist.
sublist : list
A (shorter) list to search for.
replacement : list
A new list to insert instead of sublist.

Examples
--------
>>> from listwiz.replace import replace_sublist

Replace any occurance of ``[2, 3]`` with ``[3, 2]``

>>> replace_sublist([1, 2, 3, 4], [2, 3], [3, 2])
[1, 3, 2, 4]

"""
len_sublist = len(sublist)
result = l[:] # copy the input list

i = 0
while i < len(result):
if result[i:i+len_sublist] == sublist:
result[i:i+len_sublist] = replacement
i += len_sublist
else:
i += 1

return result
28 changes: 25 additions & 3 deletions src/listwiz/sorting.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@

def merge_sort(l):
"""Use the merge sort algorithm to sort the list
elements
"""Use the merge sort algorithm to sort the list elements.

Parameters
----------
Expand All @@ -12,7 +11,30 @@ def merge_sort(l):
-------
sorted_list
"""
raise NotImplementedError
# If there is a single item, the list is already sorted, return.
if len(l) == 1:
return l

split = len(l) // 2
# Split and sort each part by calling merge_sort recursively
part1 = merge_sort(l[:split])
part2 = merge_sort(l[split:])

# The result is a new list adding always the smaller items from each list
res = []
while len(part1) and len(part2):
if part1[0] <= part2[0]:
res.append(part1.pop(0))
else:
res.append(part2.pop(0))

# One of the lists is not fully consumed, add all remaining elements:
if len(part1):
res.extend(part1)
else:
res.extend(part2)

return res


def bubble_sort(l):
Expand Down
26 changes: 26 additions & 0 deletions tests/test_replace.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import pytest

from listwiz import replace as lwr


def test_replace_elements():
# Stub to be replaced with actual tests when implementing the function,
# see issue #12.
pass


def test_replace_sublist_simple():
# Replace sublist does not have any test right now. See issue #13.
pass


def test_replace_sublist_overlap():
# The sublist may match with overlap (e.g. if it is just [1, 1])
# We should add one or more test that check we replace the first occurance.
# See issue #13.
pass


def test_replace_sublist_empty():
# This is a stub for testing the bug with empty sublists.
pass
22 changes: 16 additions & 6 deletions tests/test_sorting.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,28 @@
from listwiz import sorting as lws


@pytest.mark.xfail(reaso="mergesort doesn't work yet!")
def test_mergesort():
l = [3, 2, 1, 5, 4, 6]
res = lws.merge_sort(l)
assert res == [1, 2, 3, 4, 5, 6]
# Test even sized list:
l = [3, 2, 1, 5, 4, 6]
res = lws.merge_sort(l)
assert res == [1, 2, 3, 4, 5, 6]

# Test odd sized list:
l = [5, 4, 3, 2, 1]
res = lws.merge_sort(l)
assert res == [1, 2, 3, 4, 5]


def test_mergesort_empty():
# Stub for a test with empty lists, see issue #8
pass


@pytest.mark.skip(reason="Just a stub, not implemented yet")
def test_bubble_sort():
# Stub for basic bubble sort tests, see issue #9
pass


@pytest.mark.skip(reason="Just a stub, not implemented yet")
def test_selection_sort():
# Stub for basic selection sort tests, see issue #10
pass