24
\$\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

9
\$\begingroup\$

Pyth, 7 bytes

-zrBhz2

Try it online: Demonstration or Test Suite

Nice. The new bifurcate operator (only 8 days old) helps here to save one char. I think this is the first code, that uses this feature.

Explanation

-zrBhz2   implicit: z = input string
    hz    take the first character of z
  rB  2   B generates a list, containing the original char and the  
          result of r.2 applied to the char, which swaps the case
-z        remove these two chars from z and print the result 
\$\endgroup\$
  • 2
    \$\begingroup\$ Neat, didn't know about that. +1 \$\endgroup\$ – FryAmTheEggman Oct 24 '15 at 17:05
14
\$\begingroup\$

brainfuck, 219 bytes

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

(Requires tape that either allows the pointer to go to the negatives or loops to the end if it tries. Also requires , to return 0 at EOF. In my experience, most interpreters meet these requirements by default.)

This actually turned out to be pretty easy! I wouldn't be surprised if it's golfable (I have some idea of where there might be wasted bytes but I'm not super sure if it'll pan out). Still, getting it work wasn't really a challenge at all.

This code treats everything with an ASCII value below 97 as an uppercase character. If the first character is space, it'll try to remove any occurrences of a "lowercase space" (i.e. chr(32+32), i.e. @) from the string. This is okay, because only letters and spaces will ever be present.

With comments:

to make comments shorter: everywhere it says "fc" means "first character"

#################################### GET FC ####################################

+[+[>+<+<]>]        shortest way to get 96
                    any number above 89 and less than 97 would work because this
                    is only for figuring out if the first character is capital

,[<+<+>>-]          create two copies of the first character


### current tape: | fc | fc | 000 | 096 | ###
###      pointer:              ^          ###

########################### FIND MAX(FC MINUS 96; 0) ###########################


>[                  96 times:

  <<                  go to the cell with the first char

  [->]                if not 0: sub one and move right


  ### current tape: | fc | char being worked on | 000 | 096 | ###
  ###      pointer:                           ^ OR ^          ###      


  >[<]>               collapse the wavefunction; sync the branches

-]


### current tape: | fc | fc is lowercase ? nonzero : zero | 000 | 000 | ###
###      pointer:                                                  ^    ###

############################# GET BOTH CASES OF FC #############################

++++[<++++++++>-]   get 32 (add to uppercase character to get lowercase)

<<[                 if the character is already lowercase:

  [-]                 clear the lowercase flag

  ----[>-<----]>-     sub 64 from the cell with 32

<]

<[>+>+<<-]          add fc to the 32 or minus 32 to get both cases


### current tape: | 000 | fc | other case of fc | ###
###      pointer:    ^                            ###

###################### LOOP THROUGH REMAINING CHARACTERS #######################

<,[                 for each character:

  [>+>>>+>>+>+<<<<<<<-]
                      make four copies
                      (only 3 are strictly needed; 4th will resync a branch)

  >>                  go to the first case of fc

  [<<+>>-]<<[>->+<<-] subtract one case of fc from one copy

  >[[-]+<]            if the result is nonzero set it to 1

  >[>]<<              go to the other case of fc (and resync branches)

  [>>+<<-]>>[<-<+>>-] subtract other case of fc from other copy

  <[[-]+>]            if the result is nonzero set it to 1

  >>[<]               resync branches using extra copy

  <<<<[>>>+<<<-]>>>   add results together

  -                   subtract 1

   if the character exactly matched either case: 1 plus 0 minus 1 = 0
  if the character exactly matched neither case: 1 plus 1 minus 1 = 1
    if the character exactly matched both cases: impossible

  [                   if the result is 1:

    >>.<<               output character

    -                   set cell to 0 to kill loop

  ]

  >>>[[-]<]           clean up remaining copies

  <<<<<,              get next character; or end loop if done

]
\$\endgroup\$
  • \$\begingroup\$ Please don't golf this down too much. c: I worked so hard... \$\endgroup\$ – Addison Crump Oct 25 '15 at 21:44
13
\$\begingroup\$

Perl, 13 bytes

#!perl -p
/./,s/$&//gi

Counting the shebang as one, input is taken from stdin.


Sample Usage

$ echo Testing Testing One Two Three | perl remove-first.pl
esing esing One wo hree

$ echo \ Testing Testing One Two Three | perl primo-remove.pl
TestingTestingOneTwoThree
\$\endgroup\$
  • \$\begingroup\$ Nice. I was thinking about how Perl would probably do badly because of the need to write substr but of course you came up with a much better way! \$\endgroup\$ – hobbs Oct 24 '15 at 6:10
  • \$\begingroup\$ Why do you count hashbang as one byte? \$\endgroup\$ – Zereges Oct 24 '15 at 9:57
  • \$\begingroup\$ @Zereges the shebang line is not necessary, if the script is invoked as perl -p script.pl. Command line options are customarily counted as one byte each, at least on this site. \$\endgroup\$ – primo Oct 24 '15 at 10:00
  • \$\begingroup\$ @primo I see, thanks. \$\endgroup\$ – Zereges Oct 24 '15 at 10:08
10
\$\begingroup\$

CJam, 8 bytes

q(_32^+-

Try it online in the CJam interpreter.

How it works

q        e# Read all input from STDIN.
 (       e# Shift out the first character.
  _32^   e# Push a copy and XOR its character code with 32.
         e#   For a letter, this swaps its case.
         e#   For a space, this pushes a null byte.
      +  e# Concatenate these two characters.
       - e# Remove all occurrences of both from the input string.
\$\endgroup\$
  • \$\begingroup\$ I originally created this challenge to learn CJam, and I never knew about the XOR thing! \$\endgroup\$ – GamrCorps Oct 24 '15 at 5:17
  • 2
    \$\begingroup\$ I personally find it surprising that any language - with exception to perhaps sed - could beat perl in this challenge. \$\endgroup\$ – primo Oct 24 '15 at 6:04
  • \$\begingroup\$ @primo - You haven't noticed that CJam and/or Pyth pretty much win 99% of these? Perl did pretty good though, considering it's the only one not specifically designed as a golfing language. \$\endgroup\$ – Darrel Hoffman Oct 24 '15 at 13:29
  • \$\begingroup\$ Pyth and CJam's operator overloading is ridiculous. minus(string,string) performing per-character removal is not a core operation in any other language I've encountered. \$\endgroup\$ – Sparr Oct 25 '15 at 18:26
  • \$\begingroup\$ @Sparr APL has the same built-in. (It's called ~ though.) \$\endgroup\$ – Dennis Oct 25 '15 at 18:29
7
\$\begingroup\$

Pyth, 8 bytes

-zr*2hz3

Try it online

Uses Pyth's version of python's str.title to convert a string of the first letter twice into the form "<Upper><Lower>". Then it removes each element from the input that is in that string. Spaces work fine because they are unaffected by str.title.

\$\endgroup\$
5
\$\begingroup\$

MATLAB, 28 bytes

@(S)S(lower(S)~=lower(S(1)))
\$\endgroup\$
5
\$\begingroup\$

JavaScript (ES6), 38 36 bytes

This does not depend on the flags parameter which is Mozilla-specific.

f=x=>x.replace(RegExp(x[0],'gi'),'')

CoffeeScript, 39 37 bytes

For once it's shorter in JS than CoffeeScript!

f=(x)->x.replace RegExp(x[0],'gi'),''
\$\endgroup\$
  • 3
    \$\begingroup\$ At least in the browsers I tried, the new is optional, so RegExp(x[0],'gi') is shorter. \$\endgroup\$ – Neil Oct 24 '15 at 15:05
4
\$\begingroup\$

PHP, 41 bytes

Takes one command-line argument. Short open tags need to be enabled for PHP < 5.4.

<?=str_ireplace($argv[1][0],'',$argv[1]);
\$\endgroup\$
  • 1
    \$\begingroup\$ You can shorten it by declaring a variable containing $argv[1] : <?=str_ireplace(($a=$argv[1])[0],'',$a); \$\endgroup\$ – Benoit Esnard Oct 25 '15 at 10:05
3
\$\begingroup\$

Perl, 27 bytes

This is a complete program, although it's just based on 2 regexes that could probably copied into a Retina program to save bytes on I/O.

$_=<>;m/(.)/;s/$1//gi;print

Edit: Looks like this has already been beaten with somebody using the -p option. Oh, and using $& instead of $1.

\$\endgroup\$
  • 1
    \$\begingroup\$ Even if the entire shebang is counted (i.e. a "complete" program), #!perl -p is still two bytes shorter than $_=<>;print. \$\endgroup\$ – primo Oct 24 '15 at 6:01
  • \$\begingroup\$ @primo I've never used any of the options in my golfs. I just googled what the -p option does. There's not really any edits that I can make to my answer without just making it identical to yours. \$\endgroup\$ – PhiNotPi Oct 24 '15 at 6:07
3
\$\begingroup\$

Minkolang 0.9, 23 33 bytes

No way this will win but eh, it's fun!

" Z"od0c`1c*-$I[odd0c`1c*-2c=?O].

Try it here.

Explanation

" Z"                                 Makes it easier to uppercase
    od                               Take first character of input and duplicate
      0c`1c*-                        Uppercase if needed
             $I[               ]     Loop over the input
                odd                  Take character from input and duplicate twice
                   0c`1c*-           If it's lowercase, uppercase it.
                          0c=?       Check to see if it's equal to first character
                              O      If not, output as character
                                .    Exit

(This might fail in some edge cases, like if the first character is a symbol.)

\$\endgroup\$
3
\$\begingroup\$

TECO, 15 14 bytes

Editing text? When in doubt, use the Text Editor and COrrecter!

After much trial-and-error (mostly error), I think this is the shortest generic TECO program that will do the job.

0,1XA<S^EQA$-D>

Or, in human-readable form

0,1XA    ! Read the first character of the string into the text area of !
         ! Q-buffer A.                                                  !
<        ! Loop,                                                        !
S^EQA$   ! searching for the text in Q-buffer A in either lowercase or  !
         ! uppercase,                                                   !
-D       ! and deleting it,                                             !
>        ! until the search fails.                                      !

$ represents the escape key, and ^E represents the sequence CTRL+E. Depending on the flavor of TECO you're using, it may recognize these ASCII substitutions, or it may not.

According to the manual, some dialects of TECO accept this 13-byte version (using a find-and-delete command instead of separate "find" and "delete" commands) instead:

0,1XA<FD^EQA$>
\$\endgroup\$
3
\$\begingroup\$

Pip, 8 bytes

aR-Xa@0x

Takes the string as a command-line argument (will need to be quoted if it contains spaces). Explanation:

          a gets cmdline arg; x is "" (implicit)
    a@0   First character of a
   X      Convert to regex
  -       Make regex case-insensitive
aR     x  Replace all matches of that regex in a with empty string
          Autoprint (implicit)

This solution has the extra bonus of working on strings containing any printable ASCII characters. (The X operator backslash-escapes anything that's not alphanumeric.)

\$\endgroup\$
3
\$\begingroup\$

Python, 66 char

a=raw_input()
print a.replace(a[0],'').replace(a[0].swapcase(),'')
\$\endgroup\$
  • 5
    \$\begingroup\$ @ThomasKwa how about no. \$\endgroup\$ – TheDoctor Oct 24 '15 at 22:35
3
\$\begingroup\$

Julia, 34 bytes

s->replace(s,Regex(s[1:1],"i"),"")

This creates an unnamed function that accepts a string and returns a string. It constructs a case-insensitive regular expression from the first character in the input and replaces all occurrences of that with an empty string.

\$\endgroup\$
2
\$\begingroup\$

Mathematica, 47 bytes

StringDelete[#,#~StringTake~1,IgnoreCase->1>0]&
\$\endgroup\$
2
\$\begingroup\$

R, 43 bytes

cat(gsub(substr(s,1,1),"",s<-readline(),T))

This is a full program that reads a line from STDIN and writes the modified result to STDOUT.

Ungolfed:

# Read a line from STDIN
s <- readline()

# Replace all occurrences of the first character with an empty
# string, ignoring case
r <- gsub(substr(s, 1, 1), "", s, ignore.case = TRUE)

# Write the result to STDOUT
cat(r)
\$\endgroup\$
2
\$\begingroup\$

Ruby, 25 bytes

Anonymous function:

->s{s.gsub /#{s[0]}/i,""}

Full prgram, 29 bytes:

puts gets.gsub /#{$_[0]}/i,""
\$\endgroup\$
2
\$\begingroup\$

Python, 61 bytes (way too many)

lambda x:"".join(map(lambda y:(y.lower()!=x[0].lower())*y,x))

I imagine there is a better way to go about doing this, but I can't seem to find it. Any ideas on removing the "".join(...)?

\$\endgroup\$
  • 1
    \$\begingroup\$ @ThomasKwa perhaps you should submit that as your own answer; it seems too different from mine to count as my submission. \$\endgroup\$ – cole Oct 24 '15 at 7:28
  • \$\begingroup\$ @ThomasKwa Shorter to do x[0]+x[0].swapcase(). \$\endgroup\$ – xnor Oct 24 '15 at 8:08
  • 1
    \$\begingroup\$ @ThomasKwa Or, stealing from FryAmTheEggman's Pyth solution, (x[0]*2).title(). \$\endgroup\$ – xnor Oct 24 '15 at 9:32
  • \$\begingroup\$ @xnor yeah, I found that when I read theirs a few hours ago. \$\endgroup\$ – lirtosiast Oct 24 '15 at 16:09
  • \$\begingroup\$ lambda x:x.replace(x[0].upper(),'').replace(x[0].lower(),'') - 60 bytes \$\endgroup\$ – Mego Oct 24 '15 at 19:59
2
\$\begingroup\$

Ouroboros, 61 bytes

Hey, it's shorter than C++! Ha.

i..91<\64>*32*+m1(
)L!34*.(;Si.1+!24*(Y.@@.@=@.@32-=\@+2*.(yo

In Ouroboros, each line of the program represents a snake with its tail in its mouth. Control flow is accomplished by eating or regurgitating sections of tail, with a shared stack to synchronize between snakes.

Snake 1

i. reads a character from input and duplicates it. .91<\64>*32* pushes 32 if the character was an uppercase letter, 0 otherwise. +ing that to the character converts uppercase letters to lowercase while leaving lowercase letters and spaces unchanged. All this has been taking place on snake 1's stack, so we now push the value to the shared stack (m) for snake 2 to process. Finally, 1( eats the last character of snake 1's tail. Since that's where the instruction pointer is, the snake dies.

Snake 2

) has no effect the first time through. L!34* pushes 34 if the shared stack is empty, 0 otherwise. We then . dup and ( eat that many characters.

  • If the shared stack was empty, this puts the end of the snake right after the ( we just executed. Therefore, control loops back to the beginning of the line, where ) regurgitates the characters we just ate (having previously pushed an extra copy of 34) and repeat the stack-length test.
  • If the shared stack was not empty, no characters are eaten, ';' drops the extra 0, and execution continues:

Si switches to the shared stack and inputs another character. .1+!24* pushes 24 if that character was -1 / EOF, 0 otherwise. On EOF, ( swallows 24 characters--including the IP--and the snake dies. Otherwise, execution continues.

Y yanks a copy of the top of the shared stack (the character we just read) to snake 2's own stack for future use. Then .@@.@=@.@32-=\@+2* calculates whether the new character is equal to the first character or to the first character minus 32, pushing 2 if so and 0 if not. We . duplicate and ( eat that many characters:

  • If the characters matched, we go straight back to the head of the snake, where ( regurgitates the 2 characters we just ate and execution proceeds with the next character.
  • If not, we yank the character back from snake 2's stack, output it, and then loop.

See it in action

// Define Stack class
function Stack() {
  this.stack = [];
  this.length = 0;
}
Stack.prototype.push = function(item) {
  this.stack.push(item);
  this.length++;
}
Stack.prototype.pop = function() {
  var result = 0;
  if (this.length > 0) {
    result = this.stack.pop();
    this.length--;
  }
  return result;
}
Stack.prototype.top = function() {
  var result = 0;
  if (this.length > 0) {
    result = this.stack[this.length - 1];
  }
  return result;
}
Stack.prototype.toString = function() {
    return "" + this.stack;
  }

// Define Snake class
function Snake(code) {
  this.code = code;
  this.length = this.code.length;
  this.ip = 0;
  this.ownStack = new Stack();
  this.currStack = this.ownStack;
  this.alive = true;
  this.wait = 0;
  this.partialString = this.partialNumber = null;
}
Snake.prototype.step = function() {
    if (!this.alive) {
      return null;
    }
    if (this.wait > 0) {
      this.wait--;
      return null;
    }
    var instruction = this.code.charAt(this.ip);
    var output = null;
    if (this.partialString !== null) {
      // We're in the middle of a double-quoted string
      if (instruction == '"') {
        // Close the string and push its character codes in reverse order
        for (var i = this.partialString.length - 1; i >= 0; i--) {
          this.currStack.push(this.partialString.charCodeAt(i));
        }
        this.partialString = null;
      } else {
        this.partialString += instruction;
      }
    } else if (instruction == '"') {
      this.partialString = "";
    } else if ("0" <= instruction && instruction <= "9") {
      if (this.partialNumber !== null) {
        this.partialNumber = this.partialNumber + instruction; // NB: concatenation!
      } else {
        this.partialNumber = instruction;
      }
      next = this.code.charAt((this.ip + 1) % this.length);
      if (next < "0" || "9" < next) {
        // Next instruction is non-numeric, so end number and push it
        this.currStack.push(+this.partialNumber);
        this.partialNumber = null;
      }
    } else if ("a" <= instruction && instruction <= "f") {
      // a-f push numbers 10 through 15
      var value = instruction.charCodeAt(0) - 87;
      this.currStack.push(value);
    } else if (instruction == "$") {
      // Toggle the current stack
      if (this.currStack === this.ownStack) {
        this.currStack = this.program.sharedStack;
      } else {
        this.currStack = this.ownStack;
      }
    } else if (instruction == "s") {
      this.currStack = this.ownStack;
    } else if (instruction == "S") {
      this.currStack = this.program.sharedStack;
    } else if (instruction == "l") {
      this.currStack.push(this.ownStack.length);
    } else if (instruction == "L") {
      this.currStack.push(this.program.sharedStack.length);
    } else if (instruction == ".") {
      var item = this.currStack.pop();
      this.currStack.push(item);
      this.currStack.push(item);
    } else if (instruction == "m") {
      var item = this.ownStack.pop();
      this.program.sharedStack.push(item);
    } else if (instruction == "M") {
      var item = this.program.sharedStack.pop();
      this.ownStack.push(item);
    } else if (instruction == "y") {
      var item = this.ownStack.top();
      this.program.sharedStack.push(item);
    } else if (instruction == "Y") {
      var item = this.program.sharedStack.top();
      this.ownStack.push(item);
    } else if (instruction == "\\") {
      var top = this.currStack.pop();
      var next = this.currStack.pop()
      this.currStack.push(top);
      this.currStack.push(next);
    } else if (instruction == "@") {
      var c = this.currStack.pop();
      var b = this.currStack.pop();
      var a = this.currStack.pop();
      this.currStack.push(c);
      this.currStack.push(a);
      this.currStack.push(b);
    } else if (instruction == ";") {
      this.currStack.pop();
    } else if (instruction == "+") {
      var b = this.currStack.pop();
      var a = this.currStack.pop();
      this.currStack.push(a + b);
    } else if (instruction == "-") {
      var b = this.currStack.pop();
      var a = this.currStack.pop();
      this.currStack.push(a - b);
    } else if (instruction == "*") {
      var b = this.currStack.pop();
      var a = this.currStack.pop();
      this.currStack.push(a * b);
    } else if (instruction == "/") {
      var b = this.currStack.pop();
      var a = this.currStack.pop();
      this.currStack.push(a / b);
    } else if (instruction == "%") {
      var b = this.currStack.pop();
      var a = this.currStack.pop();
      this.currStack.push(a % b);
    } else if (instruction == "_") {
      this.currStack.push(-this.currStack.pop());
    } else if (instruction == "I") {
      var value = this.currStack.pop();
      if (value < 0) {
        this.currStack.push(Math.ceil(value));
      } else {
        this.currStack.push(Math.floor(value));
      }
    } else if (instruction == ">") {
      var b = this.currStack.pop();
      var a = this.currStack.pop();
      this.currStack.push(+(a > b));
    } else if (instruction == "<") {
      var b = this.currStack.pop();
      var a = this.currStack.pop();
      this.currStack.push(+(a < b));
    } else if (instruction == "=") {
      var b = this.currStack.pop();
      var a = this.currStack.pop();
      this.currStack.push(+(a == b));
    } else if (instruction == "!") {
      this.currStack.push(+!this.currStack.pop());
    } else if (instruction == "?") {
      this.currStack.push(Math.random());
    } else if (instruction == "n") {
      output = "" + this.currStack.pop();
    } else if (instruction == "o") {
      output = String.fromCharCode(this.currStack.pop());
    } else if (instruction == "r") {
      var input = this.program.io.getNumber();
      this.currStack.push(input);
    } else if (instruction == "i") {
      var input = this.program.io.getChar();
      this.currStack.push(input);
    } else if (instruction == "(") {
      this.length -= Math.floor(this.currStack.pop());
      this.length = Math.max(this.length, 0);
    } else if (instruction == ")") {
      this.length += Math.floor(this.currStack.pop());
      this.length = Math.min(this.length, this.code.length);
    } else if (instruction == "w") {
      this.wait = this.currStack.pop();
    }
    // Any instruction not covered by the above cases is ignored
    if (this.ip >= this.length) {
      // We've swallowed the IP, so this snake dies
      this.alive = false;
      this.program.snakesLiving--;
    } else {
      // Increment IP and loop if appropriate
      this.ip = (this.ip + 1) % this.length;
    }
    return output;
  }

// Define Program class
function Program(source, speed, io) {
  this.sharedStack = new Stack();
  this.snakes = source.split(/\r?\n/).map(function(snakeCode) {
    var snake = new Snake(snakeCode);
    snake.program = this;
    snake.sharedStack = this.sharedStack;
    return snake;
  }.bind(this));
  this.snakesLiving = this.snakes.length;
  this.io = io;
  this.speed = speed || 10;
  this.halting = false;
}
Program.prototype.run = function() {
  if (this.snakesLiving) {
    this.step();
    this.timeout = window.setTimeout(this.run.bind(this), 1000 / this.speed);
  }
}
Program.prototype.step = function() {
  for (var s = 0; s < this.snakes.length; s++) {
    var output = this.snakes[s].step();
    if (output) {
      this.io.print(output);
    }
  }
}
Program.prototype.halt = function() {
  window.clearTimeout(this.timeout);
}
var ioFunctions = {
  print: function(item) {
    var stdout = document.getElementById('stdout');
    stdout.value += "" + item;
  },
  getChar: function() {
    if (inputData) {
      var inputChar = inputData[0];
      inputData = inputData.slice(1);
      return inputChar.charCodeAt(0);
    } else {
      return -1;
    }
  },
  getNumber: function() {
    while (inputData && (inputData[0] < "0" || "9" < inputData[0])) {
      inputData = inputData.slice(1);
    }
    if (inputData) {
      var inputNumber = inputData.match(/\d+/)[0];
      inputData = inputData.slice(inputNumber.length);
      return +inputNumber;
    } else {
      return -1;
    }
  }
};
var program = null;
var inputData = null;

function resetProgram() {
  var stdout = document.getElementById('stdout');
  stdout.value = null;
  if (program !== null) {
    program.halt();
  }
  program = null;
  inputData = null;
}

function initProgram() {
  var source = document.getElementById('source'),
    stepsPerSecond = document.getElementById('steps-per-second'),
    stdin = document.getElementById('stdin');
  program = new Program(source.value, +stepsPerSecond.innerHTML, ioFunctions);
  inputData = stdin.value;
}

function runBtnClick() {
  if (program === null || program.snakesLiving == 0) {
    resetProgram();
    initProgram();
  } else {
    program.halt();
    var stepsPerSecond = document.getElementById('steps-per-second');
    program.speed = +stepsPerSecond.innerHTML;
  }
  program.run();
}

function stepBtnClick() {
  if (program === null) {
    initProgram();
  } else {
    program.halt();
  }
  program.step();
}
.container {
  width: 100%;
}
.so-box {
  font-family: 'Helvetica Neue', Arial, sans-serif;
  font-weight: bold;
  color: #fff;
  text-align: center;
  padding: .3em .7em;
  font-size: 1em;
  line-height: 1.1;
  border: 1px solid #c47b07;
  -webkit-box-shadow: 0 2px 2px rgba(0, 0, 0, 0.3), 0 2px 0 rgba(255, 255, 255, 0.15) inset;
  text-shadow: 0 0 2px rgba(0, 0, 0, 0.5);
  background: #f88912;
  box-shadow: 0 2px 2px rgba(0, 0, 0, 0.3), 0 2px 0 rgba(255, 255, 255, 0.15) inset;
}
.control {
  display: inline-block;
  border-radius: 6px;
  float: left;
  margin-right: 25px;
  cursor: pointer;
}
.option {
  padding: 10px 20px;
  margin-right: 25px;
  float: left;
}
h1 {
  text-align: center;
  font-family: Georgia, 'Times New Roman', serif;
}
a {
  text-decoration: none;
}
input,
textarea {
  box-sizing: border-box;
}
textarea {
  display: block;
  white-space: pre;
  overflow: auto;
  height: 40px;
  width: 100%;
  max-width: 100%;
  min-height: 25px;
}
span[contenteditable] {
  padding: 2px 6px;
  background: #cc7801;
  color: #fff;
}
#stdout-container,
#stdin-container {
  height: auto;
  padding: 6px 0;
}
#reset {
  float: right;
}
#source-display-wrapper {
  display: none;
  width: 100%;
  height: 100%;
  overflow: auto;
  border: 1px solid black;
  box-sizing: border-box;
}
#source-display {
  font-family: monospace;
  white-space: pre;
  padding: 2px;
}
.activeToken {
  background: #f88912;
}
.clearfix:after {
  content: ".";
  display: block;
  height: 0;
  clear: both;
  visibility: hidden;
}
.clearfix {
  display: inline-block;
}
* html .clearfix {
  height: 1%;
}
.clearfix {
  display: block;
}
<!--
Designed and written 2015 by D. Loscutoff
Much of the HTML and CSS was taken from this Befunge interpreter by Ingo Bürk: http://codegolf.stackexchange.com/a/40331/16766
-->
<div class="container">
<textarea id="source" placeholder="Enter your program here" wrap="off">i..96<\64>*32*+m1(
)L!34*.(;Si.1+!24*(Y.@@.@=@.@32-=\@+2*.(yo</textarea>
<div id="source-display-wrapper"><div id="source-display"></div></div></div><div id="stdin-container" class="container">
<textarea id="stdin" placeholder="Input" wrap="off">Testing Testing One Two Three</textarea>
</div><div id="controls-container" class="container clearfix"><input type="button" id="run" class="control so-box" value="Run" onclick="runBtnClick()" /><input type="button" id="pause" class="control so-box" value="Pause" onclick="program.halt()" /><input type="button" id="step" class="control so-box" value="Step" onclick="stepBtnClick()" /><input type="button" id="reset" class="control so-box" value="Reset" onclick="resetProgram()" /></div><div id="stdout-container" class="container"><textarea id="stdout" placeholder="Output" wrap="off" readonly></textarea></div><div id="options-container" class="container"><div class="option so-box">Steps per Second:
<span id="steps-per-second" contenteditable>1000</span></div></div>

\$\endgroup\$
2
\$\begingroup\$

C, 60 bytes

n,c,d;main(){for(;read(0,&c-n,1);n=2)d-c&31&&n&&putchar(d);}

Edit: Fixed a bug which caused a null byte to be printed at the beginning

\$\endgroup\$
  • \$\begingroup\$ does it even compile ? \$\endgroup\$ – Abr001am Oct 26 '15 at 19:28
  • 1
    \$\begingroup\$ Yes. It compiles on my machine (with warnings) and works. You can also test it on this page: golf.shinh.org/check.rb \$\endgroup\$ – xsot Oct 26 '15 at 22:55
  • \$\begingroup\$ yes it does; +1 \$\endgroup\$ – Abr001am Oct 27 '15 at 11:03
2
\$\begingroup\$

Python 2, 43

lambda s:s.translate(None,(s[0]*2).title())

This is somewhat based on my Pyth answer, but it's also related to some comments from ThomasKwa and xnor from here. Mostly posting because I wanted this answer to exist.

\$\endgroup\$
2
\$\begingroup\$

Vim, 30 Keystrokes

Sorry to unearth, but i don't see any Vim answer D:

If<Right>xh@x.<Esc>"xy07|@x2|~0"xd7|@x0x

Explanation:

  1. If<Right>xh@x.<Esc>
    Write a (recursive) macro around the first character
    Moving left (h) is needed to stay at the left of the next unread character
    Adding any character (.) at the end is needed in case the second must be removed
  2. "xy0 Copy the macro in the register x
  3. 7| Move to the 7th char
  4. @x Run the macro from x
  5. 2|~ Switch the case of the first char (actually at the 2nd position)
  6. 0"xd7| Cut the macro in the register x
  7. @x Run the macro from x
  8. 0x Remove the place-holder .
\$\endgroup\$
  • 1
    \$\begingroup\$ :D I always upvote vim! \$\endgroup\$ – DJMcMayhem Jul 27 '16 at 19:36
1
\$\begingroup\$

Haskell, 52 bytes

import Data.Char
t=toUpper
r(h:s)=filter((t h/=).t)s
\$\endgroup\$
  • 2
    \$\begingroup\$ List comprehension saves 1 byte: r(h:s)=[c|c<-s,t c/=t h]. \$\endgroup\$ – nimi Oct 24 '15 at 22:22
1
\$\begingroup\$

TI-BASIC, 164 bytes

For TI-83+/84+ series graphing calculators.

Input Str1
1→X
"sub(Str1,X,1→u
"ABCDEFGHIJKLMNOPQRSTUVWXYZ zyxwvutsrqponmlkjihgfedcba
u+sub(Ans,54-inString(Ans,u),1
For(X,2,length(Str1
If 2<inString(Ans+Str1,u
Ans+u
End
sub(Ans,3,length(Ans)-2

TI-BASIC is clearly the wrong tool for the job.

  • It doesn't have a command to remove characters, so we need to loop through the string and append characters if they don't match what is to be removed.
  • It doesn't support empty strings, so we need to start the string with the two letters to be removed, build the output onto the end, and chop the first two characters off.
  • It doesn't have case-change commands, so we need to hardcode the entire alphabet...
    • twice...
    • Did I mention lowercase letters take two bytes each in the TI-BASIC encoding?

I'm not 100% sure, but I spent over six hours on this, and it seems to be the shortest possible solution.

To test this with non-uppercase inputs (or to type it into your calculator), make a different program with the contents AsmPrgmFDCB24DEC9, and run it using Asm([whatever you named it] to enable lowercase typing mode.

\$\endgroup\$
1
\$\begingroup\$

Lua, 93 78 Bytes

a=io.read()m=a:sub(1,1):upper()print(({a:gsub("["..m..m:lower().."]","")})[1])
\$\endgroup\$
1
\$\begingroup\$

Common Lisp, 77

(princ(let((x(read-char)))(remove-if(lambda(y)(char-equal x y))(read-line))))

Curse these long function names (and parentheses (but I still love them anyway (:3))).

\$\endgroup\$
1
\$\begingroup\$

C, 65 61 bytes

main(c,v)char**v;{for(c=**++v;*++*v;**v-c&31&&putchar(**v));}

Compiles with warnings. Reads string from argv[1]. Online example

\$\endgroup\$
  • 1
    \$\begingroup\$ You can shorten main(int c,char**v) to main(c,v)char**v;, and (**v-c)%32 to **v-c&31. \$\endgroup\$ – Dennis Oct 25 '15 at 4:38
1
\$\begingroup\$

C++, 100 99 98 bytes

#include<ios>
int main(){for(int c,f=getchar();~(c=getchar());)tolower(c)-tolower(f)&&putchar(c);}

Just one more byte to get under 100. getchar() returns -1 when it reads end of stream, so that's why the ~ is in for cycle. (~-1 == 0)

Ungolfed

#include <ios>
int main()
{
    for (int c, f = getchar(); ~(c = getchar()); )
        tolower(c) - tolower(f) && putchar(c);
}
\$\endgroup\$
  • \$\begingroup\$ Can't you use &&putchar(c) instead of ?putchar(c):0? \$\endgroup\$ – Neil Oct 24 '15 at 15:07
  • \$\begingroup\$ @Neil Thank you, now I am under 100 bytes! \$\endgroup\$ – Zereges Oct 24 '15 at 15:42
  • \$\begingroup\$ @ThomasKwa Of course I can, thanks. \$\endgroup\$ – Zereges Oct 25 '15 at 8:12
  • 1
    \$\begingroup\$ Can you do f-c&31 as in the C answer? \$\endgroup\$ – lirtosiast Oct 25 '15 at 8:17
1
\$\begingroup\$

AppleScript, 209 201 bytes

My only consolation is that I beat Brainfuck.

set a to(display dialog""default answer"")'s text returned
set n to a's characters's number
set o to""
repeat n
if not a's character n=a's character 1 then set o to a's character n&o
set n to n-1
end
o

How this works is that I take input through a, get the length of a and mark it as n, and set an output variable o. For every character that I find that does not contain the first character of a (a's character 1), I concatenate it to o. The final line prints o.

Note: This automatically supports all Unicode. c:

\$\endgroup\$
  • 3
    \$\begingroup\$ My only consolation is that I beat Brainfuck. looks like i'd better get golfing ;) \$\endgroup\$ – undergroundmonorail Oct 25 '15 at 15:33
1
\$\begingroup\$

Retina, 16 bytes

i`(.)(?<=^\1.*)

Save the code with a trailing linefeed and run it with the -s flag.

How it works: the trailing linefeed makes this a replacement stage, such that any matches of the given regex are replaced with an empty string. The i turns on case-insensitive mode which also makes backreferences case-insensitive. Finally, the regex simply matches and captures a single characters and then checks whether the first character in the string is the same (up to case) using a backreference.

\$\endgroup\$

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.