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.

8
  • 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? Commented Nov 27, 2021 at 21:00
  • This should point you in a right direction stackoverflow.com/questions/4979542/… Commented Nov 27, 2021 at 21:01
  • Exercise, so want to see if I can do this without using any imports. Commented Nov 27, 2021 at 21:01
  • 2
    Fair enough, though you certainly don't need any imports in order to call list2.sort() or whatever. Commented Nov 27, 2021 at 21:02
  • Yeah, sorry.Did not mean imports, but wanted to see if I could solve it without sort as well :) Commented Nov 27, 2021 at 21:04

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.