Python Tutorial 12: Simple Python Sorting Program

  Рет қаралды 24,476

Paul McWhorter

Paul McWhorter

Күн бұрын

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:
/ paulmcwhorter
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

Пікірлер: 241
@kck.Schrodinger13
@kck.Schrodinger13 2 жыл бұрын
I folded like a cheap lawn chair AGAIN. But at least i'm learning!
@patrickdelvisohopkins
@patrickdelvisohopkins 3 жыл бұрын
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!
@ICit123
@ICit123 3 жыл бұрын
There are many ways to write code. Write this at the end of your averageGrades.py: Another way to sort the highest to lowest is to write: grades.sort(). Next line: print(grades). To reverse the grades write: grades.sort(reverse = True). Next line: print(grades)
@graydhd8688
@graydhd8688 Жыл бұрын
@@ICit123 Just doing that though doesn't do anything to teach the logic behind what's happening, that would just be a function right?
@opalprestonshirley1700
@opalprestonshirley1700 3 жыл бұрын
Enjoyed the lesson, Paul. Your solution was straightforward and to the point. Have a great week.
@paulmcwhorter
@paulmcwhorter 3 жыл бұрын
Thanks, you too!
@francescoaltamura6541
@francescoaltamura6541 3 жыл бұрын
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.
@paulmcwhorter
@paulmcwhorter 3 жыл бұрын
Excellent!
@RobVollmar
@RobVollmar 2 жыл бұрын
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.
@EFoxVN
@EFoxVN 2 жыл бұрын
What you went through was completely normal. I spent a complete public holiday last year trying to figure out a similar sorting mechanism. I had to watch two videos, and then had to learn how important the right indexes are the hard way. So it is definitely not an indication of any inability. Cheers!
@426F6F
@426F6F 9 ай бұрын
I'm completely new to programming, but I feel like it's just one of those things where you need to at least give it a shot just to get into the habit of thinking more logically. You're not always going to be able to solve every problem on your own, but you get better at it with time!
@hitormissgna
@hitormissgna 2 жыл бұрын
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.
@paulmcwhorter
@paulmcwhorter 2 жыл бұрын
Excellent!
@eddiebelzebuth
@eddiebelzebuth 2 жыл бұрын
Paul, this was another fantastic lesson, thank you! Thanks for all your hard work. Needless to say I folded. I had to watch it twice to fully understand your explanation.
@pauivorra1819
@pauivorra1819 3 жыл бұрын
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.
@paulmcwhorter
@paulmcwhorter 3 жыл бұрын
Great job!
@chrisb7250
@chrisb7250 9 ай бұрын
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.
@soufianeerrami2507
@soufianeerrami2507 2 жыл бұрын
Thank you so much uncle Paul, I really enjoy your classes like I always enjoyed your legendary arduino course.
@shuttlepodone1707
@shuttlepodone1707 3 жыл бұрын
Wow this was great! I had all kinds of crazy ideas and tried making a new list that would be in order which totally failed. I really like your homework assignment of doing this on my own. I'm going to do exactly that with different variable names to help mix things up a bit so I better understand the logic. I like it when you show your thought process when you "write" on the graph paper program.
@josmeijer1081
@josmeijer1081 2 жыл бұрын
I am Legend! It took a lot longer than I expected. I enjoy your Python tutorials very much, thank you for your effort.
@neilausten9404
@neilausten9404 3 жыл бұрын
This is the way I did it. for i in range(0,numGrades-1,1): for j in range(i+1,numGrades,1): if grades[i]
@AazamPossum
@AazamPossum 3 жыл бұрын
Thanks. I wasn't able to figure it out even with the solution somehow. This worked and then I studied what it meant. Made sense in more than a while though lol.
@martinjones1390
@martinjones1390 Жыл бұрын
I discovered you tutorials yesterday and have been having a bit of a binge. The last time I coded any sorting logic was on a Perkin Elmer minicomputer nearly 40 years ago, and in COBOL!! I needed to be able to do table lookups quickly against a large array so I sorted the array and hand-coded a binary search lookup to locate table elements. (Our compiler didn't have the extensions to allow a binary search.) Your tutorials are very well presented.
@alfredcheung6704
@alfredcheung6704 2 жыл бұрын
Thanks Paul. This is a very comprehensive and useful video.
@mohsenr4434
@mohsenr4434 2 жыл бұрын
I didn't understand much the first time watching but I took my time thinking like you asked and watching the lesson for the second time completely helped me understand it. Thank you Paul, you're the best.
@paulmcwhorter
@paulmcwhorter 2 жыл бұрын
Excellent!
@potatoz1137
@potatoz1137 2 жыл бұрын
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 :).
@RobVollmar
@RobVollmar 2 жыл бұрын
This is very similar to the approach that I took...which also did not work lol
@pennyshen5269
@pennyshen5269 Жыл бұрын
@@RobVollmar me too hahah but i guess u are enjoying this right😁
@sem1ot1c
@sem1ot1c 3 жыл бұрын
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!!!
@TheKverbeeck
@TheKverbeeck 3 жыл бұрын
Another great tutorial Paul, keep up the great work. 👍👍👍👍
@paulmcwhorter
@paulmcwhorter 3 жыл бұрын
Thanks 👍
@chrisingram7277
@chrisingram7277 3 жыл бұрын
I did use the sort method for last week's homework, I don't feel too bad as I didn't even know it existed a week ago:) Now I'll try again designing my own sort technique.
@andreasa1293
@andreasa1293 3 жыл бұрын
I folded like a cheap walmart lawn chair. I got to the code of grades[i]
@ricardobjorkeheim775
@ricardobjorkeheim775 3 жыл бұрын
Great content Paul! I put print("grades = ",grades) inside the IF statement and it shows step by step how the numbers move through the array. Then if you want it to do the same as the command grades.sort() I change the if statement to the opposite like this if grades[i+1] < grades[ i ]: Is much easier to understand if you print the array value every step in the For loop.
@dvstarzzots
@dvstarzzots Жыл бұрын
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!
@paulmcwhorter
@paulmcwhorter Жыл бұрын
Now go back and do it without sort. The whole thing is to learn to think through these tough challenges. The sort program is a classic problem and you have to be able to think through it.
@dvstarzzots
@dvstarzzots Жыл бұрын
@@paulmcwhorter I paused it before you gave the answer and worked it out. I absolutely love the homework you give. I refuse to move onto the next lesson till I figure out your homework assignment
@ellerycadel
@ellerycadel Жыл бұрын
I am Legend! Worth the work!! I haven't programmed since the Basic and Fortran days and loving it!!!
@ricklawitzky8311
@ricklawitzky8311 2 жыл бұрын
This time, I folded up like a cheap Walmart lawn chair. But luckily I have understood your code, so I am now able to sort a list! Thank you for this enrichment!
@ericracine7472
@ericracine7472 3 жыл бұрын
I wrote a program that counted from 0 to the highest possible grade. Whenever a note value was reached I saved it in a new array. That worked until I tried 11 digit numbers. Had a lot of fun thank you Paul!
@kenmohler4081
@kenmohler4081 Жыл бұрын
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.
@paulmcwhorter
@paulmcwhorter Жыл бұрын
The 'sort' is a classic programming problem, and is tougher than what one might think, and takes some real thought. I really think this type of thinking expands the mind, and keeps the mind sharp. I love as I am getting older, doing these projects keeps my mind sharper than ever.
@brandendowning1714
@brandendowning1714 Жыл бұрын
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]MiddleGrade and Grades[i]
@thebrector3823
@thebrector3823 2 жыл бұрын
I folded up like a cheap lown chair!!, but tried for about an hour when I got tired!! Thanks a lot, sir!!!!!!
@LorenBurdette
@LorenBurdette Жыл бұрын
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.
@paulmcwhorter
@paulmcwhorter Жыл бұрын
Nice work!
@ecassar1975
@ecassar1975 3 жыл бұрын
After a lot of head scratching I managed to sort the list, however I used a while loop and an if/else function instead of two for loops like you did. Every time it goes through the if function because the grades are not in the correct order, [i] will be reset to zero, while every time it goes through the else function [i] gets incremented by 1 because that means the grades are in the correct order. This goes on until all grades have been sorted and so the while loop exits. I tried it out and seems to be working. I'm not sure if I can call myself a legend this time round but I tried my best. Thanks for your excellent lesson Mr. McWhorter.
@hughpatterson1480
@hughpatterson1480 2 жыл бұрын
What a fantastic lesson Paul. I am going to have students go through this a few times. I am also going to use the idea of writing out your code on paper first (giving you credit for the idea, of course). I want my students to be able to write the code out and really understand it before writing it on a computer terminal. Thanks again!
@paulmcwhorter
@paulmcwhorter 2 жыл бұрын
Fantastic!
@davidbuckley4912
@davidbuckley4912 Жыл бұрын
i am very happy to say that I was able to do this. My code took a few more lines, and is not as concise as your answer, but I was able to write code that dorted high to low, and with only minor editing will sort low to high.I feel I am legend
@paulmcwhorter
@paulmcwhorter Жыл бұрын
LEGEND!
@advancedperformance9721
@advancedperformance9721 3 жыл бұрын
That's really cool burnes my brain a bit but I will tell worth every second watching ur lessons thank you sir
@berryblades
@berryblades Жыл бұрын
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.
@thisismyworkshop6654
@thisismyworkshop6654 3 жыл бұрын
Thank you for the tutorial. I tried hard, for about 1.5h to think for the solution, but I didn't manage to get any success haha... I tried swapping (the wrong way) and then I noticed the problem you described and thought ok, this might not be the way to go (didn't think of the extra variable). After that I tried to create a new list and go through the grades[] list, find the max value and append it to the new list. I thought, ok, I'm just going to remove the max value from the old list and will find the new max value and append it to the new list and so on, but I didn't manage to delete the max value in the first list (because I didn't know it's index in the list), so I failed in this one :) Your solution is really cool and neat, and the tutorial was great as usual. Thank you!
@burakayan3360
@burakayan3360 3 жыл бұрын
i did the same.
@davidschnider1297
@davidschnider1297 Жыл бұрын
well... I came CLOSE on my own.. yes! this was and is FUN... thanks, Paul...
@wilsonlittle4833
@wilsonlittle4833 Жыл бұрын
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.
@agsauce8723
@agsauce8723 Жыл бұрын
Great, was able to do it with 2 loops contained in one. The first one transfers the highest number into a new array, the second one replaces the number with zero, then the main loop repeats until all numbers are transferred into the new array in descending order. God Bless Paul.
@426F6F
@426F6F 9 ай бұрын
Wow, I love your solution. Mine was a lot more complicated and only works if the numbers aren't the same. As soon as I throw in multiple numbers of the same value I get a zero for that highGrade (Which I was storing in a different array) lol. I'm sure it would be possible for me to fix the error in my code, but I already spent so much time on the problem and wanted to see how I pro would solve it. I have to say, I was initially thinking about something similar, but it was hard for me to logically map it out in my head, or even on paper. What an elegant solution. Thanks Paul!
@ianneill9188
@ianneill9188 3 жыл бұрын
Really enjoyed this tutorial. Made me think!
@paulmcwhorter
@paulmcwhorter 3 жыл бұрын
Glad to hear it!
@batticha462
@batticha462 Жыл бұрын
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)
@alfredcalleja450
@alfredcalleja450 Жыл бұрын
Hi Paul, a big "mea culpa" to you: I lied in the last tutorial - maybe it was the black coffee talking! I said I did all the homework on my own but after I sent the comment I realized I hadn't done the sorting part - I had done everything else though. When I tried the sorting code, which I thought would be easy, I got close but fell for the subtle things you so clearly described in this tutorial. Thanks again for another informative and interesting tutorial.
@krishnamurthyvr7855
@krishnamurthyvr7855 4 ай бұрын
Great. I always liked your tutorial on Arduino. Now this is on Python. For swapping there is a simpler way as a,b=b,a. I tried it and it works.
@scrappybobbarker5224
@scrappybobbarker5224 2 жыл бұрын
Thanks for the great lesson. I am legend. The first time I ordered the grades from greatest to least with an overly-complicated way, so it was interesting to see and try your way of ordering the grades.
@paulmcwhorter
@paulmcwhorter 2 жыл бұрын
The poorest solution I ever encountered to this problem started like this: grades[i]=float(input('Please input your grades Highest to Lowest'))
@scrappybobbarker5224
@scrappybobbarker5224 2 жыл бұрын
@@paulmcwhorter That is a very poor solution. That would simplify the process of sorting by a great deal... That's similar to a calculator, whose addition function involves asking for both numbers, and the sum of them, before giving the equation '-'
@mogomega8825
@mogomega8825 3 жыл бұрын
I love you man ! 👍
@wayneswan3092
@wayneswan3092 2 жыл бұрын
Not only was I successful at adding sort from lowest to highest, and highest to lowest, I also managed to successfully add an option to add more grades to the array. It was a lot more simple than I was making it out to be originally.
@graydhd8688
@graydhd8688 Жыл бұрын
I didn't quite get this one, but for any of these assignments that I figure out fairly easily I always add an extra challenge like you did!
@Mouse_007
@Mouse_007 3 жыл бұрын
Thought about the homework for 2 days without an idea. Then had two ideas that didn't work but the errors helped me solve it and BAM! your clue made perfect sense. HaHa good one Paul, you should write riddles. - Legend
@paulmcwhorter
@paulmcwhorter 3 жыл бұрын
Great job!
@jonasDevhadidan
@jonasDevhadidan 11 ай бұрын
a shorter version: sortedGrades= sorted(grades, reverse=True) grades=sortedGrades for i in range(0,num_grades,1): print(grades[i])
@codecage9333
@codecage9333 3 жыл бұрын
Guess I cheated! Used the sort function. Thought that was the "thinking" part. Using the tools in your tool box. Okay I'll go back and use the old school way of sorting a list before I watch the rest of your video, Paused at the 2:21 point. I'm now back! Partially folded, handled the swap of the grades OK, my issue was not going through the list one time less than the numGrades. Got it now though. Starting the homework on a blank slate.
@EFoxVN
@EFoxVN 2 жыл бұрын
Today, exactly a year ago (it's a public holiday today here), I had to figure out how to write a bubble sort algorithm for the Arduino C-language for a filter I was creating. Fortunately, I made some notes on that. And will you believe, a year later, I encounter a lesson on this?! So..... "I am legend". But only because I had to figure this out before. Blessings to y'all.
@paulmcwhorter
@paulmcwhorter 2 жыл бұрын
Fantastic!
@adamhorn8414
@adamhorn8414 2 жыл бұрын
I am kinda legend? I brute forced it to work (using multiple buckets) with the rules for the grading program, although if anyone makes the same exact grade it glitches out other than that I got it working, I even slept on it and went at it for another 30 minutes fresh before I folded and came to see how you stepped through the swapping process, boy was it a lot simpler than what i wrote up
@John-vw1wj
@John-vw1wj 3 жыл бұрын
Boy that chat cuts off fast. I solved the high low problem a bit differently and it works for any int + or -. Thanks for doing this ..I can now write some code without looking up syntax ever second line :)
@TheUnofficialMaker
@TheUnofficialMaker 2 жыл бұрын
You are the Man!
@RappingManualYT
@RappingManualYT 2 жыл бұрын
I feel so down, I thought of this for 2 days straight and at the end, folded like a cheap walmart chair.. I knew it was going to be i+1, but I just couldn't wrap my head around how to actually code it. I drove my self nearly mad, I have a headache now hahah! Can't wait to see the full solution. After all of that struggle, best believe I will forever know how to code a sorting method!
@paulmcwhorter
@paulmcwhorter 2 жыл бұрын
It is called LEARNING. It is painful sometimes. Dont give up, dont get discouraged
@RappingManualYT
@RappingManualYT 2 жыл бұрын
@@paulmcwhorter Never! You, making me actually think through this, it feels like Im installing a new sphere in my brain - engineering thinking. It feels so foreign, but forcing my brain to operate in such a novel way, makes me laugh for some reason and whenever I find a solution myself, the happiness and pride I get, is so sweet that it makes the struggle worth it! Im totally addicted to this process of installing a new way of thinking. Thank you so much, Paul. You’re helping my being evolve.
@ModernDayWanderlust
@ModernDayWanderlust 2 жыл бұрын
Well I spent probably six hours messing around over the last couple days and would have rated myself firmly on the "I've Lost My Trash Can" level of the Trash Can Wind Meter scale which means my cheap Wal-Mart lawn chair done did blow into the ocean. But after messing around a bunch in Python Tutor's Visualizer I am now officially legend. I haven't even watched your solution yet, my brain is fried, but this is what I came up with: # this loop reorders the gradeValues highest to lowest for i in range(0,totalGrades): for j in range(0,i+1): if gradeValues[i] > gradeValues[j]: gradeValues[i], gradeValues[j] = gradeValues[j], gradeValues[I] It took me being able to really visualize the steps of the for loops to wrap my brain around it, but it's really cool to watch the gradeValues swap around in the list as the for loop cycles through.
@paulmcwhorter
@paulmcwhorter 2 жыл бұрын
This is a classic coding problem, and one that leads many freshmen engineering students into a new culinary arts major. Actually, it is not so much a coding challenge as it is a thinking challenge. You can to think through it, map it out on paper, and then when you have a strategy the coding is easy. Glad you stuck with it.
@ModernDayWanderlust
@ModernDayWanderlust 2 жыл бұрын
@@paulmcwhorter it absolutely is much more of a thinking challenge than a coding one. Seeing you sketch it out and then watching how the loops step through and the elements in the list (as well as [i]/[j]) change was tremendously helpful to me, especially as someone that learns best visually. I also really appreciate how you're having us create algorithms from scratch instead of using built in functions. Way slower and requires more brain power, but you're laying out the foundations of understanding for when stuff gets more complicated. Thank you so much for doing these!
@charlielowell4077
@charlielowell4077 2 жыл бұрын
I redid my code after watching the video again. Wow, I was way off. Thanks for the heads up on the right way!
@thirupathaiah2981
@thirupathaiah2981 2 жыл бұрын
Perfect 👌
@irete267
@irete267 Жыл бұрын
I folded in this one. I actually had the idea of using a for loop to switch the numbers but I didn't think of using nested for loops to repeat the process till the list was completely sorted. Another way to switch items in a list without having to make another variable is by putting it like this: list[i], list[i+1] = list[i+1], list[i]
@berryblades
@berryblades Жыл бұрын
I AM LEGEND! I think, I did this very differently and seemingly lazily Paul. I did it with a counter that counted down and just printed grades as it matched them: See below. Did I fail this? print('Grades in descending order:') for count in range (100,-1,-1): for grade in grades: if (grade==count): print(grade)
@arnaudmasse7839
@arnaudmasse7839 2 жыл бұрын
i am legend ! did it on my own but it was pretty tough to figure out the loop in a loop !
@patfacunla1687
@patfacunla1687 2 жыл бұрын
I am legend! I've been thinking about this for about 24 hours and boom! got the solution :D edit: I did not cheat and it satisfied me today!
@boydrogers7227
@boydrogers7227 3 жыл бұрын
I used the exact method you did, except I didn't think you could use the same variable ("i") for nested for loops. I was very surprised to see you do so successfully. I'm still scratching my head over how this is possible.
@paulmcwhorter
@paulmcwhorter 3 жыл бұрын
Yes, very very poor programming on my part. In most languages that would not work. Wish I had used i and j.
@makergc3d
@makergc3d 9 ай бұрын
After spending literally 18 hours trying and trying and trying to figure this out I finally became frustrated enough to Fold Up Like A Cheap Walmart Lawn Chair. Now to the lesson to gain a deeper understanding of how to use my brain's plasticity to greater advantage.
@jimsimpson1006
@jimsimpson1006 7 ай бұрын
Got the list of grades sorted with another 7 lines of code (no cheating!) or 10 lines if you include the 3 lines to print them out neatly. Could hardly believe when it worked first time! But I’m watching now to see how the way I did it compares with the classic solution. Just watched and my 10 lines of code are identical to yours, except I used ‘tempVal’ instead of ‘swp’.
@yousraabdullah3054
@yousraabdullah3054 2 жыл бұрын
for i in range(0, grades, 1): highgrade = gradelist[0] for i in gradelist: if i > highgrade: highgrade = i gradelist.remove(highgrade) sorting.append(highgrade) print('your Grades In sort :', sorting) for i in range(0, grades,1): print(sorting[i]) I did it like this and its Work :)
@chalmerditmars7037
@chalmerditmars7037 Жыл бұрын
I found this one easy did ascending and descending after I googled python sort commands.
@dominikbach3983
@dominikbach3983 Жыл бұрын
I am legend, after 55 lines of code and the most horrifically overcomplicated and 2 hours I did it!!
@paulmcwhorter
@paulmcwhorter Жыл бұрын
LEGEND!
@mikoajnowacki2788
@mikoajnowacki2788 2 жыл бұрын
I did it different way :-) I made 2 lists - one where we put random grades, and the second where we put them after some code lines in order. First I checked which was the highest grade, and than added it to this 2nd list and removed it from 1st list. I repeated it in a for loop as many times as there were grades and done !
@paulmcwhorter
@paulmcwhorter 2 жыл бұрын
Nice work!
@joerio2453
@joerio2453 2 жыл бұрын
Thanks!
@paulmcwhorter
@paulmcwhorter 2 жыл бұрын
Thanks!
@pedrosojunior
@pedrosojunior 10 ай бұрын
Great!
@earnests5375
@earnests5375 3 жыл бұрын
Hello legend Paul
@calvindeiterich9913
@calvindeiterich9913 Жыл бұрын
Used Bubble Sort for this one. Figured out how to create a procedure to do the sort. Question, what editor do you use with the graph paper background?
@soundarrajankannan7692
@soundarrajankannan7692 3 жыл бұрын
I am Legend. I did in a slightly different way. I compared the number in index[0] to all other numbers in the array. So, highest number comes to the right place, when we finish inside for loop once.
@runargundersen2310
@runargundersen2310 2 жыл бұрын
Hi! I didn't do it like this. I used the same logic as when I found the highest grade, and stored that grade in a separate array. Each time through the loop I found the highest grade that was lower than the last grade I added to the new array. I don't know about computations, but it is done with fewer lines of code. I spent a good three hours on that solution ;) :P Anyway, I have a question. You don't leave empty lines in your code. I do that for easy reading, and I also like to add spaces to my code. Like this "a = b" and not "a=b" My question is. Will this come back to haunt me later? Is there any downside to doing it like that? Thank you for the awesome tutorials :)
@Mayank-dd3vw
@Mayank-dd3vw 3 жыл бұрын
It'll premier on 12 may, 2021 and today is 19 feb, 2021, which are 82 days from now!
@_alfie_phillips_2058
@_alfie_phillips_2058 3 жыл бұрын
it’s time!
@saamiabbaskhan2253
@saamiabbaskhan2253 3 жыл бұрын
How
@saptangshumanna9919
@saptangshumanna9919 2 жыл бұрын
I am so glad that i figured out to use double for loops for this homework. But I used two swap variables to store grades[i] and grades[i+1] to exchange them. I guess this is little inefficient. Also while playing around a bit with this I figured that I wanted to make a new sortedList while keeping the original grades array intact. For this I used the code: sortList=grades, followed by discussed nested for loops. But I found that any change I made to the sortList, transformed the grades array too. Where am I wrong? How do keep my grades list intact.
@manavt2000
@manavt2000 3 жыл бұрын
till last lesson i was a legend but in this lesson i folded up like a lawn chair :( But learnt a lot from this lesson
@dzeykop
@dzeykop Жыл бұрын
Thank you for another great lesson, Sir! Not wasted time (#NWT) for me and that is exactly what I want! 👍
@shaggy7958
@shaggy7958 7 ай бұрын
Was able to do the sorting assignment... I have to admit I knew to use the bubble sort algorithm as I was already familiar with it from a long time ago. But... I had to scratch my head and sketch it out on paper before I got it to work. I would say this was the least trivial assignment in this course so far.
@paulmcwhorter
@paulmcwhorter 7 ай бұрын
Excellent!
@larryplatzek9017
@larryplatzek9017 3 жыл бұрын
I am legend. It did think some thinking!
@LiamAllbright
@LiamAllbright 8 ай бұрын
legend says that John 106 is still holding his breath to this day. I folded.
@jakesanchez3552
@jakesanchez3552 2 жыл бұрын
I made this a lot harder than it turned out to be. This did work (with the exception of not being able to handle duplicate grades) but it's quite a mess compared to your example. This was much more challenging than i initially assumed. for i in range(0, numberOfGrades, 1): for j in range(0, numberOfGrades, 1): if i < 1 and grades[j] = 1 and grades[j] > gradesSorted[i-1] and grades[j]
@oluwatoyinkalejaiye3331
@oluwatoyinkalejaiye3331 14 күн бұрын
i love your content i am legend
@paulmcwhorter
@paulmcwhorter 14 күн бұрын
LEGEND!
@petefontana1958
@petefontana1958 2 жыл бұрын
I am not legend but I did not fold up like a cheap Walmart lawn chair. I spent 8 hours over several days working on this. It took 16 lines of code and a new array to store the grades. It would not work if there were 2 identical grades. I knew I was over thinking it. Thanks for making me think.
@graydhd8688
@graydhd8688 Жыл бұрын
Spent 4 hours over several days and I'm SOOOO close to getting it, but folding right now. I was ready to after the first hour and not being able to start the sorted list at all, but I spent a few more days staring at the screen and working out loops until I got it finding the lowest variable, then the next lowest, then the next, and so on. Only problem, I have a manually typed loop for each time this happens, and haven't been able to condense that into something that cycles through until the lists are the same length. It's currently dependent on having the right number of grades to match the number of loops in code. I may not have solved it fully on my own, but I learned a lot and stretched my brain getting my code to the point it's at currently so I don't feel too bad for not accomplishing the final result.
@graydhd8688
@graydhd8688 Жыл бұрын
Ohhhhh wow I didn't even think to amend the existing list by swapping like that. I had a loop to search for all grades equal to the lowest grade, each time appending that to a new list of sorted grades. Then used some temp variables to search for the next lowest grade in the original list and then appending that to the sorted list.
@paulmcwhorter
@paulmcwhorter Жыл бұрын
And by struggling, you expanded your brain. When you watch me do it, it looks easy, but so important to do what you are doing. Continue to think through it and struggle. One day you will be a master at this material. Keep on!
@86Carrera911
@86Carrera911 2 жыл бұрын
I AM LEGEND! Only took 9 extra lines of code!!!
@merio177
@merio177 2 жыл бұрын
I managed to do it, but a bit differently. I found the highest grade, added it to a new list and removed it from the initial one. Until the size of the new list matched the number of grades (with a while loop)
@jeff_wild
@jeff_wild 28 күн бұрын
Double chest bump. Been close to 35 years since I had to write a bubble sort. The use of "i" in both for loops is interesting. Python obviously keeps track of proper scope.
@paulmcwhorter
@paulmcwhorter 28 күн бұрын
That was accidental on my part, and poor form. Really surprised it worked, should have used a different variable for the second loop.
@matthewsalmon5640
@matthewsalmon5640 3 жыл бұрын
Hi, I did this slightly differently! # sort grades[i] in descending order num = num_g unsorted = [] # make a copy of the grades (unsorted) for i in range(0,num,1): unsorted.append(grades[i]) sorted = [] for r in range(0,num,1): hi = 0 for i in range(0,len(unsorted),1): if (unsorted[i] > hi): hi = unsorted[i] sorted.append(hi) unsorted.remove(hi) print(sorted)
@ernestomendoza4862
@ernestomendoza4862 2 жыл бұрын
I solve it a little differently. I still used two For(s). I created a new array with the same number of members. I did all members of the new array zeros. I looked for the highest grade in the original data and copied it in the new array at the first position with a zero. Deleted the grade from the original array and repeat. It is kind of cool because one can "print" the computer logic as it process it. print('') sampleSize=int(input('How many grades do you want to sort? : ')) numbers=[] numbers2=[] highGrade=float(0) for k in range(0,sampleSize,1): print('') print('member #',k) number=float(input('number =')) numbers.append(number) numbers2.append(0) print('') print('Input Vector:',numbers) print('-----------------------------------------------------------------------------') print('') for j in range(0,sampleSize-1,1): print('') print('Looking for the',j+1,' in order') for i in range(0,sampleSize-j,1): if(numbers[i]>=highGrade): highGrade=numbers[i] index=i print('position ',' value') print(i,' ',numbers[i]) numbers2[j]=highGrade print('') print('New Vector ',numbers2) numbers.pop(index) print('') print('data left',numbers) print('_________________________________________________________________________') highGrade=0 numbers2[sampleSize-1]=numbers[0] print('') print('-------------------------------------------------------------------------') print('Sorted Vector',numbers2) print('')
@robertbrodine9841
@robertbrodine9841 3 жыл бұрын
Paul, great lesson and thank you. It really made me uneasy when you built the nested FOR LOOP and I thought it shouldn't work because you used the same variable "i" in both loops. So... even though you used the same variable name, do the two loops store their variable "i" in different locations do you think? Sorry, It just made me kringe a little :) It obviously worked but I'm not sure why.
@paulmcwhorter
@paulmcwhorter 3 жыл бұрын
I really kringed when I saw I had done that, and surprised it work. Really poor programming method, and I should not have done that. Somehow python must keep them as seperate variables, but wow never do it that way.
@robertbrodine9841
@robertbrodine9841 3 жыл бұрын
@@paulmcwhorter Who knows... you may have discovered a Python compiler malady. :)
@directhubfeexam
@directhubfeexam 3 жыл бұрын
@@paulmcwhorter Thanks Paul, I hope this is clarified in the next lesson! Loving these videos.
@arbab8114
@arbab8114 2 жыл бұрын
I am legend. I was able to do the assignment first using for loop and then by using sort function.
@paulmcwhorter
@paulmcwhorter 2 жыл бұрын
The whole purpose of the lesson was for YOU to think through the problem and come up with a way to sort. It is a classic programming problem. Using a canned library, you have learned nothing. Go back and think through the problem and do it yourself!
@bryankohn9660
@bryankohn9660 3 жыл бұрын
I am legend. Not sure if I used the best method (appended to a new list), but we shall see.
@paulmcwhorter
@paulmcwhorter 3 жыл бұрын
Legend!
@anwarzebkhan
@anwarzebkhan 2 жыл бұрын
Great
@uniqueupdaterff2234
@uniqueupdaterff2234 7 ай бұрын
Hello, Sir i do have a doubt as you said that if there are 4 numGrades we have got to iterate it 3 times i.e. 1 less than numGrades But the question is that the range function either way iterates 1 less than the original value then why do numGrades - 1 as we might miss sorting a number Please let me know if I'm correct or how to approach this doubt. Thank You ❤
@Hyfh2
@Hyfh2 2 жыл бұрын
for sort in range(0,myNumbers-1,1): for sort in range(0,myNumbers-1,1): if grades[sort]< grades[sort+1]: swap.append(grades[sort]) grades[sort] = grades[sort+1] grades[sort+1] = swap[sort] like the code above, why if I use an arraylist (.append), the code can only run 5 inputs, and more than that the list index is out of range? thankyou for your attention
@gediminasgresevicius2901
@gediminasgresevicius2901 Жыл бұрын
ok, I was close, but not manege to solve it. I figured out that it should be one loop inside another, but was unable to find a way how to manipulate an order of grades. I was about to make a new array and putting a new grade in to it on each time I find the highest grade and taking that grade out of old array, but... ok, it was fun any way :)
@fejkzenpool8130
@fejkzenpool8130 Жыл бұрын
Is it okay if I just used grades.sort() to sort them from lovest to highest and then grades.reverse() to swap the order?
@paulmcwhorter
@paulmcwhorter Жыл бұрын
No! The whole point is to think critically, develop a strategy, and implement the solution. Using a canned routine, you do not learn to think through a difficult problem.
@mikaelbringh9313
@mikaelbringh9313 2 жыл бұрын
I know I'm kind of late to the show. trying to brush up my (non-existing) Python-skills for the new Raspberry Pi series. I got this example working but did it differently. This solution was a little bit cleaner. I was puzzled by the two nested for loops using the same counter i and everything still worked. I would use different counters, i and j, but that's me. I checked and yes the outer loop doesn't care that the inner loop leaves i at 3. I guess that is because it is Python, it actually executes every row by it self. Wouldn't work in a compiled program I guess. i have to test that.
Python Tutorial 13: Understanding Python While Loops
21:56
Paul McWhorter
Рет қаралды 20 М.
Coding Challenge #143: Quicksort Visualization
30:06
The Coding Train
Рет қаралды 246 М.
ПРОВЕРИЛ АРБУЗЫ #shorts
00:34
Паша Осадчий
Рет қаралды 7 МЛН
A teacher captured the cutest moment at the nursery #shorts
00:33
Fabiosa Stories
Рет қаралды 55 МЛН
Red❤️+Green💚=
00:38
ISSEI / いっせい
Рет қаралды 90 МЛН
I gave 127 interviews. Top 5 Algorithms they asked me.
8:36
Sahil & Sarra
Рет қаралды 633 М.
Why Do Bubbles Form In Glasses Of Water?
12:33
Joe Scott
Рет қаралды 76 М.
Why The Windows Phone Failed
24:08
Apple Explained
Рет қаралды 230 М.
Solving Wordle using information theory
30:38
3Blue1Brown
Рет қаралды 10 МЛН
Python Tutorial 19: Python Classes and Methods Homework Examples
57:57
25 Nooby Pandas Coding Mistakes You Should NEVER make.
11:30
Rob Mulla
Рет қаралды 265 М.
Better Than Smart Phones☠️🤯 | #trollface
0:11
Not Sanu Moments
Рет қаралды 18 МЛН
low battery 🪫
0:10
dednahype
Рет қаралды 1,7 МЛН
iPhone socket cleaning #Fixit
0:30
Tamar DB (mt)
Рет қаралды 18 МЛН
Как противодействовать FPV дронам
44:34
Стратег Диванного Легиона
Рет қаралды 75 М.
Vision Pro наконец-то доработали! Но не Apple!
0:40
ÉЖИ АКСЁНОВ
Рет қаралды 392 М.