robot.py

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
from itertools import product
from random import choice
from string import ascii_uppercase as LETTERS


class Robot:
    name_pool = list(product(
        product(LETTERS, LETTERS),
        range(0, 1000)
    ))

    def __init__(self):
        self.reset()

    def reset(self):
        if not self.name_pool:
            raise RuntimeError("Out of names")

        alpha, numeric = choice(self.name_pool)
        self.name = ''.join(alpha) + "%03d" % numeric
        self.name_pool.remove((alpha, numeric))

Comments

I've solved this in other languages but was a little bit lazy in allowing for names to be used more than once. The tests don't allow this here, so this led me down a new path!

It's memory intensive but ensures choices are random, names will only ever get used once, and there's no chance of looping forever on trying to generate a new combination.

helenst commented 1 May 2016 at 13:05 UTC

You're not logged in right now. Please login via GitHub to comment