5

I have a list of strings, and I want to call a function for every character in a string. When I assign variables to each function I do not want them to run, I only want to call them when iterating over the string. Here is my code:

import random

def make_s():
    result = ''

    V = make_v
    C = make_c
    P = make_p
    B = make_b

    structs = ['VPCVBCC', 'VCVVC', 'VVPCC', 'VCVBCC', 'VPCCVBC', 'VCVCC', 'VPCBC', \
            'VVPCVBCC', 'VCVC', 'VPCVVBC']

    struct = random.choice(structs)

    for elem in struct:
        /* Call function defined above and add the result to the result string */
        result += elem()

    return result

What's the best way to go about doing this?

Many thanks :)

1
  • 1
    Using dict instead of list might be easier to implement Commented Dec 7, 2016 at 17:18

2 Answers 2

9

You're pretty close. You should just map your characters to the functions, as opposed to assigning to specific variables.

import random

def make_s():
    result = ''

    # Here we have a mapping between the characters you see,
    # and the functions you want to call.
    my_funcs = {"V": make_v,
                "C": make_c,
                "P": make_p,
                "B": make_b}

    structs = ['VPCVBCC', 'VCVVC', 'VVPCC', 'VCVBCC', 'VPCCVBC', 'VCVCC', 'VPCBC', \
            'VVPCVBCC', 'VCVC', 'VPCVVBC']

    struct = random.choice(structs)

    for elem in struct:
        # Lookup the function in your dictionary
        func_to_call = my_funcs[elem]
        # And call it!
        result += func_to_call()

    return result
Sign up to request clarification or add additional context in comments.

3 Comments

It's often hard to try to figure out (in code) which variable name is assigned to which function. Turning a "V" charcter into 'whichever object my variable "V" points to' is generally hacky and can be avoided by a dict just like this.
You could save a few steps by doing my_funcs = {'V': make_v(), 'C': make_c(), 'P': make_p(), 'B': make_b()} , then it's just for elem in struct: result+=my_funcs[elem]
In languages like python you can figure out variable names at runtime in a somehwat hacky way, but in other languages it cant be done at all.
3

Similar approach using dictionary to map characters to function calls, a bit more concise using list comprehension & the string join function:

import random
def make_s():
    fDict = {'V': make_v(), 'C': make_c(), 'P': make_p(), 'B': make_b()}
    structs = ['VPCVBCC', 'VCVVC', 'VVPCC', 'VCVBCC', 'VPCCVBC', 'VCVCC', 'VPCBC', \
            'VVPCVBCC', 'VCVC', 'VPCVVBC']
    struct = random.choice(structs)
    return ''.join([fDict[elem] for elem in struct])

2 Comments

Thanks, should have pointed out that the function produces a different output every time it's run, so above method works better
Ahhh I might've went a little too far with it :) Revised to isolate the random.choice(structs) so that it's not part of the comprehension (though I don't think that affects output) and I think this should be functionally same as the other answer. Otherwise, not sure what distinction you're making about "different output every time it's run". Cheers

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.