473,396 Members | 2,010 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

If you're good with arrays and lists, this one's for you

Hi,

I'm using PHP 4.4.4. Let's say I have two scalars, $list1 and $list2.
Both are comma separated lists of ordered, unsigned, unique integers.
An example would be

$list1 = "2,26,345";
$list2 = "3,4,26,35,525";

I am wondering if there is a short way of determining if all the
numbers in $list1 occur in $list2.

Grateful for any advice. Thanks, - Dave

Dec 15 '06 #1
5 1263
la***********@zipmail.com wrote:
Hi,

I'm using PHP 4.4.4. Let's say I have two scalars, $list1 and $list2.
Both are comma separated lists of ordered, unsigned, unique integers.
An example would be

$list1 = "2,26,345";
$list2 = "3,4,26,35,525";

I am wondering if there is a short way of determining if all the
numbers in $list1 occur in $list2.

Grateful for any advice. Thanks, - Dave
If, as in your example, the two lists are sorted, it can be done in
O(n+m) time by doing stepped compare. If they are unsorted, it can also
be done in O(n+m) time (with a much bigger constant factor) by making
the second list into the subscripts of an array, then checking for the
existence of the first list's elements in the new array.

Sample code (untested):

$i1 = 0;
$i2 = 0;
while($i1<count($list1) && $i2<count($list2)) {
if($list2[$i2]==$list1[$i1]) {
$i1++;
$i2++;
}
elseif($list2[$i2]<$list1[$i1]) {
$i2++;
}
else { /* this the failure case */ }
$fail = TRUE;
break;
}
}

for($i2==0; $i2<count($list2); $i2++) {
$test[$list2[$i2]] = TRUE;
}
for($i1==0; $i1<count($list1); $i1++) {
if(!array_key_exists($list1[$i1],$test) {
$fail = TRUE;
break;
}
}

We use these techniques rather than the much easier to code in_array()
because the time for this technique in O(m*n) since $list2 must be
searched completely for each element of $list1.

Dec 15 '06 #2
Or you could use array_diff:

<?php

$list1 = "2,26,345";
$list2 = "3,4,26,35,525";

$alist1 = split(',', $list1);
$alist2 = split(',', $list2);

if(count(array_diff($alist1, $alist2)) == 0) {

echo "Yep. They're all in there.\n";

} else {

echo "Nope. Yer missing some.\n";
}

?>

Bob Stearns wrote:
la***********@zipmail.com wrote:
Hi,

I'm using PHP 4.4.4. Let's say I have two scalars, $list1 and $list2.
Both are comma separated lists of ordered, unsigned, unique integers.
An example would be

$list1 = "2,26,345";
$list2 = "3,4,26,35,525";

I am wondering if there is a short way of determining if all the
numbers in $list1 occur in $list2.

Grateful for any advice. Thanks, - Dave
If, as in your example, the two lists are sorted, it can be done in
O(n+m) time by doing stepped compare. If they are unsorted, it can also
be done in O(n+m) time (with a much bigger constant factor) by making
the second list into the subscripts of an array, then checking for the
existence of the first list's elements in the new array.

Sample code (untested):

$i1 = 0;
$i2 = 0;
while($i1<count($list1) && $i2<count($list2)) {
if($list2[$i2]==$list1[$i1]) {
$i1++;
$i2++;
}
elseif($list2[$i2]<$list1[$i1]) {
$i2++;
}
else { /* this the failure case */ }
$fail = TRUE;
break;
}
}

for($i2==0; $i2<count($list2); $i2++) {
$test[$list2[$i2]] = TRUE;
}
for($i1==0; $i1<count($list1); $i1++) {
if(!array_key_exists($list1[$i1],$test) {
$fail = TRUE;
break;
}
}

We use these techniques rather than the much easier to code in_array()
because the time for this technique in O(m*n) since $list2 must be
searched completely for each element of $list1.
Dec 15 '06 #3
That is a shorter code, but we don't know what search logic is used. It
may devolve to the O(m*n) case, especially since it accommodates more
than one compare list.

Michael Cooney wrote:
Or you could use array_diff:

<?php

$list1 = "2,26,345";
$list2 = "3,4,26,35,525";

$alist1 = split(',', $list1);
$alist2 = split(',', $list2);

if(count(array_diff($alist1, $alist2)) == 0) {

echo "Yep. They're all in there.\n";

} else {

echo "Nope. Yer missing some.\n";
}

?>

Bob Stearns wrote:
>>la***********@zipmail.com wrote:
>>>Hi,

I'm using PHP 4.4.4. Let's say I have two scalars, $list1 and $list2.
Both are comma separated lists of ordered, unsigned, unique integers.
An example would be

$list1 = "2,26,345";
$list2 = "3,4,26,35,525";

I am wondering if there is a short way of determining if all the
numbers in $list1 occur in $list2.

Grateful for any advice. Thanks, - Dave

If, as in your example, the two lists are sorted, it can be done in
O(n+m) time by doing stepped compare. If they are unsorted, it can also
be done in O(n+m) time (with a much bigger constant factor) by making
the second list into the subscripts of an array, then checking for the
existence of the first list's elements in the new array.

Sample code (untested):

$i1 = 0;
$i2 = 0;
while($i1<count($list1) && $i2<count($list2)) {
if($list2[$i2]==$list1[$i1]) {
$i1++;
$i2++;
}
elseif($list2[$i2]<$list1[$i1]) {
$i2++;
}
else { /* this the failure case */ }
$fail = TRUE;
break;
}
}

for($i2==0; $i2<count($list2); $i2++) {
$test[$list2[$i2]] = TRUE;
}
for($i1==0; $i1<count($list1); $i1++) {
if(!array_key_exists($list1[$i1],$test) {
$fail = TRUE;
break;
}
}

We use these techniques rather than the much easier to code in_array()
because the time for this technique in O(m*n) since $list2 must be
searched completely for each element of $list1.

Dec 15 '06 #4
la***********@zipmail.com wrote:
$list1 = "2,26,345";
$list2 = "3,4,26,35,525";

I am wondering if there is a short way of determining if all the
numbers in $list1 occur in $list2.
Fast or easy?

Easy:

$arr1 = explode(',', $list1);
$arr2 = explode(',', $list2);
$allin = TRUE;
foreach ($arr1 as $n)
{
if (!in_array($n, $arr2))
{
$allin = FALSE;
break 1;
}
}
// $allin is now TRUE iff every number in $list1 is in $list2.

Fast:

/************************************************** *************
* We exploit the fact that $list1 and $list2 are sorted lists.
* This will speed things up, but will only be noticeable when
* dealing with lists of many hundreds of numbers.
************************************************** *************/
$arr1 = explode(',', $list1);
$arr2 = explode(',', $list2);
$allin = TRUE;
while (isset($arr1[0]))
{
$n = (int)array_shift($arr1);
while ((int)$arr2[0] < $n)
array_shift($arr2);
if ((int)$arr2[0]>$n)
{
$allin = FALSE;
break 1;
}
}
// $allin is now TRUE iff every number in $list1 is in $list2.

--
Toby A Inkster BSc (Hons) ARCS
Contact Me ~ http://tobyinkster.co.uk/contact

Dec 15 '06 #5
Rik
Toby Inkster wrote:
la***********@zipmail.com wrote:
>$list1 = "2,26,345";
$list2 = "3,4,26,35,525";

I am wondering if there is a short way of determining if all the
numbers in $list1 occur in $list2.

Fast or easy?

Easy:

$arr1 = explode(',', $list1);
$arr2 = explode(',', $list2);
$allin = TRUE;
foreach ($arr1 as $n)
{
if (!in_array($n, $arr2))
{
$allin = FALSE;
break 1;
}
}
// $allin is now TRUE iff every number in $list1 is in $list2.

Fast:

/************************************************** *************
* We exploit the fact that $list1 and $list2 are sorted lists.
* This will speed things up, but will only be noticeable when
* dealing with lists of many hundreds of numbers.
************************************************** *************/
$arr1 = explode(',', $list1);
$arr2 = explode(',', $list2);
$allin = TRUE;
while (isset($arr1[0]))
{
$n = (int)array_shift($arr1);
while ((int)$arr2[0] < $n)
array_shift($arr2);
if ((int)$arr2[0]>$n)
{
$allin = FALSE;
break 1;
}
}
Well, and now ths short code version (not more efficient (only if you
needed the difference)):

$difference = array_diff(explode(',',$list1),explode(',',$list2) );
if(count($difference)==0){
echo 'Every item in $list1 is in $list2.';
} else {
echo 'The following numbers are not present in $list2: '.implode(',
',$difference);
}
--
Rik Wasmus
Dec 15 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: lawrence | last post by:
2 Questions: 1.) Can anyone think of a way to speed up this function? It is terribly slow. I plan to reduce the number of directories to 3, which I guess will speed it up in the end. 2.) This...
11
by: Barney Norris | last post by:
Hi, The W3C validator tells me this page isn't valid HTML 4.01 Strict: http://www-student.cs.york.ac.uk/~jban100/wont_validate.html The reason it gives is I've closed meta tags with a '/'...
383
by: John Bailo | last post by:
The war of the OSes was won a long time ago. Unix has always been, and will continue to be, the Server OS in the form of Linux. Microsoft struggled mightily to win that battle -- creating a...
354
by: Montrose... | last post by:
After working in c# for a year, the only conclusion I can come to is that I wish I knew c. All I need is Linux, the gnu c compiler and I can do anything. Web services are just open sockets...
11
by: cooch17 | last post by:
Greetings - I have a number of websites that make use of popups spawned by a user clicking a link. Nothing fancy - here is typical code: <a href="popup.html" target="pop_win"...
8
by: dick | last post by:
I am just trying to print/report the results of a "filter by selection" which is done by right-clicking a form, filling in values, and "applying the filter." I have searched the newsgroups, and...
4
by: sea | last post by:
I have a database in Access 2002 but I am unable to view code or write any modules when logged in with a limited user account using Windows XP, service pack 2 -- no problems when logging in as...
6
by: MLH | last post by:
I'm tired of reading the fine print! Too many registrars out there. So I'm asking this group for a little help. I'm looking for a Registrar who will do an honest day's work for a fair price. I'm...
10
by: smerf | last post by:
Don't ask why (it'll just make your head hurt - I know mine does just thinking about the screwed up logic my manager has for this little project)......but I need to be able to tunnel a TCP...
3
by: Rob | last post by:
Hi all, I have a bit of a complicated question, hope we have an SQL guru out there that can help us solve this killer problem. Due to the size of SQL Database we have (largest in the US), we...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.