python/CodeChallenge_34.py

40 lines
1.4 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

'''
Sometimes, certain words like optimization or kubernetes are so lengthy that writing them repeatedly in a single text becomes quite tedious.
Lets define a word as too long if its length is strictly greater than 7 characters. All such words should be replaced with a special abbreviation.
The abbreviation is created as follows:
Write the first and last letter of the word, and between them, insert the number of letters that appear between the first and last letters. This number is
written in decimal format and should not contain any leading zeros.
Your task is to automate the process of abbreviating words. Replace all too long words with their abbreviations, while leaving shorter words unchanged.
Example 1
Input:
optimization
Output:
o10n
Example 2
Input:
kubernetes
Output:
k8s
Example 3
Input:
word
Output:
word
'''
class Solution(object):
def solve(self, s:str) -> str:
if len(s) > 7:
return f"{s[0]}{len(s) - 2}{s[-1]}"
return s
solution = Solution()
print(solution.solve("optimization")) # Output: "o10n"
print(solution.solve("kubernetes")) # Output: "k8s"
print(solution.solve("word")) # Output: "word"
print(solution.solve("hello")) # Output: "hello"
print(solution.solve("supercalifragilisticexpialidocious")) # Output: "s30s"
print(solution.solve("bergermeisterblindarmentzuntuntoperationschwierigkeiten")) # Output: "b56n"