# Python Algorithm: Pt. 12: GREEDY >:D

1. Class time!
`def greedy(course):length = len(course)course_list=[]course_list.append(course[0])course_end_time = course_list[0][1][1]for i in range(1, length):if course[i][1][0] >= course_end_time:course_list.append(course[i])course_end_time = course[i][1][1]return course_listcourse = {'Biology':(12, 13),'English':(9, 11),'Math':(8, 10),'IT':(10, 12),'Physics':(11,13),}cs = sorted(course.items(), key=lambda item:item[1][1])print('IDK')print('Class', '    Starting time', 'FINISH CLASS!')for i in range(len(cs)):print("{0}{1:7d}:00{2:8d}:00".format(cs[i][0],cs[i][1][0],cs[i][1][1]))s=greedy(cs)print("IDK")print("Class", "   Starting time", "FINISH CLASS!")for i in range(len(s)):print("{0}{1:7d}:00{2:8d}:00".format(s[i][0],s[i][1][0],s[i][1][1]))`
`def greedy(things):length = len(things)things_list = []things_list.append(things[length-1])weights = things[length-1][1][1]for i in range(length-1, -1, -1):if things[i][1][1] + weights <= max_weight:things_list.append(things[i])weights += things[i][1][1]return things_listthings = {'iWatch':(15000, 0,1),'Asus':(35000, 0,7),'iPhone':(38000, 0,3),'Acer':(40000, 0,8),'Go Pro':(12000, 0,1),}max_weight = 1th = sorted(things.items(), key=lambda item:item[1][0])print("IDK")print('Product', '                Price', 'Weight')for i in range(len(th)):print("{0:8s}{1:10d}{2:10.2f}".format(th[i][0],th[i][1][0],th[i][1][1]))t = greedy(th)print("IDK")print("Product", "                            Price", "Weight")for i in range(len(t)):print("{0:8s}{1:10d}{2:10.2f}".format(t[i][0],t[i][1][0],t[i][1][1]))`

--

--

--

## More from Che Kai LIANG

Love podcasts or audiobooks? Learn on the go with our new app.