Skip to content
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 .github/workflows/flake8.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ jobs:
matrix:
python-version: ["3.12"]
steps:
- uses: "actions/checkout@v5"
- uses: "actions/checkout@v6"
- name: Set up Python ${{ matrix.python-version }}
# This is the version of the action for setting up Python,
# not the Python version.
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/pylint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ jobs:
matrix:
python-version: ["3.12"]
steps:
- uses: "actions/checkout@v5"
- uses: "actions/checkout@v6"
- name: Set up Python ${{ matrix.python-version }}
uses: "actions/setup-python@v6"
with:
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/pytest.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jobs:
timeout-minutes: 10
steps:
- name: "Checkout code"
uses: "actions/checkout@v5"
uses: "actions/checkout@v6"
- name: "Set up Python 3.12"
uses: "actions/setup-python@v6"
with:
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/ruff.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:
# indefinitely if something goes wrong
timeout-minutes: 10
steps:
- uses: "actions/checkout@v5"
- uses: "actions/checkout@v6"
- name: "Set up Python 3.12"
uses: "actions/setup-python@v6"
with:
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/yamllint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:
# indefinitely if something goes wrong
timeout-minutes: 10
steps:
- uses: "actions/checkout@v5"
- uses: "actions/checkout@v6"
- name: "Set up Python 3.12"
uses: "actions/setup-python@v6"
with:
Expand Down
51 changes: 51 additions & 0 deletions solutions/python/secret-handshake/1/secret_handshake.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
"""
Secret Handshake.

Convert a number's binary representation (up to five bits) to a
sequence of actions in the secret handshake.

The sequence of actions is determined by inspecting the rightmost five
binary digits (least-significant bit on the right):

- 1 (..00001): "wink"
- 2 (..00010): "double blink"
- 4 (..00100): "close your eyes"
- 8 (..01000): "jump"
- 16(..10000): reverse the order of the operations selected above

Only the presence of a bit matters; higher bits beyond the five listed
are ignored. The reverse bit (16) inverses the final list of actions.
"""

ACTIONS: dict[int:tuple[str, str]] = {
1: ("00001", "wink"),
2: ("00010", "double blink"),
3: ("00100", "close your eyes"),
4: ("01000", "jump"),
5: ("10000", "Reverse the order of the operations in the secret handshake"),
}


def commands(binary_str: str) -> list[str]:
"""
Return the list of secret-handshake actions for the given binary string.

The input should be a binary string whose rightmost character is the
least-significant bit. Up to five bits are considered, mapping to the
actions defined by the classic Exercism "Secret Handshake" exercise.
If the fifth bit is set, the final list of actions is reversed.

:param binary_str: Binary string (e.g. "10101"). Rightmost char is LSB.
:returns: A list of action strings in the computed order.
"""
results: list = []
# Iterate from least- to most-significant bit by reversing the string.
for i, char in enumerate(binary_str[::-1]):
# Check if the corresponding bit position matches the expected "1".
if ACTIONS[i + 1][0][-(i + 1)] == char:
if i + 1 != 5:
results.append(ACTIONS[i + 1][1])
else:
# Fifth bit indicates the final list should be reversed.
results = results[::-1]
return results
49 changes: 49 additions & 0 deletions solutions/python/secret-handshake/2/secret_handshake.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
"""
Secret Handshake.

Convert a number's binary representation (up to five bits) to a
sequence of actions in the secret handshake.

The sequence of actions is determined by inspecting the rightmost five
binary digits (least-significant bit on the right):

- 1 (..00001): "wink"
- 2 (..00010): "double blink"
- 4 (..00100): "close your eyes"
- 8 (..01000): "jump"
- 16(..10000): reverse the order of the operations selected above

Only the presence of a bit matters; higher bits beyond the five listed
are ignored. The reverse bit (16) inverses the final list of actions.
"""

ACTIONS: dict[int : tuple[str, str]] = {
0: ("00001", "wink"),
1: ("00010", "double blink"),
2: ("00100", "close your eyes"),
3: ("01000", "jump"),
}


def commands(binary_str: str) -> list[str]:
"""
Return the list of secret-handshake actions for the given binary string.

The input should be a binary string whose rightmost character is the
least-significant bit. Up to five bits are considered, mapping to the
actions defined by the classic Exercism "Secret Handshake" exercise.
If the fifth bit is set, the final list of actions is reversed.

:param binary_str: Binary string (e.g. "10101"). Rightmost char is LSB.
:returns: A list of action strings in the computed order.
"""
results: list[str] = []
# Iterate from least- to most-significant bit by reversing the string.
for i, char in enumerate(binary_str[::-1]):
# Check if the corresponding bit position matches the expected "1".
if char == "1" and i < 4:
results.append(ACTIONS[i][1])
elif char == "1" and i == 4:
# Fifth bit indicates the final list should be reversed.
results = results[::-1]
return results
44 changes: 44 additions & 0 deletions solutions/python/secret-handshake/3/secret_handshake.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
"""
Secret Handshake.

Convert a number's binary representation (up to five bits) to a
sequence of actions in the secret handshake.

The sequence of actions is determined by inspecting the rightmost five
binary digits (least-significant bit on the right):

- 1 (..00001): "wink"
- 2 (..00010): "double blink"
- 4 (..00100): "close your eyes"
- 8 (..01000): "jump"
- 16(..10000): reverse the order of the operations selected above

Only the presence of a bit matters; higher bits beyond the five listed
are ignored. The reverse bit (16) inverses the final list of actions.
"""

ACTIONS: tuple[str, str, str, str] = ("wink", "double blink", "close your eyes", "jump")


def commands(binary_str: str) -> list[str]:
"""
Return the list of secret-handshake actions for the given binary string.

The input should be a binary string whose rightmost character is the
least-significant bit. Up to five bits are considered, mapping to the
actions defined by the classic Exercism "Secret Handshake" exercise.
If the fifth bit is set, the final list of actions is reversed.

:param binary_str: Binary string (e.g. "10101"). Rightmost char is LSB.
:returns: A list of action strings in the computed order.
"""
results: list[str] = []
# Iterate from least- to most-significant bit by reversing the string.
for i, char in enumerate(binary_str[::-1]):
# Check if the corresponding bit position matches the expected "1".
if char == "1" and i < 4:
results.append(ACTIONS[i])
elif char == "1" and i == 4:
# Fifth bit indicates the final list should be reversed.
results = results[::-1]
return results