Home > OS >  How do a sort a list using a for loop?
How do a sort a list using a for loop?

Time:06-15

I need to create this function, sort_by_chips(player_list). This is what it is meant to do:

This function takes the list of players (list of lists) and returns a copy of the list in descending order of chip balance. Where two players have the same chip balance, the player with the lower number of games played value should appear first. This function must NOT modify the player list being passed in as a parameter. Your solution must NOT make use of List methods or any of the sort functions available in the Python Standard Library. This function returns a copy of the player list (list of lists) sorted in descending order of chip balance. You may wish to create/define additional functions (which are called from within this function) to assist with this task.

player_list = [['Bruce Wayne', 5, 5, 0, 0, 100, 15], ['Jessica Jones', 12, 0, 6, 6, 10, 6], ['Johnny Rose', 6, 2, 0, 4, 20, 10], ['Gina Linetti', 7, 4, 0, 3, 300, 15], ['Buster Bluth', 3, 0, 2, 1, 50, 1]]

And this is what I want:

new_list = [['Gina Linetti', 7, 4, 0, 3, 300, 15], ['Bruce Wayne', 5, 5, 0, 0, 100, 15], ['Buster Bluth', 3, 0, 2, 1, 50, 1]], ['Johnny Rose', 6, 2, 0, 4, 20, 10], ['Jessica Jones', 12, 0, 6, 6, 10, 6]]

This is what I have so far for the function.

def sort_by_chips(player_list):
largest_chip = 0
highest_chip_list = []

    for player in player_list:
        if player[5] > largest_chip:
            largest_chip = player[5]
    
    for i in range(len(player_list)):
        games_played = player_list[i][1]
        amount_of_chip = player_list[i][5]
        if amount_of_chip >= largest_chip:
            highest_chip_list.append(player_list[i])

    print(highest_chip_list)

    return highest_chip_list

However this only returns the highest list index and nothing else from the list when I want it to go from descending order. I'm not sure how to make it loop through my entire list. Please note that I can't use any list methods or sort functions and I need to return a copy of the list, like what I've done already.

Thank you!

CodePudding user response:

There are so many algorithms for sorting out there, a good exercise for you would be to explore some of them and compare their advantages and disadvantages (performance, efficiency, simplicity, etc..).

Here is a working example for your problem:

def swapPositions(list, pos1, pos2):
    tmp = list[pos1]
    list[pos1] = list[pos2]
    list[pos2] = tmp

def sort_by_chips(player_list):
    newlist = player_list

    print("Starting list: ",newlist)
    for i in range(1, len(player_list)):
        for j in range(0, i):
            if newlist[i][5] > newlist[j][5]:
                swapPositions(newlist, i,j)
    print("Ending list: ",newlist)
    return newlist        

player_list = [['Bruce Wayne', 5, 5, 0, 0, 100, 15], ['Jessica Jones', 12, 0, 6, 6, 10, 6], ['Johnny Rose', 6, 2, 0, 4, 20, 10], ['Gina Linetti', 7, 4, 0, 3, 300, 15], ['Buster Bluth', 3, 0, 2, 1, 50, 1]]
sort_by_chips(player_list=player_list)

CodePudding user response:

Please specify index of chips in your question. Assuming index is 5, I tried to write a code section. It is not a function, but does the job and might give an alternative viewpoint:

player_list = [['Bruce Wayne', 5, 5, 0, 0, 100, 15], \
              ['Jessica Jones', 12, 0, 6, 6, 10, 6], \
              ['Johnny Rose', 6, 2, 0, 4, 20, 10]  , \
              ['Gina Linetti', 7, 4, 0, 3, 300, 15], \
              ['Buster Bluth', 3, 0, 2, 1, 50, 1]]

chip_list = [player[5] for player in player_list]

idx_list = []
for index, chip in enumerate(chip_list): 
    i_num = len(player_list)-1
    for index2, chip2 in enumerate(chip_list):
        if chip > chip2:
            i_num = i_num-1
    idx_list.append(i_num)

final_list = [0 for i in range(len(player_list)) ]
for i,idx in enumerate(idx_list):
    final_list[idx] = player_list[i]
  • Related