aboutsummaryrefslogtreecommitdiff
path: root/challenge-207/lubos-kolouch/python/ch-1.py
blob: 4410928b5310387d39f6ecdce8cbd2e1c20b1770 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
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()