AW: mini-golf: first differing position

"Winter Christian" <[email protected]>
Newsgroups gmane.comp.lang.perl.fun
Message-ID <[email protected]>
No, this one also counts similarities after the first differing character.
$x = "abcfff"; $y = "abcggg";
correctly gives 3, but just try out
$x = "abcfff"; $y = "abcgff";
which gives you 5 because of the matching "f"s.

This way it should work:
$_=$x^$y; $n=s/[^\0]//?$-[0]:()

A little toyed around with kynn's idea:
for(split//,$x){$_eq substr($y,$n++,1)||last}$n x=$n<length$y

-Christian


> -----Ursprüngliche Nachricht-----
> Von: Terje Kristensen [mailto:[email protected]] 
> Gesendet: Mittwoch, 21. April 2004 07:49
> An: Fwp@Perl. Org
> Betreff: RE: mini-golf: first differing position
> 
> 
> This should work.
> 
> $_=$x^$y;$n=s/\0//g||undef
> 
> Terje
> 
> > -----Original Message-----
> > From: [email protected] [mailto:[email protected]]
> > Sent: Wednesday, April 21, 2004 2:02 AM
> > To: [email protected]
> > Cc: [email protected]
> > Subject: Re: mini-golf: first differing position
> > 
> > 
> > 
> > 
> >    X-Original-To: [email protected]
> >    Mailing-List: contact [email protected]; run by ezmlm
> >    X-Spam-Status: No, hits=0.3 required=7.0
> > 	   tests=NO_REAL_NAME
> >    X-Spam-Check-By: la.mx.develooper.com
> >    Date: Tue, 20 Apr 2004 19:57:27 -0400 (EDT)
> >    From: <[email protected]>
> > 
> > 
> > 
> > 
> >    We want to find out the first position $n at which 
> strings $x and $y
> >    differ.  If $x eq $y, then $n is undef.  If $x ne $y but $x is a
> >    prefix (aka initial segment) of $y, then $n should be 
> equal to length
> >    $x.  The operation must preserve both $x and $y (i.e. destructive
> >    operations are disallowed).
> > 
> >    By way of baseline, here's an utterly uninspired solution:
> > 
> >    $n=0;while(($c=substr($x,$n,1)||undef $n) && $c eq
> > substr($y,$n++,1)){;}
> > 
> > It's worse than uninspired, actually.  It's wrong.
> > 
> > kj
>
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.