How to generate a username by reference to its real name?

2

I have a question on how to automatically generate user names from the user's real name, so that the result is a reference to the real name.

Knowing that the method should be called by telling the person full name and the minimum character limit that this username will have, in addition to verifying that this name already exists and modifying it so there is no conflict, how would this operation be done?

nomes_de_usuarios_atuais = ['ACBETR', 'ZRPACA', 'TRDPOR', 'TRDPOR1']
def gerar_nome_de_usuario(nome, min_caracteres=6):
    # ...
    nomes_de_usuarios_atuais.append(nome_de_usuario)
    print nome_de_usuario

self.assertEqual(gerar_nome_de_usuario('Luis Inacio Lula Da Silva', 6), 'LILDSI')
self.assertEqual(gerar_nome_de_usuario('Ricardo Magalhães', 6), 'RMAGAL')
self.assertEqual(gerar_nome_de_usuario('Ana Carolina Viana', 6), 'ACVIAN')
self.assertEqual(gerar_nome_de_usuario('David Rio', 6), 'DRIO01')
self.assertEqual(gerar_nome_de_usuario('Luis Inacio Lula Da Silva', 6), 'LILDSI1')

In case the return must be the initials of the full name. If the full name has a few words, the algorithm should get more letters (eg: 'Luis Inacio' would be 'LUIINA' ) should complete, and if it still does not result in a minimum of 6 characters, add numbers to the end. If the end result conflicts with already registered user names, the algorithm should add a number to the end.

    
asked by anonymous 02.12.2014 / 18:49

1 answer

1

My suggestion is to first mount the "radical" of the name (the part before the number) and then use a counter to determine the most appropriate suffix (if any):

radical = ''.join(x[0] for x in nome.split(' '))
if len(radical) < min_caracteres:
    radical += nome.split(' ')[-1][1:min_caracteres - len(radical) + 1]

radical = radical.upper()

If the root is smaller than the minimum size, one or more digits will be required. One way to simplify logic is by creating a formatting pattern that requires at least that number of digits:

formatar = "{:0" + str(min_caracteres - len(radical)) + "d}"
# Ex.: "{:03d}".format(12) ==> 012

Then just create a counter and use it at the end of the name:

if len(radical) < min_caracteres or radical in nomes_de_usuarios_atuais:
    contador = 1
    while radical + formatar.format(contador) in nomes_de_usuarios_atuais:
        contador += 1
    nome_de_usuario = radical + formatar.format(contador)
else:
    nome_de_usuario = radical
  

Note: The previous answer (on file) does not answer the question after clarification.

    
02.12.2014 / 19:23