2

So i got this function. It must not be changed!

class TestUnikati(unittest.TestCase):
    def test_02_clean(self):
        s = [5, 4, 1, 4, 4, 4, 4, 4, 3]
        unique_array(s) #<-- calls the function
        self.assertEqual(s, [5, 4, 1, 3]) 

Basically we test if the function clean() only returns a unique array. The variable s being the array. This is my function that get's the messy array s and tries to return an array of no duplicate elements

 def unique_array(s):
     s=unique(s) #<-- this is a function that just returns a unique array
     x=TestUnikati() #<-- I store the class in the x variable
     [insert a way to push s to the "TestUnikati.test_02_clean.s"]

I tried many things. I've tried some experiments with globals() and locals() as well as many things with the x object variable but I don't seem to get it right.

I've tried to push it to the locals() of TestUnikati.test_02_clean.s with the x object. Is there a way to save it so the s in the class function will be over-ridden and the self.assertEqual(s, [5, 4, 1, 3]) will compare the 2 and pass it? Something like this:

x.test_02_clean.s=unique(s)
or
x.s=unique(s)
3
  • 2
    It's passing the object to your function. You need to mutate it, not replace it. Commented Nov 29, 2017 at 14:40
  • 3
    I'm confused... unique() looks like it's going to recurse infinitely. Commented Nov 29, 2017 at 14:41
  • my bad i changed the labels, I was translating it quickly from my language to english so @glibdud thank's for the notice Commented Nov 29, 2017 at 15:00

3 Answers 3

1

As others have stated, since test_02_clean() doesn't assign the results of its call of unique_array() to anything, it's expecting you to mutate the given list in place. Something like this:

def unique_array(s):
    # Keep a set of all the items we've seen so far
    seen = set()
    # Index into list
    i = 0
    while i < len(s):
        if s[i] in seen:
            # Delete element from list if we've already seen it
            del s[i]
        else:
            # Or else add it to our seen list and increment the index
            seen.add(s[i])
            i += 1

 

>>> s = [5, 4, 1, 4, 4, 4, 4, 4, 3]
>>> unique_array(s)
>>> s
[5, 4, 1, 3]
Sign up to request clarification or add additional context in comments.

1 Comment

Brilliant! Instead of creating a new array we just modify the existing one. It slipped my mind completely. Cheers!
0

you must have to change your class variable to access globally like below:-

class TestUnikati(unittest.TestCase):
  def test_02_clean(self):
    self.s = [5, 4, 1, 4, 4, 4, 4, 4, 3]
    unique(self.s) <-- calls the function
    self.assertEqual(self.s, [5, 4, 1, 3])

and also to reassign it with shorted value you have to do like:-

self.s=unique(s)

1 Comment

That would work I am aware but the problem with this is that the class must NOT be touched. Only the unique function is the one that can be edited everything else is not allowed. (part of a coding project)
0

You need a function unique() that changes the list in place, instead of returning a new list, e.g.

def unique(l):
count = {}
for entry in l:
    count[entry] = count.get(entry, 0) + 1
for entry in l:
    for _ in range(count[entry] - 1):
        l.remove(entry)

This works in place:

a = [1, 1, 1, 3, 4, 2]
unique(a)
print(a)
>>> [1, 3, 4, 2]

1 Comment

Sorry, this function does not really work with more than two occurances. I'll improve it.

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.