| Bytes | Lang | Time | Link |
|---|---|---|---|
| 004 | Vyxal | 240812T023914Z | emanresu |
| 052 | Ruby | 221022T023559Z | Jordan |
| 039 | Attache | 180503T031315Z | Conor O& |
| 037 | Perl 6 | 160602T035915Z | Brad Gil |
| 058 | Haskell | 160531T173707Z | nimi |
| 030 | Perl 5 | 180501T194620Z | Dom Hast |
| 165 | Tcl | 160531T211246Z | Dút |
| 090 | JavaScript Firefox 3057 | 160531T225041Z | Neil |
| nan | Perl 5 | 171212T215930Z | Xcali |
| 055 | Wolfram Language Mathematica | 171212T125906Z | Martin E |
| 096 | Python3 | 170221T170803Z | Blocks |
| 252 | C | 170218T130416Z | Abel Tom |
| 216 | C | 170218T145434Z | MrPaulch |
| 022 | Brachylog | 160531T144910Z | Fatalize |
| 028 | Actually | 160602T063319Z | user4594 |
| nan | Pyth | 160531T132650Z | FryAmThe |
| 069 | Python | 160601T054000Z | RootTwo |
| 060 | Julia | 160531T195305Z | Dennis |
| 103 | JavaScript ES6 | 160531T205417Z | edc65 |
| 276 | Oracle SQL 11.2 | 160531T183432Z | Jeto |
| 071 | Python | 160531T172558Z | Dennis |
| 006 | Jelly | 160531T150710Z | Dennis |
| 242 | Hoon | 160531T150057Z | RenderSe |
| 017 | 05AB1E | 160531T143917Z | Emigna |
| 013 | MATL | 160531T140505Z | Luis Men |
Attache, 39 bytes
&Cross[Sum@V]##Unique@V#SwapCase=>Chars
Similar to the perl answer. (I've lost my more interesting alternative, I should be posting those in the next few hours.)
Perl 6, 37 bytes
{[X~] '',|.comb.map:{unique .lc,.uc}}
Explanation:
{
[X[~]] # cross combine using &infix:<~> operator
'', # empty string so that 1 character strings work
| # flatten the following into outer list
.comb # get every character from input string
.map: # and map it with:
{ unique .lc, .uc }
}
Test:
#! /usr/bin/env perl6
use v6.c;
use Test;
my &case-permutation = {[X~] '',|.comb.map: {unique .lc,.uc}}
my @tests = (
'a1a' => <a1a a1A A1a A1A>,
'abc' => <abc abC aBc aBC Abc AbC ABc ABC>,
'Hi!' => <hi! hI! Hi! HI!>,
'ž1a' => <ž1a ž1A Ž1a Ž1A>,
);
plan +@tests;
for @tests -> $_ (:key($input),:value($expected)) {
is case-permutation($input).sort, $expected.sort, .gist
}
1..4
ok 1 - a1a => (a1a a1A A1a A1A)
ok 2 - abc => (abc abC aBc aBC Abc AbC ABc ABC)
ok 3 - Hi! => (hi! hI! Hi! HI!)
ok 4 - ž1a => (ž1a ž1A Ž1a Ž1A)
Haskell, 69 58 bytes
import Data.Char
mapM(\x->toLower x:[toUpper x|isAlpha x])
Edit: @Angs saved 11 bytes. Thanks!
Perl 5, 30 bytes
s/\pl/{\l$&,\u$&}/g;say<"$_ ">
Outputs an additional space at the end of the output.
Tcl, 165 181 bytes
set n -1
while {[incr n]<1<<[llength [set s [split $argv {}]]]} {puts [join [lmap c $s b [split [format %0[llength $s]b $n] {}] {string to[expr $b?{u}:{l}] $c}] ""]}
Improvements thanks to sergiol. Previous answer:
set s [split $argv {}]
set n -1
while {[incr n]<1<<[llength $s]} {set r ""
foreach c $s b [split [format %0[llength $s]b $n] {}] {set r $r[string [expr $b?{tou}:{tol}] $c]}
puts $r}
Uses a binary number to choose between upper/lower case when creating the output text.
JavaScript (Firefox 30-57), 92 90 bytes
f=([c,...s])=>c?[for(t of f(s))for(d of new Set(c.toUpperCase()+c.toLowerCase()))d+t]:['']
Edit: Saved 2 bytes because new Set will happily extract the unique characters from a string.
Wolfram Language (Mathematica), 55 bytes
""<>#&/@Union@Tuples[{#,ToUpperCase@#}]&@*Characters
is the transpose operator (and displays as a superscript T in Mathematica).
Python3, 96 bytes
i=input().lower()
for l in{*__import__('itertools').product(*zip(i,i.upper()))}:print(*l,sep='')
Late to the party but still had a go. Thanks to DLosc for reminding me of the stuff I missed, giving me golfing tips and saving me a bunch of bytes. :)
C 229 252 bytes
i,n,j,k,l;f(char *s){l=strlen(s);for(i=0;i<l;i++)s[i]=tolower(s[i]);int v[l];for(i=0;i<l;i++)v[i]=0;for(i=0;i<pow(2,l);i++){n=i,k=0;for(;n;k++){v[k]=n;n/=2;}for(j=0;j<l;j++){v[j]%=2;if(v[j])s[j]=toupper(s[j]);else s[j]=tolower(s[j]);}printf("%s ",s);}}
Ungolfed version:
void f(char *s)
{
int i,num,k,l=strlen(s);
for(i=0;i<l;i++)
s[i]=tolower(s[i]);
int v[l];
for(i=0;i<l;i++)
v[i]=0;
for(i=0;i<pow(2,l);i++)
{
num=i,k=0;
for(;num;k++)
{
v[k]=num;
num/=2;
}
for(int j=0;j<l;j++)
{
v[j]%=2;
if(v[j])
s[j]=toupper(s[j]);
else
s[j]=tolower(s[j]);
}
printf("%s \n",s);
}
}
Explanation:
- Accept the character string, convert the string to lowercase.
- Declare integer array of length equal to that of the string. Fill it with zeroes.
- Store the numbers from 0 to
2^strlen(s)in binary form in anintarray.( For a 3 byte string: 000,001,010...111) - Depending on if a bit at a position is set or, toggle the case.
- Output the string for every possible combination.
C, 216 bytes
k,i,j,p,n,m;z(char *c){n=-1;m=0;while(c[++n])if(c[n]>64&c[n]<90)c[n]+=32;else if(c[n]<'a'|c[n]>'z')m++;k=1<<(n-m);for(j=0;j<k;j++){for(i=0;i<n;i++){p=1<<i;putc((j&p)==p?toupper(c[i]):c[i],stdout);}putc(0xa,stdout);}}
This is a different approach, the same approach as the other C answer.
Should I delete this, and put it under the other answer as a comment?
Let me explain with the Ungolfed version
k,i,j,p,n,m;
z(char * c) {
int n=-1; // We start at -1 because of forward incrementation
int m=0; // this will count the characters we don't have to manipulate
while(c[++n]) // go until we reach '\0'
{
if(c[n]>='a'&c[n]<='z')c[n]-=32; // If we are lower case, then convert
else if(c[n]<'A'|c[n]>'Z')m++; // If we are neigther lower case
// nor upper, then make a note
}
// get 2 ^ ("length" - "number of invonvertibles")
k=1<<(n-m);
for(j=0;j<k;j++) { // go through the combinations
for(i=0;i<n;i++) { // for each combination go though the characters
p=1<<i; // for each character get it's bit position
putc(
// if the bit position is set (==1)
(j&p)==p ?
tolower(c[i]) // convert
: c[i], // else: don't
stdout);
}
putc(0xa, stdout); // print a newline
}
}
Brachylog, 25 22 bytes
:ef:1fd.
:2ac.
@u.|@l.
This works as well as the lowercase/uppercase predicates of Prolog, therefore it works on non-ASCII letters too:
?- run("ž1a",Z).
Z = ["Ž1A", "Ž1a", "ž1A", "ž1a"] .
Explanation
Unlike all other answers as of the moment I'm posting this, this does not use the cartesian product approach at all.
Main Predicate
:ef Split the Input string into a list of 1-char strings :1f Find all valid outputs of predicate 1 with the previous list of outputs as input d. Unify the Output with that list excluding all duplicatesPredicate 1
This is used to apply uppercasing or lowercasing on each char of the input, thus computing one possible permutation. Using findall on this predicate in the main predicate allows to compute all possible permutations (with some duplicates).
:2a Apply predicate 2 on the each element of the Input
c. Unify the Output with the concatenation of the elements of
the previous list
- Predicate 2
This is used to turn a character of the string into either its uppercase or its lowercase version.
@u. Unify the Output with the uppercase version of the Input
| Or
@l. Unify the Output with the lowercase version of the input
Actually, 28 bytes
;╗l2r∙`"'Ö*£"£M╜@Z"iƒ"£MΣ`M╔
This program can handle non-ASCII characters, thanks to the magic of Python 3.
Explanation:
;╗l2r∙`"'Ö*£"£M╜@Z"iƒ"£MΣ`M╔
;╗ save a copy of input to reg0
l length of input
2r [0,1]
∙ Cartesian product with self (length of input) times
` `M map:
"'Ö*£"£M push `Ö` (swapcase) if 1 else `` for each value in list
╜@Z zip with input
"iƒ"£M swap the case of those values
Σ join string
╔ unique elements
Pyth, 13 12 11
{msrVQd^U2l
1 byte thanks to Leaky Nun!
Another byte thanks to Jakube!
Try it here or run a Test Suite
We create a list of lists True/False values by taking the cartesian product of the list [0, 1] with itself a number of times equal to the length of the input string. So each of the sublists has the same length as the input string. Then we apply the r function as a vector operation over the input and the list, so we get r letter value for each sub element. r with second argument zero is to lowercase and with one it is to upper case. This creates duplicates on non-letters, which means we need to remove duplicates from the result.
Python, 69 bytes
import itertools as i;f=lambda s:set(i.product(*zip(s,s.swapcase())))
JavaScript (ES6), 103
Handles non-ASCII characters
(a,r=new Set)=>a?f(a.slice(1)).map(v=>(C=o=>r.add(a[0][`to${o}erCase`]()+v),C`Upp`,C`Low`))&&[...r]:[a]
Test
f=(a,r=new Set)=>a?f(a.slice(1)).map(v=>(C=o=>r.add(a[0][`to${o}erCase`]()+v),C`Upp`,C`Low`))&&[...r]:[a]
function test() { O.textContent = f(I.value).join('\n') }
test()
<input id=I oninput='test()' value='ž1a'>
<pre id=O></pre>
Oracle SQL 11.2, 276 bytes
WITH v AS(SELECT SUBSTR(:1,LEVEL,1)c,ROWNUM p FROM DUAL CONNECT BY LEVEL<=LENGTH(:1))SELECT w FROM(SELECT REPLACE(SYS_CONNECT_BY_PATH(c,','),',','')w FROM(SELECT UPPER(c)c,p FROM v UNION SELECT LOWER(c),p FROM v)START WITH p=1CONNECT BY PRIOR p=p-1)WHERE LENGTH(:1)=LENGTH(w);
Un-golfed
WITH v AS
( -- Split input into an array of characters
SELECT SUBSTR(:1,LEVEL,1)c,ROWNUM p FROM DUAL CONNECT BY LEVEL<=LENGTH(:1)
)
SELECT w
FROM ( -- Build every string combination
SELECT REPLACE(SYS_CONNECT_BY_PATH(c,','),',','')w
FROM ( -- Merge upper and lower arrays, keep same position for each character, it allows to mix cases
SELECT UPPER(c)c,p FROM v UNION SELECT LOWER(c),p FROM v
)
START WITH p=1 -- Start with first character (either lowercase or uppercase)
CONNECT BY PRIOR p=p-1 -- Add the next character (either lowercase or uppercase)
)
WHERE LENGTH(:1)=LENGTH(w); -- Keep only full strings
Ugly as hell, must be more golfable.
Python, 74 71 bytes
f=lambda s:s and{r[0]+t for r in{s,s.swapcase()}for t in f(s[1:])}or{s}
Handles non-ASCII characters. Test it on Ideone.
Jelly, 6 bytes
żŒsŒpQ
This is a monadic link (function) that expects a string as left argument and returns a list of strings.
Handles non-ASCII characters. Try it online!
How it works
żŒsŒpQ Monadic link. Argument: s (string)
Œs Swapcase; change the case of all letters in s.
ż Zipwith; pair each character with itself with changed case.
Œp Take the Cartesian product of all pairs.
Q Unique; deduplicate the Cartesian product.
Hoon, 242 bytes
|=
t/tape
=+
l=(reap (pow 2 (lent t)) t)
%+
roll
(gulf 0 (dec (lent l)))
|=
{a/@ b/(set tape)}
=+
%+
turn
(gulf 0 (dec (lent t)))
|=
n/@
=+
t=(snag n t)
=+
k=(trip t)
?:
=(0 (cut 0 n^1 a))
?:
=((cuss k) t)
(cass k)
(cuss k)
t
(~(put in b) -)
Ungolfed:
|= t/tape
=+ l=(reap (pow 2 (lent t)) t)
%+ roll (gulf 0 (dec (lent l)))
|= {a/@ b/(set tape)}
=+ %+ turn (gulf 0 (dec (lent t)))
|= n/@
=+ t=(snag n t)
=+ k=(trip t)
?: =(0 (cut 0 n^1 a))
?: =((cuss k) t)
(cass k)
(cuss k)
t
(~(put in b) -)
I'm not sure how much smaller this could be, unfortunately.
First, we set l equal to a list with 2^(length t) repetitions of t. Hoon doesn't have a fac function in the stdlib, but 2^n is always bigger than n!, so we simply map over the bigger list and use a set (hashmap) to de-duplicate entries.
We then fold over the list [0..(length l)], accumulating into a (set tape). We need to do this instead of mapping over l directly because we also need to know what number repetition it is (a), but can't simply increment an accumulator due to Hoon being a pure language.
We map over [0..(length t)] (again so we have the current index), setting t to the nth character in the string, checking if the nth bye of a and inverting the case (cuss or cass, depending if it changes or not). The return type of this map is a tape.
We then put the string into our hashmap, and return the hashmap of all the strings.
05AB1E, 17 bytes
Code:
vyDš‚N0Êiâvy˜J})Ù
Explained:
vy # for each character in input
Dš‚ # create a pair of different case, eg: ['ž', 'Ž']
N0Êiâ # for all pairs but the first, take cartesian product
result will be a list of layered lists eg: [['ž', '1'], 'a']
vy # for each such list
˜J} # deep flatten and join as a string eg: ž1a
)Ù # wrap in array and remove duplicates
MATL, 13 bytes
tYov!Z}N$Z*Xu
Explanation
t % Implicit input string. Duplicate
Yo % Change case of string
v % Concatenate as a 2xN char array, where N is input length
! % Transpose: Nx2 char array. Each row has different case, if letter
Z} % Split into rows: gives N strings of 2 chars. Each char has different
% case if it's a letter, or is repeated otherwise
N$ % Specify N inputs for next function
Z* % Cartesian product of the N strings. Each combination is a row.
% Repeated chars (i.e. non-letters) give rise to duplicate rows.
Xu % Remove duplicate rows. Implicit display