0

A rather simple question but as a newbie, I can get a little lost.

How would I format my function to take a list as an input for my function, and then use another function to sort the said list? I have a functioning code for what I underneath call func1.

func1 (x, list1)
    Doing code to sort list1 when x is added 

func2 (list2)
    out = []
    for i in list2:
    func1(mylist[i],out)

Not really sure how to define func2 to make it take a list like ex. func2([4,2,3,1]), use func1, to give [1,2,3,4]. out is in the start an empty list. So performing func2 should first look at list[0], take that into func1(4,out) --> out = [4] Next step, it should take list [1] = 2. func1(2, out) --> [4,2] --> [2,4]. And so on until all elements are reviewed and sorted.

If anyone has a good explanation of how to fix func2 I would appreciate it a lot.

Dojje
  • 37
  • 1
  • 6
  • One issue here is that lists have a built-in `sort` method which is the obvious (and efficient) way to sort them. Is there a real problem that you are trying to solve, or is this just for sake of an exercise? – alani Nov 27 '21 at 21:00
  • This should point you in a right direction https://stackoverflow.com/questions/4979542/python-use-list-as-function-parameters – Gregory Sky Nov 27 '21 at 21:01
  • Exercise, so want to see if I can do this without using any imports. – Dojje Nov 27 '21 at 21:01
  • 2
    Fair enough, though you certainly don't need any imports in order to call `list2.sort()` or whatever. – alani Nov 27 '21 at 21:02
  • Yeah, sorry.Did not mean imports, but wanted to see if I could solve it without sort as well :) – Dojje Nov 27 '21 at 21:04
  • If I understand correctly, `func1` should insert element `x` into the sorted `list1` at the right position so that the resulting list is still sorted? – Stef Nov 27 '21 at 21:10
  • You marked your question as `[python]`. However, the code you have shown does not really look like python. To define a function in python, the first line should be `def func1(x, l):`, not just `func1(x, l)` – Stef Nov 27 '21 at 21:17
  • It is python. Just posed it as an example, should have Been more elaborate in my writing. Exactly, x will be inserted into the list and put in the right position. – Dojje Nov 27 '21 at 23:31

1 Answers1

0

There are two approaches when working with a list in python. You can either modify the list in-place, or you can build a new list.

Unfortunately for your "I want to do everything myself" attitude, modifying a list in-place means that you'll have to rely only on list methods, such as .sort or .insert or .remove. Here is a list of useful list methods: https://docs.python.org/3/tutorial/datastructures.html#more-on-lists

# insert element x into sorted list l in-place
def func1(x, l):
    i = 0
    while i < len(l) and l[i] < x:
        i += 1
    l.insert(i, x)

# returns a sorted copy of list l
def func2(l):
    result = []
    for x in l:
        func1(x, result)
    return result

This might be disappointing to you because you were hoping to avoid relying on .insert to insert the element.

Now the second approach: instead of modifying the list in-place to insert an element into it, we'll return a new list, which is a copy of the original list, with the new element inserted.

# return a copy of sorted list l, with x inserted
def func1(x, l):
    i = 0
    while i < len(l) and l[i] < x:
        i += 1
    return l[:i] + [x] + l[i:]

# return a sorted copy of list l
def func2(l):
    result = []
    for x in l:
        result = func1(x, result)
    return result

Important note: Take a close look at the two versions of func2. They are very similar, but there is an important difference between the two. Because they rely on func1, and we changed func1, we also had to change func2.

Stef
  • 13,242
  • 2
  • 17
  • 28