-1

Hello i am doing my homework on coursera and the task is to build a sorting program that sorts numbers from least to greatest, the program seems to work fine until I run the test on it my code and error below.

def move_zero(lst):
    """
    Given a list of integers, moves all non-zero numbers to the beginning of the list and
    moves all zeros to the end of the list.  This function returns nothing and changes the given list itself.

    For example:
    - After calling move_zero([0,1,0,2,0,3,0,4]), the given list should be [1,2,3,4,0,0,0,0] and the function returns nothing
    - After calling move_zero([0,1,2,0,1]), the given list should be [1,2,1,0,0] and the function returns nothing
    - After calling move_zero([1,2,3,4,5,6,7,8]), the given list should be [1,2,3,4,5,6,7,8] and the function returns nothing
    - After calling move_zero([]), the given list should be [] and the function returns nothing
    """
    # your code here
    
    lst_join = ""
    n_lst = lst_join.join(str(lst))
    l = []
    k = []
    counter = 0
    for i in (n_lst):
        if i == ",":
            continue
        if i == "[":
            continue
        if i == " ":
            continue
        if i == "]":
            continue
        counter = counter + 1
        l.append(int(i))
    while len(k) != counter:
        for n_num in l:
            m_num = min(l)
            k.append((m_num))
            l.remove((m_num))
    return k
lst = input("user input: ")
move_zero(lst)
mov = move_zero(lst)
print(mov)

and here is the error that I get when I run the test

---------------------------------------------------------------------------
AssertionError                            Traceback (most recent call last)
<ipython-input-24-7d2b0d5dd8e0> in <module>
      4 
      5 lst = [0,1,0,2,0,3,0,4]
----> 6 assert_equal(None,move_zero(lst))
      7 nose.tools.assert_list_equal([1,2,3,4,0,0,0,0],lst)
      8 

/opt/conda/lib/python3.7/unittest/case.py in assertEqual(self, first, second, msg)
    850         """
    851         assertion_func = self._getAssertEqualityFunc(first, second)
--> 852         assertion_func(first, second, msg=msg)
    853 
    854     def assertNotEqual(self, first, second, msg=None):

/opt/conda/lib/python3.7/unittest/case.py in _baseAssertEqual(self, first, second, msg)
    843             standardMsg = '%s != %s' % _common_shorten_repr(first, second)
    844             msg = self._formatMessage(msg, standardMsg)
--> 845             raise self.failureException(msg)
    846 
    847     def assertEqual(self, first, second, msg=None):

AssertionError: None != [0, 0, 0, 0, 1, 2, 3, 4]
serge
  • 1
  • 3
  • this is the test your solution case that is used ########################## ### TEST YOUR SOLUTION ### ########################## lst = [0,1,0,2,0,3,0,4] assert_equal(None,move_zero(lst)) nose.tools.assert_list_equal([1,2,3,4,0,0,0,0],lst) lst = [] move_zero(lst) nose.tools.assert_list_equal([],lst) lst = [0,0,0,0,0,0,0,0,0] move_zero(lst) nose.tools.assert_list_equal([0,0,0,0,0,0,0,0,0],lst) lst = [1,2,3,4,5,6,7,8] move_zero(lst) nose.tools.assert_list_equal([1,2,3,4,5,6,7,8],lst) print("Success!") – serge Oct 02 '22 at 16:41
  • how would you expect `0` to have a higher value than `4`? – rv.kvetch Oct 02 '22 at 16:41
  • where do you see 0 being higher than 4? – serge Oct 02 '22 at 16:42
  • *"the given list should be [..., 3, 4, 0, ...]"* – rv.kvetch Oct 02 '22 at 16:43
  • 1
    I see that's part of the homework, thank you for pointing that out looks liek i didnt take that into account. – serge Oct 02 '22 at 16:45
  • 1
    @serge If you have more details to add, edit the question so you can format them readably, don't put them in comments. – Barmar Oct 02 '22 at 16:53
  • The error you occur is because your implementation returns a list rather than the desired `None`. – ILS Oct 02 '22 at 18:22
  • The problem is that you have not understood the specification. You are supposed to modify the list that was passed in, not return a new list. In order to do this, it is necessary to **treat the input list as a list** - do not convert it into a string and then parse the string. Also, the goal is **not** to sort the list. Notice how the expected result for `[0, 1, 2, 0, 1]` is `[1, 2, 1, 0, 0]`? – Karl Knechtel Oct 02 '22 at 18:57

2 Answers2

0

Do this:

lst = [0,1,2,3,0,3,0,2]
l1 = []
new_lst =[]
for i in lst:
    if i ==0:
        l1.append(i)
    else:
        new_lst.append(i)
new_lst.extend(l1)

print(new_lst)
Elicon
  • 206
  • 1
  • 11
0

There are three requirements.

  1. Move zeros to the last and keep the order of non-zeros.
  2. Operate on the original list.
  3. Return nothing, i.e., return None.

The error you posted violates the third requirement.

Code snippet.

def move_zero(lst):  # for list type, changes are visible outside the function
    # your code here
    nz_idx = 0
    for i, v in enumerate(lst):
        if v != 0:
            lst[nz_idx] = v
            nz_idx += 1
    for z_idx in range(nz_idx, len(lst)):
        lst[z_idx] = 0

lst = [0,1,0,2,0,3,0,4]
assert None == move_zero(lst)
assert [1,2,3,4,0,0,0,0] == lst

This is not a sorting problem, you can achieve it in linear time complexity.

ILS
  • 1,224
  • 1
  • 8
  • 14