Skip to content

Instantly share code, notes, and snippets.

@vthily
Last active June 9, 2022 03:13
Show Gist options
  • Save vthily/e396dba8dfa0a1ab9cd1e9e66a5d85c5 to your computer and use it in GitHub Desktop.
Save vthily/e396dba8dfa0a1ab9cd1e9e66a5d85c5 to your computer and use it in GitHub Desktop.

Revisions

  1. vthily revised this gist May 18, 2022. No changes.
  2. vthily created this gist May 6, 2022.
    26 changes: 26 additions & 0 deletions normalizeAcronym.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,26 @@
    #!/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)