How to generate a username with its real name as a reference?

Asked

Viewed 789 times

2

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

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

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 this case the return must be the initials of the full name. If the full name has few words, the algorithm should pick up more letters (ex: 'Luis Inacio' would be 'LUIINA') the last word must be completed, and if it still does not result in at least 6 characters, you must add numbers to the end. If the final result conflicts with registered user names, the algorithm must add a number at the end.

  • 1

    This question seems to me very broad and/ or subjective: there are N ways to do this, and the "quality" of the result will depend very much on the taste of each one. Please try to restrict more what you expect from an answer, otherwise it will go a lot in the opinion of each.

  • Now it’s much more specific (and "responsive"). A question: if you’ve added numbers at the end for lack of sufficient characters (e.g..: DRIO01) and that name conflicted with another existing one, you change the number at the end or add another number?

  • After your comment I ended up editing the question to specify better the final result, which ended up simplifying the logic.

  • @mgibsonbr in the case would be changed, if there can be no more repetitions and get very extensive.

1 answer

1


My suggestion is to first assemble the name "radical" (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

So just create an accountant and use 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.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.