Skip to content
Open
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
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,14 @@
- URL: [Check Same Case Challenge](https://www.codewars.com/kata/5dd462a573ee6d0014ce715b)
</blockquote></details>

<details>
<summary><b>Replace With Alphabet Position (6kyu)</b></summary><blockquote>

- Module: [code_challenges/code_wars/replace_with_alphabet_position.py](code_challenges/code_wars/replace_with_alphabet_position.py)
- Test: [tests/code_wars/test_replace_with_alphabet_position.py](tests/code_wars/test_replace_with_alphabet_position.py)
- URL: [Replace With Alphabet Position Challenge](https://www.codewars.com/kata/546f922b54af40e1e90001da)
</blockquote></details>

<details>
<summary><b>Find Unique Number (6kyu)</b></summary><blockquote>

Expand Down
10 changes: 10 additions & 0 deletions code_challenges/code_wars/replace_with_alphabet_position.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# https://www.codewars.com/kata/546f922b54af40e1e90001da/train/python

def replace_characters(characters):
characters = characters.lower()
position_index = ["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"]
positions = ""
for character in characters:
if character.isalpha():
positions += f"{position_index.index(character) + 1} "
return positions.rstrip()
Comment on lines +5 to +10
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't readily see how the spaces are being added to positions?

The .index(character) method isn't very efficient, it has to look through the whole list to find the index. Since it has to be written out anyway, you might use a dict like {"a": 1, "b": 2...}. The lookup would then be o(1) instead of needing to search the whole alphabet each time.

There is probably also a way to use the built in ord method on the lower letter and some math to get the number.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh I see f"{position_index.index(character) + 1} " has a space at the end that is then trimmed.

9 changes: 9 additions & 0 deletions tests/code_wars/test_replace_with_alphabet_position.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# https://www.codewars.com/kata/546f922b54af40e1e90001da/train/python

from code_challenges.code_wars.replace_with_alphabet_position import replace_characters


def test_replace_characters():
assert replace_characters("A B C") == "1 2 3"
assert replace_characters("c b a") == "3 2 1"
assert replace_characters("AzBy") == "1 26 2 25"