Compare two numbers given as strings

21

1

I have a problem at work. I need to compare two numbers that come as strings from two different databases. The numbers may come with leading zeroes and/or leading/trailing spaces. So I may have "0001 " from one database and " 1 " from the other one.

I solved the problem in C# with the following code:

Func<string, string, bool> f = (a,b) => int.Parse(a.Trim()) == int.Parse(b.Trim())

The challenge

This is a really simple challenge, suitable for beginners and any kind of esoteric languages. Given two numbers as strings that may come with leading zeroes and/or leading/trailing spaces, write the shortest code that checks if the two strings represent the same number.

  • The inputs need to be two strings or the equivalent in your language (a char array is OK), and they will always represent integer values greater than zero.
  • The output must be any two consistent values that represent a truthy value and a falsey value.

Examples

A          B          Result
----------------------------
"0001"     "1    "    true
"1450"     "1450 "    true
"0010001 " " 10001  " true
"0010000"  "  10  "   false
"101023"   "101024"   false

This is , so may the shortest code for each language win!

Charlie

Posted 2018-01-09T10:23:22.390

Reputation: 11 448

3... – user202729 – 2018-01-09T11:14:01.083

8As least it isn't inspired by your kids this time... – caird coinheringaahing – 2018-01-09T15:13:17.517

2@cairdcoinheringaahing my own work is my second source of inspiration. I know this is a very simple challenge, but I think these easy challenges are also needed sometimes. I'll try to come up with something more difficult next time. – Charlie – 2018-01-09T15:15:56.607

By the way, it seems that my work-inspired challenges are either too easy or too hard...

– Charlie – 2018-01-09T15:51:16.587

@JonathanAllan no, each input string will represent only one number. There won't be any spaces inbetween the strings. – Charlie – 2018-01-09T19:49:33.410

Related sub-problem: Test if two numbers are equal: just the compare part, with the inputs already as a numeric type. (Too trivial for codegolf in most languages; only a few languages like Brainfuck have interesting answers).

– Peter Cordes – 2018-01-10T01:59:26.637

In your C# solution, is Trim() necessary? As far as I can tell, int.Parse can handle white space. – Ayb4btu – 2018-01-11T20:42:48.817

@Ayb4btu no, it's most probably not needed, but I wrote the code like that on purpose so that people could golf my C# answer. – Charlie – 2018-01-11T20:56:16.683

Another task: guess the DBs. I call SAP to be one of them. – n0rd – 2018-01-13T04:03:12.133

ah ok @charlie, I just wrote that shorter c# solution before reading these comments – lee – 2018-04-04T02:22:02.940

Answers

22

Javascript, 11 bytes

a=>b=>+a==b

Abusing Javascript's casting rules a bit; +a coerces a into a numeric type.

-6 bytes thanks to Shaggy and Martin Ender♦

Also a cool take by LiefdeWen:

a=>b=>~~a==~~b

elementbound

Posted 2018-01-09T10:23:22.390

Reputation: 321

2

Welcome to PPCG. You can do this in 11 bytes using currying and by only converting one of the inputs to an integer: https://tio.run/##y0osSyxOLsosKNHNy09J/Z@cn1ecn5Oql5OfrqHxv8LWrtLWTrvC1rbyv6aGkoKBhRKQAhKa/wE Note as well that, unless a function references itself, you don't need to include the variable assignment in your byte count.

– Shaggy – 2018-01-09T11:06:26.063

2Welcome to PPCG! Unnamed functions are fine so you don't need c=, and currying is also fine so you can use a=>b=>... instead of (a,b)=>. Finally, my JavaScript is a bit rusty, but I think something like !(a-b) would also work? – Martin Ender – 2018-01-09T11:06:27.137

The same bytes but it looks much cooler is ~~a==~~b – LiefdeWen – 2018-01-09T11:07:10.680

Hey, thanks for the warm welcome and the cool tips! For some reason I didn't realize that currying could be used to actually save bytes. I'll update the answer in a minute. Thanks again! :) – elementbound – 2018-01-09T12:29:00.810

1

See the tips for golfing in ES6 (and general JS golfing tips) for more tricks to squeeze bytes out of your solutions.

– Shaggy – 2018-01-09T12:42:19.213

1The cool versiopn is cool but limited in scope, try input '9123456789' and '533522197'. These are string rapresentation of integer values, are well inside the valid range of integers in javascript (but more than 32 bit) – edc65 – 2018-01-09T14:39:52.037

1Your text no longer matches the code; there's no subtraction. You're coercing a to a numeric type with unary +. – Peter Cordes – 2018-01-10T02:01:58.103

You are right, updated. – elementbound – 2018-01-10T09:28:57.370

18

05AB1E, 1 byte

Q

Try it online!

Explanation

Comparison for equality Q will automatically try to evaluate strings as ints before comparing.

Emigna

Posted 2018-01-09T10:23:22.390

Reputation: 50 798

Oh really? And I thought |0+Q was short enough... – Erik the Outgolfer – 2018-01-09T19:03:35.810

18

Operation Flashpoint scripting language, 33 bytes

f={call format(["%1==%2"]+_this)}

Call with:

hint format["%1\n%2\n%3\n%4\n%5",
    ["0001", "1    "] call f,
    ["1450", "1450 "] call f,
    ["0010001 ", " 10001  "] call f,
    ["0010000", "  10  "] call f,
    ["101023", "101024"] call f]

Output:

Alternative version (41 bytes):

f={{t=call _x;r=t==s;s=t}forEach _this;r}

Still 5 bytes shorter than the more straightforward f={t=_this;call(t select 0)==call(t select 1)}

39 bytes:

f={{t=call _x;r=t==s;s=t}count _this;r}

count (which returns the size of an array) works instead of forEach, because you can give it a "lambda" that is used as a condition to count only the array elements that meet that condition. The "condition" used in this case is not a valid condition, but it doesn't matter here because it doesn't cause an error and the return value of the count is not needed.

Steadybox

Posted 2018-01-09T10:23:22.390

Reputation: 15 798

7I like the variety of situations you take screenshots of your code in. – Οurous – 2018-01-09T19:58:17.973

11

Taxi, 488 bytes

Go to Post Office:w 1 l 1 r 1 l.Pickup a passenger going to The Babelfishery.Pickup a passenger going to The Babelfishery.Go to The Babelfishery:s 1 l 1 r.Pickup a passenger going to Equal's Corner.Pickup a passenger going to Equal's Corner.Go to Equal's Corner:n 1 l 1 l 1 l.Switch to plan "b" if no one is waiting.'1' is waiting at Writer's Depot.[b]'0' is waiting at Writer's Depot.Go to Writer's Depot:n 1 l 1 r.Pickup a passenger going to Post Office.Go to Post Office:n 1 r 2 r 1 l.

Try it online!

Ungolfed:

Go to Post Office: west 1st left 1st right 1st left.
Pickup a passenger going to The Babelfishery.
Pickup a passenger going to The Babelfishery.
Go to The Babelfishery: south 1st left 1st right.
Pickup a passenger going to Equal's Corner.
Pickup a passenger going to Equal's Corner.
Go to Equal's Corner: north 1st left 1st left 1st left.
Switch to plan "b" if no one is waiting.
'1' is waiting at Writer's Depot.
[b]
'0' is waiting at Writer's Depot.
Go to Writer's Depot: north 1st left 1st right.
Pickup a passenger going to Post Office.
Go to Post Office: north 1st right 2nd right 1st left.

Taxi is (relatively) well-suited to this challenge because strings are the only input or output type allowed. The Babelfishery is what converts strings to number (and vice versa) and it handles stripping all the spaces and leading zeroes. It will also handle negative numbers if the - is immediately before the first digit. After that, Equal's Corner checks the two values against each other and Writer's Depot provides the output in string format. Output is 1 for truthy and 0 for falsey.

Engineer Toast

Posted 2018-01-09T10:23:22.390

Reputation: 5 769

1Nice one! Just out of curiosity, why is the debug window showing the message error: The boss couldn't find your taxi in the garage. You're fired!? – Charlie – 2018-01-09T15:57:52.910

6@Charlie Taxi requires you to drive back to the taxi garage when you're done with your route. If you don't bring the car back, you get fired. (You also have to be sure to stop and refuel every now and then or you run out of gas.) In [tag:code-golf] questions that don't disallow output to STDERR, I generally don't worry about getting fired. You can add this to get rid of the error if you don't want to lose your job: Go to Taxi Garage:n 1 r 1 l 1 r. – Engineer Toast – 2018-01-09T16:01:28.180

This answer and its comments are totally surrealistic. I love this community. – Right Leg – 2018-01-11T23:10:43.867

@RightLeg My favorite Taxi program I've written took me two weeks and a lot of debugging. You really want a trip, though? Check out Shakespeare.

– Engineer Toast – 2018-01-11T23:28:36.343

10

C (gcc), 27 bytes

f(s,t){s=atoi(s)==atoi(t);}

With -O0 (which is the default setting).

Try it online!

C, 32 bytes

f(s,t){return atoi(s)==atoi(t);}

Try it online!

Steadybox

Posted 2018-01-09T10:23:22.390

Reputation: 15 798

In the debug window you can see that the types of s and t default to int, but still you can pass strings as parameters? C will always amaze me... – Charlie – 2018-01-09T10:34:55.277

2@Charlie In C, strings are arrays of char, and when you pass an array to a function you actually just pass the pointer to the first element of the array. Here, the pointers to the char arrays are implicitly converted to integers when passed to the function, and the integers are converted back to pointers when calling atoi. – Steadybox – 2018-01-09T10:38:57.250

1which could blow up horribly on a platform where pointers are wider than ints ... just saying, of course it's fine for site standards here :) – Felix Palmen – 2018-01-09T12:33:29.203

2If you want to use ridiculous crap like s=foo; instead of return foo; you need to label this as "x86 gcc with optimization disabled", not just "C", because this code doesn't return a value except as an implementation artefact. – Peter Cordes – 2018-01-09T13:18:08.943

@PeterCordes Well, yeah. I'm not a big fan of "returning" this way, either, but you see it on this site a lot (because answers here can have undefined behaviour as long as they work seemingly consistently) and it's always just labeled as C (gcc). I added the x86, but because -O0 is the default for gcc, the "optimizations disabled" doesn't need to be explicitly mentioned. – Steadybox – 2018-01-09T15:05:40.473

In the real world, most people who build code with gcc do so with optimization enabled (usually with a CFLAGS= setting in a Makefile that came with the code...). x86 gcc implies (to me) that the code uses some C extensions that gcc provides, like __builtin_ia32_crc32qi (for the CRC32 instruction), and will work with any gcc options. i.e. the word GCC means "Gnu Compile Collection", not that it only works with a bare gcc command, and not with any options. I think the hack is dumb, and we shouldn't let people get away with it without a big ugly warning label to show how hacky it is. – Peter Cordes – 2018-01-10T01:27:59.893

2

I went looking to see if there were any architectures where s=retval; instead of return retval; actually broke. It looks like gcc's internal logic (with -O0 only) actually treats the last expression as the return value, maybe the same way that a GNU C statement-expression works. (But it does require s=). On ARM32, it calculates the == result in r3, and then uses an extra mov r0, r3 to make that the return value! So it's not an x86-specific hack, it's a gcc -O0 hack that gcc seems to go out of its way to support.

– Peter Cordes – 2018-01-10T01:50:35.583

https://godbolt.org/g/44nzQy – Peter Cordes – 2018-01-10T01:54:01.630

For f(s,t), if sizeof(char*) != sizeof(int) this will fail, such as on old Windows with 16 bit integers but 32 bit FAR pointers, and here is an example on more modern architectures.

– Ken Y-N – 2018-01-10T06:51:13.173

2@KenY-N You're missing the entire point of c code golf. It's about finding those specific hacky edge cases where you can input the minimum amount of code and get the results you want, not providing a portable experience. – None – 2018-01-10T18:47:07.437

8

J, 4 bytes

=&do

Compare = after & evaluating do. Can also be =&". Try it online!

FrownyFrog

Posted 2018-01-09T10:23:22.390

Reputation: 3 112

Interestingly enough this will work for both negatives as other languages understand them (e.g. -10) and negatives as J understands them (e.g. _10) due to the way monadic - works. – cole – 2018-01-09T19:17:25.497

6

Retina, 11 bytes

.+
$*
D`
¶$

Try it online!

Input is linefeed separated, but the test suite uses comma separation for convenience. Prints 1 for equality and 0 for inequality.

Explanation

.+
$*

Convert each line to unary. This ignores leading zeros and spaces.

D`

Deduplicate: clear the second line if both are the same.

¶$

Check that the string now ends in a linefeed.

Martin Ender

Posted 2018-01-09T10:23:22.390

Reputation: 184 808

4

Brain-Flak, 22 bytes

(([{}]{})[{(){}}](){})

Try it online!

Saved 4 bytes thanks to Jo King.

Mr. Xcoder

Posted 2018-01-09T10:23:22.390

Reputation: 39 774

22 bytes – Jo King – 2018-01-13T03:29:28.690

4

Python 3, 25 bytes

lambda a,b:int(a)==int(b)

Try it online!

Mr. Xcoder

Posted 2018-01-09T10:23:22.390

Reputation: 39 774

3

Triangularity, 17 bytes

..)..
.Ii).
@Ii=.

Try it online!

Triangularity is, for once, competitive!

How it works

Triangularity requires the code to have a triangular distribution of the dots. That is, the length of each row must be equal the number of rows multiplied by 2 and decremented, and each row must have (on each side) a number of dots equal to its position in the program (the bottom row is row 0, the one above it is row 1 and so forth). Keeping this in mind, let's analyse how the code works:

..).. || Push a 0 onto the stack.
.Ii   || Get the 0th input and cast it to an integer.
   ). || Push another 0 onto the stack.
@I    || Increment the ToS => 1. Get the first input.
  i=. || Then cast it to an integer and compare their equality.

Mr. Xcoder

Posted 2018-01-09T10:23:22.390

Reputation: 39 774

3

APL (Dyalog), 4 bytes

3 bytes saved thanks to @Adám

=/⍎¨

Try it online!

Uriel

Posted 2018-01-09T10:23:22.390

Reputation: 11 708

=/⍎¨ – Adám – 2018-01-09T10:46:09.587

3

Japt, 3 bytes

¥Vn

Try it

Converts the second input to an integer and compares equality with the first.

Shaggy

Posted 2018-01-09T10:23:22.390

Reputation: 24 623

3

Alice, 7 bytes

X/n
o@i

Try it online!

Any non-digit separator works. Prints 1 for equality and 0 otherwise.

Explanation

X   XOR, does nothing.
/   Switch to Ordinal mode.
i   Read all input as a string.
/   Switch to Cardinal mode.
X   XOR. Implicitly finds all integers in the string and pushes them separately
    onto the stack. The XOR gives 0 if the values are identical.
n   Logical NOT. Gives 1 for equal inputs and 9 otherwise.
/   Switch to Ordinal.
o   Print the 0 or 1 as a string.
/   Switch to Cardinal.
@   Terminate the program.

Martin Ender

Posted 2018-01-09T10:23:22.390

Reputation: 184 808

3

jq, 24 characters

map(tonumber)|.[0]==.[1]

The 2 strings are passed as items of an array.

Sample run:

bash-4.4$ jq 'map(tonumber)|.[0]==.[1]' <<< '["0010001 ", " 10001  "]'
true

bash-4.4$ jq 'map(tonumber)|.[0]==.[1]' <<< '["0010000", "  10  "]'
false

Try it online! (All test cases)

manatwork

Posted 2018-01-09T10:23:22.390

Reputation: 17 865

3

Excel VBA, 27 16 Bytes

-9 Thanks to @Nayrb and @TaylorScott

[C1]=[A1]-[B1]=0

Where you input the values on the Cells with 'string.

Where x and y are the input Strings and z is a Boolean output.

If CInt(x)=CInt(y) Then z=1

Using CInt

danieltakeshi

Posted 2018-01-09T10:23:22.390

Reputation: 139

1Why not: z=x-y=0? – Nayrb – 2018-01-09T15:44:03.330

Unfortunately this solution is invalid as it does not take input and rather relies on having predefined values (which has been deemed against the community's rules) and outputing to a variable (which is also against community rules)

– Taylor Scott – 2018-01-11T14:36:19.417

As for a valid solution perhaps a vbe immediate window function such as ?[Int(A1)=Int(B1)] that takes input from the ranges A1 and B1 and outputs to the VBE immediate window – Taylor Scott – 2018-01-11T14:38:04.350

3

Husk, 3 bytes

¤=r

Try it online!

Explanation

¤    -- combine both arguments of ..
 =   -- .. equality ..
  r  -- .. with read

ბიმო

Posted 2018-01-09T10:23:22.390

Reputation: 15 345

1If you take input as a two-element list, Ër works too. – Zgarb – 2018-01-10T11:56:22.287

3

PowerShell, 20 bytes

param($a,$b)+$a-eq$b

Similar to the JavaScript answer, just longer because PowerShell doesn't have currying. Uses + to cast the first string to integer, and then the -equals automatically casts the second string to integer. Output is True/False.

Try it online!

AdmBorkBork

Posted 2018-01-09T10:23:22.390

Reputation: 41 581

3

T-SQL, 35 bytes

Per our standards, data is input via pre-existing table t with varchar fields a and b.

    SELECT IIF(ABS(a)=ABS(b),1,0)FROM t

Returns 1 if they match, 0 if they don't.

A few of SQL's mathematical functions (including ABS, FLOOR and CEILING) will do an implicit conversion to numeric if given string parameters, this is shorter than an explicit CAST(a AS INT) or CONVERT(INT,b), and works in this case since we know the input values are always positive.

IIF is specific to MS SQL 2012 and above, so no guarantee about other implementations.

BradC

Posted 2018-01-09T10:23:22.390

Reputation: 6 099

3

Q (Kdb+), 13 bytes

=/["J"$(x;y)]

Explanation

(x;y) : a list of the two inputs.

"J"$ : casting each input to a long (7j) type from string (10c), which can correctly interpret
white space and leading 0s.

=/ : checks equality over elements in a list (each subsequent pair).
As there is only one pair, returns single boolian 0b/1b.

Sidney

Posted 2018-01-09T10:23:22.390

Reputation: 31

Welcome to PPCG! Usually, a link to an online interpreter/compiler is appreciated, and if not obvious, an explanation as well. – FantaC – 2018-01-10T19:40:46.550

Thanks! Explanation added there. I doubt there is an online interpreter for q, I did a quick look and couldn't find one. – Sidney – 2018-01-11T15:15:28.430

I would say that this should be written as a lambda including the curly braces, i.e. {=/["J"$(x;y)]} for 15 bytes... Although for 8 bytes you could have this: (~/)"J"$ if you are just using the REPL and passing the inputs as a list of strings... or {(~/)"J"$x} for 11 as a function. – streetster – 2018-01-13T11:05:40.040

3

PowerShell, 19 bytes

$args-join'-eq'|iex

Try it online!

-join the argument array ($args) with the string representation of the comparison operator (-eq) then evaluate the expression with Invoke-Expression (iex).

briantist

Posted 2018-01-09T10:23:22.390

Reputation: 3 110

Interesting. I wonder how you get by without needing to cast to integer, whereas mine breaks if I remove the + since it's doing string equality checking. – AdmBorkBork – 2018-01-12T13:40:24.763

@AdmBorkBork because I'm essentially generating PowerShell code and then executing it, and since leading zeroes and leading/trailing spaces don't matter for the interpreter, it "just works". If the 2 numbers are 00009 and 077 then the resultant code is 00009 -eq077, a perfectly valid piece of code. You're dealing directly with the string at runtime, so you must cast it first so the operation is done on a numeric. – briantist – 2018-01-12T20:18:56.443

Right, yes, that makes sense. Thanks for the explanation. – AdmBorkBork – 2018-01-12T21:00:23.853

3

R, 28 27 bytes

!diff(as.double(scan(,'')))

Reads numbers as strings, converts them to doubles and checks if their difference is not zero.

Try it online!

−1 byte thanks to Giuseppe

Robert Hacken

Posted 2018-01-09T10:23:22.390

Reputation: 371

Just "" is sufficient – Giuseppe – 2018-04-04T01:44:07.507

2

Haskell, 20 bytes

-11 bytes thanks to Laikoni. -2 bytes thanks to Zgarb.

a#b=0+read a==read b

Try it online!

totallyhuman

Posted 2018-01-09T10:23:22.390

Reputation: 15 378

1@Laikoni It's even better to not bind read: a#b=0+read a==read b – Zgarb – 2018-01-09T10:58:38.257

2

Lua, 20 bytes

print(...-arg[2]==0)

Try it online!

Jonathan S.

Posted 2018-01-09T10:23:22.390

Reputation: 423

2

Sinclair ZX81/Timex TS1000/1500 BASIC, ~29 tokenized BASIC bytes

New solution thanks to Neil (thanks for the tip).

 1 INPUT A$
 2 INPUT B$
 3 PRINT VAL A$=VAL B$

This solution requires user input, so enter in two strings with white spaces and/or leading zeros, or enter two strings of non-equal numeric value; 0 is false and 1 is true once line three has compared the value of each string entered.

Old solution: Sinclair ZX81/Timex TS1000/1500 BASIC, ~46 tokenized BASIC bytes

 1 LET A$=" 001 "
 2 LET B$="1"
 3 PRINT (VAL A$=VAL B$)

The actual check is done in line three, which is only ~16 tokenized BASIC bytes; so entering each test case pair using direct mode will save ~30 bytes from the listing. Note that this byte count does not include the var stack.

Shaun Bebbers

Posted 2018-01-09T10:23:22.390

Reputation: 1 814

1Shouldn't you use INPUT A$ and INPUT B$? Also I don't think you need the ()s. – Neil – 2018-01-09T16:19:00.877

Yes, I could use that also - I just wanted a proof of concept – Shaun Bebbers – 2018-01-09T18:42:08.110

2

Gema, 21 characters

*\n*=@cmpn{*;*;0;1;0}

No boolean in Gema. As the @get-switch{}/@set-switch{} functions use 0 and 1 to represent switch statuses, also used 0 and 1.

The 2 strings are passed on separate input lines.

Sample run:

bash-4.4$ gema '*\n*=@cmpn{*;*;0;1;0}' <<< $'0010001\n10001  '
1

bash-4.4$ gema '*\n*=@cmpn{*;*;0;1;0}' <<< $'0010000\n  10  '
0

manatwork

Posted 2018-01-09T10:23:22.390

Reputation: 17 865

2

Perl 5, 9 + 1 (-p) = 10 bytes

$_=$_==<>

try it online

Nahuel Fouilleul

Posted 2018-01-09T10:23:22.390

Reputation: 5 582

2

Stacked, 8 bytes

[#~\#~=]

Try it online!

Explanation

[#~\#~=]   anonymous function
 #~        eval TOS
   \       swap top two
    #~     eval TOS
      =    push equality of top two

Conor O'Brien

Posted 2018-01-09T10:23:22.390

Reputation: 36 228

2

Swift, 80 79 78 bytes

import Foundation;func r(g:[NSString])->Bool{return g[0].intValue==g[1].intValue}

Try it online!

Tamás Sengel

Posted 2018-01-09T10:23:22.390

Reputation: 211

2

Welcome to the site! As Uriel mentioned, this currently counts as a snippet, rather than a function or full program, as it assumes the input is stored in variables. You should change this to be in accordance to these I/O rules. Also, you can check out these tips for golfing in Swift!

– caird coinheringaahing – 2018-01-09T15:31:50.673

1@Uriel Edited my answer. – Tamás Sengel – 2018-01-09T15:31:53.483

2

Attache, 11 bytes

Same@Map&:N

Try it online!

This takes an array of strings, such as V["0001", "1 "]. Simply stated, Map&:N is a function that maps N over its argument, and Same checks that the array contains only equal members. (Fun fact: this function works for more than 2 string arguments.)

Conor O'Brien

Posted 2018-01-09T10:23:22.390

Reputation: 36 228

2

SNOBOL4 (CSNOBOL4), 42 bytes

	OUTPUT =EQ(EVAL(INPUT),EVAL(INPUT)) 1
END

Try it online!

Outputs 1 for truthy, nothing for falsey. Since (space) is the concatenation operator in SNOBOL, EVALing a number with leading/trailing spaces yields the number itself, and it also neatly takes care of any leading zeroes. EQ tests for numerical equality, conditionally setting OUTPUT to 1 on Success.

Giuseppe

Posted 2018-01-09T10:23:22.390

Reputation: 21 077

2

Java 8, 58 51 bytes

7 bytes thanks to Kevin

b->c->new Long(b.trim()).equals(new Long(c.trim()))

Try it online!

Explanation

Trims off spaces, casting to Long removes leading zeros, and then returns boolean value if they are equal or not.

DevelopingDeveloper

Posted 2018-01-09T10:23:22.390

Reputation: 1 415

1

You can save a byte by using currying: b->c-> instead of (b,c)->. In addition, the trailing semi-colon doesn't have to be counted with Java/.NET lambdas. Try it online: 56 bytes

– Kevin Cruijssen – 2018-01-10T08:36:28.553

151 bytes by using new Long(...) instead of Long.parseLong(...) – Kevin Cruijssen – 2018-01-10T08:40:54.747

1Had to check the docs, cause I did not think equals would work, but it does, Long equals checks value not the object – dwana – 2018-01-12T15:59:19.193

@dwana Yea, I wish == didn't compare objects or I could have used that and saved a few bytes :P – DevelopingDeveloper – 2018-01-12T16:41:35.600

2

APL (NARS2000), 7 bytes

=⍥⍎

Well, yes, I do know NARS2000 can't compete over Dyalog here since it uses Unicode, but I thought I'd rather show off (called Composition in NARS2000, even though it's actually unrelated to function composition), something Dyalog doesn't have as a built-in and I haven't ever seen used here. In Dyalog, it has to be implemented as {(⍵⍵⍺)⍺⍺⍵⍵ ⍵}. What it does is call the right operand monadic function on both the left and right argument, and then call the left operand dyadic function on the results.

Here, the right operand is (Execute, i.e. eval) and the left operand is = (Equal To, i.e. check if its arguments are equal).

Erik the Outgolfer

Posted 2018-01-09T10:23:22.390

Reputation: 38 134

Are composition and execute supposed to render as whitespace? – John Dvorak – 2018-01-09T23:04:35.180

@JohnDvorak um, no? They're not whitespace, the second char is U+2365 APL FUNCTIONAL SYMBOL CIRCLE DIAERESIS and the third is U+234E APL FUNCTIONAL SYMBOL DOWN TACK JOT. – Erik the Outgolfer – 2018-01-10T11:21:12.517

@JohnDvorak Whatever font you're viewing this in may not have symbols for those codepoints. – Οurous – 2018-01-11T07:04:00.557

@Οurous I highly suspect that's the case too, but if you're using well-updated software you shouldn't have any problem viewing such chars. – Erik the Outgolfer – 2018-01-11T12:22:50.077

2

Jelly, 8 bytes

t€⁶V€€ḌE

Try it online! or see the test suite

35 answers, and no Jelly submission? Takes the argument as a list (["0001", "1 "] for the first example)

How it works

t€⁶V€€ḌE - Main link. Argument: list       e.g.    ["0001", "1    "]
 €       - For €ach element...                     ["0001", "1"]
t        -   trim from both sides...
  ⁶      -   spaces
     €   - For €ach element...
   V€    -   evaluate €ach character               [[0, 0, 0, 1], [1]]
      Ḍ  - Convert from digits                     [1, 1]
       E - Are they equal?                         1

caird coinheringaahing

Posted 2018-01-09T10:23:22.390

Reputation: 13 702

1Isn't this surprisingly long for Jelly? – Charlie – 2018-01-09T21:16:29.403

@Charlie V (eval) is evil, meaning that it can't use the same approach as most other submissions – caird coinheringaahing – 2018-01-09T21:20:50.523

2

R, 37 35 bytes

pryr::f(as.double(x)==as.double(y))

There's probably some cleverer way to coerce strings to numerics but I haven't figured it out yet.

Try it online!

rturnbull

Posted 2018-01-09T10:23:22.390

Reputation: 3 689

1as.double is shorter. – Giuseppe – 2018-01-09T21:48:24.350

@Giuseppe Thanks! – rturnbull – 2018-01-09T22:12:58.547

2

><>, 2 + 3 = 5 bytes

=n

Try it online!

Uses the -v flag to pass the values into the program. This automatically converts them to numbers, removing the spaces and leading 0s. It then outputs if the two are equal, and exits with an error as it tries to compare an empty stack.

Jo King

Posted 2018-01-09T10:23:22.390

Reputation: 38 234

2

dc, 9 bytes

[1p]sQ?=Q

Outputs 1 if the numbers are equal, or nothing if unequal. The input numbers are separated by ',', and this gives a warning since data is code in dc. I wasn't sure if space was allowed as delimiter, so I chose one that doesn't stop the program.

Try it online!

seshoumara

Posted 2018-01-09T10:23:22.390

Reputation: 2 878

2

Excel, 8 bytes

=A1-B1=0

Excel does the string to num conversion for us.

(To input as string, prefix values with '.)

Wernisch

Posted 2018-01-09T10:23:22.390

Reputation: 2 534

2

Batch, 89 bytes

@call:c %1
@set e=%errorlevel%
@call:c %2
@if %e%==%errorlevel% echo 1
:c
@exit/b%~1

Prints 1 if the two numbers are the same. Explanation: Batch's numeric processing usually uses leading zeros to indicate an octal number, however exit/b always takes a decimal number. The ~ removes any quotes that would be needed to pass leading or trailing spaces in the parameter.

Neil

Posted 2018-01-09T10:23:22.390

Reputation: 95 035

2

Ruby, 23 bytes

s=->x,y{x.to_i==y.to_i}

NTCG

Posted 2018-01-09T10:23:22.390

Reputation: 151

Usage: s.call("0123", "123") – NTCG – 2018-01-11T04:56:43.970

1

Pyth, 4 bytes

qFsM

Try it online!

Int-parse each element of the input list with sM(Q), then Fold over equality.

Dave

Posted 2018-01-09T10:23:22.390

Reputation: 432

1

Wolfram Language, 27 bytes

ToExpression[#1<>"=="<>#2]&

Test cases:

In[1]:= ToExpression[#1 <> "==" <> #2] &["0001", "1"]
Out[1]= True

In[2]:= ToExpression[#1 <> "==" <> #2] &["1450", "1450 "]
Out[2]= True

In[3]:= ToExpression[#1 <> "==" <> #2] &["0010001", " 10001"]
Out[3]= True

In[4]:= ToExpression[#1 <> "==" <> #2] &["0010000", "  10  "]
Out[4]= False

In[5]:= ToExpression[#1 <> "==" <> #2] &["101023", "101024"]
Out[5]= False

user6014

Posted 2018-01-09T10:23:22.390

Reputation: 288

1

AWK, 12 bytes

{$0=$1==$2}1

Try it online!

AWK, 9 bytes

$0=$1==$2

Alternate version: outputs 1 on match, otherwise outputs nothing

Try it online!

Noskcaj

Posted 2018-01-09T10:23:22.390

Reputation: 421

Oftentimes nothing is not considered a falsey value. If this is run on mutliline input, it's not possible to tell which lines fail. Just my $0.02. Adding {}1 would print the 0 falsey values. – Robert Benson – 2018-01-11T18:59:09.677

1

Clean, 52 bytes

import StdEnv
$n=toInt{#c\\c<-n|c>' '}
?n=(==)($n)o$

Try it online!

Οurous

Posted 2018-01-09T10:23:22.390

Reputation: 7 916

1

C++ (gcc), 65 bytes

#include<cstdlib>
auto a(auto b,auto c){return atoi(b)==atoi(c);}

Try it online!

I initially tried used a lambda expression, but auto a=[] is three more characters, and less one more for no trailing semicolon. On the other hand, since atoi is not referenced until the lambda is instantiated, could I delete the #include and put that in the test harness footer instead?

Ken Y-N

Posted 2018-01-09T10:23:22.390

Reputation: 396

1

Javascript, 6 bytes

Pretty standard stuff, but using Javascript's auto type casting

!(a-b)

Or if we don't need it as a function (19 bytes)

let f=(a,b)=>!(a-b)

Try it online! https://jsfiddle.net/pgz4ctLy/2/

General Sirhc

Posted 2018-01-09T10:23:22.390

Reputation: 19

2

Welcome to the site! Note that this is quite similar to the existing Javascript answer...

– Charlie – 2018-01-10T12:08:18.637

1Thanks @charlie, I saw that answer and I didn't believe it met the requirements of "inputs and output" so I posted this answer. Unfortunately being new to the site I wasn't allowed to comment on the other answer so this seemed like the most suitable option and is different enough to stand as it's own. – General Sirhc – 2018-01-10T12:13:30.567

1There's a lot of ways you can post a valid answer, and one of them is writing a lambda expression. So a valid answer (using your code) would be just (a,b)=>-a==-b. If you use currying, you can write a=>b=>-a==-b. – Charlie – 2018-01-10T12:18:04.267

1

Just to let you know even duplicate answers are fine (although they're rare).

– Martin Ender – 2018-01-10T12:29:45.857

1I have edited my post to be a bit more unique and shorter ;) – General Sirhc – 2018-01-10T12:47:56.773

1

PHP 5.6+, 16 (42 when callable) bytes

The minimum code that meets the puzzle's requirements (16 bytes):

(int)$a==(int)$b

In callable form (42 bytes):

function f($a,$b){return(int)$a==(int)$b;}

Try it online, with tests

Explanation

Declare a regular function with a single-character name f with two, otherwise undetermined parameters. It returns the result of comparing both arguments after casting each to integers, which safely removed the padding and whitespace. The code could have been made 2 bytes shorter by converting the function to a closure, but then the closure would not be assigned (which takes another 3 bytes), and therefore not callable.

Xano

Posted 2018-01-09T10:23:22.390

Reputation: 111

2Welcome to PPCG! All answers need to be full programs or callable functions (although they may be unnamed function expressions). So the first snippet isn't valid, but the second one is fine. – Martin Ender – 2018-01-10T17:04:35.073

@Xano, PHP's type juggling will implicitly convert "numerical looking" strings to numbers, so all you need to do is use the == operator, as long as one is a number. A 32 byte solution using type-coercion would be: function($a,$b){return$a==0+$b;} TIO: https://tio.run/##ZY7BisMgEIbP9SmGQTBhhZrd9rLW9rZPsLckBxOUFEoSojmVPnt2TMKysB50nO/7dcZuXC63sRuBe7P4uW/jfegzbiVv8ufk4jz13Bqj3nijX4vm0YUYwEDJDiUqpQqUgAXQwlrSfjgeIU6zS7g4ndWK6VzxX0jRFAeUCFuVlP@GSgIZv9jbR9ieJ/7@QXgtTjuGXag1Y36YnG27DPaxbaAKcngy0h73EIlYCbyhniGkU9@13QAChVwZVljFCjFZ@w3MFcrU8NmWzuEG4pumFvAJ4it9T2msqx41ey0/

– 640KB – 2019-01-24T16:19:33.860

1

Mumps, 7 bytes

W +X=+Y

In Mumps, all data are strings, the '+' operator evaluates the string to a numeric value. There is no implicit output function, so the 'W' (Write) function provides the output.

zmerch

Posted 2018-01-09T10:23:22.390

Reputation: 541

1

Tcl, 45 bytes

proc x a\ b {expr [scan $a %d]==[scan $b %d]}

Try it online!

sergiol

Posted 2018-01-09T10:23:22.390

Reputation: 3 055

1

K (oK), 5 bytes

Solution:

=/.:'

Try it online!

Examples:

=/.:'("0001";"1    ")
1
=/.:'("1450";"1450 ")
1
=/.:'("0010001";" 10001  ")
1
=/.:'("0010000";"  10  ")
0
=/.:'("101023";"101024")
0

Explanation:

Convert each input to an integer, and then check for equality.

=/.:' / the solution
  .:' / value (.:) each ('), converts char list to integer
=/    / equality (=) over (/)

streetster

Posted 2018-01-09T10:23:22.390

Reputation: 3 635

1

Perl 6, 4 bytes

*==*

Test it

The == operator converts to Numeric which ignores leading and trailing whitespace, and leading 0's get ignored just the same. (0o127 is how you write an octal)

The two * are just there to turn it into a lambda that takes two arguments.

Brad Gilbert b2gills

Posted 2018-01-09T10:23:22.390

Reputation: 12 713

1

Implicit, 5 bytes

'ì'ì=

Try it online!

'ì'ì=
'      «read string, push to stack                  »;
 ì     «swap type (integer/string) of top of stack  »;
  'ì   «do it again for the second integer string   »;
    =  «compare for equality                        »;

MD XF

Posted 2018-01-09T10:23:22.390

Reputation: 11 605

1

C# (32 bytes)

a=>b=>int.Parse(a)==int.Parse(b)

Try it online

as already mentioned in the comments, you can do it shorter than the C# provided in the question. I wrote this thinking I was clever before reading the reply to the comments by the OP. :)

lee

Posted 2018-01-09T10:23:22.390

Reputation: 200

1

The whitespace isn't needed at all, and currying can save an additional byte. Try it online!

– Dennis – 2018-04-04T02:27:47.440

1

R, 55 bytes

V<-strtoi(gsub(" ","",c(A,B),fixed=T),base=9)
V[1]==V[2]

This is my first attempt at codegolf; gave it my best try. Suggestions welcome!

Assumptions:

  1. The inputs are given as strings A and B

  2. The integers contained are less than 1888888888. Larger values will cause strtoi() to return NA.

KamRa

Posted 2018-01-09T10:23:22.390

Reputation: 11

0

Stratos, 3 bytes

_=_

Both of the _ take an input as a string and convert it to a number, then negate it.

Java converts the input to a number by removing 0s and spaces.

The = takes the results of this and checks for equality

Try it!

Okx

Posted 2018-01-09T10:23:22.390

Reputation: 15 025

0

Pyt, 1 byte

=

Explanation

         Implicitly takes two inputs, and casts them to integers/floats if possible.
=        Checks if the two are equal

Try it online!

mudkip201

Posted 2018-01-09T10:23:22.390

Reputation: 833

0

FALSE, 36 bytes

[0[^$9>][$32>[48-+10*]?]#%10/]$!\!=.

Numbers are received from the console, separated by a tab character. True=-1, False=0.

12Me21

Posted 2018-01-09T10:23:22.390

Reputation: 6 110