Python Forum
Manual Sort without Sort function
Thread Rating:
  • 2 Vote(s) - 3.5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Manual Sort without Sort function
#11
(Oct-13-2017, 04:55 PM)dtweaponx Wrote: Man we haven’t even dove that deep into python yet. I’m very interested in python but this assignment is giving me a headache with how strict the rules to it are. Practically the teacher only wants us to use if, elif and else statements and >, <, comparisons for this project. I’m getting really confused on the if statements. Like a single if statement is going to check for true or false? Than another if below it will check another condition then elif would assign?
Ok, baby steps.

The problem most people have when they start programming is that they cannot believe how dumb computers really are. Humans make lots of intuitive jumps and use learned-long-ago behaviours and we find it very hard at first to lay out each little step a computer must do.

Consider for example living in a block of flats, with a set of open mailboxes in the lobby. You realise that the mailman has been putting post for flat 6 in box 9 and flat 9 in box 6. You decide to swap the post over, but you see that the post (one mailbox sized heavy box in each) entirely fills each box and you cannot hold one while you move the other.

Instinctively, as a human, you would grab one and stick it into a spare box temporarily (or on the floor, or on top of the radiator, or ... something else), then move the other, then move the first from its temporary location to correct mailbox.

You can't just tell a computer to swap things over. You have to write detailed instructions.

With the if statement, yes you can only have two possible outcomes. Either a condition is True or it is False. The evaluation of the condition can be very complex involving lots of considerations.

Thinking about going out tonight:

if it is not raining and I have money in my wallet and there is nothing good on the tv then I can go out.

As you know, you can break this down into multiple conditions. Maybe you are not worried about the rain, but you need to check you have the rain coat with you rather than having left it at a friends.

When you start out programming, try to think about things in the real world. These things will be pretty dumb. For this problem, there is nothing wrong with putting three placemats on the table in front of you, writing some numbers down on small squares of paper, and randomly placing three of them into the placemats.

Now, rather than looking at the computer screen and getting frustrated, walk through how to tell someone that is really really stupid how to sort those three numbers into the right order. Use very simple, basic instructions. (You can assume they know how to pick something up and how to put it down, but you can't let them hold anything whilst anything else happens. You can't just point or be vague either. Saying "pick the next one up" or "put it in a spare slot" will not work because you are asking them to figure something out, and that part is the programmer's job.)

I know this sounds really dumb, but trust me, make it real, make it simple, do it and the light will dawn, and soon you will not need to do things so basically.

Regarding the code I gave you,

def threesort(three):
    if three[0] <= three[1]:
        if three[1] <= three[2]:
            return
        else:
            threeswap(three, 1)
    else:
        threeswap(three, 0)
    threesort(three)
Apologies for posting something ahead of your current learning stage.

Let me break it down for you.

def defines a block of code. I'm sure you've had to sing music at school at some point, and on the song sheet, there will have been a section marked as the chorus, but rather than having it printed every time it is needed between verses, it will be printed only the first time and after that they will have just printed "chorus" and you know to sing the chorus printed earlier on in the song sheet.

In code,
  • easy to do the same thing in more than one place in your main code
  • you only need to change the code that does that thing in one place
  • makes it easy to understand the main code, because fiddly detail is written elsewhere

A defined block can be passed arguments to work with. The singing version might be to indicate a chorus should be sung faster, louder, softer (or maybe the name of a person mentioned in the chorus should be different each time it is sung).

Instead of having a def block, you can simply have a loop in your main code, to execute the same block of code as many times as required. You just have to figure out how to control the loop.

OR, with something very simple, that you know will need to happen only a few times at most, well, you can repeat the code. Thanks to copy and paste, it is not hard.

The odd part of the code I shared is probably the last line of the block where it calls itself. This is called recursion. If it happens too many times, you can crash the computer because all of the memory gets used up. In this case, because we are dealing with only three numbers, I knew that would not happen. If I didn't have that line, after the code has been executed once, it would not happen again UNLESS the part of the main code that was calling it was doing so in a loop. Alternatively, I could build a loop into that def block so it executed the required number of times to solve the problem. We would be back to that thing about controlling the loop I mentioned before.

The recursion calls makes sure that block of code keeps being called until the job is done. How do we know the job is done? When both if statements confirm things are in the right order, in which case the return command is used to exit the block of code. In the case of recursion, this will back out much like packing a set of Russian dolls.
I am trying to help you, really, even if it doesn't always seem that way
Reply


Messages In This Thread
Manual Sort without Sort function - by dtweaponx - Oct-13-2017, 02:04 AM
RE: Manual Sort without Sort function - by buran - Oct-13-2017, 08:29 AM
RE: Manual Sort without Sort function - by snippsat - Oct-13-2017, 02:58 PM
RE: Manual Sort without Sort function - by gruntfutuk - Oct-13-2017, 05:27 PM
RE: Manual Sort without Sort function - by wavic - Oct-13-2017, 06:33 PM
RE: Manual Sort without Sort function - by Mekire - Oct-13-2017, 11:28 PM
RE: Manual Sort without Sort function - by wavic - Oct-14-2017, 01:05 AM
RE: Manual Sort without Sort function - by Mekire - Oct-14-2017, 01:17 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
Question Frog codility leap sort variant MoreMoney 5 599 Apr-06-2024, 08:47 PM
Last Post: deanhystad
  How to sort a list with duplicates and return their unique indices. Echoroom 3 3,612 Sep-23-2022, 07:53 AM
Last Post: deanhystad
  sorting a list using unicodes acending order, no loops, no sort(), using recursion lrn2codee 14 6,572 Jun-23-2021, 07:33 PM
Last Post: deanhystad
  Sort and merge flintstone 1 1,954 Jun-14-2021, 07:32 PM
Last Post: Larz60+
  How can I sort my column so that NaN is at the bottom? soft 3 2,477 Feb-06-2021, 01:02 PM
Last Post: ibreeden
  how to sort a list without .sort() function letmecode 3 3,512 Dec-28-2020, 11:21 PM
Last Post: perfringo
  Sort on basis of (name,age,score) pyzyx3qwerty 9 9,714 May-14-2020, 08:29 AM
Last Post: pyzyx3qwerty
  Sort last pyzyx3qwerty 7 4,862 May-05-2020, 02:58 PM
Last Post: DeaD_EyE
  insertion sort brobro 3 2,264 Apr-24-2020, 01:29 PM
Last Post: Godserena
  Sort objects by protected properties. Sigriddenfeta 1 1,929 Mar-17-2020, 04:11 AM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020