Python Tutorial 12: Simple Python Sorting Program

preview_player
Показать описание
You guys can help me out over at Patreon, and that will help me keep my gear updated, and help me keep this quality content coming:

In this video we show step-by-step instructions on how to reorder and sort list from low to high or high to low. I do not assume you are an expert, so these lessons are designed for complete beginners.
#Python
#Lessons
#Programming
Рекомендации по теме
Комментарии
Автор

I folded like a cheap lawn chair AGAIN. But at least i'm learning!

kck.Schrodinger
Автор

I folded like a ch.... I AM LEGEND! I initially thought it was an easy assignment, but it took me a way longer than I expected to get my head around it! It was "most satisfiying" when I got it (no cheating involved, 20 extra lines of code). Thanks again for the great content Paul!

patrickdelvisohopkins
Автор

Thanks Paul, ... as a physicist I always appreciate your approach encouraging all of us to dig deeper in how to approach the problem from the root (even when there are other simpler ways like the sort() and reverse() methods in this case). Keep going... your tutorials are great! Thanks and Best from Italy.

francescoaltamura
Автор

Well, I spent two hours on this one before folding like a cheap Wal-Mart lawn chair. It was interesting to watch my code (that failed) in development. It kept getting longer and longer as I tried inserting more and more logic tests to achieve the desired result. As I began to realize that my approach was never going to get me to the final result, I started pulling out code until I had four broken lines.

I understood your approach as you explained it but I'm not confident I would have ever gotten there on my own. I still don't know why we didn't need to declare swp as a variable before setting equal to grades[1]. I'm going to watch back through your explanation a few more times to see if it settles in.

I'm disappointed I wasn't able to solve it on my own but I hope it's not indicative of my inability to eventually be able to work with Python effectively. I'll keep putting in the work.

RobVollmar
Автор

I JUST MADE IT, 5 hours of trying and not acchieving, made me refine my code and reach enlightment .

dependablesad
Автор

I have to say that I am so glad that I found the Sort function prior to this lesson so that I could fully appreciate the different type of thought process. I was not able to think it through without your help but I did work it through as you did and this helps develop my critical thinking process. Thank You.

hitormissgna
Автор

Years ago I taught this sorting method at a college in England using Basic. When it came to remembering how I did that, my mind got very badly tangled up. Well, it was 40 years ago. My gosh, those kids are in their sixties now! Anyway, I folded like a cheap WalMart lawn chair. Your explanation brought it back to me. Thanks.

kenmohler
Автор

I added the following lines to the progrram we already had:

list1=grades
n=0

for n in range(n, numGrades):
for j in range(n+1, numGrades):
if (list1[n]>list1[j]):
temp=list1[n]
list1[n]=list1[j]
list1[j]=temp
print ('the sorted list is: ', list1)

seems to work fine.

Great series of tutorials, gets my mid 70s brain working, if a litte slowly!!!

semotc
Автор

Legend. Definitely needed to work it out in paper first. Took a minute. Worked it slightly differently, I found the higher grade and swapped it and continued to compare it. So, I worked the highest down. Great Lesson.

LorenBurdette
Автор

This was my eventual solution for Paul's previous assignment. Took me a couple of days...but.. I AM LEGEND! Excited to watch the rest of this vid to see what the most straight forward solution is. Some nested for loops with ever changing iterables.
for i in range(0, numGrades, 1):
MiddleGrade=-100
for i in range(0, numGrades, 1):
if Grades[i]>MiddleGrade and Grades[i]<HighGrade:
MiddleGrade=Grades[i]
PreviousHighGrade=Grades[i]

MiddleGrade=-100
for i in range(0, numGrades, 1):
if Grades[i]>MiddleGrade and Grades[i]<PreviousHighGrade:
MiddleGrade=Grades[i]
HighGrade=Grades[i]


Awesome series Paul! Thanks so much!!!

brandendowning
Автор

Talk about sorting the grades... There were many times growing up where I was the lowest grade in the class room. Just hearing you say that, brought back some of those memories. I some way some how graduated from Engineering school. It was not easy for me, but I would not quit. I had to study my butt off. I think online tutorials really will be the preferred education method in the future. I can go back, review and listen again to previous topics. I rarely catch everything on the first pass through these videos. These videos are awesome. Thank you Paul.

chrisb
Автор

i folded like a lawn chair. I kept thinking about this assignment for a day straight. The only solution i could come up with was to compare the maximum and minimum to the array and if the numbers were not a max or min to save it into a separate array. Then redo the codes to get max and min save them as variables over and over again and print the variables from maximum1-minimum. Love the tutorials and your making me think so hard paul :).

potatoz
Автор

Great lessons from great teacher
Another sorting algorithm:
for i in range (0, gradeNum, 1):
swp=0
for j in range(i, gradeNum, 1):
if grades[j]>swp:
swp=grades[j]
num=j
grades[num]=grades[i]
grades[i]=swp
print(grades)

batticha
Автор

I see the limits of what I did wrong previously Paul! How I must look like the cheapest of lawn chairs, on the second pass I fully appreciate why this sorting program is far stronger.

berryblades
Автор

Great lesson. I was successful with the homework, but did not catch the inefficiencies. I redid the program to eliminate the one excess outer "FOR" loop per the lesson. I further improved the efficiency by having the inner "FOR" loop decrease with each pass of the outer loop. Thanks for pointing out the inefficiency. I love logical problem solving.

wilsonlittle
Автор

Enjoyed the lesson, Paul. Your solution was straightforward and to the point. Have a great week.

opalprestonshirley
Автор

I wont lie, I used sort() then when I started the video and you called me out I had to pause and try with only for loops. TOOK FOREVER but I learned so much through this one. Best teacher alive!

dvstarzzots
Автор

I folded like a cheap walmart lawn chair. I got to the code of grades[i]<grades[i+1] but I couldn't get passed the numGrades-1. nor did I think of having a for loop within a for loop. Such a good learning experience. thank you so much!

andreasa
Автор

I am Legend! Worth the work!! I haven't programmed since the Basic and Fortran days and loving it!!!

ellerycadel
Автор

As always, great lesson, Sir.

I did the homework in another way but it also worked well. I did it by creating a new list and popping items in the main list and adding them to the sorted list in order.

pauivorra