Home > Back-end >  How to call a function according to the value entered?
How to call a function according to the value entered?

Time:04-08

I built functions that search for the words by direction, right left, up, down , and diagonally. Each such function returns the word if it appears and the number of times it appears. I actually built a separate function for each direction so that there could be a word that is in the search up and not in the search down. I now want to build a function that accepts the word list ,and the matrix structure as a list of lists. In addition it gets the direction in which it should look up the list of words, can be two directions and more. I want to summon any function I have already written according to the desired direction but I have two problems: 1. I do not want to use if 'u' in directions return upper (words, pattern (matrix)) for every direction because it is too long. 2.If i want a word to appear in two directions for example - both in the search above('u') and in the search below('d') I want to make a union so that instead of a word appearing to me in two different tupels - (word, 1),(word, 1) but I will get one tuple (word,2) the total appearance number.

def fins_words(word,pattern,direction_letters):
    letters_lst=['u','d','r','l','w','x','y','z']
    functions_lst=[upper(word, pattern),down(word, pattern),right(word, pattern),
                   left(word, pattern),up_right_diagonals(word, pattern),
                   up_left_diagonals(word, pattern),
                   down_right_diagonals(word, pattern),down_left_diagonals(word, pattern)]
    for i in range(8):
        if letter_lst[i] in direction_letters:
            return letter_lst[i]

def right(word_list, pattern):
    word_occur = dict()
    one_word = [''.join(row) for row in pattern]
    for word in word_list:
        for string in one_word:
            count = string.count(word)
            if count > 0:
                if word in word_occur:
                    word_occur[word]  = 1
                else:
                    word_occur[word] = 1

    return [item for item in word_occur.items()]

pattern- matrix (list of lists of letters-[['a','b'],['d','e']]) word-words list for search in the matrix. letter - the direction of sercg 'u' for up 'd' for down ..... for example-

print(right(['apple','app','lg'],[['a','p','p','l','e'],['a','p','p','d','n']])

will return ('apple',1), (app,2) (wont return 'lg' beacuse it is nor appear)

CodePudding user response:

Replace your two lists with a dict that maps each letter to its corresponding function:

direction_funcs = {
    'u': upper,
    'd': down,
    'r': right,
    'l': left,
    'w': up_right_diagonals,
    'x': up_left_diagonals,
    'y': down_right_diagonals,
    'z': down_left_diagonals,
}

and then you can do something like this to aggregate the results of calling all the helpers according to direction_letters (I'm going to rename your word list parameter from word to words so the code is legible):

results = {word: 0 for word in words}
for c in direction_letters:
    for word, count in direction_funcs[c](words, pattern):
        results[word]  = count
return list(results.items())
  • Related