Home > other >  Make Python Pre-sort Mode Function Handle Multiple/No Mode
Make Python Pre-sort Mode Function Handle Multiple/No Mode

Time:03-22

I specifically want to use Transform and Conquer for a Python mode function, and have written the version below, which I believe is basically correct (please correct me if I'm wrong). However, when there are multiple modes, only one is given, which seems technically incorrect to me, as this value is "a" mode rather then "the" mode.

How would you recommend modifying the function to resolve this problem?

def mode_presort(arr):
    arr.sort() # Array must be sorted before we apply the algorithm.
    i = 1
    mode_frequency = 0
    while i < len(arr):
        run_length = 1
        run_value = arr[i]
        while i   run_length < len(arr) and arr[i   run_length] == run_value:
            run_length  = 1
        if run_length > mode_frequency:
            mode_frequency = run_length
            mode_value = run_value
        i  = run_length
    return mode_value
    
arr = [1, 1, 1, 2, 2]
print(mode_presort(arr))  # Output: 1
arr = [1, 1, 2, 2]
print(mode_presort(arr))  # Output: 2

Any help much appreciated.

CodePudding user response:

There is one bug in your code: i should not be initialised to 1, but to 0.

To get multiple results, let your function always return a list, even if there is only one mode.

Then, just add the case where the frequency is equal to the maximum you had found so far. So the if block should be changed to:

        if run_length > mode_frequency:
            mode_frequency = run_length
            mode_value = [run_value]  # Make it a list
        elif run_length == mode_frequency:  # Also deal with this case
            mode_value.append(run_value)  # Add alternative to existing list of modes

Like often, there are libraries that already offer the mode feature, like Pandas:

import pandas as pd

def mode(arr):
    return list(pd.Series(arr).mode())
  • Related