3 added 954 characters in body
source | link

Perl, 64 63 bytes

#!/usr/bin/perl -p
s/ /" is ".("equal to ",greaterx,lessx)[$`<=>$']/e;s/x/ than /

62 bytes + 1 byte for -p. Takes input from STDIN, with the two numbers separated by a single space:

$ echo 1 2 | ./cmp
1 is less than 2
$ echo 42 -17 | ./cmp
42 is greater than -17
$ echo 123456789 123456789 | ./cmp
123456789 is equal to 123456789

How it works:

The <=> operator returns -1, 0, or 1 depending on whether the first operand is less than, equal to, or greater than the second. Conveniently, Perl allows negative subscripts with arrays and slices, where the last element is at position -1, the second-to-last element is at position -2, and so on.

In the code

("equal to ",greaterx,lessx)[$`<=>$']

we use the return value of <=> as the subscript in a list slice to get the corresponding string, where $` is the first number and $' is the second.

To avoid repeating than, x is used as a placeholder and replaced in a second substitution at the end.


Alternative solution, 63 bytes

#!/usr/bin/perl -p
@a=(equal,greater,than,to,less);s/ / is @a[$i=$`<=>$',!$i+2] /

62 bytes + 1 byte for -p. Takes space-separated input from STDIN just like the first solution.

How it works:

This solution also uses a slice, but takes advantage of the fact that unlike list slices, array slices can be interpolated into strings (and the RHS of substitutions). This lets us drop the /e modifier and the quotes in the substitution operator.

The real trick is in the slice subscript:

@a[$i=$`<=>$',!$i+2]

For the different values of <=>, this gives:

$i  !$i+2  $a[$i]  $a[!$i+2]
----------------------------
-1    2     less      than
 0    3     equal     to
 1    2     greater   than

When an array or array slice is interpolated into a string, the elements are automatically joined by $" (by default, a single space).

Perl, 64 63 bytes

#!/usr/bin/perl -p
s/ /" is ".("equal to ",greaterx,lessx)[$`<=>$']/e;s/x/ than /

62 bytes + 1 byte for -p. Takes input from STDIN, separated by a single space:

$ echo 1 2 | ./cmp
1 is less than 2
$ echo 42 -17 | ./cmp
42 is greater than -17
$ echo 123456789 123456789 | ./cmp
123456789 is equal to 123456789

How it works:

The <=> operator returns -1, 0, or 1 depending on whether the first operand is less than, equal to, or greater than the second. Conveniently, Perl allows negative subscripts with arrays and slices, where the last element is at position -1, the second-to-last element is at position -2, and so on.

In the code

("equal to ",greaterx,lessx)[$`<=>$']

we use the return value of <=> as the subscript in a list slice to get the corresponding string, where $` is the first number and $' is the second.

To avoid repeating than, x is used as a placeholder and replaced in a second substitution at the end.

Perl, 64 63 bytes

#!/usr/bin/perl -p
s/ /" is ".("equal to ",greaterx,lessx)[$`<=>$']/e;s/x/ than /

62 bytes + 1 byte for -p. Takes input from STDIN, with the two numbers separated by a single space:

$ echo 1 2 | ./cmp
1 is less than 2
$ echo 42 -17 | ./cmp
42 is greater than -17
$ echo 123456789 123456789 | ./cmp
123456789 is equal to 123456789

How it works:

The <=> operator returns -1, 0, or 1 depending on whether the first operand is less than, equal to, or greater than the second. Conveniently, Perl allows negative subscripts with arrays and slices, where the last element is at position -1, the second-to-last element is at position -2, and so on.

In the code

("equal to ",greaterx,lessx)[$`<=>$']

we use the return value of <=> as the subscript in a list slice to get the corresponding string, where $` is the first number and $' is the second.

To avoid repeating than, x is used as a placeholder and replaced in a second substitution at the end.


Alternative solution, 63 bytes

#!/usr/bin/perl -p
@a=(equal,greater,than,to,less);s/ / is @a[$i=$`<=>$',!$i+2] /

62 bytes + 1 byte for -p. Takes space-separated input from STDIN just like the first solution.

How it works:

This solution also uses a slice, but takes advantage of the fact that unlike list slices, array slices can be interpolated into strings (and the RHS of substitutions). This lets us drop the /e modifier and the quotes in the substitution operator.

The real trick is in the slice subscript:

@a[$i=$`<=>$',!$i+2]

For the different values of <=>, this gives:

$i  !$i+2  $a[$i]  $a[!$i+2]
----------------------------
-1    2     less      than
 0    3     equal     to
 1    2     greater   than

When an array or array slice is interpolated into a string, the elements are automatically joined by $" (by default, a single space).

2 added 333 characters in body
source | link

Perl, 6464 63 bytes

#!/usr/bin/perl -p
s/ /" is ".($`==$'?"equal to ":($`>$'?greater:less,greaterx,lessx)."[$`<=>$']/e;s/x/ than ")/e

6362 bytes + 1 byte for -p. Takes input from STDIN, separated by a single space:

$ echo 1 2 | ./cmp
1 is less than 2
$ echo 42 -17 | ./cmp
42 is greater than -17
$ echo 123456789 123456789 | ./cmp
123456789 is equal to 123456789

How it works:

Everything happensThe <=> operator returns -1, 0, or 1 depending on whether the first operand is less than, equal to, or greater than the second. Conveniently, Perl allows negative subscripts with a single invocation ofarrays and slices, where the substitution operatorlast element is at position -1, using the second-to-last element is at position /e modifier to allow-2, and so on.

In the code inside

("equal to ",greaterx,lessx)[$`<=>$']

we use the RHS. The LHS matchesreturn value of <=> as the space betweensubscript in a list slice to get the two numberscorresponding string, which automatically setswhere $` tois the first number and $' tois the second number. The rest

To avoid repeating than, x is pretty self explanatoryused as a placeholder and replaced in a second substitution at the end.

Perl, 64 bytes

#!/usr/bin/perl -p
s/ /" is ".($`==$'?"equal to ":($`>$'?greater:less)." than ")/e

63 bytes + 1 byte for -p. Takes input from STDIN, separated by a single space:

$ echo 1 2 | ./cmp
1 is less than 2
$ echo 42 -17 | ./cmp
42 is greater than -17
$ echo 123456789 123456789 | ./cmp
123456789 is equal to 123456789

How it works:

Everything happens with a single invocation of the substitution operator, using the /e modifier to allow code inside the RHS. The LHS matches the space between the two numbers, which automatically sets $` to the first number and $' to the second number. The rest is pretty self explanatory.

Perl, 64 63 bytes

#!/usr/bin/perl -p
s/ /" is ".("equal to ",greaterx,lessx)[$`<=>$']/e;s/x/ than /

62 bytes + 1 byte for -p. Takes input from STDIN, separated by a single space:

$ echo 1 2 | ./cmp
1 is less than 2
$ echo 42 -17 | ./cmp
42 is greater than -17
$ echo 123456789 123456789 | ./cmp
123456789 is equal to 123456789

How it works:

The <=> operator returns -1, 0, or 1 depending on whether the first operand is less than, equal to, or greater than the second. Conveniently, Perl allows negative subscripts with arrays and slices, where the last element is at position -1, the second-to-last element is at position -2, and so on.

In the code

("equal to ",greaterx,lessx)[$`<=>$']

we use the return value of <=> as the subscript in a list slice to get the corresponding string, where $` is the first number and $' is the second.

To avoid repeating than, x is used as a placeholder and replaced in a second substitution at the end.

1
source | link

Perl, 64 bytes

#!/usr/bin/perl -p
s/ /" is ".($`==$'?"equal to ":($`>$'?greater:less)." than ")/e

63 bytes + 1 byte for -p. Takes input from STDIN, separated by a single space:

$ echo 1 2 | ./cmp
1 is less than 2
$ echo 42 -17 | ./cmp
42 is greater than -17
$ echo 123456789 123456789 | ./cmp
123456789 is equal to 123456789

How it works:

Everything happens with a single invocation of the substitution operator, using the /e modifier to allow code inside the RHS. The LHS matches the space between the two numbers, which automatically sets $` to the first number and $' to the second number. The rest is pretty self explanatory.