-
Notifications
You must be signed in to change notification settings - Fork 13
/
solution_01.py
30 lines (24 loc) · 996 Bytes
/
solution_01.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
'''
Write a script that sorts a list of tuples based on the number value in the tuple.
For example:
unsorted_list = [('first_element', 4), ('second_element', 2), ('third_element', 6)]
sorted_list = [('second_element', 2), ('first_element', 4), ('third_element', 6)]
'''
unsorted_list = [('first_element', 4), ('second_element', 2), ('third_element', 6)]
sorted_list = []
#while we still have values in the unsorted list
while len(unsorted_list) > 0:
#loop through the unsorted list to find the minimum value
current_value = unsorted_list[0][1]
min_value = unsorted_list[0][1]
index = 0
for tuple_ in unsorted_list:
#save the minimum value to use outside of this for loop
if tuple_[1] <= min_value:
min_value = tuple_[1]
min_index = index
index += 1
#push the minimum value onto the sorted list
sorted_list.append(unsorted_list.pop(min_index))
print("unsorted list is " + str(unsorted_list))
print(sorted_list)