-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Selection sort exercise #7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
groverkds
merged 9 commits into
codebasics:master
from
udistructor:selection-sort-exercise
Nov 2, 2020
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
268310a
Selection Sort Exercise
udistructor 4cbddc3
Minor modification in MD
udistructor 4adc6d9
Fixed CamelCase to snake_case
udistructor a918f5f
Changed multilevel_sort Function to Generalized
udistructor 1ebe3de
Removed Excessive PrintStatements
udistructor ea30df3
Generalized Sort Function
udistructor ae2863e
removed extra data
udistructor 701666e
removed selection sort
udistructor 12fe94b
Updated .md to describe the generalized problem
udistructor File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
# Exercise: Selection Sort | ||
|
||
Implement a Multi-Level Sort of a given list of dictionaries based on a given sorting order. If user wants to sort dictionary based on First Key 'A', Then Key 'B', they shall pass list of keys in the order of preference as a list ['A','B']. Your code should be able to sort list of dictionaries for any number of keys in sorting order list. | ||
|
||
Using this multi-level sort, you should be able to sort any list of dictionaries based on sorting order preference | ||
|
||
Example: | ||
A single dictionary entry contains two keys 'First Name' and 'Last Name'. the list should be sorted first based on 'First Name', then based on 'Last Name', w.r.t. common/same 'First Name' entries. | ||
|
||
for this, one shall past sorting order of preference list [ 'First Name' , 'Last Name' ] | ||
|
||
For this, Given the following sequence List: | ||
|
||
``` | ||
[ | ||
{'First Name': 'Raj', 'Last Name': 'Nayyar'}, | ||
{'First Name': 'Suraj', 'Last Name': 'Sharma'}, | ||
{'First Name': 'Karan', 'Last Name': 'Kumar'}, | ||
{'First Name': 'Jade', 'Last Name': 'Canary'}, | ||
{'First Name': 'Raj', 'Last Name': 'Thakur'}, | ||
{'First Name': 'Raj', 'Last Name': 'Sharma'}, | ||
{'First Name': 'Kiran', 'Last Name': 'Kamla'}, | ||
{'First Name': 'Armaan', 'Last Name': 'Kumar'}, | ||
{'First Name': 'Jaya', 'Last Name': 'Sharma'}, | ||
{'First Name': 'Ingrid', 'Last Name': 'Galore'}, | ||
{'First Name': 'Jaya', 'Last Name': 'Seth'}, | ||
{'First Name': 'Armaan', 'Last Name': 'Dadra'}, | ||
{'First Name': 'Ingrid', 'Last Name': 'Maverick'}, | ||
{'First Name': 'Aahana', 'Last Name': 'Arora'} | ||
] | ||
``` | ||
|
||
|
||
Your algorithm should generate sorted list: | ||
|
||
``` | ||
[ | ||
{'First Name': 'Aahana', 'Last Name': 'Arora'} | ||
{'First Name': 'Armaan', 'Last Name': 'Dadra'} | ||
{'First Name': 'Armaan', 'Last Name': 'Kumar'} | ||
{'First Name': 'Ingrid', 'Last Name': 'Galore'} | ||
{'First Name': 'Ingrid', 'Last Name': 'Maverick'} | ||
{'First Name': 'Jade', 'Last Name': 'Canary'} | ||
{'First Name': 'Jaya', 'Last Name': 'Seth'} | ||
{'First Name': 'Jaya', 'Last Name': 'Sharma'} | ||
{'First Name': 'Karan', 'Last Name': 'Kumar'} | ||
{'First Name': 'Kiran', 'Last Name': 'Kamla'} | ||
{'First Name': 'Raj', 'Last Name': 'Nayyar'} | ||
{'First Name': 'Raj', 'Last Name': 'Sharma'} | ||
{'First Name': 'Raj', 'Last Name': 'Thakur'} | ||
{'First Name': 'Suraj', 'Last Name': 'Sharma'} | ||
] | ||
``` | ||
|
||
|
||
|
||
|
||
|
||
[Solution](https://github.com/codebasics/data-structures-algorithms-python/blob/master/algorithms/7_SelectionSort/selection_sort_exercise_solution.py) |
33 changes: 33 additions & 0 deletions
33
algorithms/7_SelectionSort/selection_sort_exercise_solution.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
def multilevel_selection_sort(elements, sort_by_list): | ||
for sort_by in sort_by_list[-1::-1]: | ||
for x in range(len(elements)): | ||
min_index = x | ||
for y in range(x, len(elements)): | ||
if elements[y][sort_by] < elements[min_index][sort_by]: | ||
min_index = y | ||
if x != min_index: | ||
elements[x], elements[min_index] = elements[min_index], elements[x] | ||
|
||
|
||
if __name__ == '__main__': | ||
elements = [ | ||
{'First Name': 'Raj', 'Last Name': 'Nayyar'}, | ||
{'First Name': 'Suraj', 'Last Name': 'Sharma'}, | ||
{'First Name': 'Karan', 'Last Name': 'Kumar'}, | ||
{'First Name': 'Jade', 'Last Name': 'Canary'}, | ||
{'First Name': 'Raj', 'Last Name': 'Thakur'}, | ||
{'First Name': 'Raj', 'Last Name': 'Sharma'}, | ||
{'First Name': 'Kiran', 'Last Name': 'Kamla'}, | ||
{'First Name': 'Armaan', 'Last Name': 'Kumar'}, | ||
{'First Name': 'Jaya', 'Last Name': 'Sharma'}, | ||
{'First Name': 'Ingrid', 'Last Name': 'Galore'}, | ||
{'First Name': 'Jaya', 'Last Name': 'Seth'}, | ||
{'First Name': 'Armaan', 'Last Name': 'Dadra'}, | ||
{'First Name': 'Ingrid', 'Last Name': 'Maverick'}, | ||
{'First Name': 'Aahana', 'Last Name': 'Arora'} | ||
] | ||
|
||
print(f'Given unsorted array:', *elements, sep='\n') | ||
multilevel_selection_sort( | ||
elements, ['First Name', 'Last Name']) | ||
print(f'Array after Multi-Level Sorting:', *elements, sep='\n') |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
multilevel_selection_sort(
elements, ['First Name', 'Last Name']
)