>>separate the original list to different sublist
initialist a empty list: new_list
add the first number of orginal list into new_list
for each number in the original list:
if number equals to the first element of any sublist:
add number to that sublist
else:
add number to an new empty sublist
code
defadvanced_sort(original_list:list)->list:# create a list with the first element of the original_list
new_list=[[original_list[0]]]forindex,iteminenumerate(original_list):ifindex==0:continuenot_added=True# To iterate over every sublist in the new_list
forsublist_index,sublistinenumerate(new_list):# try:
# if item is the same with first element of the sublist of new list and not already added
ifitem==sublist[0]andnot_added:# add item to that sublist
sublist.append(item)not_added=False# if no same item appear after checking the whole new list and not already added
ifitem!=sublist[0]andsublist_index==len(new_list)-1andnot_added:# add the item the end of the new_list
new_list.append([item])not_added=Falsereturnnew_list
>>transform the original list into a set, thus only unique item will appear
>>rearrange the transformed set by the index order
>>count how many time each unique appear in the original list
>>form a new list by putting in each item times their occurance in a sublist
My reflection
about while loop
the loop will not instantly end when the boolean condition is changed to false
need to use if condition to control the execution of code
Just admire people can write short code in elegant way
Top comments (0)