dimanche 28 novembre 2021

Generating 10 x 10 table of values from random strings

I want to create a 10 x 10 table of values. Each entry in the table row or column value should be the count of difference in the string characters. I want to also keep track of minimum character differences between strings. However, in my case, I need 0 and at least 3 mismatches between randomly generated strings. Here is an example to give an idea about the problem.

    AMUNN  NUUNN ANUNN AAANN 
AMUNN   0   2   1   2   

NUUNN   2   0   2   3

ANUNN   1   2   0   2

AAANN   2   3   2   0

Here is my code so far.

import random
import itertools
from itertools import combinations


### Random string generation function starts
def shout():

    List1 = list()
    List2 = list()

##### Generate Random Strings 
    original_list = ['P', 'U', 'B','G']

    for i in range(0, 10):
        sample_list = random.choices(original_list, k=5)
        #print("".join(sample_list))
        List1.append("".join(sample_list))
        
    List2 = random.sample(List1, len(List1))


    my_list = list()

    for original in List1:
        for shuffled in List2:
            count=0
            for i,(x,y) in enumerate(zip(original,shuffled)):
                if x != y:
                    #print(original,shuffled,f'char miss-match {x,y} in element {i}')
                    count +=1
#           print(original,shuffled,count)
            myFString = original + " " + shuffled + " " + str(count)
            my_list.append(myFString)
    return (my_list)        

### Random string generation function Ends

        
my_list = shout()
for items in my_list:
            print(items)

I am able to do things correctly. However, I need to keep at least 3 mismatches between each string in the table. This is where the problem comes. Sometimes I get only 2 or 1 mismatch between the strings. One of the solutions was to call the random string generation function again and again.

Can you guys kindly have a look at my code and suggest how I can improve my code to take care of this issue?

Thanks in advance.




Aucun commentaire:

Enregistrer un commentaire