Skip to main content
We’ve updated our Terms of Service. A new AI Addendum clarifies how Stack Overflow utilizes AI interactions.
Code Golf

Return to Answer

replaced http://codegolf.stackexchange.com/ with https://codegolf.stackexchange.com/
Source Link

##Perl (no +/-, no tie-breakers, 29 chars)##

s!!xx!;s!x!$"x<>!eg;say y!!!c

As a bonus, you can make the code sum more than two numbers by adding more xs to the s!!xx!.

Alternatively, here are two 21-char solutions with 1 and 3 tie-breakers respectively

say length$"x<>.$"x<>
say log exp(<>)*exp<>

Note: These solutions use the say function, available since Perl 5.10.0 with the -E command line switch or with use 5.010. See the edit history of this answer edit history of this answer for versions that work on older perls.


How does the solution with no tie-breakers work?

  • s!!xx! is a regexp replacement operator, operating by default on the $_ variable, which replaces the empty string with the string xx. (Usually / is used as the regexp delimiter in Perl, but really almost any character can be used. I chose ! since it's not a tie-breaker.) This is just a fancy way of prepending "xx" to $_ — or, since $_ starts out empty (undefined, actually), it's really a way to write $_ = "xx" without using the equals sign (and with one character less, too).

  • `s!x!$"x!eg` is another regexp replacement, this time replacing each `x` in `$_` with the value of the expression `$" x `. (The `g` switch specifies global replacement, `e` specifies that the replacement is to be evaluated as Perl code instead of being used as a literal string.) `$"` is a [special variable](http://perldoc.perl.org/perlvar.html#$LIST_SEPARATOR) whose default value happens to be a single space; using it instead of `" "` saves one char. (Any other variable known to have a one-character value, such as `$&` or `$/`, would work equally well here, except that using `$/` would cost me a tie-breaker.)

    The `` [line input operator](http://perldoc.perl.org/perlop.html#I%2fO-Operators), in scalar context, reads one line from standard input and returns it. The `x` before it is the Perl [string repetition operator](http://perldoc.perl.org/perlop.html#Multiplicative-Operators), and is really the core of this solution: it returns its left operand (a single space character) repeated the number of times given by its right operand (the line we just read as input).

  • y!!!c is just an obscure way to (ab)use the transliteration operator to count the characters in a string ($_ by default, again). I could've just written say length, but the obfuscated version is one character shorter. :)

##Perl (no +/-, no tie-breakers, 29 chars)##

s!!xx!;s!x!$"x<>!eg;say y!!!c

As a bonus, you can make the code sum more than two numbers by adding more xs to the s!!xx!.

Alternatively, here are two 21-char solutions with 1 and 3 tie-breakers respectively

say length$"x<>.$"x<>
say log exp(<>)*exp<>

Note: These solutions use the say function, available since Perl 5.10.0 with the -E command line switch or with use 5.010. See the edit history of this answer for versions that work on older perls.


How does the solution with no tie-breakers work?

  • s!!xx! is a regexp replacement operator, operating by default on the $_ variable, which replaces the empty string with the string xx. (Usually / is used as the regexp delimiter in Perl, but really almost any character can be used. I chose ! since it's not a tie-breaker.) This is just a fancy way of prepending "xx" to $_ — or, since $_ starts out empty (undefined, actually), it's really a way to write $_ = "xx" without using the equals sign (and with one character less, too).

  • `s!x!$"x!eg` is another regexp replacement, this time replacing each `x` in `$_` with the value of the expression `$" x `. (The `g` switch specifies global replacement, `e` specifies that the replacement is to be evaluated as Perl code instead of being used as a literal string.) `$"` is a [special variable](http://perldoc.perl.org/perlvar.html#$LIST_SEPARATOR) whose default value happens to be a single space; using it instead of `" "` saves one char. (Any other variable known to have a one-character value, such as `$&` or `$/`, would work equally well here, except that using `$/` would cost me a tie-breaker.)

    The `` [line input operator](http://perldoc.perl.org/perlop.html#I%2fO-Operators), in scalar context, reads one line from standard input and returns it. The `x` before it is the Perl [string repetition operator](http://perldoc.perl.org/perlop.html#Multiplicative-Operators), and is really the core of this solution: it returns its left operand (a single space character) repeated the number of times given by its right operand (the line we just read as input).

  • y!!!c is just an obscure way to (ab)use the transliteration operator to count the characters in a string ($_ by default, again). I could've just written say length, but the obfuscated version is one character shorter. :)

##Perl (no +/-, no tie-breakers, 29 chars)##

s!!xx!;s!x!$"x<>!eg;say y!!!c

As a bonus, you can make the code sum more than two numbers by adding more xs to the s!!xx!.

Alternatively, here are two 21-char solutions with 1 and 3 tie-breakers respectively

say length$"x<>.$"x<>
say log exp(<>)*exp<>

Note: These solutions use the say function, available since Perl 5.10.0 with the -E command line switch or with use 5.010. See the edit history of this answer for versions that work on older perls.


How does the solution with no tie-breakers work?

  • s!!xx! is a regexp replacement operator, operating by default on the $_ variable, which replaces the empty string with the string xx. (Usually / is used as the regexp delimiter in Perl, but really almost any character can be used. I chose ! since it's not a tie-breaker.) This is just a fancy way of prepending "xx" to $_ — or, since $_ starts out empty (undefined, actually), it's really a way to write $_ = "xx" without using the equals sign (and with one character less, too).

  • `s!x!$"x!eg` is another regexp replacement, this time replacing each `x` in `$_` with the value of the expression `$" x `. (The `g` switch specifies global replacement, `e` specifies that the replacement is to be evaluated as Perl code instead of being used as a literal string.) `$"` is a [special variable](http://perldoc.perl.org/perlvar.html#$LIST_SEPARATOR) whose default value happens to be a single space; using it instead of `" "` saves one char. (Any other variable known to have a one-character value, such as `$&` or `$/`, would work equally well here, except that using `$/` would cost me a tie-breaker.)

    The `` [line input operator](http://perldoc.perl.org/perlop.html#I%2fO-Operators), in scalar context, reads one line from standard input and returns it. The `x` before it is the Perl [string repetition operator](http://perldoc.perl.org/perlop.html#Multiplicative-Operators), and is really the core of this solution: it returns its left operand (a single space character) repeated the number of times given by its right operand (the line we just read as input).

  • y!!!c is just an obscure way to (ab)use the transliteration operator to count the characters in a string ($_ by default, again). I could've just written say length, but the obfuscated version is one character shorter. :)

Rollback to Revision 6
Source Link
Ilmari Karonen
  • 21k
  • 5
  • 55
  • 101

##Perl (no +/-, no tie-breakers, 2829 chars)##

s!!xx!;s!x!1x<>$"x<>!eg;say y!!!c

As a bonus, you can make the code sum more than two numbers by adding more xs to the s!!xx!.

Alternatively, here are two 21-char solutions with 1 and 3 tie-breakers respectively

say length$"x<>.$"x<>
say log exp(<>)*exp<>

Note: These solutions use the say function, available since Perl 5.10.0 with the -E command line switch or with use 5.010. See the edit history of this answer for versions that work on older perls.


How does the solution with no tie-breakers work?

  • s!!xx! is a regexp replacement operator, operating by default on the $_ variable, which replaces the empty string with the string xx. (Usually / is used as the regexp delimiter in Perl, but really almost any character can be used. I chose ! since it's not a tie-breaker.) This is just a fancy way of prepending "xx" to $_ — or, since $_ starts out empty (undefined, actually), it's really a way to write $_ = "xx" without using the equals sign (and with one character less, too).

  • `s!x!1x$"x!eg` is another regexp replacement, this time replacing each `x` in `$_` with the value of the expression `1`$" x `. (The `g` switch specifies global replacement, `e` specifies that the replacement is to be evaluated as Perl code instead of being used as a literal string.) `$"` is a [special variable](http://perldoc.perl.org/perlvar.html#$LIST_SEPARATOR) whose default value happens to be a single space; using it instead of `" "` saves one char. (Any other variable known to have a one-character value, such as `$&` or `$/`, would work equally well here, except that using `$/` would cost me a tie-breaker.)

    The `` [line input operator](http://perldoc.perl.org/perlop.html#I%2fO-Operators), in scalar context, reads one line from standard input and returns it. The `x` before it is the Perl [string repetition operator](http://perldoc.perl.org/perlop.html#Multiplicative-Operators), and is really the core of this solution: it returns its left operand (the number `1`a single space character) repeated the number of times given by its right operand (the line we just read as input).

  • y!!!c is just an obscure way to (ab)use the transliteration operator to count the characters in a string ($_ by default, again). I could've just written say length, but the obfuscated version is one character shorter. :)

##Perl (no +/-, no tie-breakers, 28 chars)##

s!!xx!;s!x!1x<>!eg;say y!!!c

As a bonus, you can make the code sum more than two numbers by adding more xs to the s!!xx!.

Alternatively, here are two 21-char solutions with 1 and 3 tie-breakers respectively

say length$"x<>.$"x<>
say log exp(<>)*exp<>

Note: These solutions use the say function, available since Perl 5.10.0 with the -E command line switch or with use 5.010. See the edit history of this answer for versions that work on older perls.


How does the solution with no tie-breakers work?

  • s!!xx! is a regexp replacement operator, operating by default on the $_ variable, which replaces the empty string with the string xx. (Usually / is used as the regexp delimiter in Perl, but really almost any character can be used. I chose ! since it's not a tie-breaker.) This is just a fancy way of prepending "xx" to $_ — or, since $_ starts out empty (undefined, actually), it's really a way to write $_ = "xx" without using the equals sign (and with one character less, too).

  • `s!x!1x!eg` is another regexp replacement, this time replacing each `x` in `$_` with the value of the expression `1 x `. (The `g` switch specifies global replacement, `e` specifies that the replacement is to be evaluated as Perl code instead of being used as a literal string.)

    The `` [line input operator](http://perldoc.perl.org/perlop.html#I%2fO-Operators), in scalar context, reads one line from standard input and returns it. The `x` before it is the Perl [string repetition operator](http://perldoc.perl.org/perlop.html#Multiplicative-Operators), and is really the core of this solution: it returns its left operand (the number `1`) repeated the number of times given by its right operand (the line we just read as input).

  • y!!!c is just an obscure way to (ab)use the transliteration operator to count the characters in a string ($_ by default, again). I could've just written say length, but the obfuscated version is one character shorter. :)

##Perl (no +/-, no tie-breakers, 29 chars)##

s!!xx!;s!x!$"x<>!eg;say y!!!c

As a bonus, you can make the code sum more than two numbers by adding more xs to the s!!xx!.

Alternatively, here are two 21-char solutions with 1 and 3 tie-breakers respectively

say length$"x<>.$"x<>
say log exp(<>)*exp<>

Note: These solutions use the say function, available since Perl 5.10.0 with the -E command line switch or with use 5.010. See the edit history of this answer for versions that work on older perls.


How does the solution with no tie-breakers work?

  • s!!xx! is a regexp replacement operator, operating by default on the $_ variable, which replaces the empty string with the string xx. (Usually / is used as the regexp delimiter in Perl, but really almost any character can be used. I chose ! since it's not a tie-breaker.) This is just a fancy way of prepending "xx" to $_ — or, since $_ starts out empty (undefined, actually), it's really a way to write $_ = "xx" without using the equals sign (and with one character less, too).

  • `s!x!$"x!eg` is another regexp replacement, this time replacing each `x` in `$_` with the value of the expression `$" x `. (The `g` switch specifies global replacement, `e` specifies that the replacement is to be evaluated as Perl code instead of being used as a literal string.) `$"` is a [special variable](http://perldoc.perl.org/perlvar.html#$LIST_SEPARATOR) whose default value happens to be a single space; using it instead of `" "` saves one char. (Any other variable known to have a one-character value, such as `$&` or `$/`, would work equally well here, except that using `$/` would cost me a tie-breaker.)

    The `` [line input operator](http://perldoc.perl.org/perlop.html#I%2fO-Operators), in scalar context, reads one line from standard input and returns it. The `x` before it is the Perl [string repetition operator](http://perldoc.perl.org/perlop.html#Multiplicative-Operators), and is really the core of this solution: it returns its left operand (a single space character) repeated the number of times given by its right operand (the line we just read as input).

  • y!!!c is just an obscure way to (ab)use the transliteration operator to count the characters in a string ($_ by default, again). I could've just written say length, but the obfuscated version is one character shorter. :)

shave off one more char... silly me for not thinking of that sooner
Source Link
Ilmari Karonen
  • 21k
  • 5
  • 55
  • 101

##Perl (no +/-, no tie-breakers, 2928 chars)##

s!!xx!;s!x!$"x<>1x<>!eg;say y!!!c

As a bonus, you can make the code sum more than two numbers by adding more xs to the s!!xx!.

Alternatively, here are two 21-char solutions with 1 and 3 tie-breakers respectively

say length$"x<>.$"x<>
say log exp(<>)*exp<>

Note: These solutions use the say function, available since Perl 5.10.0 with the -E command line switch or with use 5.010. See the edit history of this answer for versions that work on older perls.


How does the solution with no tie-breakers work?

  • s!!xx! is a regexp replacement operator, operating by default on the $_ variable, which replaces the empty string with the string xx. (Usually / is used as the regexp delimiter in Perl, but really almost any character can be used. I chose ! since it's not a tie-breaker.) This is just a fancy way of prepending "xx" to $_ — or, since $_ starts out empty (undefined, actually), it's really a way to write $_ = "xx" without using the equals sign (and with one character less, too).

  • `s!x!$"x1x!eg` is another regexp replacement, this time replacing each `x` in `$_` with the value of the expression `$"`1 x `. (The `g` switch specifies global replacement, `e` specifies that the replacement is to be evaluated as Perl code instead of being used as a literal string.) `$"` is a [special variable](http://perldoc.perl.org/perlvar.html#$LIST_SEPARATOR) whose default value happens to be a single space; using it instead of `" "` saves one char. (Any other variable known to have a one-character value, such as `$&` or `$/`, would work equally well here, except that using `$/` would cost me a tie-breaker.)

    The `` [line input operator](http://perldoc.perl.org/perlop.html#I%2fO-Operators), in scalar context, reads one line from standard input and returns it. The `x` before it is the Perl [string repetition operator](http://perldoc.perl.org/perlop.html#Multiplicative-Operators), and is really the core of this solution: it returns its left operand (a single space characterthe number `1`) repeated the number of times given by its right operand (the line we just read as input).

  • y!!!c is just an obscure way to (ab)use the transliteration operator to count the characters in a string ($_ by default, again). I could've just written say length, but the obfuscated version is one character shorter. :)

##Perl (no +/-, no tie-breakers, 29 chars)##

s!!xx!;s!x!$"x<>!eg;say y!!!c

As a bonus, you can make the code sum more than two numbers by adding more xs to the s!!xx!.

Alternatively, here are two 21-char solutions with 1 and 3 tie-breakers respectively

say length$"x<>.$"x<>
say log exp(<>)*exp<>

Note: These solutions use the say function, available since Perl 5.10.0 with the -E command line switch or with use 5.010. See the edit history of this answer for versions that work on older perls.


How does the solution with no tie-breakers work?

  • s!!xx! is a regexp replacement operator, operating by default on the $_ variable, which replaces the empty string with the string xx. (Usually / is used as the regexp delimiter in Perl, but really almost any character can be used. I chose ! since it's not a tie-breaker.) This is just a fancy way of prepending "xx" to $_ — or, since $_ starts out empty (undefined, actually), it's really a way to write $_ = "xx" without using the equals sign (and with one character less, too).

  • `s!x!$"x!eg` is another regexp replacement, this time replacing each `x` in `$_` with the value of the expression `$" x `. (The `g` switch specifies global replacement, `e` specifies that the replacement is to be evaluated as Perl code instead of being used as a literal string.) `$"` is a [special variable](http://perldoc.perl.org/perlvar.html#$LIST_SEPARATOR) whose default value happens to be a single space; using it instead of `" "` saves one char. (Any other variable known to have a one-character value, such as `$&` or `$/`, would work equally well here, except that using `$/` would cost me a tie-breaker.)

    The `` [line input operator](http://perldoc.perl.org/perlop.html#I%2fO-Operators), in scalar context, reads one line from standard input and returns it. The `x` before it is the Perl [string repetition operator](http://perldoc.perl.org/perlop.html#Multiplicative-Operators), and is really the core of this solution: it returns its left operand (a single space character) repeated the number of times given by its right operand (the line we just read as input).

  • y!!!c is just an obscure way to (ab)use the transliteration operator to count the characters in a string ($_ by default, again). I could've just written say length, but the obfuscated version is one character shorter. :)

##Perl (no +/-, no tie-breakers, 28 chars)##

s!!xx!;s!x!1x<>!eg;say y!!!c

As a bonus, you can make the code sum more than two numbers by adding more xs to the s!!xx!.

Alternatively, here are two 21-char solutions with 1 and 3 tie-breakers respectively

say length$"x<>.$"x<>
say log exp(<>)*exp<>

Note: These solutions use the say function, available since Perl 5.10.0 with the -E command line switch or with use 5.010. See the edit history of this answer for versions that work on older perls.


How does the solution with no tie-breakers work?

  • s!!xx! is a regexp replacement operator, operating by default on the $_ variable, which replaces the empty string with the string xx. (Usually / is used as the regexp delimiter in Perl, but really almost any character can be used. I chose ! since it's not a tie-breaker.) This is just a fancy way of prepending "xx" to $_ — or, since $_ starts out empty (undefined, actually), it's really a way to write $_ = "xx" without using the equals sign (and with one character less, too).

  • `s!x!1x!eg` is another regexp replacement, this time replacing each `x` in `$_` with the value of the expression `1 x `. (The `g` switch specifies global replacement, `e` specifies that the replacement is to be evaluated as Perl code instead of being used as a literal string.)

    The `` [line input operator](http://perldoc.perl.org/perlop.html#I%2fO-Operators), in scalar context, reads one line from standard input and returns it. The `x` before it is the Perl [string repetition operator](http://perldoc.perl.org/perlop.html#Multiplicative-Operators), and is really the core of this solution: it returns its left operand (the number `1`) repeated the number of times given by its right operand (the line we just read as input).

  • y!!!c is just an obscure way to (ab)use the transliteration operator to count the characters in a string ($_ by default, again). I could've just written say length, but the obfuscated version is one character shorter. :)

use `say`
Source Link
Ilmari Karonen
  • 21k
  • 5
  • 55
  • 101
Loading
added 2320 characters in body
Source Link
Ilmari Karonen
  • 21k
  • 5
  • 55
  • 101
Loading
added 98 characters in body
Source Link
Ilmari Karonen
  • 21k
  • 5
  • 55
  • 101
Loading
added 191 characters in body
Source Link
Ilmari Karonen
  • 21k
  • 5
  • 55
  • 101
Loading
added 83 characters in body
Source Link
Ilmari Karonen
  • 21k
  • 5
  • 55
  • 101
Loading
Source Link
Ilmari Karonen
  • 21k
  • 5
  • 55
  • 101
Loading

AltStyle によって変換されたページ (->オリジナル) /