0

I have two lists, for example:

digits = ['1', '2', '3', '4']
chars = ['!', '@', '#', '$']

And I need to get all permutations without repetition from these two tables, but on the assumption if I use in a combination first element from "digits" table, I cannot use first one from "chars". Same for second, third etc.

Specific order is not important. But in need to work in other lengths of input list. Max length of input lists is 10 and max length of permutations is 10.

For example, 2 chars length permutations need to look like:

12
13
14
21
23
24
31
32
34
41
42
43
!@
!#
!$
@!
@#
@$
#!
#@
#$
$!
$@
$#
!2
!3
!4
@1
@3
@4
#1
#2
#4
$1
$2
$3
1@
1#
1$
2!
2#
2$
3!
3@
3$
4!
4@
4#

I will be grateful for your help, my mind is blowing from thinking about the solution :)

Kelly Bundy
  • 23,480
  • 7
  • 29
  • 65
Radoslav
  • 3
  • 2
  • 3
    Hi! SO is not a code writing service. Please try the problem on your own first and show us what you've tried. Read [how-to-ask](https://stackoverflow.com/help/how-to-ask). – porrrwal Nov 21 '21 at 23:18
  • 2
    Welcome to Stack Overflow! Please take the [tour]. The example output is a bit long to be comfortably readable. Could you illustrate the same thing with inputs of length 3 and 3? You can [edit] if you want. – wjandrea Nov 21 '21 at 23:22
  • 2
    Does the order matter, e. g., would it be ok if `1@` comes before `13`? – Kelly Bundy Nov 21 '21 at 23:50
  • @KellyBundy specific order is not important - sorry my mistake. But it need to work in other lenghts. – Radoslav Nov 22 '21 at 09:10
  • When the question says "other" lengths, it means other than what shown length? The length of the input lists or the number of input lists? (Can't mean the length of the permutations, as that hasn't yet been talked about at that point.) – Kelly Bundy Nov 22 '21 at 19:51
  • @Kelly [Your solution](https://tio.run/##VY6xDsIwDET3fIURSElQhETLgJCQ@A/EULWUmtI4ct0Bfj40KQsebvDznS@8pSNfHgPH2DINgHJnIXqNgEMgFgh3HiapBMmPDgJTM9WiVIMPlBHOcNV77UAXScokB31TdVfxAldpdUmyTrKZYT@DQqmWOIcD@r8n5oPBLPEOcpB10NuTgnmyae6QTUsXs03uH08TGL0YrXdPQm/SlbUxfgE) would cover all three different lengths, wouldn't it? – wjandrea Nov 22 '21 at 19:55
  • 1
    @wjandrea Yes, and with a *list* of lists as input it would even get shorter :-). Though would need another loop if "*all* premutations" (i.e., all lengths) is the real goal. – Kelly Bundy Nov 22 '21 at 20:02
  • @KellyBundy The length of the input lists. Sorry for being imprecise, english is not my primary language. Max length of input lists is 10 and max length of premutations is 10. As far as I can see your solution is what I need. – Radoslav Nov 22 '21 at 20:10

2 Answers2

2
from itertools import permutations, product

digits = ['1', '2', '3', '4']
chars = ['!', '@', '#', '$']
k = 2

for perm in permutations(zip(digits, chars), k):
    for prod in product(*perm):
        print(''.join(prod))

That's for permutations of length k. If you want all lengths, replace the hardcoded k with for k in range(len(digits) + 1):.

Zipping the lists gives you option pairs:

('1', '!')
('2', '@')
('3', '#')
('4', '$')

Then permutations gives you permutations of those option pairs:

(('1', '!'), ('2', '@'))
(('1', '!'), ('3', '#'))
(('1', '!'), ('4', '$'))
(('2', '@'), ('1', '!'))
(('2', '@'), ('3', '#'))
(('2', '@'), ('4', '$'))
(('3', '#'), ('1', '!'))
(('3', '#'), ('2', '@'))
(('3', '#'), ('4', '$'))
(('4', '$'), ('1', '!'))
(('4', '$'), ('2', '@'))
(('4', '$'), ('3', '#'))

And then feeding each such permutation into product gives you permutations of the strings, for example the product of (('3', '#'), ('1', '!')) gives these:

('3', '1')
('3', '!')
('#', '1')
('#', '!')

Just need to be joined then.

Kelly Bundy
  • 23,480
  • 7
  • 29
  • 65
  • I would like to pay tribute for that. Personally I stuck on permutations() functions and dont know what to do next. Time to learn what cartesian product work. Thanks! – Radoslav Nov 23 '21 at 10:35
0

You can do something like this:

def calculate(digits, chars):
    from itertools import permutations
    indices = list(permutations(range(len(digits)), 2))
    a = [digits[i] + digits[j] for i,j in indices]
    b = [chars[i] + chars[j] for i,j in indices]
    c = [chars[i] + digits[j] for i,j in indices]
    d = [digits[i] + chars[j] for i,j in indices]
    return a + b + c + d

This just computes the permutation indices required. Your goal is to then just combine the values from the indices in the respective arrays. Here, a, b, c, d are the 4 combinations required to produce your output. Notice how the digits and chars are combined.

Chrispresso
  • 3,660
  • 2
  • 19
  • 31