| Bytes | Lang | Time | Link |
|---|---|---|---|
| 057 | Perl 5 p | 240327T152001Z | Xcali |
| 014 | Vyxal | 240828T063617Z | emanresu |
| nan | Ruby | 161220T054754Z | Value In |
| 100 | Python | 170101T205135Z | Cormac |
| 093 | JavaScript | 161220T215056Z | Washingt |
| 197 | C# | 161222T210253Z | Erresen |
| 207 | Haskell | 161220T213749Z | archaeph |
| 016 | 05AB1E | 161219T234453Z | Adnan |
| 200 | Java 7 | 161220T154556Z | Kevin Cr |
| 239 | C# | 161220T131031Z | satibel |
| nan | 161220T053118Z | Brad Gil | |
| 084 | Perl 6 | 161220T005844Z | smls |
| 102 | PHP | 161219T235204Z | Titus |
| 025 | Pyke | 161220T000626Z | Blue |
| 030 | MATL | 161219T231453Z | Luis Men |
| 036 | V | 161219T230738Z | DJMcMayh |
Vyxal, 14 bytes
‛ß₌£Nƛ⇩¥h=[N&Ǔ
Try it Online! Outputs as a char list, s flag formats into string.
£ # Store into the register
‛ß₌ # compressed string "christmas"
N # Swap case of each letter
ƛ # Over each letter
[ # If
⇩ # The letter, lowercased
= # equals
¥h # The first char of the register
N # Switch the case again
&Ǔ # And shift the first char of the register to the end
Ruby, 63+1 = 64 bytes
Uses the -p flag.
i=0;gsub(/./){|c|c=~/#{"christmas"[i%9]}/i?(i+=1;c):c.swapcase}
Python 100 bytes
def a(s,i=0,g=''):
for c in s:a=c.lower()=='christmas'[i%9];i+=a;g+=[c.swapcase(),c][a]
return g
JavaScript, 122 118 114 107 104 93 bytes
f=
s=>s.replace(/./g,c=>(k=c.toLowerCase())=='christmas'[i%9]?++i&&c:k!=c?k:c.toUpperCase(),i=0)
F=s=>console.log(f(s))
F(`Hello World!`)
F(`I like pie :)`)
F(`hELP my KeYboarD
iS BROKEN`)
F(`cHRISTMAS IS COMING REALLY SOON!`)
F(`C is the first letter in cHRISTMAS`)
- 11 bytes off thanks @Neil.
C# 197 bytes
Not going to win with this, but hopefully the smallest C# implementation that works...
string C(string s){int i=0,j=0;var r="";for(;i<s.Length;){char c=s[i++],o=(char)32;if(c=="christmas"[j]|c=="CHRISTMAS"[j])j=j>7?0:j+1;else if(c>64&c<91)c+=o;else if(c>96&c<123)c-=o;r+=c;}return r;}
Explanation:
string C(string s)
{
// define our two index ints
// i for indexing across the input string
// j for indexing across christmas
int i = 0, j = 0;
// r is our return string
var r = "";
// declare our loop
// skip the initialisation and afterthought
for (; i < s.Length;)
{
// get our current character c, and increment index i
// initial our offset char o (difference between upper and lower case)
char c = s[i++], o = (char)32;
// check if c is the current character in our christmas bug
if (c == "christmas"[j] | c == "CHRISTMAS"[j])
// increment j (or reset to 0)
j = j > 7 ? 0 : j + 1;
// else if c is an upper case char
else if (c > 64 & c < 91)
// add our offset to make it lower case
c += o;
// else if c is lower case
else if (c > 96 & c < 123)
// subtract our offset to make it upper case
c -= o;
// append c to our return string r
r += c;
}
return r;
}
Haskell, 222 207 Bytes
import Data.Char
s=(+(-65)).ord
f=(`divMod`32).s
l=[['a'..'z'],['A'..'Z']]
c=cycle$map s"CHRISTMAS"
k _[]=[]
k(a:b)(x:y)|isAlpha x=let(d,m)=f x in if(m==a)then(x:k b y)else(l!!d!!m:k(a:b)y)|1>0=x:k(a:b)y
main=interact$k c
updated:
import Data.Char
s=(+(-65)).ord
k _[]=[]
k(a:b)(x:y)|isAlpha x=let(d,m)=s x`divMod`32 in if(m==a)then(x:k b y)else([['a'..'z'],['A'..'Z']]!!d!!m:k(a:b)y)|1>0=x:k(a:b)y
main=interact$k$cycle$map s"CHRISTMAS"
How it works:
s=(+(-65)).ords x = ASCII value of x - ASCII value of 'A'
f=(`divMod`32).sf (s x) = (0,s x) for uppercase, (1, (s x-32)) for lowercase
l=[['a'..'z'],['A'..'Z']]parallel list of letters, indexable by f (lowercase->1->uppercase, uppercase->0->lowercase)
c = cycle $ map s "CHRISTMAS"infinite list of the ascii values of uppercase christmas repeated
k _ []=[]base case
k (a:b) (x:y) | isAlpha x = let (d,m) =f x in if m == a then x : k b y else (l!!d)!!m : k (a:b) y | 1 > 0 = x : k (a:b) yreturn non-alphanumeric characters, and either keep the letter if its s-value is the same as the current letter of christmas (going to the next letter), otherwise convert it to the other case and proceed
main=interact$k cIO
05AB1E, 16 bytes
Thanks to Emigna for saving a byte and fixing a bug!
vyÐl'ŒÎ¾èQi¼ëš}?
Explanation:
vy # For each character in the string...
Ð # Triplicate that character
l # Convert to lowercase
'ŒÎ # Compressed version of "christmas"
¾ # Push the counting variable (let's call this N)
è # Get the nth character of "christmas", with modular indexing
Qi } # If equal...
¼ # N += 1
ë # Else...
š # Swapcase
? # Print that character
Uses the CP-1252 encoding. Try it online!
Java 7, 200 bytes
String c(char[]a){String r="";int i=0,s;Character l='a';for(char c:a)if((s="christma".indexOf(l=l.toLowerCase(c)))==i|i>7&s==4){r+=c;i=i>7?0:i+1;}else r+=l.isUpperCase(c)?l:l.toUpperCase(c);return r;}
Ugly, but it works.. Can definitely without any doubt be golfed more.. I'm rusty..
Ungolfed:
String c(char[] a){
String r = "";
int i = 0,
s;
Character l = 'a';
for(char c : a){
if((s = "christma".indexOf(l = l.toLowerCase(c))) == i) | i > 7 & s == 4){
r += c;
i = i > 7
? 0
: i+1;
} else{
r += l.isUpperCase(c)
? l
: l.toUpperCase(c);
}
}
return r;
}
Test code:
class M{
static String c(char[]a){String r="";int i=0,s;Character l='a';for(char c:a)if((s="christma".indexOf(l=l.toLowerCase(c)))==i|i>7&s==4){r+=c;i=i>7?0:i+1;}else r+=l.isUpperCase(c)?l:l.toUpperCase(c);return r;}
public static void main(String[] a){
System.out.println(c("i CAN HARDLY WORK LIKE THIS please GET ME A NEW KEYBOARD FOR cHRISTMAS".toCharArray()));
System.out.println(c("Hello World!".toCharArray()));
System.out.println(c("I like pie :)".toCharArray()));
System.out.println(c("hELP my KeYboarD\niS BROKEN".toCharArray()));
System.out.println(c("cHRISTMAS IS COMING REALLY SOON!".toCharArray()));
System.out.println(c("C is the first letter in cHRISTMAS".toCharArray()));
}
}
Output:
I Can HaRdly work lIke thiS PLEASE geT Me A new keyboard for ChriStmas
hELLO wORLD!
i LIKE PIE :)
Help MY kEyBOARd
Is broken
cHRISTMAS is Coming really soon!
C IS ThE FIrST LETTER iN ChriSTMAS
C# 239 chars
var s=Console.ReadLine().ToCharArray();int j=0,i=0;var c="christmas";for(;j<s.Length;j++)if(s[j]==c[i%9]|s[j]==(c[i%9]-32))i++;else if(s[j]>64&s[j]<91)s[j]=(char)(s[j]+32);else if(s[j]>96&s[j]<123)s[j]=(char)(s[j]-32);Console.WriteLine(s);
more explicit version:
var s = Console.ReadLine().ToCharArray();
int j = 0,i = 0;
var c = "christmas";
for (var j = 0; j < s.Length; j++)
if (s[j] == c[i%9]|s[j] == (c[i%9] - 32))// non case sensitive compare with c
i++;//next char in christmas
else
if (s[j] > 64 & s[j] < 91)//if between A and Z
s[j] = (char)(s[j] + 32);//convert to a-z
else
if (s[j] > 96 & s[j] < 123)//if between a and z
s[j] = (char)(s[j] - 32);//convert to A-Z
Console.WriteLine(s);
this is a pretty naïve solution, and probably can be improved (maybe we can allow implicit conversion to char?).
it assumes to be inside a function, reads from the console (stdin), and writes to it (stdout).
edit: Char.IsUpper(s[j]) is 2 bytes longer than s[j]>64&&s[j]<91, Char.ToUpper is longer than my version too.
Perl 6, 80 bytes
{my$i=0;S:g{.?<!{'christmas'.comb[$i%9]eq$/.lc&&++$i}>}=$/eq$/.lc??$/.uc!!$/.lc}
{ # bare block lambda with implicit parameter 「$_」
my $i = 0; # counter
S # substitute and return ( implicitly against 「$_」 )
:global
{
. # any char
? # work around a bug where 「$/」 doesn't get set
<!{ # fail this match if this block returns True
'christmas'.comb\ # a list of the characters of 「christmas」
[ $i % 9 ] # grab a char from the list
eq # is it equal to
$/.lc # the lowercase version of the char
&& # if so
++$i # increment 「$i」 ( result is True )
}>
}
= # for each matched char
$/ eq $/.lc # is it lowercase?
?? $/.uc # the uppercase it
!! $/.lc # otherwise lowercase it
}
Perl 6, 84 bytes
{my $i=0;[~] (.lc~~"christmas".comb[$i%9]??(++$i&&$_)!!.ord>90??.uc!!.lc for .comb)}
PHP, 113 110 102 bytes
while($o=ord($c=$argv[1][$i++]))echo chr(32|$o==ord(christmas[$k%9])?$o|0&$k++:ctype_alpha($c)*32^$o);
takes input from first command line argument. Run with -r.
breakdown
while($o=ord($c=$argv[1][$i++]))// loop through string characters
echo chr(
32|$o==ord(christmas[$k%9]) // if $c equals next character in "christmas"
?$o|0&$k++ // no change, increase "christmas" index
:ctype_alpha($c) // else if $c is a letter
*32^$o // toggle bit 5 of the ascii code
);
MATL, 36 30 bytes
"@tk'schristma'H)=?HQXHx}Yo]&h
Strings with newlines need to be defined by concatenating with the ASCII code 10 (see example in the link with the test cases).
Try it online! Or verify all test cases.
Explanation
" % Implicit input of a string. For each character in that string
@ % Push current character
tk % Duplicate and convert to lowercase
'schristma' % Push string. This is 'Christmas' in lowercase and circularly
% shifted such that the 'c' is in the second position
H % Push contents of clipboard H, which is initiallized to 2.
% This value will be gradually increased when a new character
% from the the sequence is found
) % Get character from 'schristma' at that (modular) position
= % Are they equal?
? % If so
HQ % Push contents of clipboard H and add 1
XHx % Copy into clipboard K and delete
} % Else
Yo % Change case
] % End
&h % Concatenate stack contents horizontally. This gives a string
% with all characters processed up to now
% Implicit end. Implicit display
V, 38, 36 bytes
ÄVumaOchristmasòÉf2x`a@"maj~HòHdjV~
Try it online! (contains input and expected output for comparison)
When I first saw this, I thought it would be extremely easy. In fact, if it were not for the "christmas" bug, this would just be 2 bytes: V~. The christmas bug makes it significantly harder, for a very hacky answer.
As usual, here is a hexdump:
00000000: c456 756d 614f 6368 7269 7374 6d61 731b .VumaOchristmas.
00000010: f2c9 6632 7860 6140 226d 616a 7e48 f248 ..f2x`a@"maj~H.H
00000020: 646a 567e djV~