The problem
You’re growing a picture internet hosting web site.
You need to create a operate for producing random and distinctive picture filenames.
Create a operate for producing a random 6 character string that shall be used to entry the picture URL.
To verify the title shouldn’t be already in use, you might be given entry to a PhotoManager object.
You’ll be able to name it like so to verify the title is exclusive
# at this level, the web site has just one picture, hosted on the 'ABCDEF' url
photoManager.nameExists('ABCDEF'); # returns true
photoManager.nameExists('BBCDEF'); # returns false
Notice: We think about two names with the identical letters however totally different instances to be distinctive.
The answer in Python code
Choice 1:
import uuid
def generateName():
return str(uuid.uuid4())[:6]
Choice 2:
from random import pattern
from string import ascii_letters
def generateName(size=6):
whereas True:
title = ''.be a part of(pattern(ascii_letters, size))
if not photoManager.nameExists(title):
return title
Choice 3:
generateName=lambda:str(__import__("time").time())[-6:]
Check instances to validate our resolution
for i in vary(10):
title = generateName();
take a look at.anticipate(isinstance(title, str), "Identify needs to be a string.");
take a look at.anticipate(photoManager.nameWasUnique(title), "Identify needs to be distinctive.");
take a look at.assert_equals(len(title), 6, "Identify needs to be 6 digits lengthy.");