Light Mode
Dark Mode
Code runs without errors but fail checking

Sort Array by Element frequency


def frequency_sort(items):
    a = []
    for i in items:
        for j in list(range(len(items))):
            if i not in a:
                for n in list(range(items.count(i))):
                    a.append(i)
    return a


if __name__ == '__main__':
    print("Example:")
    print(frequency_sort([4, 6, 2, 2, 6, 4, 4, 4]))

    # These "asserts" are used for self-checking and not for an auto-testing
    assert list(frequency_sort([4, 6, 2, 2, 6, 4, 4, 4])) == [4, 4, 4, 4, 6, 6, 2, 2]
    assert list(frequency_sort(['bob', 'bob', 'carl', 'alex', 'bob'])) == ['bob', 'bob', 'bob', 'carl', 'alex']
    assert list(frequency_sort([17, 99, 42])) == [17, 99, 42]
    assert list(frequency_sort([])) == []
    assert list(frequency_sort([1])) == [1]
    print("Coding complete? Click 'Check' to earn cool rewards!")

Run Output:

Example: [4, 4, 4, 4, 6, 6, 2, 2]

Coding complete? Click 'Check' to earn cool rewards!

Check Results:

Your result:[4,4,4,4,6,6,2,2,2] Right result:[4,4,4,4,2,2,2,6,6] HOW COME THIS IS THE RIGHT ANSWER IF 2 APPEARS AFTER 6??????????

Fail:frequency_sort([4,6,2,2,2,6,4,4,4])

I'm afraid something's wrong... and it's not my code... he he he What should I do to collect my points and move on?

HELP!

Created: May 30, 2019, 8:06 p.m.
Updated: May 31, 2019, 4:05 p.m.
0
7
User avatar
JoeFuk