Home > Software engineering >  Find every possibility to insert random number into a string? (python)
Find every possibility to insert random number into a string? (python)

Time:10-30

I'm new to programming and for an exercise I need to create a list of every possibility to insert a random number (from 0 to 9) in to a string. The number can be inserted et every position in this string.

For example I have the string "Aht50rj2" and I need to find every possibility to insert one number at any position in this string (including the beginning and the end).

So far I wasn't able to find a way to solve this. What is the best way to do this?

Edit: The Input is a String (like for example "Aht50rj2") And the expected output is a list with all possible ways. For example ["0Aht50rj2", "Ah1t50rj2", "Aht50rj29", "Aht501rj2", etc.]

CodePudding user response:

def possibility(word):
    possibilities = []
    for i in range(0,10):
        for j in range(len(word) 1):
            H  = [k for k in word]
            H.insert(j,str(i))
            possibilities.append(''.join(H))
    return possibilities

CodePudding user response:

I'm not sure what you mean but try my code and see if it worked:

import random   # import module "random"

example_string = input('Input a string: ') # Asks user to input a string

length_of_string= len(example_string)  # Counts how many characters there are in the string, it'll make sense trust me

example_list=[]  # Create a temporary list
example_list[:0]=example_string  # Get string, turns it into list

example_list.insert(random.randrange(0, length_of_string), str(random.randrange(0, 9)))  # Insert a random number into a random position of the list

list_into_string= ''.join(example_list)  # Get string, turns it into string
print(list_into_string)  # Print result

Result number 1:

Input a string: stackoverflow
sta1ckoverflow

Result number 2:

Input a string: stackoverflow
stacko8verflow

Result number 3:

Input a string: stackoverflow
stackoverfl5ow

Hope this helps!

CodePudding user response:

You can loop with string length, then replace str(num) with each char in any index like below:

import random
st = "Aht50rj2"
res = []
for idx in range(len(st)):
    r_c = random.choice(range(9))
    char = st[idx]
    res.append(st.replace(st[idx], str(r_c)))

print(res)

Output: (one runtime)

['3ht50rj2', 'A6t50rj2', 'Ah050rj2', 'Aht20rj2', 'Aht54rj2', 'Aht506j2', 'Aht50r22', 'Aht50rj0']

CodePudding user response:

I'm sure there are multiple ways of doing this, but since python strings can be easily split I used a simple loop to split the string into two halves:

import random

string = "STRING"
out_arr = [None] * (len(string)   1)

for pos in range(len(string)   1):
    rad_int = str(random.randint(0, 9))
    str_a = string[:pos]
    str_b = string[pos:]
    out_arr[pos] = str_a   rad_int   str_b

print(out_arr)

The int is inserted between the two halves, and the split point moves at each index of the string so this makes the number get inserted at each point of the string.

Output:

['4STRING', 'S7TRING', 'ST4RING', 'STR9ING', 'STRI1NG', 'STRIN9G', 'STRING2']
  • Related