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 code-golf, so may the shortest code for each language win!
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.637In 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