25
\$\begingroup\$

Given an input string only containing the characters A-Z, a-z, and spaces, remove all occurrences of the uppercase and lowercase versions of the first character of the string (if the first character is A remove all As and as, if the first character is (space) remove all spaces), and print the output.

Example cases:

  • Testing Testing One Two Three -> esing esing One wo hree
  • Programming Puzzles and Code Golf -> rogramming uzzles and Code Golf
  • How much wood would a woodchuck chuck if a woodchuck could chuck wood -> ow muc wood would a woodcuck cuck if a woodcuck could cuck wood
  • {space}hello world -> helloworld
  • welcome to WATER WORLD -> elcome to ATER ORLD

This is , shortest code in bytes wins!

Notes:

  • Input will always be 2 or more valid characters.
  • Output will never be an empty string.
\$\endgroup\$
  • \$\begingroup\$ Do we need to handle the case where output is the empty string? What if input is the empty string? \$\endgroup\$ – lirtosiast Oct 24 '15 at 17:23
  • \$\begingroup\$ @ThomasKwa you may assume output will alway be 2 or more characters \$\endgroup\$ – GamrCorps Oct 24 '15 at 17:51
  • \$\begingroup\$ @ThomasKwa and output will never be empty \$\endgroup\$ – GamrCorps Oct 24 '15 at 17:52

52 Answers 52

1
2
1
\$\begingroup\$

Gema, 19 characters

\A?=@set{c;?}
\C$c=

Sample run:

bash-4.3$ gema '\A?=@set{c;?};\C$c=' <<< 'Testing Testing One Two Three'
esing esing One wo hree

bash-4.3$ gema '\A?=@set{c;?};\C$c=' <<< ' Testing Testing One Two Three'
TestingTestingOneTwoThree
| improve this answer | |
\$\endgroup\$
1
\$\begingroup\$

TeaScript, 16 bytes

TeaScript is JavaScript with shortened property names.

x.g(x[0],'','i')

Try it online


TeaScript 3 came out and now this is 10 bytes.

xg(x░,u,'i

which compiles to:

xg(x[0],u,'i

which compiles to:

x.g(x[0],u,'i')

where u is preset to ""

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ TeaScript does not work for me in firefox. Test aht should be es ah, right? Second (friendly) TeaScript does work. \$\endgroup\$ – Zereges Oct 24 '15 at 6:51
  • \$\begingroup\$ @Zereges weird. I'll use the other version while I see why that's not working \$\endgroup\$ – Downgoat Oct 24 '15 at 14:52
1
\$\begingroup\$

Brainfuck, 72 bytes

,>>,
[
  <<[>+>->+<<<-]
  >>
  [
    >+>>+<
    [>-]
    >[+++[<++++++++>-]>]
    <<-<<-
  ]
  >>[<.>[-]]
  <,
]

Try it online.

Checks whether the differences between the first character and subsequent characters are divisible by 32.

| improve this answer | |
\$\endgroup\$
1
\$\begingroup\$

Vim, 11 keystrokes

x:s/<C-r>-//gi

With a trailing newline

Although there already is a Vim answer, this one uses a different approach.

This deletes the first first character, which automatically gets stored in register -. Then we do a search and replace, replacing the deleted character (<C-r>-) with nothing (ie deleting it). The gi flags are used (global and case-insensitive).

| improve this answer | |
\$\endgroup\$
0
\$\begingroup\$

Hassium, 57 Bytes

func main(){s=input().toLower()print(s.replace(s[0],""))}

Run online and see expanded here

| improve this answer | |
\$\endgroup\$
  • 4
    \$\begingroup\$ This will change all uppercase letters into lowercase. E.g, the input HEY should be EY but not ey. \$\endgroup\$ – Zereges Oct 24 '15 at 6:48
0
\$\begingroup\$

Julia, 40 bytes

t->t[find([(l=lowercase(t))...].!=l[1])]

l=lowercase(t) makes l store the string t reduced to lowercase. [l...] then converts the string into a character array. .!=l[1] compares that array element-wise with the first value of it, returning true if they don't match. find then converts the resulting boolean array into an array containing the true indices, and t[] then returns the desired string.

It feels like Julia should be able to do better than this, but so far I can't see it. It's a lot easier if you don't have to catch both upper- and lower-case instances of the letter.

| improve this answer | |
\$\endgroup\$
0
\$\begingroup\$

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.

| improve this answer | |
\$\endgroup\$
0
\$\begingroup\$

Simplex v.0.6, 10 bytes

bC&0/gi//g
b          ~~ takes input as a string
 C  /  //  ~~ match as SiRegex
  &0       ~~ replace the first cell
     gi    ~~ globally, ignoring case,
           ~~ with the empty string
         g ~~ output the result

No regrets about adding a new type of Regular Expression. (SiRegex FTW!)

| improve this answer | |
\$\endgroup\$
0
\$\begingroup\$

Bash, 35 characters

c=${1::1}
echo "${1//[${c^}${c,}]}"

Sample run:

bash-4.3$ bash rm-1st-char.sh 'Testing Testing One Two Three'
esing esing One wo hree

bash-4.3$ bash rm-1st-char.sh ' Testing Testing One Two Three'
TestingTestingOneTwoThree
| improve this answer | |
\$\endgroup\$
0
\$\begingroup\$

C(136)

#define t(x)(x+32*(x>>5==2)) 
char*h(char*a,int b,int*c){char*v=a[b+1]?h(a,b+1,c):0;return(b<=*c)?v:&(a[b]=a[b-(*c+=t(*a)==t(a[b]))]);}

Execution

void main(){

    char a[]="abac";int v=0;
    printf("%s",h((char*)a,0,&v));
    return;
}

Output

bc

  • the function is recursive which is called from the initial parameters: h(string,0,0) and returns the beginning of the resulting string.
| improve this answer | |
\$\endgroup\$
0
\$\begingroup\$

Powershell, 62 60 44 bytes

$args-replace"$($args.substring(0,1))",""

Powershell of all things.

Edit: removed whitespace to save 2 bytes
Edit2: removed type accelerators to save 16 more bytes

| improve this answer | |
\$\endgroup\$
  • 2
    \$\begingroup\$ Hi and welcome to Programming Puzzles and Code Golf Stack Exchange (PPCG.SE for short)! Just FYI, the standard format of an answer starts with #[Language], [N] bytes as the first line where N is the length (in UTF-8 bytes) of your program. Otherwise, I, along with the rest of the PPCG.SE community wishes you good luck in your golfing adventure! \$\endgroup\$ – GamrCorps Oct 28 '15 at 3:38
  • \$\begingroup\$ Thanks for the welcome and the advice. You barely beat the downvotes. Gotta love SE. I've been struggling to use regex instead of .net string handling, thinking it would save me some bytes, however, I can't seem to find a way to capture the first character AND search for it using $string.replace("...","") \$\endgroup\$ – Xalorous Oct 28 '15 at 4:02
  • \$\begingroup\$ Can be further shortened to $argz-replace($argz[0]) (23 bytes). \$\endgroup\$ – beatcracker Oct 18 '17 at 0:43
0
\$\begingroup\$

How about a Nodejs RegEx?

My answer is 60 bytes. Without all the function wrapper stuff it could be as little as 35 bytes. The function called 'lop()' could even be one character name like 'x()' or something, making it 58 bytes.

Answer

function lop(s){return s.replace(new RegExp(s[0],'gi'),'');}
or
s.replace(new RegExp(s[0],'gi'),'')  

Test Code

function test(s)  
{  
    console.log();  
    console.log('> ' + s);  
    console.log('< ' + lop(s));  
}  

// **test data**  
test("Testing Testing One Two Three");  
test("Programming Puzzles and Code Golf");  
test("How much wood would a woodchuck chuck if a woodchuck could chuck wood");  
test(" hello world");  
test("welcome to WATER WORLD");  
test("A welcome to WATER WORLD");  
test("A first test of a solid A remover.");  

Output

> Testing Testing One Two Three
< esing esing One wo hree

> Programming Puzzles and Code Golf
< rogramming uzzles and Code Golf

> How much wood would a woodchuck chuck if a woodchuck could chuck wood
< ow muc wood would a woodcuck cuck if a woodcuck could cuck wood

>  hello world
< helloworld

> welcome to WATER WORLD
< elcome to ATER ORLD

> A welcome to WATER WORLD
<  welcome to WTER WORLD

> A first test of a solid A remover.
<  first test of  solid  remover. 
| improve this answer | |
\$\endgroup\$
  • 1
    \$\begingroup\$ You should aim to reduce your code as much as possible. This is code-golf, so answering as minimally as possible is the name of the game. Also, for code blocks, use the <pre> html tag. If you include test data, make sure you put the actual output of the code in as well. \$\endgroup\$ – Addison Crump Oct 27 '15 at 1:53
  • \$\begingroup\$ Please also make the first line of your program a heading with the language used and the number of bytes. \$\endgroup\$ – lirtosiast Oct 29 '15 at 1:59
0
\$\begingroup\$

𝔼𝕊𝕄𝕚𝕟, 6 chars / 13 bytes (non-competitive)

ïĥ/⏖î⍀

Try it here (Firefox only).

As does happen a lot, I beat everyone in char count but not in byte count :P

Explanation

ï is input, ĥ is replace, /...⍀ transpiles to /.../gi in JS, and ⏖î directly replaces the result of ï[0] in the compiled code.

| improve this answer | |
\$\endgroup\$
0
\$\begingroup\$

Common Lisp, 47 bytes

(lambda(s)(remove(aref s 0)s :test'char-equal))
| improve this answer | |
\$\endgroup\$
0
\$\begingroup\$

J, 17 bytes

#~1-(={.)@tolower

Usage

   f =: #~1-(={.)@tolower
   f 'Testing Testing One Two Three'
esing esing One wo hree
   f 'Programming Puzzles and Code Golf'
rogramming uzzles and Code Golf
   f ' hello world'
helloworld
| improve this answer | |
\$\endgroup\$
0
\$\begingroup\$

Rebol, 25 bytes

print replace/all s s/1{}

Alternative function at 28 bytes:

func[s][replace/all s s/1{}]
| improve this answer | |
\$\endgroup\$
0
\$\begingroup\$

C#, 101 bytes

void m(string s){Console.Write(s.Replace(char.ToUpper(s[0]),'\0').Replace(char.ToLower(s[0]),'\0'));}

Sadly C# falls short on this one due to not having a case insensitivity option in Replace and not having an empty char literal

I tried using regex but it's more bytes (someone might manage shorter with it though)

| improve this answer | |
\$\endgroup\$
0
\$\begingroup\$

Java, 96 bytes.

Java is an unlikely winner, it's pretty verbose even in a golfed form. Here |32 is to force lower-case comparison regardless of the actual value, doesn't affect space.

void m(String s){char[]c=s.toCharArray();for(char a:c)System.out.print((a|32)!=(c[0]|32)?a:"");}

At a slightly higher level of String class it would be more explicit (121 bytes):

void m(String s){String f=s.substring(0,1).toLowerCase();System.out.println(s.replaceAll("["+f+f.toUpperCase()+"]",""));}
| improve this answer | |
\$\endgroup\$
0
\$\begingroup\$

Convex, 5 bytes, noncompeting

(_±+-

Try it online!

| improve this answer | |
\$\endgroup\$
0
\$\begingroup\$

05AB1E, 5 bytes

ćDš«м

Try it online!

Newer version than what Adnan used.

| improve this answer | |
\$\endgroup\$
0
\$\begingroup\$

Jelly, 7 6 bytes

żŒsḢ⁸ḟ

Try it online!

1 byte saved thanks to Erik The Outgolfer

How it works

żŒsḢ⁸ḟ - Main link. Argument: s (string) e.g. "ABCabc"

 Œs    - Swap the case                        "abcABC"
ż      - zip with the input                   ["Aa", "Bb", "Cc"]
   Ḣ   - take the first element               "Aa"
     ḟ - Filter those characters from...
    ⁸  -   the input                          "BCbc"

Original

ḟḢ;Œs$$ - Main link. Argument: s (string) e.g. "ABCabc"

 Ḣ      - Generate the first character of s    "A"
   Œs   - Generate the swapped case of ^       "a"
  ;     - Concatenate                          "Aa"
ḟ    $$ - Filter ^ from the input              "BCbc"
| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ Alternatively for 6 bytes: żŒsḢ⁸ḟ \$\endgroup\$ – Erik the Outgolfer Oct 17 '17 at 16:47
  • \$\begingroup\$ @EriktheOutgolfer oh, nice. Thanks! \$\endgroup\$ – caird coinheringaahing Oct 17 '17 at 16:52
0
\$\begingroup\$

05AB1E, 7 Bytes

¬DŠ-sš-

Explanation:

¬        # Take the first character of the input
 D       # Duplicate top of the stack
  Š      # Pop a,b,c and push c,a,b
   -     # Remove all occurencies of the first letter
    s    # Swap the two top elements
     š   # Swapcase the top of the stack
      -  # Remove all occurencies of the swapcased letter
         # Implicit: print top of the stack

Uses ISO 8859-1 encoding

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ ¬Dš‚K is 5, AFAICT. \$\endgroup\$ – Magic Octopus Urn Oct 17 '17 at 17:14
1
2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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