Succ distance between two strings

  • Thread starter trans. (T. Onoma)
  • Start date
T

trans. (T. Onoma)

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.
 
D

David A. Black

Hi --

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


David
 
C

(Curne) Simon Conrad-Armes

Hi --



'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
 
G

gabriele renzi

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

trans. (T. Onoma)

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.

| > 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.
/ \ (e-mail address removed)

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').chr}
-Tadayoshi Funaba
 
B

Brian Schröder

Hi --



'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
 
D

David A. Black

Hi --

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top