Skip to content

Commit

Permalink
Finished Unique in Order Kata
Browse files Browse the repository at this point in the history
  • Loading branch information
garciadias committed Apr 27, 2023
1 parent bf9ea6c commit 9a69484
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 2 deletions.
7 changes: 7 additions & 0 deletions .vscode/settings.json
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
}
5 changes: 3 additions & 2 deletions py_codewars/binary_arrays.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
url: https://www.codewars.com/kata/578553c3a1b8d5c40300037c/train/python
Instructions:
Given an array of ones and zeroes, convert the equivalent binary value to an integer.
Given an array of ones and zeroes, convert the equivalent binary value to an
integer.
Eg: [0, 0, 0, 1] is treated as 0001 which is the binary representation of 1.
Expand Down Expand Up @@ -34,4 +35,4 @@ def binary_array_to_number(arr: list) -> int:
int
The integer value of the binary array.
"""
return int(''.join([str(i) for i in arr]), 2)
return int("".join([str(i) for i in arr]), 2)
42 changes: 42 additions & 0 deletions py_codewars/unique_in_order.py
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
24 changes: 24 additions & 0 deletions tests/test_unique_in_order.py
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(()) == []

0 comments on commit 9a69484

Please sign in to comment.