-
Notifications
You must be signed in to change notification settings - Fork 2
Create selectionsort.py #2
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
base: main
Are you sure you want to change the base?
Conversation
Added selection sort algorithm code to data structure
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.
Pls check my comments
@@ -0,0 +1,40 @@ | |||
# Here we use dictionary data structure to store our count of values mapped to respective names in the above photos | |||
singers={"Selena Gomez": 112, "Taylor Swift": 178,"Ed Sheeran": 230,"Justin Bieber": 90,"John Legend": 190,"Beyoncé": 80} |
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.
please write global variable in
if name == 'main':
you can check in Jan's Binary Tree code.
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.
Will do and I searched for why is it necessary to add this, but nothing could clearly show me the reason. Could you please tell me why we should include this in ,sis?
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.
I updated if name == 'main':, but I wonder if the place I put in is correct
singers={"Selena Gomez": 112, "Taylor Swift": 178,"Ed Sheeran": 230,"Justin Bieber": 90,"John Legend": 190,"Beyoncé": 80} | ||
|
||
# We then convert dictionary to list of only count values and in singers_list, it will be like this[112,178,230,90,190,80] | ||
singers_list=list(singers.values()) |
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.
same as first one
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.
Oh so, it's needed to add if name == 'main': two times?
|
||
# Starting from here, we define a function for selection sort | ||
def selectionSort(singers_list): | ||
#This is an empty list which will later be filled with sorted counting values |
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.
write docstring instead of comments
Please check Jan's code.
docstring should follow this format
'''
description of the function
INPUT:
OUPUT:
'''
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.
As my comments are a lot, would it be fine if I added every line of comments in docstring?
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.
You don't need to write all. Just descriptive explanation is ok.
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.
Example docstring
'''
Sort the array by finding the largest elements and swapping the elements according to the index.
INPUT:
singer_list: list of singers corresponding to play counts
OUTPUT:
sorted_singer_lsit: list of sorted singer lists by play counts
'''
return singers_rating | ||
# You remember largest variable above that calls for this function? Here we define it with the following code | ||
def findLargest(singers_list): | ||
#We assume the first value of singers' list as the largest value |
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.
write docstring instead of comments
Make a short description. You don't need to explain everything in the function in detail. You can just describe some points which might be confusing.
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.
Yes, sis I thought I could do . But I am concerned about other beginners who don't know at all about code. So, I took a lot of time to write line by line. How do you want to further suggest me?
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.
we should follow the correct coding style. As long as the code is clear enough, they will understand.
Check out the coding styles of python.
https://www.python.org/dev/peps/pep-0008/
https://www.python.org/dev/peps/pep-0257/
#After updating our largest_index, return it to the function. Don't forget to look back at two skipped lines above :xD | ||
return largest_index | ||
#After applying selection sort algorithm, we add it to the variable name called favorite_singers | ||
favorite_singers=selectionSort(singers_list) |
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.
call function from
if name == 'main':
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.
Done
# Then we print favorite_singers and get the following output | ||
print(favorite_singers) | ||
|
||
Output:[230, 190, 178, 112, 90, 80] |
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.
what does this output mean? Please remove this if not necessary.
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.
It's sth code testers will get after they have run code in the editor. I think I had better write it in comment.
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.
They will get the output when they run. You can write the output in docstring output if you wish to.
For example.
'''
....
OUTPUT:
sorted_list: .....
Output [230, 190, 178, 112, 90, 80]
'''
#We call findLargest function and search for largest value in the sigers' list and I recommend to look first at next third line | ||
largest = findLargest(singers_list) | ||
#We got the largest value by calling findLargest function below and we afterwards remove that largest value from singers_list and then add to our empty list declared above | ||
singers_rating.append(singers_list.pop(largest)) |
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.
you can simply add singers_list[index] -> pop is not required here.
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.
sis, all this code exists in selection sort chapter of Grokking algorithm. What I changed is only names. Every line of code is the same
Here is the source code
def findSmallest(arr):
smallest = arr[0] Stores the smallest value
smallest_index = 0 Stores the index of the smallest value
for i in range(1, len(arr)):
if arr[i] < smallest:
smallest = arr[I]
smallest_index = I
return smallest_index
Now you can use this function to write selection sort:
def selectionSort(arr): Sorts an array
newArr = []
for i in range(len(arr)):
smallest = findSmallest(arr)
newArr.append(arr.pop(smallest))
return newArr
print selectionSort([5, 3, 6, 2, 10])
#As we consider the first value of singers' list as the largest value, we make it as the largest index of 0 | ||
largest_index = 0 | ||
#We start from index 1 to the end within the length of singers' list | ||
for i in range(1, len(singers_list)): |
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.
you don't need to compare everything from the 1 to the last index.
what you need to do is compare i+1 to the last index because 0 to 1 assumes to be sorted after one iteration is over.
#After applying selection sort algorithm, we add it to the variable name called favorite_singers | ||
favorite_singers=selectionSort(singers_list) | ||
# Then we print favorite_singers and get the following output | ||
print(favorite_singers) |
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.
if you want to give the variable names 'favourite_singers', the output should be singer names.
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.
Yes, I wanna output singers' names instead of list of values. I am still finding ways to do it ,sis. But if you have recommendation on how to do it, I'm looking forward to it.
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.
if you use a dictionary, instead of an array, you need to sort the dictionary instead of a list. (key-value pair)
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.
you can write lambda function to sort of key-value pair.
Added selection sort algorithm code to data structure and algorithm. Looking forward to feedback.