Jump to content
php.lv forumi

Unikāli skaitļu diapazoni


goma smile

Recommended Posts


tāds primitīvs variants kad katru ar katru salidzina. atnaks codez un pa 3 min. uzrakstīs normālu algoritmu

 

foreach ( $ranges as $key1 => $range1)

{

foreach ( $ranges as $key2 => $range2)

{

if ($key1 != $key2 &&

$range1['cena_no'] <= $range2['cena_lidz'] &&

$range1['cena_lidz'] >= $range2['cena_no']

{

//error

}

}

}

Link to comment
Share on other sites

Jā, bet tad loģika ir nepareiza jautājumā, jo, ja abos diapazonos ir 5, tad tie diapazoni jebkurā gadījumā pārklājas.

 

Ja vajag atļaut, ka pēdējais un / vai pirmais pārklājas, tad var vienkārši pārbaudīt, vai sakrīt viens vai mazāk ciparu:

 

<?php

$range1 = range( 1, 5 );
$range2 = range( 5, 6 );

if ( count( array_intersect( $range1, $range2 ) ) <= 1 )
    echo 'Clean';
else
    echo 'Wrong';

?>
Link to comment
Share on other sites

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.   Paste as plain text instead

  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.

Loading...
×
×
  • Create New...