-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add mergesort and two more replace stubs
- Loading branch information
Showing
5 changed files
with
105 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
|
||
def replaced_element(l, element, replacement): | ||
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import pytest | ||
|
||
from listwiz import replace as lwr | ||
|
||
|
||
def test_replace_element(): | ||
# Stub to be replaced with actual tests when implementing the function | ||
pass | ||
|
||
|
||
def test_replace_sublist_simple(): | ||
# Replace sublist does not have any test right now. | ||
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. | ||
pass | ||
|
||
|
||
def test_replace_sublist_empty(): | ||
# This is a stub for testing the bug with empty sublists. | ||
pass |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters