The Full Counting Sort

  • + 0 comments

    Independently, here is my Python3 code, which is similar but has no execution time wasted in if statements. In modern HackerRank, the inputs are already taken in the main method, and we fill-in the given method. In GeoMatt22's simplification of abehjat's C-to-Python code, the sorting according to key was excluded accidentally and str is a reserved word, meaning the suggested code is untested? This sorting challenge does not involve a counting sort, as the string values are not repeated from a small fixed list of possibilities.

    from collections import defaultdict
    def countSort(arr):
    
        d = defaultdict(list) ; n = len(arr)
        for i in range(n//2) : d[int(arr[i][0])].append("-")
        for i in range(n//2,n) : d[int(arr[i][0])].append(arr[i][1])
        print( *[' '.join(d[k]) for k in sorted(d)] )