Returns numerical pattern of character appearances in given word >>> get_word_pattern("") '' >>> get_word_pattern(" ") '0' >>> get_word_pattern("pattern") '0.1.2.2.3.4.5' >>> get_word_pattern("word pattern") '0.1.2.3.4.5.6.7.7.8.2.9' >>> get_word_pattern("get
(word: str)
| 1 | def get_word_pattern(word: str) -> str: |
| 2 | """ |
| 3 | Returns numerical pattern of character appearances in given word |
| 4 | >>> get_word_pattern("") |
| 5 | '' |
| 6 | >>> get_word_pattern(" ") |
| 7 | '0' |
| 8 | >>> get_word_pattern("pattern") |
| 9 | '0.1.2.2.3.4.5' |
| 10 | >>> get_word_pattern("word pattern") |
| 11 | '0.1.2.3.4.5.6.7.7.8.2.9' |
| 12 | >>> get_word_pattern("get word pattern") |
| 13 | '0.1.2.3.4.5.6.7.3.8.9.2.2.1.6.10' |
| 14 | >>> get_word_pattern() |
| 15 | Traceback (most recent call last): |
| 16 | ... |
| 17 | TypeError: get_word_pattern() missing 1 required positional argument: 'word' |
| 18 | >>> get_word_pattern(1) |
| 19 | Traceback (most recent call last): |
| 20 | ... |
| 21 | AttributeError: 'int' object has no attribute 'upper' |
| 22 | >>> get_word_pattern(1.1) |
| 23 | Traceback (most recent call last): |
| 24 | ... |
| 25 | AttributeError: 'float' object has no attribute 'upper' |
| 26 | >>> get_word_pattern([]) |
| 27 | Traceback (most recent call last): |
| 28 | ... |
| 29 | AttributeError: 'list' object has no attribute 'upper' |
| 30 | """ |
| 31 | word = word.upper() |
| 32 | next_num = 0 |
| 33 | letter_nums = {} |
| 34 | word_pattern = [] |
| 35 | |
| 36 | for letter in word: |
| 37 | if letter not in letter_nums: |
| 38 | letter_nums[letter] = str(next_num) |
| 39 | next_num += 1 |
| 40 | word_pattern.append(letter_nums[letter]) |
| 41 | return ".".join(word_pattern) |
| 42 | |
| 43 | |
| 44 | if __name__ == "__main__": |