Sorting String Characters by First Occurrence

  • Share this:

Code introduction


Sorts the characters in the input string based on their first occurrence and converts them to uppercase letters.


Technology Stack : String, list comprehension, character encoding

Code Type : String processing

Code Difficulty : Intermediate


                
                    
def a_to_z_counter(string):
    def ord_to_alpha(n):
        return chr(n - 96)

    result = [ord_to_alpha(n) for n in sorted(string, key=string.index)]
    return ''.join(result)