2
\$\begingroup\$

This question already has an answer here:

Challenge

For any string that is composed of alphabetical characters of any case, make a function that returns a list of all of it's variations, order and case-wise empty strings returns empty list.

Test Cases

"" -> []
"a" -> ['a', 'A']
"ab" -> ['ab', 'Ab', 'aB', 'AB', 'ba', 'Ba', 'bA', 'BA']
"abc" -> ['abc', 'Abc', 'aBc', 'ABc', 'abC', 'AbC', 'aBC', 'ABC', 'bac', 'baC', 'bAc', 'bAC', 'Bac', 'BaC', 'BAc', 'BAC', 'CBa', 'CBA', 'cba', 'cbA', 'cBa', 'cBA', 'Cba', 'CbA', 'caB', 'cAb', 'cAB', 'cab', 'CaB', 'CAb', 'CAB', 'Cab', 'Acb', 'AcB', 'acb', 'acB', 'aCb', 'aCB', 'ACb', 'ACB', 'Bca', 'BcA', 'bca', 'bcA', 'bCa', 'bCA', 'BCa', 'BCA']
'aa' -> ['aa', 'Aa', 'aA', 'AA']  (for duplicate letters extra, duplicate permutations are allowed if necessary)

As the permutations rack up quickly you only need to handle up to 4 ASCII letter chars.

This is , so the shortest code in bytes wins!

\$\endgroup\$

marked as duplicate by FlipTack, pajonk, Mego code-golf Dec 17 '17 at 12:33

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 1
    \$\begingroup\$ Why is this getting reopen votes? It's an exact dupe... \$\endgroup\$ – Rɪᴋᴇʀ Dec 16 '17 at 19:03
  • 2
    \$\begingroup\$ This uses place variation too \$\endgroup\$ – FantaC Dec 16 '17 at 19:05
  • \$\begingroup\$ A couple questions. What should the input 'aa' give? Also, for the output format, since each permutation is of known length, do we need delimiters between all of them? \$\endgroup\$ – dylnan Dec 17 '17 at 1:48
  • 2
    \$\begingroup\$ This seems like a combination of two problems, permutations and case-variants lol \$\endgroup\$ – HyperNeutrino Dec 17 '17 at 1:55
  • 1
    \$\begingroup\$ I still don’t understand why people want this closed. \$\endgroup\$ – dylnan Dec 17 '17 at 17:29
3
\$\begingroup\$

Python 2, 89 bytes

lambda s:map(list,map(permutations,product(*zip(s.swapcase(),s))))
from itertools import*

Try it online!

Doesn't work in Python 3. Swap case idea from HyperNeutrino.

\$\endgroup\$
  • 1
    \$\begingroup\$ You can golf this a tad with replacing "list" with "set". Uglier output, but it works fine. \$\endgroup\$ – Rɪᴋᴇʀ Dec 17 '17 at 21:14
2
\$\begingroup\$

Jelly, 9 bytes

żŒsp/Œ!€Ẏ

Try it online!

Explanation

żŒsp/Œ!€Ẏ  Main Link
ż          zip each character with
 Œs        its case swapped
   p/      reduce over cartesian product
       €   for each sublist
     Œ!    find all permutations
        Ẏ  flatten by one layer
\$\endgroup\$
  • \$\begingroup\$ Lol I always get beat by a lot on my 12+ byte answers. Although if you're not doing ŒṘ or removing duplicates I might take mine out... \$\endgroup\$ – dylnan Dec 17 '17 at 2:09
  • 1
    \$\begingroup\$ @dylnan I personally think that's unecessary. OP didn't specify so just leave it out unless OP specifies :P \$\endgroup\$ – HyperNeutrino Dec 17 '17 at 2:13
  • \$\begingroup\$ Updated! @dylnan \$\endgroup\$ – FantaC Dec 29 '17 at 16:55
1
\$\begingroup\$

Jelly, 11 bytes

Œu,ŒlZŒpŒ!€

Try it online!

16 bytes if we need formatting and no repeated permutations.

Œu,ŒlZŒpŒ!€;/QŒṘ

Try it online!

This is assuming (a) we need delimiters between the permutations (achieved by ŒṘ at the end) and (b) the input 'aa'-->['AA','Aa','aA','aa'] and doesn't count the two 'a's as unique, which would give the same answer but each permutation would appear twice.

Explanation

Œu,ŒlZŒpŒ!€;/QŒṘ   Main link
Œu                 Upper case. 'aB'->'AB'
   Œl              Lower case. 'aB'->'ab'
  ,                Pair dyad. -> [['A','B'],['a','b']]
     Z             Zip the columns. -> [['A','a'],['B','b']]
      Œp           Cartesian product of the elements in the outermost list. -> [['A','B'],['A','b'],['a','B'],['a','b']]
        Œ!€        The permutations applied at €ach element in the list. -> [[['A','B'],['B','A']],[['A','b'],...
                   The rest is deleting duplicates and formatting.
           ;/      Concatenates the lists of permutations. In Essentially flattens the list by one level.
             Q     Remove repeated elements
              ŒṘ   Print in Python's string format
\$\endgroup\$
0
\$\begingroup\$

JavaScript (ES6), 169 bytes

I/O format: arrays of characters.

a=>(r=[],P=(a,m=[])=>a.map((_,i)=>P(b=[...a],[...m,...b.splice(i,1)]))>[]||(g=i=>i>>m.length||g(-~i,r.push(m.map((c,j)=>c[`to${i>>j&1?'Upp':'Low'}erCase`]()))))())(a)&&r

Not the right tool for the job... Still, there's probably a golfier way.

Demo

let f =

a=>(r=[],P=(a,m=[])=>a.map((_,i)=>P(b=[...a],[...m,...b.splice(i,1)]))>[]||(g=i=>i>>m.length||g(-~i,r.push(m.map((c,j)=>c[`to${i>>j&1?'Upp':'Low'}erCase`]()))))())(a)&&r

f([...'abc']).forEach(a => console.log(a.join('')))

\$\endgroup\$

Not the answer you're looking for? Browse other questions tagged or ask your own question.