monkfan has asked for the wisdom of the Perl Monks concerning the following question:
#!/usr/bin/perl -w # The original code here is constructed # thanks to ysth's suggestion use strict; my @AR = qw(a b c); # First Case my @ar = qw(z a b c); # Second Case my @ar2 = qw(b a); my @ar3 = qw(a b); my @ar4 = qw(a b c); my @ar5 = qw(z a b c); print "First case\n"; # L R print test_ordered(\@ar,\@ar2),"\n"; # Answer:False(0) print test_ordered(\@ar,\@ar3),"\n"; # Answer:True(1) print test_ordered(\@ar,\@ar4),"\n"; # Answer:True(1) print test_ordered(\@ar,\@ar5),"\n"; # Answer:True(1) print "Second case\n"; print test_ordered(\@AR,\@ar2),"\n"; # Answer: False(0) print test_ordered(\@AR,\@ar3),"\n"; # Answer: True(1) print test_ordered(\@AR,\@ar4),"\n"; # Answer: True(1) sub test_ordered { #test if two arrays are ordered in same way #assuming no duplicate and left array is always #be greater/superset or equal to the right one # L R my ($ar1,$ar2)= @_; my %h; @h{@$ar1} = (0..$#{$ar1}); my $decision = grep($h{$ar2[$_]} != $_, 0..$#{$ar2}) ? 0 : 1; return $decision; }
The desired answer is:First Case: Second Case: 0 0 0 1 0 1 1
First Case: Second Case: 0 0 1 1 1 1 1
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Testing if Two Arrays are Ordered in a Same Way
by ikegami (Patriarch) on May 25, 2005 at 04:52 UTC | |
|
Re: Testing if Two Arrays are Ordered in a Same Way
by BrowserUk (Patriarch) on May 25, 2005 at 04:42 UTC | |
|
Re: Testing if Two Arrays are Ordered in a Same Way
by tlm (Prior) on May 25, 2005 at 04:50 UTC | |
|
Re: Testing if Two Arrays are Ordered in a Same Way
by johnnywang (Priest) on May 25, 2005 at 05:29 UTC |