[Python] Trying a simple problem about sorting [python]

Discussão em 'Python' iniciado por Stack, Maio 27, 2025.

  1. Stack

    Stack Membro Participativo

    I saw a short on youtube about a prompt their teacher gave them: Given a list of numbers filter out duplicates and sort them.

    This seemed simple enough, and I'm still learning python, so I decided to give it a go.

    What I've come up with creates a list of 20 random numbers between 1 and 10 and filters out dupes, and everything works up to that point, but as soon as I get to the sorting part everything breaks down.

    I didn't want to just use .sort() because that felt like cheating, and I thought to use a binary search tree but that looked a bit too complex for my current skill level, and brute forcing a search over less than 20 numbers seemed like a good idea, so this is what I came up with:

    #Find the lowest digit in the list, append it to sorted_list

    search_list = filtered_list

    for number in filtered_list:
    lowest_digit = number
    for i in search_list: #check the number against each remaining number in search list
    if lowest_digit > i:
    lowest_digit = i
    print(f"Current Number:{number} Checked Number:{i} Lowest Digit:{lowest_digit}")

    print("")
    sorted_list.append(lowest_digit)
    search_list.remove(lowest_digit)

    print(f"Sorted and Filtered: {sorted_list}")


    For some reason it keeps skipping numbers in the filtered_list. At first I figured it was because I was originally removing the lowest value from filtered_list, so I put in search_list to change that instead, but it's still skipping. It's probably something really stupid simple, but any help you guys could give is appreciated.

    Continue reading...

Compartilhe esta Página