diff options
| author | Lubos Kolouch <lubos@kolouch.net> | 2023-03-07 18:05:14 +0100 |
|---|---|---|
| committer | Lubos Kolouch <lubos@kolouch.net> | 2023-03-07 18:05:14 +0100 |
| commit | 3d41b13eee3184f3cd62707e1f662ef91f3a4c4f (patch) | |
| tree | 4573b0c512674e251fa19536f52179189947c0d2 /challenge-207/lubos-kolouch/python/ch-1.py | |
| parent | d75c83429332efc88f29eee14f988b199f2fa10c (diff) | |
| download | perlweeklychallenge-club-3d41b13eee3184f3cd62707e1f662ef91f3a4c4f.tar.gz perlweeklychallenge-club-3d41b13eee3184f3cd62707e1f662ef91f3a4c4f.tar.bz2 perlweeklychallenge-club-3d41b13eee3184f3cd62707e1f662ef91f3a4c4f.zip | |
Challenge 207 LK Perl Python Bash
Diffstat (limited to 'challenge-207/lubos-kolouch/python/ch-1.py')
| -rw-r--r-- | challenge-207/lubos-kolouch/python/ch-1.py | 58 |
1 files changed, 58 insertions, 0 deletions
diff --git a/challenge-207/lubos-kolouch/python/ch-1.py b/challenge-207/lubos-kolouch/python/ch-1.py new file mode 100644 index 0000000000..4410928b53 --- /dev/null +++ b/challenge-207/lubos-kolouch/python/ch-1.py @@ -0,0 +1,58 @@ +#!/usr/bin/env python3 + +import unittest +from typing import List + + +def is_single_row_word(word: str) -> bool: + """Check if a word can be typed using only one row of the keyboard. + + Args: + word (str): The word to check. + + Returns: + bool: True if the word can be typed using only one row of the keyboard, + False otherwise. + + """ + keyboard_rows = ['qwertyuiop', 'asdfghjkl', 'zxcvbnm'] + keyboard_map = {} + for i in range(len(keyboard_rows)): + for c in keyboard_rows[i]: + keyboard_map[c] = i+1 + row = keyboard_map.get(word[0].lower(), 0) + for c in word: + if keyboard_map.get(c.lower(), 0) != row: + return False + return True + + +def filter_single_row_words(words: List[str]) -> List[str]: + """Filter out the words that can be typed using only one row of the keyboard. + + Args: + words (List[str]): The list of words to filter. + + Returns: + List[str]: A new list containing only the words that can be typed using + only one row of the keyboard. + + """ + return [w for w in words if is_single_row_word(w)] + +class TestSingleRowWords(unittest.TestCase): + def test_example1(self): + """Test the first example from the task description.""" + words = ["Hello", "Alaska", "Dad", "Peace"] + single_row_words = filter_single_row_words(words) + self.assertCountEqual(single_row_words, ["Alaska", "Dad"]) + + def test_example2(self): + """Test the second example from the task description.""" + words = ["OMG", "Bye"] + single_row_words = filter_single_row_words(words) + self.assertCountEqual(single_row_words, []) + + +if __name__ == '__main__': + unittest.main() |
