[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Succ distance between two strings

T. Onoma

11/6/2004 1:34:00 PM


Is there an efficient way to calculate the "succ distance" between two strings
(without iterating over each succession)? Examples:

'a'..'a' 0
'a'..'b' 1
'a'..'z' 25
'a'..'aa' 26

T.


7 Answers

dblack

11/6/2004 2:08:00 PM

0

Curne) Simon Conrad-Armes

11/6/2004 2:23:00 PM

0


On Nov 6, 2004, at 3:07 PM, David A. Black wrote:

> Hi --
>
> On Sat, 6 Nov 2004, trans. (T. Onoma) wrote:
>
>>
>> Is there an efficient way to calculate the "succ distance" between
>> two strings
>> (without iterating over each succession)? Examples:
>>
>> 'a'..'a' 0
>> 'a'..'b' 1
>> 'a'..'z' 25
>> 'a'..'aa' 26
>
> 'a'...'aa'.to_a.size # would be one way

That still iterates over all elements but it looks sweet :)
Only for large strings would be a problem


My stab:

def diff_succ s1, s2
t1 = t2 = 0
s1.each_byte{|c| t1 = t1*26 + c - 96}
s2.each_byte{|c| t2 = t2*26 + c - 96}
t2 - t1
end

def t s1, s2
puts "#{s1}..#{s2} => #{diff_succ s1, s2}"
end

t 'a', 'a' #0
t 'a', 'b' #1
t 'a', 'z' #25
t 'a', 'aa' #26

t 'A', 'B'
t 'A', 'Z'
t 'A', 'AA'


output:
a..a => 0
a..b => 1
a..z => 25
a..aa => 26
A..B => 1
A..Z => 25
A..AA => -806


Notice how it breaks on multi-letter uppercase strings? that is because
of the -96. Maybe one should just lowercase the args...

/Curne
--
Mailing lists confuse me. Often I don't recognize my own posts and set
about writing a flaming reply where I violently disagree.
[curne@curnomatic.dk]



Chad Fowler

11/6/2004 2:25:00 PM

0

On Sat, 6 Nov 2004 23:07:59 +0900, David A. Black <dblack@wobblini.net> wrote:
> Hi --
>
>
>
> On Sat, 6 Nov 2004, trans. (T. Onoma) wrote:
>
> >
> > Is there an efficient way to calculate the "succ distance" between two strings
> > (without iterating over each succession)? Examples:
> >
> > 'a'..'a' 0
> > 'a'..'b' 1
> > 'a'..'z' 25
> > 'a'..'aa' 26
>
> 'a'...'aa'.to_a.size # would be one way

For the first 3 examples you can also treat them as characters and do:

?a - ?a #=> 0
?b - ?a #=> 1
?z - ?a #=> 25

Not exactly what you want though.

Chad Fowler
http://chad...
http://rubyc...
http://ruby...
http://rubygems.rub... (over 20,000 gems served!)


gabriele renzi

11/6/2004 2:36:00 PM

0

trans. (T. Onoma) ha scritto:

> Is there an efficient way to calculate the "succ distance" between two strings
> (without iterating over each succession)? Examples:
>
> 'a'..'a' 0
> 'a'..'b' 1
> 'a'..'z' 25
> 'a'..'aa' 26
>
> T.
>
>

my tentativ impl would be
class String
def value
i=-26
split(//).inject(0){|x,y|
i+=26
x+y[0]-?a+i
}
end
end

class Range
def dist
max.value - min.value
end
end
p 'aa'.value - 'a'.value
p 'a'.value - 'a'.value
p ('a'..'b').dist
p ('a'..'z').dist

note that '-?a' could be moved in the 'i' initialization but I'm running
away :)

T. Onoma

11/6/2004 9:28:00 PM

0

Thanks all for the responses. Looks like it _might_ be doable, but the
complexity quickly racks up. The presented quick stabs at it fail on capital
letters and other characters.

Running away might be the best course of action ;)

T.

On Saturday 06 November 2004 09:38 am, gabriele renzi wrote:
| trans. (T. Onoma) ha scritto:
| > Is there an efficient way to calculate the "succ distance" between two
| > strings (without iterating over each succession)? Examples:
| >
| > 'a'..'a' 0
| > 'a'..'b' 1
| > 'a'..'z' 25
| > 'a'..'aa' 26
| >
| > T.
|
| my tentativ impl would be
| class String
| def value
| i=-26
| split(//).inject(0){|x,y|
| i+=26
| x+y[0]-?a+i
| }
| end
| end
|
| class Range
| def dist
| max.value - min.value
| end
| end
| p 'aa'.value - 'a'.value
| p 'a'.value - 'a'.value
| p ('a'..'b').dist
| p ('a'..'z').dist
|
| note that '-?a' could be moved in the 'i' initialization but I'm running
| away :)

--
( o _ ���
// trans.
/ \ transami@runbox.com

I don't give a damn for a man that can only spell a word one way.
-Mark Twain

[8,16,20,29,78,65,2,14,26,12,12,28,71,114,12,13,12,82,72,21,17,4,10,2,95].
each_with_index{|x,i| $><<(x^'Begin landing your troops'[i]).chr}
-Tadayoshi Funaba



Brian Schröder

11/8/2004 12:51:00 PM

0

On Sat, 6 Nov 2004 23:07:59 +0900
"David A. Black" <dblack@wobblini.net> wrote:

> Hi --
>
> On Sat, 6 Nov 2004, trans. (T. Onoma) wrote:
>
> >
> > Is there an efficient way to calculate the "succ distance" between two strings
> > (without iterating over each succession)? Examples:
> >
> > 'a'..'a' 0
> > 'a'..'b' 1
> > 'a'..'z' 25
> > 'a'..'aa' 26
>
> 'a'...'aa'.to_a.size # would be one way


That does not work, as the ... have a very low binding. It seems a common error I made often.

bschroed@black:~ $ irb
irb(main):001:0> 'a'...'aa'.to_a.size
=> "a"...1
irb(main):002:0> ('a'...'aa').to_a.size
=> 26

Regards,

Brian


--
Brian Schröder
http://www.brian-sch...



dblack

11/8/2004 1:32:00 PM

0