1

I've come to an end with my assignment, I don't know where I go from where I am right now, the code is currently looking like this:

def radixsorting1(n,m):
    div=1
    mod=10
    bin_list=[]
    alist=[]
    r=[]
    s=[]
    for bins in range(0,10):
        bin_list.append(Queue())
    for k in range(0,m):
        r.append(random.randint(1,10**n))
    if not len(r)==0:
        o=max(r)
        y=len(str(o))
    for p in range(y):
        for num in r:
            minsta_tal=num%mod
            minsta_tal=int(minsta_tal//div)
            bin_list[minsta_tal].put(num)
        new_list=[]
        for bins in bin_list:
            while not bins.isempty():
                new_list.append(bins.dequeue())
            alist=new_list
        return alist

What I've been trying to do is to create 10 queues in put them in a list, then random m numbers from 1 to 10^n. Lets say I get 66 and 72, then I first sort them by the "small number", that is 6 and 2 in my numbers, then put them in a lost, and then do the process all over again but for the number 6 and 7 (the bigger number). In its current shape I get the error "Queue" object is not iterable.

My Queue class is looking like this, I think this one is okay.

class Queue:
    def __init__(self):
        self.lista=[]

    def put(self,x):
        self.lista.append(x)

    def get(self):
        if not len(self.lista)==0:
            return self.lista.pop(0)

    def isempty(self):
        if len(self.lista)==0:
            return True
        else:
            False

    def length(self):
        return len(self.lista)


    def dequeue(self):
        if not len(self.lista)==0:
            n=self.lista.pop(0)
            return n
A.Maine
  • 41
  • 8
  • Yes, that's not [iterable](http://stackoverflow.com/q/9884132/3001761); you don't define `__iter__` or `__getitem__`. Also, per [the data model](https://docs.python.org/3/reference/datamodel.html), you should rename the existing methods – jonrsharpe Jun 24 '16 at 10:39
  • Cant say Im sure what Ive done but i did put def __iter__(self): return self in my class function but now I get the error: iter() returned non-iterator of typ "Queue", and I think my ending of the code is wrong as well... – A.Maine Jun 24 '16 at 10:46
  • Please [edit] the question, code (particularly Python) is all but unreadable in comments. – jonrsharpe Jun 24 '16 at 10:46
  • sorry, this is what I meant: As you can say in the code over, I did put a little bit of new code in my Class Queue. I still get the error Iter() returned non-iterator of type "Queue". Also I have a feeling the end of my radixsorting1 code is wrong, but Im unsure how to do the ending of it.... – A.Maine Jun 24 '16 at 10:52
  • If you `return self` from `__iter__` you also need to implement `__next__`. – jonrsharpe Jun 24 '16 at 11:32
  • Alternatively, you could use a deque from the collections module. It seems to have everything you need and slightly more. https://docs.python.org/2/library/collections.html – Oscar Smith Jun 24 '16 at 13:19

1 Answers1

1

You need to add a bit more code to make it an iterable. __iter__ should return an iterator. The iterator should have a next method.

Take a look at this:

Build a Basic Python Iterator

So it is my understanding that the thing you want to iterate over is the contents of self.lista... Why not just return lista's iterator.

Here is the easiest way to do that:

class Queue:
    ...
    def __iter__(self):
        return self.lista.__iter__()

It's a bit hard to see what exactly it is that you want.. If what you are trying to do is empty listaas you iterate over it (Queue is a fifo kinda deal) it then rather do this:

class Queue:
    ...
    def __iter__(self):
        return self
    def next(self):
        if self.lista: #since empty lists are Falsey
            return self.lista.pop(0)
        raise StopIteration
Community
  • 1
  • 1
Sheena
  • 15,590
  • 14
  • 75
  • 113
  • Thank you for your comment, this is pretty new to me, didn't realize my task would be this complicated. – A.Maine Jun 24 '16 at 11:03
  • Is there like a lot easier way to solve the problem with radixsorting, I cant say I understand even though I did read what you posted, Im new to this and I dont understand how I can solve the problem when I only have the self-reference in my __init__ and he got low and high as well.... – A.Maine Jun 24 '16 at 11:18
  • Thank you so much for taking time! – A.Maine Jun 24 '16 at 16:02
  • Im calling the function right now, but nothing is happening pretty much, calling it through labb73.radixsorting1(5,5)...Nothing is happening tho, Where did I mess up? – A.Maine Jun 25 '16 at 12:52
  • @A.Maine Have you updated your code? It doesn't look lke you are actually making use of the iterator. Also `__next__` should be `next` for an iterator I think. But maybe those are the same thing. You should be able to do something like: `for x in q: do_whatever(x)`. – Sheena Jun 26 '16 at 08:52
  • If you are still having issues be specific about what you are doing, what you expect, what you tried, etc. I'm happy to help but I'm not fond of having to make guesses and assumptions. It looks like `radixsorting1` does some redundant things. And this is homework, right? Try going over the function line by line and just make sure you understand each one. Use `pdb` if you need to, pdb is a wonderful tool for figuring out what your code is really doing – Sheena Jun 26 '16 at 08:55
  • This is homework, I didnt need iteration, so I changed it a little bit, I want to make it sort after, so what I wanna do is, if I have the numbers [6,132,75], then I want them to first sort the smallest digit, that is 6,2 and 5 in the right bin. After ive done that, I want mod and div to get 10 times bigger so that I sort after (no number in 6),3 and 7. I think I need two nestled for-loops, because I need to go through all the elements m, and I need to do this n times.... this assingment is hard.. – A.Maine Jun 26 '16 at 13:07
  • actually, I changed my code to this right now, based on another example here on stackoverflow.. its not working but.... – A.Maine Jun 26 '16 at 14:02
  • My current code works for sorting up to 9, need to make sure it works for bigger numbers as well – A.Maine Jun 26 '16 at 15:02
  • It's solved, thanks to everyone who helped! Im so grateful, I was so tired of this, thanks a lot, im new to the community so I dont know if I can promote you guys or something, but thanks! – A.Maine Jun 26 '16 at 15:20