Note that there are some explanatory texts on larger screens.

plurals
  1. POPython lists - codes & algorithem
    primarykey
    data
    text
    <p>I need some help with python, a new program language to me. So, lets say that I have this list:</p> <pre><code>list= [3, 1, 4, 9, 8, 2] </code></pre> <p>And I would like to sort it, but without using the built-in function "sort", otherwise where's all the fun and the studying in here? I want to code as simple and as basic as I can, even if it means to work a bit harder. Therefore, if you want to help me and to offer me some of ideas and code, please, try to keep them very "basic".</p> <p>Anyway, back to my problem: In order to sort this list, I've decided to compare every time a number from the list to the last number. First, I'll check 3 and 2. If 3 is smaller than 2 (and it's false, wrong), then do nothing. </p> <p>Next - check if 1 is smaller than 2 (and it's true) - then change the index place of this number with the first element. On the next run, it will check again if the number is smaller or not from the last number in the list. But this time, if the number is smaller, it will change the place with the second number (and on the third run with the third number, if it's smaller, of course).</p> <p>and so on and so on. In the end, the (<em>)function will return the sorted list. Hop you've understand it. So I want to use a (</em>)recursive function to make the task bit interesting, but still basic. Therefore, I thought about this code:</p> <pre><code>def func(list): if not list: for i in range(len(list)): if list[-1] &gt; lst[i]: #have no idea what to write here in order to change the locations i = i + 1 #return func(lst[i+1:])? return list </code></pre> <p>2 questions: 1. How can I change the locations? Using pop/remove and then insert? 2. I don't know where to put the recursive part and if I've wrote it good (I think I didn't). the recursive part is the second "#", the first "return".</p> <p>What do you think? How can I improve this code? What's wrong?</p> <p>Thanks a lot!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload