# Succ distance between two strings

**URL:** <https://rubytalk.org/t/succ-distance-between-two-strings/14648>\
**Category:** ruby-talk\
**Created:** [6 November 2004 13:34 UTC](https://rubytalk.org/t/succ-distance-between-two-strings/14648 "2004-11-06T13:34:07Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![T\_Onoma](https://avatars.discourse-cdn.com/v4/letter/t/c2a13f/32.png) [@T\_Onoma](https://rubytalk.org/u/T_Onoma)\
**Post date:** [6 November 2004 13:34 UTC](https://rubytalk.org/t/succ-distance-between-two-strings/14648/1 "2004-11-06T13:34:07Z")

</div>

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

&nbsp;&nbsp;'a'..'a' 0  
&nbsp;&nbsp;'a'..'b' 1  
&nbsp;&nbsp;'a'..'z' 25  
&nbsp;&nbsp;'a'..'aa' 26

T.

---

<div class="post-metadata">

**Author:** ![David\_A\_Black3](https://avatars.discourse-cdn.com/v4/letter/d/6a8cbe/32.png) [@David\_A\_Black3](https://rubytalk.org/u/David_A_Black3)\
**Post date:** [6 November 2004 14:07 UTC](https://rubytalk.org/t/succ-distance-between-two-strings/14648/2 "2004-11-06T14:07:59Z")

</div>

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:
> > 
> > &nbsp;&nbsp;'a'..'a' 0  
> > &nbsp;&nbsp;'a'..'b' 1  
> > &nbsp;&nbsp;'a'..'z' 25  
> > &nbsp;&nbsp;'a'..'aa' 26
> 
> 'a'...'aa'.to\_a.size # would be one way
> 
> David
> 
> --  
> David A. Black  
> dblack@wobblini.net

---

<div class="post-metadata">

**Author:** ![gabriele\_renzi](https://avatars.discourse-cdn.com/v4/letter/g/e19b73/32.png) [@gabriele\_renzi](https://rubytalk.org/u/gabriele_renzi)\
**Post date:** [6 November 2004 14:38 UTC](https://rubytalk.org/t/succ-distance-between-two-strings/14648/3 "2004-11-06T14:38:40Z")

</div>

trans. (T. Onoma) ha scritto:

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

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

class Range  
&nbsp;&nbsp;def dist  
&nbsp;&nbsp;&nbsp;max.value - min.value  
&nbsp;&nbsp;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 🙂

---

<div class="post-metadata">

**Author:** ![Simon\_Conrad-Armes](https://avatars.discourse-cdn.com/v4/letter/s/3da27b/32.png) [@Simon\_Conrad-Armes](https://rubytalk.org/u/Simon_Conrad-Armes)\
**Post date:** [6 November 2004 14:23 UTC](https://rubytalk.org/t/succ-distance-between-two-strings/14648/4 "2004-11-06T14:23:11Z")

</div>

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  
&nbsp;&nbsp;&nbsp;t1 = t2 = 0  
&nbsp;&nbsp;&nbsp;s1.each\_byte{|c| t1 = t1\*26 + c - 96}  
&nbsp;&nbsp;&nbsp;s2.each\_byte{|c| t2 = t2\*26 + c - 96}  
&nbsp;&nbsp;&nbsp;t2 - t1  
end

def t s1, s2  
&nbsp;&nbsp;&nbsp;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

> **···**
>
> 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:
> > > 
> > > &nbsp;&nbsp;'a'..'a' 0  
> > > &nbsp;&nbsp;'a'..'b' 1  
> > > &nbsp;&nbsp;'a'..'z' 25  
> > > &nbsp;&nbsp;'a'..'aa' 26
> > 
> > 'a'...'aa'.to\_a.size # would be one way
> 
> --  
> 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]

---

<div class="post-metadata">

**Author:** ![Chad\_Fowler4](https://avatars.discourse-cdn.com/v4/letter/c/9fc29f/32.png) [@Chad\_Fowler4](https://rubytalk.org/u/Chad_Fowler4)\
**Post date:** [6 November 2004 14:25 UTC](https://rubytalk.org/t/succ-distance-between-two-strings/14648/5 "2004-11-06T14:25:14Z")

</div>

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://chadfowler.com](http://chadfowler.com)

> **[Ruby Central](https://rubycentral.org)**
>
> Supporting the Ruby Community Since 2001

> **[WebMD - Better information. Better health.](https://www.rubygarden.org/)**
>
> The leading source for trustworthy and timely health and medical news and information. Providing credible health information, supportive community, and educational services by blending award-winning expertise in content, community services, expert...

[http://rubygems.rubyforge.org](http://rubygems.rubyforge.org) (over 20,000 gems served!)

> **···**
>
> 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

---

<div class="post-metadata">

**Author:** ![T\_Onoma](https://avatars.discourse-cdn.com/v4/letter/t/c2a13f/32.png) [@T\_Onoma](https://rubytalk.org/u/T_Onoma)\
**Post date:** [6 November 2004 21:27 UTC](https://rubytalk.org/t/succ-distance-between-two-strings/14648/6 "2004-11-06T21:27:45Z")

</div>

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  
> > &nbsp;&nbsp;def value  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;i=-26  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;split(//).inject(0){|x,y|  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;i+=26  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;x+y[0]-?a+i  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}  
> > &nbsp;&nbsp;end  
> > end
> > 
> > class Range  
> > &nbsp;&nbsp;def dist  
> > &nbsp;&nbsp;&nbsp;max.value - min.value  
> > &nbsp;&nbsp;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

---

<div class="post-metadata">

**Author:** ![Brian\_SchrA\_der](https://avatars.discourse-cdn.com/v4/letter/b/c67d28/32.png) [@Brian\_SchrA\_der](https://rubytalk.org/u/Brian_SchrA_der)\
**Post date:** [8 November 2004 12:50 UTC](https://rubytalk.org/t/succ-distance-between-two-strings/14648/7 "2004-11-08T12:50:42Z")

</div>

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

> **···**
>
> 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
> 
> --  
> Brian Schröder  
> [http://www.brian-schroeder.de/](http://www.brian-schroeder.de/)

---

<div class="post-metadata">

**Author:** ![David\_A\_Black3](https://avatars.discourse-cdn.com/v4/letter/d/6a8cbe/32.png) [@David\_A\_Black3](https://rubytalk.org/u/David_A_Black3)\
**Post date:** [8 November 2004 13:31 UTC](https://rubytalk.org/t/succ-distance-between-two-strings/14648/8 "2004-11-08T13:31:48Z")

</div>

Hi --

> **···**
>
> On Mon, 8 Nov 2004, Brian [ISO-8859-15] Schröder wrote:
> 
> > 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
> 
> Whoops -- I did actually do it correctly in irb, to make sure, and  
> then wrote it out wrongly instead of pasting it.
> 
> It's like putting (i.e., putt-ing, in golf) -- you can never take it  
> for granted 🙂
> 
> David
> 
> --  
> David A. Black  
> dblack@wobblini.net
