-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
bf9ea6c
commit 9a69484
Showing
4 changed files
with
76 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"python.testing.pytestArgs": [ | ||
"." | ||
], | ||
"python.testing.unittestEnabled": false, | ||
"python.testing.pytestEnabled": true | ||
} |
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,42 @@ | ||
""" | ||
Solves the `Unique In Order` codewars kata | ||
url: https://www.codewars.com/kata/54e6533c92449cc251001667/train/python | ||
Instructions: | ||
Implement the function unique_in_order which takes as argument a sequence and | ||
returns a list of items without any elements with the same value next to each | ||
other and preserving the original order of elements. | ||
For example: | ||
unique_in_order('AAAABBBCCDAABBB') == ['A', 'B', 'C', 'D', 'A', 'B'] | ||
unique_in_order('ABBCcAD') == ['A', 'B', 'C', 'c', 'A', 'D'] | ||
unique_in_order([1, 2, 2, 3, 3]) == [1, 2, 3] | ||
unique_in_order((1, 2, 2, 3, 3)) == [1, 2, 3] | ||
""" | ||
from typing import Union | ||
|
||
|
||
def unique_in_order(sequence: Union[str, list, tuple]) -> list: | ||
"""Return a list of items without any elements with the same value next to | ||
each other and preserving the original order of elements. | ||
Parameters | ||
---------- | ||
sequence : str or list or tuple | ||
A sequence of elements. | ||
Returns | ||
------- | ||
list | ||
A list of items without any elements with the same value next to each | ||
other and preserving the original order of elements. | ||
""" | ||
sequence = list(sequence) | ||
if len(sequence) == 0: | ||
return [] | ||
unique_in_order_list = [sequence[0]] | ||
for i, item in enumerate(sequence[1:], start=1): | ||
if item != sequence[i - 1]: | ||
unique_in_order_list.append(item) | ||
return unique_in_order_list |
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 @@ | ||
from py_codewars.unique_in_order import unique_in_order | ||
|
||
|
||
def test_unique_in_order_str(): | ||
"""Test unique_in_order on a string.""" | ||
assert unique_in_order("AAAABBBCCDAABBB") == ["A", "B", "C", "D", "A", "B"] | ||
assert unique_in_order("ABBCcAD") == ["A", "B", "C", "c", "A", "D"] | ||
|
||
|
||
def test_unique_in_order_list(): | ||
"""Test unique_in_order on a list.""" | ||
assert unique_in_order([1, 2, 2, 3, 3]) == [1, 2, 3] | ||
|
||
|
||
def test_unique_in_order_tuple(): | ||
"""Test unique_in_order on a tuple.""" | ||
assert unique_in_order((1, 2, 2, 3, 3)) == [1, 2, 3] | ||
|
||
|
||
def test_unique_in_order_empty(): | ||
"""Test unique_in_order on an empty sequence.""" | ||
assert unique_in_order("") == [] | ||
assert unique_in_order([]) == [] | ||
assert unique_in_order(()) == [] |