Home > Back-end >  Keywords with total combination of words and character input given by user using a word in Python
Keywords with total combination of words and character input given by user using a word in Python

Time:04-12

I am new user learning python. I have query if it can be done or not. If user input a word suppose "Dance" and character "$", he would get a all possibilities of word and character combination for example ['D$a$n$c$e', 'Da$n$c$e', 'D$an$c$e', 'Danc$e'], etc. It should give a combinations.

I tried

Code:

test_str = "time"  
# Using join()   list comprehension
res = '@'.join(test_str[i:i   1] for i in range(0, len(test_str), 1))
  
# printing result 
res

Output Ans: 't@i@m@e'

It doesnt give the combinations it can form with the character without changing the word("test_str"). Can anyone help with this.

CodePudding user response:

For each combination, whether or not there is a character between two certain letters can be represented as either: a 1 (there is a character), or 0 (there is not). In this way, each combination can be represented as a binary number, and to get all possible combinations, we simply need to count up in binary, and convert each binary number to the correct string.

To do this, we get the range of values whose binary representation is the length of the string minus 1, which is 2 to the power of (the length of the string minus 1). Then, for each of those values, we convert to binary, and then get the correct string from there.

Here is the code:

word = "goose"  # Whatever word you want
char = "$"      # Whatever character you want

combinations = []
for n in range(2 ** (len(word) - 1)):
    bin_n = bin(n)[2:].zfill(len(word) - 1)
    combination = word[:1]
    for i, c in enumerate(word[1:]):
        if bin_n[i] == "1":
            combination  = char
        combination  = c
    combinations.append(combination)

print(combinations)

This prints:

['goose', 'goos$e', 'goo$se', 'goo$s$e', 'go$ose', 'go$os$e', 'go$o$se', 'go$o$s$e', 'g$oose', 'g$oos$e', 'g$oo$se', 'g$oo$s$e', 'g$o$ose', 'g$o$os$e', 'g$o$o$se', 'g$o$o$s$e']

CodePudding user response:

The idea is to "add" permutations of '' and $ to a STRING, e.g. ABCD $''$'' = A$BC$D.
We can get all permutations of '' and $ in the length of STRING by using itertools.product():

Assuming STRING is ABCD, itertool.product(['', '$'], repeat=len(STRING)) generates the tuples
('', '', '', ''), ('', '', '', '$'), ('', '', '$', ''), ... ('$', '$', '$', '$').

import itertools as it

string = 'ABCD'  # input('enter string: ')
fill_char = '$'  # input('enter fill_char: ')

fillers = it.product(['', fill_char], repeat=len(string))
# fillers = ('', '', '', ''), ('', '', '', '$'), ('', '', '$', ''), ... ('$', '$', '$', '$')

For every tuple we then append the first element of the tuple to the first character of our STRING,
the second element to the second character, and so on.

This is done by using itertools.zip_longest():

for filler in fillers:
    tmp = it.zip_longest(string, filler)
    # tmp = (('A', ''), ('B', ''), ('C', ''), ('D', '')),
    #       (('A', ''), ('B', ''), ('C', ''), ('D', '$')),
    #       ...
    #       (('A', '$'), ('B', '$'), ('C', '$'), ('D', ''))
    #       (('A', '$'), ('B', '$'), ('C', '$'), ('D', '$'))

Finally we join the tuples' elements using join() and print the resulting string:

    print(''.join([''.join(char_and_fill_char) for char_and_fill_char in tmp]))
    #                      char_and_fill_char = ('A', '$'), ('B', '$'), ('C', '$'), ('D', '$')
    #              ''.join(char_and_fill_char) = 'A$', 'B$', 'C$', 'D$'
    #     ''.join([''.join(char_and_fill_char)]) = 'A$B$C$D$'

Summary:

import itertools as it

string = 'ABCD'  # input('enter string: ')
fill_char = '$'  # input('enter fill_char: ')

fillers = it.product(['', fill_char], repeat=len(string))

for filler in fillers:
    tmp = it.zip_longest(string, filler)
    print(''.join([''.join(char_and_fill_char) for char_and_fill_char in tmp]))

Output:

ABCD
ABCD$
ABC$D
ABC$D$
AB$CD
AB$CD$
AB$C$D
AB$C$D$
A$BCD
A$BCD$
A$BC$D
A$BC$D$
A$B$CD
A$B$CD$
A$B$C$D
A$B$C$D$
  • Related