Python Challenge - Top Score Sorter

import secrets def is_sorted_desc(arr): arr_len = len(arr) if arr_len < 2: return True i = 0 while i < (arr_len - 1): if arr[i] < arr[i+1]: return False i += 1 return True def randomize(arr): rnd = secrets.SystemRandom() arr_len = len(arr) sorted_arr = [] arr_cpy = arr.copy() i = 0 while i < arr_len: sorted_arr.append(arr_cpy.pop(rnd.randint(0, len(arr_cpy)-1))) i += 1 return sorted_arr def random_sort(arr, top_score): sorted_arr = arr counter = 0 while not is_sorted_desc(sorted_arr): sorted_arr = randomize(arr) counter += 1 print("Sorted after {0} tries!".format(counter)) return sorted_arr score_list = [1, 2, 3, 9999, 13] top = 10000 print(random_sort(score_list, top))

Ladies and gents, I present to you random_sort(). And yes , it does exactly what it sounds like. random_sort() takes an array as input, reassembles its elements in random order and checks if it’s sorted. If not: repeat.

Simple and beautiful. No other sorting algorithm can be explained this fast. And it works! At least, stochastic tells us it should always terminate eventually. Just don’t let the input arrays get too long if you’re an impatien person… :upside_down_face:


def score_sorter(array, top_score):
  # Write your code here
  idx = 0
  while idx < len(array):
    for num in range(0,len(array), 1):
      if array[idx] > array[num]:
        array[idx], array[num] = array[num], array[idx]
    idx+=1
  return array


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

print(score_sorter(score_list, top))

def score_sorter(array, top_score):
ls = [n for n in array if n <= top_score]
return sorted(ls, reverse=True

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

print(score_sorter(score_list, top))

def score_sorter(array, top_score):
  # Write your code here
  new_list = []
  for num in range(len(array)):
    x = 0
    y = 0
    for num in array:
      if num >= y:
        y = num
    new_list.append(y)
    array.remove(y)
  return new_list

def score_sorter(array, top_score):
  sorted_scores = []
  while len(array) > 0:
    highest_index = 0
    highest_val = 0
    for i in range(len(array)):
      if array[i] > highest_val:
        highest_val = array[i]
        highest_index = i
    sorted_scores.append(array.pop(highest_index))
  return sorted_scores

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 new_score_list = [] for i in range(0,len(array)): new_score_list.append(max(array)) array.remove(max(array)) return new_score_list score_list = [1, 2, 3, 9999, 13] top = 10000 print(score_sorter(score_list, top))

This doesn’t filter out any scores that exceed the top allowed score. Or did I misunderstand the problem?

My code only passed 4 out of 5 tests. What seems to be the problem? It would be helpful if the site would tell you in what tests your code failed, unless it does show you but I don’t know how to view it.

def score_sorter(array, top_score): for score in array: if score > top: array.remove(score) for i in range(len(array)): for j in range(len(array)): if array[j] < array[i]: array[i], array[j] = array[j], array[i] return(array) score_list = [1, 2, 3, 99999, 13] top = 10000 print(score_sorter(score_list, top))

My other code worked but it is much more hideous:

def score_sorter(array, top_score): # define maximum score (below top allowed score) max_score = 0 sorted_array = [] # Stop once array is empty while len(array) > 0 : # Iterate through each score in array for score in array: # Remove score if exceeds top score if score > top_score: array.remove(score) for score in array: # If score is larger than the last max_score if score > max_score: max_score = score # Remove the maximum score from the original array # to compare the rest of the scores with each other if max_score in array: array.remove(max_score) # Add the max score to another array which will be sorted sorted_array.append(max_score) # Remember to set max_score back to 0, otherwise # the max_score will always be the largest number # so far and code won't sort max_score = 0 return(sorted_array) score_list = [1, 2, 3, 9909999999, 13] top = 10000 print(score_sorter(score_list, top))

Quite creative! I like it

bro you complicated things

1 Like
def score_sorter(array, top_score): from numpy import Inf arr = [] for n_iter in range(len(array)): max_n = -Inf for num in array: if num > max_n: i = array.index(num) max_n = num array.pop(i) arr.append(max_n) return arr score_list = [1, 2, 3, 9999, 13] top = 10000 print(score_sorter(score_list, top))
def score_sorter(array, top_score): l = [] #defining empty list for i in range(top_score): #counting up towards top_score if i in score_list: #checks for appearance in array #this block is needed if there are duplicate values for j in range(array.count(i)): l.append(i) return l[::-1] #reverse needed since we are appending from 0 to top_score score_list = [1, 3, 3, 9999, 13] top = 10000 print(score_sorter(score_list, top))

This code works, but it seems the runtime is too long since this got 0/5 tests…
However it is a very simple solution

Easy and straight to the point

def score_sorter(array, top_score): # Write your code here score_sorter = []; while array != []: score_sorter.append(max(array)); array.remove(max(array)); return score_sorter score_list = [1, 2, 3, 0, 3, 9999, 13] top = 10000 print(score_sorter(score_list, top))
def score_sorter(array, top_score): new_list = [] while array != []: new_list.append(max(array)) array.remove(max(array)) return new_list score_list = [1, 2, 3, 9999, 13] top = 10000 print(score_sorter(score_list, top))

time: O(n)
space O(n)

I think the time for that is O(n^2) since
max(array) is O(n)
and the while loop iterates ‘n’ times.

I think you’re right good catch

def score_sorter(array, top_score): # Write your code here sorted_list = [] i = 0 for count in range(0,top_score + 1): for num in array: if top_score - num == i: sorted_list.append(num) i += 1 return sorted_list 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

#Liste des éléments triés
scores_sorted =
array_to_sort = array
length = len(array)
iterate = 0
score = 0
for i in range(length-iterate):
score = array[0]
for j in range(len(array)):
# if score is lower than other scores, then we get the value
if score <= array[j]:
score = array[j]
# If score greater than top_score, then it equals top_score
if score > top_score:
scores_sorted.append(top_score)
else:
scores_sorted.append(score)

array.remove(score)
iterate += 1

return scores_sorted

score_list = [1, 2, 3, 9999, 13]
top = 10000
print(score_sorter(score_list, top))

Solved using bubble sort:

def score_sorter(array, top_score):
isSorted = False
while not isSorted:
isSorted = True
for i in range(1, len(array)):
if array[i] < array[i - 1]:
swap(i, i - 1, array)
isSorted = False
return array[::-1]

def swap(i, j, array):
array[i], array[j] = array[j], array[i]

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

print(score_sorter(score_list, top))

def score_sorter(array, top_score): listy = array listz = [] while len(listy) != 0: if max(listy) > top_score: listy.remove(max(listy)) continue listz.append(max(listy)) listy.remove(max(listy)) return listz score_list = [1, 2, 3, 9999, 13] top = 10000 print(score_sorter(score_list, top))