Home > AI > Language > Python >

(interview) Round Robin

## Round Robin

Using only `collections`, implement a data structure that implements these methods:

* `__iter__`
* `__next__`
* `__getitem__`
* `__setitem__`
* `__delitem__`

This data structure is constructed from a list of values.

When enumerated, it must cycle through the values forever from the first value to the last value.

The list of values is also mutable, so the round robin structure continues to work even as you replace values or delete values.

```py
class RoundRobin():
    def __init__(self, l):
        pass
        
rr = RoundRobin(["a", "b", "c", "d"])

for (_, v) in zip(range(4), rr):
    print(v)

print('---')

rr[0] = 'A'

for (_, v) in zip(range(3), rr):
    print(v)

print('---')

del rr[2]

for (_, v) in zip(range(10), rr):
    print(v)
```

When the above code is executed, the printed output should be:

```
a
b
c
d
---
A
b
c
---
d
A
b
d
A
b
d
A
b
d
```

Leave a Reply