FWDrew Posted April 26, 2009 Share Posted April 26, 2009 Hey Everyone, I'm a long time fan of this site but I hardly post. I was reading some programming interview questions on a blog the other day and one of them was "Write a function that checks if two ints are equal without using comparison operators." Now, I know it's a dumb question as this would really never happen in a production environment, but it got me thinking. I am not a php pro by any means, but I enjoy the language and would like to learn more. Below is what I came up with, which gets the job done but feels very sloppy. What would you guys do to solve this? How could I improve my code? My attempt: <?php ini_set("display_errors", "1"); error_reporting(E_ALL); function int_equal_check($int_one, $int_two){ $int_one = explode(' ', $int_one); $int_two = explode(' ', $int_two); $int_array = array_merge($int_one, $int_two); //Will strip out any repeated values in an array //Now we can check if there is only one value in the array, meaning a match was found. $uniq_array = array_unique($int_array); if(isset($uniq_array[1])){ echo 'Numbers are different!'; } else{ echo 'Numbers are the same!'; } } int_equal_check(1, 20); echo '<br />'; int_equal_check(35, 35); ?> A bit nervous putting my code out there as I know it will get torn apart but I'm always willing to learn a better way Regards, Drew Edit-I also wasn't sure what section to put this in as technically I don't need 'help'. I'm just curious as to what others would do and how I could improve my code. Apologies if this thread belongs elsewhere. Quote Link to comment Share on other sites More sharing options...
DarkWater Posted April 26, 2009 Share Posted April 26, 2009 That's a stupid interview question. <?php function int_equal_check($one, $two) { if (!($one + -$two)) { return false; } return true; } Quote Link to comment Share on other sites More sharing options...
FWDrew Posted April 26, 2009 Author Share Posted April 26, 2009 I agree it is a dumb question, I guess sometimes these things just amuse me. Thanks for your response too, your solution is a lot more simple than mine. Regards, Drew Quote Link to comment Share on other sites More sharing options...
DarkWater Posted April 26, 2009 Share Posted April 26, 2009 Why were you exploding by " " in your code, by the way? Just curious. Quote Link to comment Share on other sites More sharing options...
.josh Posted April 26, 2009 Share Posted April 26, 2009 function int_equal_check($one,$two) { if (strcmp($one,$two)) return false; return true; } function int_equal_check($one,$two) { if (bccomp($one,$two)) return false; return true; } function int_equal_check($one,$two) { if (gmp_cmp($one,$two)) return false; return true; } function int_equal_check($one,$two) { if (preg_match("~^{$one}$~",$two)) return true; return false; } man I could go on all night... (edit: fixed regex pattern forgot start/end) Quote Link to comment Share on other sites More sharing options...
.josh Posted April 26, 2009 Share Posted April 26, 2009 Why were you exploding by " " in your code, by the way? Just curious. yeah...I meant to also post on that...just cut to the chase and do $int_array = array($int_one,$int_two); Quote Link to comment Share on other sites More sharing options...
DarkWater Posted April 26, 2009 Share Posted April 26, 2009 <?php function int_equal_check($one, $two) { return (bool) !($one - $two); } Win. Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted April 26, 2009 Share Posted April 26, 2009 function int_equal_check($one, $two) { return !($one - $two); } Quote Link to comment Share on other sites More sharing options...
FWDrew Posted April 26, 2009 Author Share Posted April 26, 2009 Why were you exploding by " " in your code, by the way? Just curious. yeah...I meant to also post on that...just cut to the chase and do $int_array = array($int_one,$int_two); I was completely over thinking this when I wrote it (hindsight is always 20/20) but I should have just cut to the chase like Crayon Violent said. Anyway, like I said I am no php pro, but I enjoy watching everyone come up with different (and much better solutions) than mine. I'm learning BTW, thanks very much Crayon Violent, you have introduced me to some new functions that I'm looking up now. Regards, Drew Quote Link to comment Share on other sites More sharing options...
DarkWater Posted April 26, 2009 Share Posted April 26, 2009 Nice, PFM. I forgot ! will just convert turn it to a bool value. xD Quote Link to comment Share on other sites More sharing options...
FWDrew Posted April 26, 2009 Author Share Posted April 26, 2009 function int_equal_check($one, $two) { return !($one - $two); } Well now I am just embarrassed Nice work. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.