Ceylon, 93 84
String r(String i)=>String{if(exists f=i[0])for(c in i.rest)if(c.offset(f)%32!=0)c};
This defines a function r
which takes and returns a String, solving the task.
Formatted:
String r(String i) =>
String {
if (exists f = i[0])
for (c in i.rest)
if (c.offset(f) % 32 != 0)
c };
The function creates and returns a string from an Iterable comprehension, composed of several if- and for-clauses.
The first if
makes sure that the string is non-empty (if its empty, the iterable will be empty, thus the returned string too), and at the same time declares the f
variable to be used later. (i[0]
could have also been written i.first
, but this way is shorter.)
While the question says we can be sure the input has always length > 1, the compiler doesn't know this. (A different way of saying this would be to use an assert
statement, but this would be even longer.)
The next for
clause iterates over the remainder of the string (i.e. the string without its first character). Each item is named c
.
The second if
does the case-check. It turns out that in ASCII, the upper- and lower case letters have distance 32 from each other, so c.offset(f) % 32 != 0
is a slightly shorter way of writing c.uppercased != f.uppercased
. (This would also filter other character, e.g. in the input strings Testing Testing One Two Three4
, Programming Puzzles and 0Code0 Golf00
, ((((How much wood would (a( woodchuck chuck (if a woodchuck could chuck (wood
, @ hello` `world
, 7welcome to WATER WORLD7
would also result in the example outputs in the question. But as the input is restricted to [A-Z a-z]+
, we don't care.)
The last clause it simply the expression c
– which will be included if the if clause's condition is true, and omitted otherwise.
The resulting iterable is passed to the String's class constructor, which iterates over it and creates the string.
As a bonus, here is a version which works for all of unicode:
String r(String i) =>
String {
if (exists f = i[0])
for (c in i.rest)
if (c.uppercased != f.uppercased)
c };
After space-removal this is 93 bytes:
String r(String i)=>String{if(exists f=i[0])for(c in i.rest)if(c.uppercased!=f.uppercased)c};
It converts (beside the example phrases) also Übel wütet der Gürtelwürger
to bel wtet der Grtelwrger
.