Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

List compares

5 views
Skip to first unread message

Niv (KP)

unread,
Aug 17, 2010, 2:06:24 PM8/17/10
to
How do I check a set of equal length lists, which all contain just 2
bit hexadecimal strings, are all different to one another.

I then sort the lists, and now....

w do I check the set of equal length lists, which all contain just 2
bit hexadecimal strings, are all the same as one another.


I can code this up into nested loops, exiting when differences found,
but is there a better way?

Gerald W. Lester

unread,
Aug 17, 2010, 2:19:11 PM8/17/10
to

set isEqual [expr {[lsort $list1] == [lsort $list2]}]


--
+------------------------------------------------------------------------+
| Gerald W. Lester, President, KNG Consulting LLC |
| Email: Gerald...@kng-consulting.net |
+------------------------------------------------------------------------+

Arjen Markus

unread,
Aug 18, 2010, 2:43:35 AM8/18/10
to
On 17 aug, 20:19, "Gerald W. Lester" <Gerald.Les...@KnG-

Consulting.net> wrote:
> Niv (KP) wrote:
> > How do I check a set of equal length lists, which all contain just 2
> > bit hexadecimal strings, are all different to one another.
>
> > I then sort the lists, and now....
>
> > w do I check the set of equal length lists, which all contain just 2
> > bit hexadecimal strings, are all the same as one another.
>
> > I can code this up into nested loops, exiting when differences found,
> > but is there a better way?
>
> set isEqual [expr {[lsort $list1] == [lsort $list2]}]
>
> --
> +------------------------------------------------------------------------+
> | Gerald W. Lester, President, KNG Consulting LLC                        |
> | Email: Gerald.Les...@kng-consulting.net                                |
> +------------------------------------------------------------------------+

set isEqual [expr {[lsort $list1] eq [lsort $list2]}]

might be slightly faster - no need to interpret the list as a number
then.

Regards,

Arjen

Niv (KP)

unread,
Aug 19, 2010, 3:15:42 PM8/19/10
to
> Arjen- Hide quoted text -
>
> - Show quoted text -

Assuming I have 16 lists to check against one another, I can compare
sorted (-unique) L1 with L2 with L3.....with L16 to check are all the
same, or (N-1) compares for N lists; but checking unsorted are
different is not so easy, testing (L1 is diff to L2) and L2 is diff to
L3 does nothing for L1 vs L3. I need to loop through all others to
test L1 diffs, then all L2 (except L1) then L3 (except L1 & L2), which
makes N*(N-1)/2 tests altogether! Not too bad for 16 lists, but I may
expand to 256, so ~ 32000 comapres to do. (Gulp!) Is there a quicker
method anyone can see?

Regards, Niv

Alexandre Ferrieux

unread,
Aug 19, 2010, 5:12:29 PM8/19/10
to

Well, sorting the master list is O(N*log(N)), which is better than the
dumb quadratic way as you noticed. So why insist on "unsorted" ?
-Alex

Gerald W. Lester

unread,
Aug 19, 2010, 5:30:06 PM8/19/10
to

Ok, assume masterList contains the names of your list. Assuming that you
want to check for order equality within the list (i.e. {1 2} != {2 1}) then,

set allDiff 1
foreach listName $masterList {
set list [set $listName]
if {[info exists seen($list)]} {
set allDiff 0
break
}
set seen($list) 1
}

if {$allDiff} {
puts {All different}
} else {
puts {Opps! Not all unique!!!}
}

Note, you are still doing a bunch of compares, but allowing [info exists] to
do it for you.

--
+------------------------------------------------------------------------+
| Gerald W. Lester, President, KNG Consulting LLC |

| Email: Gerald...@kng-consulting.net |
+------------------------------------------------------------------------+

Arjen Markus

unread,
Aug 20, 2010, 3:03:48 AM8/20/10
to
On 19 aug, 23:30, "Gerald W. Lester" <Gerald.Les...@KnG-
> | Email: Gerald.Les...@kng-consulting.net                                |
> +------------------------------------------------------------------------+- Tekst uit oorspronkelijk bericht niet weergeven -
>
> - Tekst uit oorspronkelijk bericht weergeven -

As arrays in Tcl are implemented as efficient hash arrays, the order
of
comparisons is N. Check if an individual array element exists may be a
bit more
costly than checking the individual list element, as you compare the
whole
string representation at once, but the other method is O(N ln N) ...

Regards,

Arjen

0 new messages