Last active
June 9, 2022 03:13
-
-
Save vthily/e396dba8dfa0a1ab9cd1e9e66a5d85c5 to your computer and use it in GitHub Desktop.
Group the single characters (they are adjacent in order) into meaningful acronyms
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/python3 | |
| import functools | |
| def normalizeAcronyms(inputStrInWords): | |
| # Step1: Add extra space to prevent concatenate with previous word | |
| inputStrInWords = list(map(lambda a: a+' ' if len(a) > 1 else a, inputStrInWords)) | |
| # Step2: Remove the underscore, eg: c_d_a, l_p_a, etc. | |
| inputStrInWords = list(map(lambda a: a.replace('_', '').upper() if '_' in a else a, inputStrInWords)) | |
| # Step3: Combine if the next word is a single letter. | |
| retStr = functools.reduce(lambda a, b: a+b.upper() if len(b)==1 else a + ' ' + b, inputStrInWords) | |
| # Step4: Uppercase the first letter in the sentence | |
| retStr = retStr[0].upper() + retStr[1:] | |
| # Step5: Clean the space as added in the step1. | |
| return retStr.replace(' ', ' ') | |
| if __name__ == '__main__': | |
| inputStrInWords = ['c', 'd', 'a', 'trustee', 'and', 'b', 'b'] | |
| normalizeAcronyms(inputStr) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Version 2: