0

1) The question I'm working on: Write an algorithm for a function called removeAll which takes 3 parameters: an array of array type, a count of elements in the array, and a value. As with the remove method we discussed in class, elements passed the count of elements are stored as None. This function should remove all occurrences of value and then shift the remaining data down. The last populated element in the array should then be set to None. The function then returns the count of “valid” (i.e. non-removed) data elements left. This function should do the removal “by hand” and SHOULD NOT use the remove method.

2) Below I have what I think works for the question, but it seems inefficient and repetitive. Is there any way to simplify it?

'''

 def myremove(mylist, elements, val):
    for i in range(elements):    # Changes all val that needs to be removed to None
        if mylist[i] == val:
            mylist[i] = None

    for i in range(elements):
        if mylist[i] is None:    # Moves elements remaining left
            for j in range(i, elements- 1):
                mylist[j] = mylist[j + 1]
            mylist[elements- 1] = None

        while mylist[0] is None:    # If the first element is None move left until it is not
            for j in range(i, elements - 1):
                mylist[j] = mylist[j + 1]
            mylist[elements - 1] = None

    for i in range(elements):    # Counts remaining elements
        if mylist[i] is None:
            elements -= 1

    return mylist, elements

"""

"""

# Testing the function
print(removeAll([8, 'N', 24, 16, 1, 'N'], 6, 'N'))
print(removeAll([1, 'V', 3, 4, 2, 'V'], 6, 3))
print(removeAll([0, 'D', 5, 6, 9, 'D'], 6, 'N'))
print(removeAll(['X', 'X', 7, 'X', 'C', 'X'], 6, 'X'))

"""

"""

OUTPUT
([8, 24, 16, 1, None, None], 4)
([1, 'V', 4, 2, 'V', None], 5)
([0, 'D', 5, 6, 9, 'D'], 6)
([7, 'C', None, None, None, None], 2)

"""

1 Answer 1

1

You can just sort the list based on whether or not the value equals the hole value.

l = [8, 'N', 24, 16, 1, 'N']
sorted(l, key=lambda x: x == 'N')

output:

[8, 24, 16, 1, 'N', 'N']

If you need None instead of the hole value in the output, use a list comprehension and then sort based on None first.

l = [i if i != 'N' else None for i in [8, 'N', 24, 16, 1, 'N']]
sorted(l, key=lambda x: x == None)
[8, 24, 16, 1, None, None]

Then all thats left is to add in the count which you can just get by counting how many elements are None and subtract that from your input parameter.

def myremove(mylist, elements, val):
    ret_list = sorted([i if i != val else None for i in mylist], key=lambda x: x == None)
    return ret_list, elements - ret_list.count(None)
Sign up to request clarification or add additional context in comments.

1 Comment

@L3viathan - Thanks I was just testing out whether it was better doing that before or after sorting before adding it to my answer

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.