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. Below is an example Table to give an idea about the problem. In this example, even 1 or 2 are not allowed. I want to allow only >= 3 mismatches. That is where I am struggling actually as you can see from my program below.

AANAU   0   4   4   4   3

AMMMN   4   0   3   5   4

NMNMM   4   3   0   4   3

NUAUU   4   5   4   0   5

UANMA   3   4   3   5   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)
    List2 = random.sample(List1, len(List1))

    my_list = list()

    for original in List1:
        for shuffled in List2:
            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)
    return (my_list)        

### Random string generation function Ends

my_list = shout()
for items in my_list:

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.

1 answer

  • answered 2021-11-29 13:45 Websurfer

    Maybe something like below could help ?

    a = 1 
    my_list = list()
    ListRandom = list()
    while (a<10):
        my_list = shout()
        for items in my_list:
            if (int(items.split(' ')[2]) != 1 or int(items.split(' ')[2]) != 2 ):
                my_list = shout()
                print ('While loop terminated')

    Suggestions please ?

How many English words
do you know?
Test your English vocabulary size, and measure
how many words do you know
Online Test
Powered by Examplum