Tag: problem

3: endless list

Endless List

Suppose you need items pulled at random from a finite list of items.
The items should not repeat until all items in the list are returned.

See the code (overleaf)

Approach (for random):

  • Keep 2 lists viz: Used & Remaining.
  • first time / if items have changed, reset used and initialize remaining.
  • If nothing remains, move all used to remaining and clear used.
  • pick a random number from 0 to remaining.count
  • get corres item from remaining, remove it from there, add it to used and return it

Read more »