Generate random tuple combination based on their score in Python

Viewed 111

I am trying to create a python program, I have 4 list of tuples, I would like to create different 'words', each letter having it's own probability to appear, I would also like the words to be unique.

I first tried creating by assigning probability to each letter, which worked using numpy.random.choice() function.

Now I would like to take the problem from the other side. I put a weight (or score) to each letter (second part of each tuple) so each word I create have a score, here: from 4 to 16. (see below the 4 list of tuple, it's an exemple of what I am working with, the 4 lists are different)

liste1 = [('A',1), ('E',1), ('I',1), ('O',1), ('U',1), ('M',2), ('N',2), ('B',2), ('Y',2), ('R',3), ('E',3), ('T',3), ('G',4), ('J',4)]

liste2 = [('A',1), ('E',1), ('I',1), ('O',1), ('U',1), ('L',2), ('N',2), ('Z',2), ('Y',2), ('R',3), ('E',3), ('P',3), ('F',4), ('X',4)]

liste3 = [('A',1), ('E',1), ('I',1), ('O',1), ('U',1), ('Q',2), ('N',2), ('B',2), ('Y',2), ('R',3), ('E',3), ('T',3), ('H',4), ('J',4)]

liste4 = [('A',1), ('E',1), ('I',1), ('O',1), ('U',1), ('M',2), ('N',2), ('B',2), ('Y',2), ('R',3), ('E',3), ('T',3), ('S',4), ('J',4)]

And what I would like to do is telling my program I want x number of word with a score of 16, and then the program will create randomly x unique words with this score, then also do it for a score of 15, 14 etc...

I have no idea how to do that and I know it is a pretty specific demand, So I would be glad if anyone can bring me an answer.

Thank you for your time!

2 Answers

Try:

import random

liste1 = [
    ("A", 1),
    ("E", 1),
    ("I", 1),
    ("O", 1),
    ("U", 1),
    ("M", 2),
    ("N", 2),
    ("B", 2),
    ("Y", 2),
    ("R", 3),
    ("E", 3),
    ("T", 3),
    ("G", 4),
    ("J", 4),
]


def get_words(lst, n, target):
    def get_combinations(candidates):
        res = []

        def fn(arr, start):
            s = sum(arr)

            if s == target:
                res.append(arr[:])
                return
            if s > target:
                return

            for i in range(start, len(candidates)):
                arr.append(candidates[i])
                fn(arr, i)
                arr.pop()

        fn([], 0)
        return res

    tmp = {}
    for ch, v in lst:
        tmp.setdefault(v, []).append(ch)

    all_comb = get_combinations(list(tmp))

    res = []
    for _ in range(n):
        while True:
            s = ""
            comb = random.choice(all_comb)
            random.shuffle(comb)
            for v in comb:
                s += random.choice(tmp[v])
            if s not in res:
                res.append(s)
                break

    return res


print(get_words(liste1, 10, 16))

This prints 10 random words from liste1 that has characters whose value sums to 16 (for example):

[
    "UNINONMBAM",
    "TMYRNYY",
    "UOUUAEOUUUIAEOAA",
    "JIJMJA",
    "MNNJJN",
    "OIMAIIGOJ",
    "TTNBYMY",
    "EIAAIUUAMIEOI",
    "IEUAOJENUU",
    "BBGJENI",
]

hi what i understand form the above code the ans might be like ,you could have a loop running on each list and a if statement will check the condition here is a example

list1 = [("a",1),("b",2)....,("z",  16)]
wordlist = [] 
for tuple_1 in list1:
    (word,score) = tuple_1.split(",")
    if score == userinput #you can define that :
        wordlist.append(word)
    else:
        pass
# do random.choice() here

note if there are some error please tell

Related