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
#21
(Oct-14-2017, 03:09 AM)ichabod801 Wrote: The middle is just like the small. Is the first number in between the other two? Then it's the middle. Is the second number between the other two? then it's the middle.

So would it be....?
--
If this project was less restrictive on the rules I’d understand it a bit more. But all these if statements are just so confusing...

Or would it be.....please tell me I’m correct.

Would this be correct?:
--
Reply
#22
Look at your first condition. It's testing if first is greater than the other two, not in between them. Your idea of getting the small and the large first will work well, because it would make the test easier. Middle may be higher than second and lower than third, or it may be lower than second and higher than third. But it will always be higher than low and smaller than large (if you have three different numbers).
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#23
So my second set of code I posted where I was using the user input and comparing it to small and large. Would you say that it’s correct? Like the if statements are right?
Reply
#24
Yes, that looks good, sorry for missing it.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#25
Still having a small issue, anyone tell me whats wrong here. I type in 3, 6, 9 and get 3, 9, 9 in the return. Here is my code:
--
Reply
#26
When you are doing middle, you haven't figured out what large is yet. Large is zero, since that's what you set it to before the if statements. You need to put the middle section after the large section.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#27
Another way to do this is -
import time
#get valid input
while True:
    numList = []
    numbers = input('Type your numbers seperated by spaces: ')
    try:
        for num in numbers.split():
            #debug numbers starting with 0
            outputNum = ''
            zero = True
            for character in num:
                if character == '0' and zero:
                    pass
                else:
                    outputNum += character
                    zero = False
                        
            int(outputNum)
            numList.append(outputNum)
        break
    except:
        print('Invalid input detected. Please refer to the message below')

#Exit if they typed nothing
if not numList:
    print('If you didn\'t want to use me you could\'ve just clicked the red x')
    time.sleep(5)
    quit()

#Sort from least to most
outputList = []
for x in range(0, len(numList)):
    num = min(numList)
    outputList.append(num)
    numList.remove(num)

#print results
for output in outputList:
    print(output)

But for this assignment, only the debugging part from my code might be useful
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Question Frog codility leap sort variant MoreMoney 5 357 Apr-06-2024, 08:47 PM
Last Post: deanhystad
  How to sort a list with duplicates and return their unique indices. Echoroom 3 3,453 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,355 Jun-23-2021, 07:33 PM
Last Post: deanhystad
  Sort and merge flintstone 1 1,896 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,397 Feb-06-2021, 01:02 PM
Last Post: ibreeden
  how to sort a list without .sort() function letmecode 3 3,419 Dec-28-2020, 11:21 PM
Last Post: perfringo
  Sort on basis of (name,age,score) pyzyx3qwerty 9 9,527 May-14-2020, 08:29 AM
Last Post: pyzyx3qwerty
  Sort last pyzyx3qwerty 7 4,737 May-05-2020, 02:58 PM
Last Post: DeaD_EyE
  insertion sort brobro 3 2,197 Apr-24-2020, 01:29 PM
Last Post: Godserena
  Sort objects by protected properties. Sigriddenfeta 1 1,876 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