[Python] How would you simplify this useless overcomplicated one-line function? for fun...

Discussão em 'Python' iniciado por Stack, Março 6, 2025.

  1. Stack

    Stack Membro Participativo

    Disclaimer: This is just a pet project for fun and for programming practice


    Long ago I wanted to make a random name generator, I originally made it in shell script so I could learn the language better, but I recently decided to try and convert it to python for funsies.

    The premise is we start with a random character, and randomly choose the next character from a list of characters that typically occur after the current character, nothing super fancy at all. I was pretty happy with the output, it makes very "fantasy-like" names, like Zox, Gadimoa, or Zojitene for example.

    Anyway, the bash script was about 80 ~ish lines, but when I simplified it to python, I got it down to just this:

    import random
    import string

    next_char = {...} # Large dictionary of character associations, check the bottom for full thing.

    def name_generator():
    name = ''
    for idx in range( -1, int((random.randint(1,9) * 0.7) + 3) - 1 ):
    name += random.choice( next_char.get( name[idx:], string.ascii_lowercase ) )
    return name


    I don't know about you but when I see a small for loop like that, I get the itch to see if it can be a one liner.

    I'm rambling so I'll save you the details, 7 iterations later and I have this monstrosity:

    Made multi-line for your viewing pleasure

    def name_generator7(i=0):
    '''This iteration uses a Wichmann-Hill type PRNG to remove need for any imported modules'''
    return [
    (
    ''.join( name ),
    name.append({'a': 'abcdefghijklmnopqrstuvwxyz', 'b': 'aeiloruaeiloruaeiloruaeilo', 'c': 'aehikloruyzaehikloruyzaehi', 'd': 'aeijoruyaeijoruyaeijoruyae', 'e': 'abcdefghijklmnpqrstvwxyzab', 'f': 'aeiloruaeiloruaeiloruaeilo', 'g': 'aehiloruyaehiloruyaehiloru', 'h': 'aeiouyaeiouyaeiouyaeiouyae', 'i': 'abcdefgjklmnopqrstvwzabcde', 'j': 'aeiouaeiouaeiouaeiouaeioua', 'k': 'aeilnoruyaeilnoruyaeilnoru', 'l': 'aeilouaeilouaeilouaeilouae', 'm': 'aeiouyaeiouyaeiouyaeiouyae', 'n': 'aeiouaeiouaeiouaeiouaeioua', 'o': 'abcdefghijklmnopqrstuvwxyz', 'p': 'aehilmnorsuyaehilmnorsuyae', 'q': 'uuuuuuuuuuuuuuuuuuuuuuuuuu', 'r': 'aeiouyaeiouyaeiouyaeiouyae', 's': 'acehiklmnopqrstuwacehiklmn', 't': 'aehioruyaehioruyaehioruyae', 'u': 'abcdefghijklmnoprstvwxyzab', 'v': 'aeiouaeiouaeiouaeiouaeioua', 'w': 'aehioruaehioruaehioruaehio', 'x': 'aeiraeiraeiraeiraeiraeirae', 'y': 'aeiouaeiouaeiouaeiouaeioua', 'z': 'aeiouaeiouaeiouaeiouaeioua',
    }.get( name[idx], 'abcdefghijklmnopqrstuvwxyz')[int(((((171*seed[0]) % 30269)/30269.0 + ((172 * seed[1]) % 30307)/30307.0 + ((170 * seed[2]) % 30323)/30323.0)%1)*25)]
    ),
    seed.extend([(171*seed[0]) % 30269, (172 * seed[1]) % 30307, (170 * seed[2]) % 30323]),
    seed.pop(0),seed.pop(0),seed.pop(0)
    )
    for seed
    in [[int( divmod((i + 1)*10_000_000_000_000_000, 30268)[1] ) + 1, int( divmod( divmod((i + 1)*10_000_000_000_000_000, 30268)[0],30306)[1] ) + 1, int( divmod(divmod(divmod((i + 1)*10_000_000_000_000_000, 30268)[0],30306)[0],30322)[1]) + 1]]
    for name
    in [['abcdefghijklmnopqrstuvwxyz'[int(((((171*seed[0]) % 30269)/30269.0 + ((172 * seed[1]) % 30307)/30307.0 + ((170 * seed[2]) % 30323)/30323.0)%1)*25)]]]
    for idx
    in range( int( ( [1,2,3,4,5,5,6,7,8,9][int(((((171*seed[0]) % 30269)/30269.0 + ((172 * seed[1]) % 30307)/30307.0 + ((170 * seed[2]) % 30323)/30323.0)%1)*10)] * 0.7 ) + 3))
    ][-1][0].title()



    And here it is as a one line lambda for completeness, coming in at a whopping 1709 characters!

    g=lambda i=0:[(''.join(name),name.append({'a':'abcdefghijklmnopqrstuvwxyz','b':'aeiloruaeiloruaeiloruaeilo','c':'aehikloruyzaehikloruyzaehi','d':'aeijoruyaeijoruyaeijoruyae','e':'abcdefghijklmnpqrstvwxyzab','f':'aeiloruaeiloruaeiloruaeilo','g':'aehiloruyaehiloruyaehiloru','h':'aeiouyaeiouyaeiouyaeiouyae','i':'abcdefgjklmnopqrstvwzabcde','j':'aeiouaeiouaeiouaeiouaeioua','k':'aeilnoruyaeilnoruyaeilnoru','l':'aeilouaeilouaeilouaeilouae','m':'aeiouyaeiouyaeiouyaeiouyae','n':'aeiouaeiouaeiouaeiouaeioua','o':'abcdefghijklmnopqrstuvwxyz','p':'aehilmnorsuyaehilmnorsuyae','q':'uuuuuuuuuuuuuuuuuuuuuuuuuu','r':'aeiouyaeiouyaeiouyaeiouyae','s':'acehiklmnopqrstuwacehiklmn','t':'aehioruyaehioruyaehioruyae','u':'abcdefghijklmnoprstvwxyzab','v':'aeiouaeiouaeiouaeiouaeioua','w':'aehioruaehioruaehioruaehio','x':'aeiraeiraeiraeiraeiraeirae','y':'aeiouaeiouaeiouaeiouaeioua','z':'aeiouaeiouaeiouaeiouaeioua',}.get(name[idx],'abcdefghijklmnopqrstuvwxyz')[int(((((171*seed[0])%30269)/30269.0+((172*seed[1])%30307)/30307.0+((170*seed[2])%30323)/30323.0)%1)*25)]),seed.extend([(171*seed[0])%30269,(172*seed[1])%30307,(170*seed[2])%30323]),seed.pop(0),seed.pop(0),seed.pop(0))for seed in[[int(divmod((i+1)*10_000_000_000_000_000,30268)[1])+1,int(divmod(divmod((i+1)*10_000_000_000_000_000,30268)[0],30306)[1])+1,int(divmod(divmod(divmod((i+1)*10_000_000_000_000_000,30268)[0],30306)[0],30322)[1])+1]]for name in[['abcdefghijklmnopqrstuvwxyz'[int(((((171*seed[0])%30269)/30269.0+((172*seed[1])%30307)/30307.0+((170*seed[2])%30323)/30323.0)%1)*25)]]]for idx in range(int(([1,2,3,4,5,5,6,7,8,9][int(((((171*seed[0])%30269)/30269.0+((172*seed[1])%30307)/30307.0+((170*seed[2])%30323)/30323.0)%1)*10)]*0.7)+3))][-1][0].title()


    To use it, just copy it into python and run it, if you don't give it a value it should print "Fryia" which is the name for seed=0, to get randomness you must supply it with a random value for "i".

    My Question:


    How would you make this even more pointlessly compact, self-contained, or efficient?

    My goals were to make it self contained, no modules or anything, but it could go further. For instance you could figure out how to remove the range call, or figure out how to get rid of all the duplication of the PRNG calculations.

    Good luck! I'll mark an answer eventually based on reception.


    Base character association dictionary for if you want to edit it and make it your own:

    default_next_char = {
    'a': 'abcdefghijklmnopqrstuvwxyz',
    'b': 'aeiloru',
    'c': 'aehikloruyz',
    'd': 'aeijoruy',
    'e': 'abcdefghijklmnpqrstvwxyz',
    'f': 'aeiloru',
    'g': 'aehiloruy',
    'h': 'aeiouy',
    'i': 'abcdefgjklmnopqrstvwz',
    'j': 'aeiou',
    'k': 'aeilnoruy',
    'l': 'aeilou',
    'm': 'aeiouy',
    'n': 'aeiou',
    'o': 'abcdefghijklmnopqrstuvwxyz',
    'p': 'aehilmnorsuy',
    'q': 'u',
    'r': 'aeiouy',
    's': 'acehiklmnopqrstuw',
    't': 'aehioruy',
    'u': 'abcdefghijklmnoprstvwxyz',
    'v': 'aeiou',
    'w': 'aehioru',
    'x': 'aeir',
    'y': 'aeiou',
    'z': 'aeiou',
    }

    Continue reading...

Compartilhe esta Página