Python Forum
sorted list not displaying
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
sorted list not displaying
#1
Hello all,

Sorry to bother you with yet another noob mystery but again, I find myself stumped. And I'd like to start by saying that I spent a solid hour or more trying to resolve this myself before coming before you to beg for your assistance, again.

Quick recap, I am using a coding for beginners book as the first step in teaching myself Python, I have had fairly good results and even feel bolstered by the fact that when I submitted my previous cry for help I didn't even really know what I was looking for. I now know the idea of what I'm looking for and even have some clue where in my code the problem lies, (I think  Confused ) I just still don't have the chops to figure out exactly what I'm missing. I have checked and double checked the code I've created vs. the example in my book and am confident in saying that it matches the example exactly. Here is the code:


def copy_sort(array):
   copy = array[:]
   sorted_copy = [ ]
   #Algorithm sequence to be added here
   return sorted_copy
   while len( copy ) > 0 :
       minimum = 0
       for element in range( 0, len(copy)):
           if copy[ element ] < copy[ minimum ] :
               minimum = element
       print( '\tRemoving value', copy[ minimum ],\
                   'from', copy )
       sorted_copy.append( copy.pop(minimum))
array = [5, 3, 1, 2, 6, 4]
print( 'Copy Sort...\nArray :', array )
print( 'Copy :', copy_sort( array ))
print( 'Array :', array )
I see where the "copy_sort" is defined and I see where it is called back again near the bottom, I've gone through the algorithm and traced it out in my head, and I think I understand it, however I am not getting the same output as the example in my book and I can't figure out why.

Here is my output:
Output:
Copy Sort... Array : [5, 3, 1, 2, 6, 4] Copy : [] Array : [5, 3, 1, 2, 6, 4] >>>
The example in my book looks like this:
Output:
Copy Sort... Array : [5, 3, 1, 2, 6, 4]                Removing value 1 from [5, 3, 1, 2, 6, 4]                Removing value 2 from [5, 3, 2, 6, 4]                Removing value 3 from [5, 3, 6, 4]                Removing value 4 from [5, 6, 4]                Removing value 5 from [5, 6]                Removing value 6 from [6] Copy : [1, 2, 3, 4, 5, 6] Array : [5, 3, 1, 2, 6, 4] >>>
Do I have my indent[s] wrong? or did the code not properly define the algorithm?
I appreciate any input you can offer.
Best regards,
Quote:If you can't learn to do something well?... Learn to enjoy doing it poorly.
Reply
#2
your function is returning sorted_copy on line 5, before any of the processing of the arrays happens. move that line to the end of the function.
Reply
#3
When you use the return statement the execution of the function stops and you get the returned object
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#4
That was it!! Thank you!
That was something I actually considered, but didn't try it... so, FAIL!  Smile

Here is the code with your suggested change:
def copy_sort(array):
   copy = array[:]
   sorted_copy = [ ]
   #Algorithm sequence to be added here

   while len( copy ) > 0 :
       minimum = 0
       for element in range( 0, len(copy)):
           if copy[ element ] < copy[ minimum ] :
               minimum = element
       print( '\tRemoving value', copy[ minimum ],\
                  'from', copy )
       sorted_copy.append( copy.pop(minimum))
   return sorted_copy
array = [5, 3, 1, 2, 6, 4]
print( 'Copy Sort...\nArray :', array )
print( 'Copy :', copy_sort( array ))
print( 'Array :', array )
I just cut and pasted the return command to the end, I also left the empty line as a place holder in case it didn't work then saved and ran it again and this is my new and correct output:

Output:
Copy Sort... Array : [5, 3, 1, 2, 6, 4] Removing value 1 from [5, 3, 1, 2, 6, 4] Removing value 2 from [5, 3, 2, 6, 4] Removing value 3 from [5, 3, 6, 4] Removing value 4 from [5, 6, 4] Removing value 5 from [5, 6] Removing value 6 from [6] Copy : [1, 2, 3, 4, 5, 6] Array : [5, 3, 1, 2, 6, 4] >>>
Actually, now that I look at it again, it is pretty much the same as the example in the book but in the book's example all of the "Removing value" lines are indented in the output. I'm guessing it's just a space or something I'm missing from the code? At least the sort is working now! Out of time at the moment but will revisit later.
Thanks again!!
Quote:If you can't learn to do something well?... Learn to enjoy doing it poorly.
Reply
#5
The output is indented because of the TAB symbol at the begining of the string '\t' - line 11.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#6
That makes sense to me except that I have the \t in my code and yet the output is not indented as in the book's example...

Ok! Now I'm really confused. The above example is exactly what I got when I corrected the code by moving the "return" statement. I copied it and pasted it just as it was on my screen which is the example I posted above.

Just now I went back and double checked the code, which I haven't changed since and ran it again and now it's exactly the same as the book. ???  How can I get 2 different outputs from the same code???

b.t.w. this is what I got this time:
[NOTE*] when I first copied this the indent[s] were there, however after previewing the indents disappeared as it is here, so I just left it

Output:
Copy Sort... Array : [5, 3, 1, 2, 6, 4] Removing value 1 from [5, 3, 1, 2, 6, 4] Removing value 2 from [5, 3, 2, 6, 4] Removing value 3 from [5, 3, 6, 4] Removing value 4 from [5, 6, 4] Removing value 5 from [5, 6] Removing value 6 from [6] Copy : [1, 2, 3, 4, 5, 6] Array : [5, 3, 1, 2, 6, 4] >>>
WAIT!!! Holy crap! ok I see what happened now but I still don't have an explanation for it. So I'll try to be brief here. I ran the fixed code, got the right output ( I think, honestly I don't remember from yesterday because I was so stoked that the fix worked I guess I didn't pay close attn. to the output in my Python shell.) I know that I copied and pasted just as I did a minute ago. However, what I just noticed is that when I first copied it into this message, the indent[s] were there, I then went to preview it before posting and when I scrolled back down to edit again I noticed that the afore-mentioned indent[s] were now gone again...
I don't know why my previewing my post would have that effect here in this forum, but that's most likely what happened to me yesterday as well and I just didn't catch it.
WIERD!   Huh  Huh

Thanks again for your help!
Quote:If you can't learn to do something well?... Learn to enjoy doing it poorly.
Reply
#7
If you suspect some sort of bug, you should report it in the "Board" forum, giving as much information as possible so the admins can try and replicate the scenario.
If it ain't broke, I just haven't gotten to it yet.
OS: Windows 10, openSuse 42.3, freeBSD 11, Raspian "Stretch"
Python 3.6.5, IDE: PyCharm 2018 Community Edition
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Displaying list correspond to the column number danlopek14q 9 3,898 Aug-27-2021, 04:32 AM
Last Post: naughtyCat
  How to Sorted and display the Subclasses of BaseException Fernando_7obink 9 3,599 Feb-10-2021, 12:04 PM
Last Post: buran
  Functions returns content of dictionary as sorted list kyletremblay15 1 2,005 Nov-21-2019, 10:06 PM
Last Post: ichabod801
  A function that checks if the list is sorted pooyan89 13 15,963 Jun-18-2019, 04:02 PM
Last Post: ThomasL
  defining a function to see if a list is sorted Siylo 14 6,856 Nov-29-2018, 05:25 PM
Last Post: wavic
  Displaying a long list with Rows and Columns ngr33n 5 10,872 Sep-21-2017, 10:17 PM
Last Post: ngr33n

Forum Jump:

User Panel Messages

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