Python Challenge - Top Score Sorter

This community-built FAQ covers the “Top Score Sorter” code challenge in Python. You can find that challenge here, or pick any challenge you like from our list.

Top Discussions on the Python challenge Top Score Sorter

There are currently no frequently asked questions or top answers associated with this challenge – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this challenge. Ask a question or post a solution by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this challenge, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!
You can also find further discussion and get answers to your questions over in #get-help.

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head to #get-help and #community:tips-and-resources. If you are wanting feedback or inspiration for a project, check out #project.

Looking for motivation to keep learning? Join our wider discussions in #community

Learn more about how to use this guide.

Found a bug? Report it online, or post in #community:Codecademy-Bug-Reporting

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

def score_sorter(array, top_score):
  # simulate a do...while loop
  changed = True
  while changed:
    changed = False
    for item in enumerate(array):
      # check if the element is higher than the top score and remove it if true
      if array[item[0]] > top_score:
        del(array[item[0]])
        continue
      # this check is not necessary for the first element
      if item[0] > 0:
        # get the value of the previous element
        previous = array[item[0]-1]
        # get the value of the current element
        current = item[1]
        if current < previous:
          # replace previous element with the current, change location
          array[item[0]-1] = current
          array[item[0]] = previous
          # set the changed flag to true so the main loop starts again
          changed = True
  # return elements of array in reverse order
  return array[::-1]

score_list = [1, 200, 2, 799, 3,, 3000, 9999, 13]
top = 1000

print(score_sorter(score_list, top))
def score_sorter(array, top_score):
  r = [*range(len(array) -1, -1, -1)]
  for i in range(len(array)):
    r.pop()
    for j in r:
      if array[i] < array[j]:
        array[i], array[j] = array[j], array[i]
  return array

score_list = [1, 2, 3, 9999, 13]
top = 10000

print(score_sorter(score_list, top))
def score_sorter(array, top_score): for i in range(1, len(array)): value = array[i] j = i - 1 while j >= 0 and value < array[j]: array[j + 1] = array[j] j -= 1 array[j + 1] = value return list(reversed(array)) score_list = [1, 2, 3, 9999, 13] top = 10000 print(score_sorter(score_list, top))

Now seeing other codes, mine is waay too simple :grimacing:

def score_sorter(array, top_score): sorted_ar = sorted(array, reverse=True) return sorted_ar score_list = [1, 2, 3, 9999, 13] top = 10000 print(score_sorter(score_list, top))

And also did not implement your own sort algorithm. If memory serves, this challenge specifically states to not use a built sort algo. Give it another try and pass the tests. DM me when you are ready to swap out this one with a real one.

This seems to do the job without using .sort() or sorted() by using recursion, but I don’t know if that’s good practice…

def score_sorter(array, top_score): # Write your code here sorted_array = [] def get_max(): while len(array) > 0: top = max(array) sorted_array.append(top) array.pop(array.index(top)) get_max() get_max() return sorted_array score_list = [1, 2, 3, 9999, 13] top = 10000 print(score_sorter(score_list, top))
# Insertion sort algorithm. O(n^2). def score_sorter(array, top_score): for i in range(0, len(array)): while i > 0 and array[i-1] < array[i]: array[i], array[i-1] = array[i-1], array[i] i -= 1 return array score_list = [1, 2, 3, 9999, 13] top = 10000 print(score_sorter(score_list, top))

I was wondering why my loop doesn’t continue for all the numbers in the array. I’m only able to return a few of the scores before it stops, why is that?

def score_sorter(array, top_score):
  x = []
  for i in array:
    a = max(array)
    x.append(a)
    array.remove(a)
  return(x)    

score_list = [1, 2, 3, 9999, 13, 45]
top = 10000

print(score_sorter(score_list, top))

Hello,

I was able to pass the test using a combination of pop() and max(), but I am not sure why my first solution is not passing. Can anyone give me some insight? Thanks!

Passing code

def score_sorter(array, top_score): output = [] while len(array) > 0: output.append(max(array)) array.pop(array.index(max(array))) return output score_list = [1, 2, 3, 9999, 13] top = 10000 print(score_sorter(score_list, top))

Failing code

def score_sorter(array, top_score): output = [] for i in range(top_score, 0, -1): if i in array: for j in range(array.count(i)): output.append(array[array.index(i)]) return output score_list = [1, 2, 3, 9999, 13] top = 10000 print(score_sorter(score_list, top))

It seems like this question is also asking for a test to see if any scores in the array are greater than top_score. Not many solutions in this thread include that test, since the sample array only includes scores less than the top score. It seems to me the question is a little confusing and needs clarification.

def score_sorter(array, top_score): # Write your code here valid_scores = [score for score in array if score < top_score] sorted_scores = [] for score in range(len(valid_scores)): sorted_scores.append(max(valid_scores)) valid_scores.remove(max(valid_scores)) return sorted_scores score_list = [1, 2, 3, 9999, 13, 9998, 9998, 101, 101, 101, 98] top = 100 print(score_sorter(score_list, top))
1 Like

I was stuck with this problem as well, realized the bug.

The array is changing as the loop increments. Since values are being removed, the loop does not complete for each value in the original array.

Try

for i in range(len(array)):

def score_sorter(array, top_score): arr = array list = [] max = 0 for n in range(len(array)): for i, j in enumerate(arr): if j >= max: max = j arr.remove(max) list.append(max) max = 0 return list score_list = [1, 2, 3, 9999, 13] top = 10000 print(score_sorter(score_list, top))

you should consider learning some algorithms like bubble_sort and binary search

Insertion sort:

def score_sorter(array, top_score): for i in range (1, len(array)): placeholder = array[i] j = i - 1 while array[j] < placeholder and j >= 0: array[j+1] = array[j] j -= 1 array[j+1] = placeholder return array score_list = [1, 2, 3, 9999, 13] top = 10000 print(score_sorter(score_list, top))

Any suggestions for improvements?

def sort(inpot): outpot=[] for n in range(len(inpot)): outpot.append(inpot[n]) i = len(outpot) - 1 while i >= 1 and outpot[i] <= outpot[i-1]: temp = outpot[i] outpot[i] = outpot[i - 1] outpot[i - 1] = temp i -= 1 return outpot def score_sorter(array, top_score): # Write your code here a=(sort(array)) L = len(a) for i in range(int(L / 2)): n = a[i] a[i] = a[L - i - 1] a[L - i - 1] = n return a score_list = [1, 2, 3, 9999, 13] top = 10000 print(score_sorter(score_list, top))

def score_sorter(array, top_score):
count = len(array)
reversed_list =
while len(reversed_list) != count :
for number in array :
if number == max(array) :
reversed_list.append(number)
array.remove(number)
elif number != max(array) :
continue

return reversed_list

score_list = [1, 2, 3, 9999, 13]
top = 1

print(score_sorter(score_list, top))

def score_sorter(array, top_score): count = len(array) reversed_list = [] while len(reversed_list) != count : for number in array : if number == max(array) : reversed_list.append(number) array.remove(number) elif number != max(array) : continue return reversed_list score_list = [1, 2, 3, 9999, 13] top = 1 print(score_sorter(score_list, top))

I also have the same problem with you. I think that maybe the question is not really clear related to its top_score parameter.

def score_sorter(array, top_score): for num in array: for x in range(1, len(array)): if array[x] > array[x-1]: temp = array[x] array.pop(x) array.insert(x - 1, temp) return array score_list = [1, 2, 3, 9999, 13] top = 10000 print(score_sorter(score_list, top))
def score_sorter(array, top_score): # Write your code here sorted_list = list() while array: max_num = max(array) sorted_list.append(max_num) array.remove(max_num) return sorted_list score_list = [1, 2, 3, 9999, 13] top = 10000 print(score_sorter(score_list, top))