Jump to content

[SOLVED] Compare two ints for equality, without comparison operators.


FWDrew

Recommended Posts

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.

Link to comment
Share on other sites

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)

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.