1

I have following list that contains string

data=[]
data.append('BBBB')
data.append('AAAA')
data.append('CCCC')

How can i make my code print the following result? Each line cannot have repeated values like BBBBBBBB.

BBBB
AAAA
CCCC
BBBBAAAA
BBBBCCCC
AAAABBBB
... snippet ...

3 Answers 3

1

Here is a one line solution:

>>> from itertools import chain, permutations
>>> print(*chain(*(map("".join, permutations(data, i)) for i in range(1, 3))), sep="\n")
BBBB
AAAA
CCCC
BBBBAAAA
BBBBCCCC
AAAABBBB
AAAACCCC
CCCCBBBB
CCCCAAAA
Sign up to request clarification or add additional context in comments.

Comments

0

This code might help you out,

from itertools import permutations as per

input_nos = int(input('the number of strings'))   #for specifying the input iterations

strings = []

for i in range(input_nos):
    strings.append(input())

strings_per = per(strings, input_nos -1 )  # as specified

for i in strings_per:
    print(''.join(i))

Comments

0
strings_list = ['AAAA', 'BBBB', 'CCCC', 'DDDD', 'EEEE']

def permute_string(a):
    b = []
    b.extend(a)
    for i in range(len(a)-1):
        for j in range(i+1, len(a)):
            b.append(a[i] + a[j])
    return b

permute_string(strings_list)

Based on what information you have give, this should do your work.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.