Thread: Calculation

    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    18
    Rep Power
    0

    Calculation


    Hey,

    Ok so today I need to calculate the shortest distance between 2 numbers in an array, given the array loops the end to the beginning, and likewise the beginning to the end. The array can contain endless amount of numbers.

    Example:
    PHP Code:
    $numbers = array( 12345678910); 
    The distance between 2 and 5 would be 3, and the distance between 9 and 2 would also be 3.

    Im kind of stuck on this, and would very much appreciate the help.

    Timmey
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Jul 2003
    Posts
    3,497
    Rep Power
    594
    What constitutes the distance? The array indices or the values? Perhaps your example should not have been sequential values that correspond to the index-1.
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    18
    Rep Power
    0
    That is a very good question.The distance will increase by 1 for each element in the array, and not the actual values. I have found a solution in the meanwhile, if the values are sequential, and do not miss any values in between. However its not going to work as i need it to.

    PHP Code:
    $numbers = array( 13710305090 ); 
    In this case the distance between 7 - 30 would be 2 and 50 - 1 is also 2.

    Current solution:
    PHP Code:
    $value1 2;         
    $value2 9;         
    $numbers = array( 12345678910);        
    $max count$numbers ) ;        
    $one = ( $max $value1 $value2 ) % $max;          
    $two = ( $max $value2 $value1 ) % $max;

    $result min$one$two ); 
    Hope you can help me further.
  6. #4
  7. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,959
    Rep Power
    1014
    Originally Posted by Timmeyy
    The distance will increase by 1 for each element in the array, and not the actual values.
    Then your task makes no sense. The shortest distance between any two numbers would simply be 1 (or 0).
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    18
    Rep Power
    0
    No basically I want to count how many 'other' array elements I have to pass to get from value1 to value2, but you can walk both ways in the array like a loop.

    Sorry if I dont make any sense.. hard to explain myself properly.
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Jul 2003
    Posts
    3,497
    Rep Power
    594
    So distance is represented by the indices with wrap. What are the inputs to the algorithm? I'm guessing the values. You first would need to find the indices for the corresponding values then get the difference. I did not try this but off the top of my head the wrap is the problem. If the difference is > $max/2, add the indices, take the modulo $max then add 1 since the indices are zero based.
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  12. #7
  13. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,959
    Rep Power
    1014
    Sorry, I misunderstood you.

    The distance between two values v1, v2 is simply
    PHP Code:
    min(abs($key1 $key2), count($numbers) - abs($key1 $key2)); 
    With $key1 and $key2 being the keys of the values -- use array_search() for that.

    Comments on this post

    • Timmeyy agrees
    • gw1500se agrees : Damn! Why didn't I think of that?
  14. #8
  15. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    18
    Rep Power
    0
    Originally Posted by Jacques1
    PHP Code:
    min(abs($key1 $key2), count($numbers) - abs($key1 $key2)); 
    Worked perfectly. Thank you very much!

IMN logo majestic logo threadwatch logo seochat tools logo