I need some help, how to use the algorithm selection sort to sort a list by the values of a dict. I wrote some code but I don't know how continue, that the code work.
months = {"January": 1, "February": 2, "March": 3, "April": 4, "May": 5, "June": 6, "July": 7, "August": 8, "September": 9, "October": 10, "November": 11, "December": 12}
L = ["March", "January", "December"]
eg sort the list by the values of the dict
def month(L):
for i in months:
minpos = i
for j in range (months[i], len(L)):
if months[L[j]] > months[minpos]:
months[minpos] = months[L[j]]
L[j], L[minpos] = L[minpos], L[i]
return L
Your code:
months = {"January": 1, "February": 2, "March": 3, "April": 4, "May": 5, "June": 6, "July": 7, "August": 8, "September": 9, "October": 10, "November": 11, "December": 12}
L = ["March", "January", "December"]
Convert month name(str) in L to corresponding number, then sort it.
num_L = sorted( [months[month] for month in L] )
Convert sorted number to corresponding month name.
res = [k for number in num_L for k,v in months.items() if number == v]
Maybe you need some string operations just like uppercase() or lower() before matching month name.
New Answer
selection_sort
which has get_sorted()
method that will sort the list based on dictionary values using selection sort.class selection_sort:
"""Sort the list based on dictionary value using selection sort."""
def __init__(self,L):
self.months = {"January": 1, "February": 2, "March": 3, "April": 4,
"May": 5,
"June": 6, "July": 7, "August": 8, "September": 9,
"October": 10, "November": 11, "December": 12}
self.L = L
def get_sorted(self):
""" sorting list."""
month_num = [self.months[i] for i in self.L]
flag = True # set Flag is True to enter the loop.
while flag:
flag = False # set Flag to False for no swaping done
for i in range(len(month_num)-1):
if month_num[i] > month_num[i+1]:
flag = True # set Flag to False if no swaping is done
# swap
month_num[i], month_num[i+1] = month_num[i+1], month_num[i]
L = [k for k, v in self.months.items() if v in month_num]
print(L)
return L
Test Case
Test which you have written is comparing L
with Expected
. It should compare actual
with expected
. self.assertEqual(L, expected)
Also, expected = ["March", "May", "December", "October", "September"]
is incorrect. It should have been ["March", "May","September", "October", "December"]
import unittest
from selectionSort import selection_sort
# @unittest.skip("")
class TestInPlace(unittest.TestCase):
def test_1(self):
L = ["December", "September", "March", "October", "May"]
obj = selection_sort(L)
actual = obj.get_sorted()
expected = ["March", "May","September", "October", "December"]
self.assertEqual(actual, expected)
Old Answer
Try this,
#creating list L1
L1 = [months[L[i]] for i in range(len(L))]
print(L1)
#sorting list using algorithm selection sort
for i in range(1,len(L1)):
j = 0
if L1[j] > L1[i] :
L1[j], L1[i] = L1[i] ,L1[j]
print(L1)
#Replacing values with key
sorted_list = [ k for k,v in months.items() for i in L1 if i == v]
print(sorted_list)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.