# How can I do this

Hello,

I have a exercise where I must count differences between two strings.

Now I can do

for 0 … string1.length

But is there a better mor rubisch way to do this ?

Roelof

In general I would prefer using #each over ‘for’, like

(0…string1.length).each {…}

If you only want to produce e.g. a single number out of a string
comparison maybe #reduce/#inject would fit.

(0…string1.length).reduce(…) {…}

Regards, Paul

Am 29.05.2014 11:52, schrieb Roelof W.:

Paul G?tze schreef op 29-5-2014 12:11:

Thanks for the tip.
I will read the documentation and try to figure out how reduce can help
me.

Roelof

Roelof W. wrote in post #1147527:

I have a exercise where I must count differences between two strings.

Now I can do

for 0 … string1.length

for 0 … string1.length

otherwise the last index used will point after the last character.

But is there a better mor rubisch way to do this ?

Depends on what you want to do.

Kind regards

robert

use zip:

#shows which are different
“stringa”.chars.zip(“stringb”.chars).map{|a,b| a != b}
#=> [false, false, false, false, false, false, true]

#count the different
“stringa”.chars.zip(“stringb”.chars).count{|a,b| a != b}
#=> 1

or something
#group and count the same chars
“stringa1”.chars.zip(“stringb1”.chars).chunk{|a,b| a == b}.map {|k,v|
[k,v.size]}
#=> [[true, 6], [false, 1], [true, 1]]

#return the difference marked with a “/”
“stringa1”.chars.zip(“stringb1”.chars).chunk(&:uniq).map(&:first).chunk(&:size).map{|a|a.last.join(a.first
!= 1 ? “/” : “”)}
#=> [“string”, “a/b”, “1”]

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA512

On 29/05/14 11:52, Roelof W. wrote:

Hello,

I have a exercise where I must count differences between two
strings.

Now I can do

for 0 … string1.length

But is there a better mor rubisch way to do this ?

irb:0> s=“foo”
irb:0> s.chars.each {|c| p c}
“f”
“o”
“o”
=> [“f”, “o”, “o”]
irb:0> s.length.times {|i| p i}
0
1
2
=> 3
irb:0> s.chars.zip(t.chars)
=> [[“f”, “b”], [“o”, “a”], [“o”, “r”]]

All the best, Sandor Sz?cs
-----BEGIN PGP SIGNATURE-----
Version: GnuPG/MacGPG2 v2.0.22 (Darwin)
Comment: GPGTools - http://gpgtools.org
Comment: Using GnuPG with Thunderbird - http://www.enigmail.net/

iQIcBAEBCgAGBQJTh5t2AAoJEIiuFRwovs5reSgP/jjdiealotNrfQRu9LD/MKWD
QhgfQHLYQfXBk+ro5bao6VuriuHZ3Rm2HKv7zv7Jdzm+sKr40C2jRcn3r624MA7x
S0OzP+Mbxl5jJxTY2WZ420wA3ipyfmK+qElJVSsJwjCT0aYeLJVMOksR+44x2Xuy
ft2lRqdwv5YOL4bcRn147eEhnuUgS3wQ1NB+zLbFzRW1XfBucMN4omF1LcDPbgY3
4YPdinfKgs9Ql6tJXTfnckFimtbOZ0y9ySkf72bS9A9DKxn7f7f8i190T7GedDn0
3VYGdHr0jgmwTuNnIzOXOCDf8wV1tNLJhInggswuhpPKihfazX2QkME3m7xri/eS
aK1Qtlf/XAwqZh2mza9gs7cgkMf8iqGNqG9PqZnDCH/dPpqJIiLPmN12+xIfAbY4
lbRvOTB8byHRjQUMHvV8NVwkAszBtd94wvYbH3Fv0fRDDj5xvIMPiziINn8gCNGS
ikaez0g7GJ8wKAnbmhho6J+vdYdV7vKn/8x2VMNkSAYXkK4vSMHM5UUfoSa0V4Xt
tsYP8WZe8aL4ziisoKZUiDAzkXOoSHZpnMHx+nLr/FNirb2NFLwe/SjxEhSAwy+u
DR1QdYIb2tgXrRkAYWqz/vB6ar33u2L8ZkrJmAksrxlST4uSAGFrFDlFfe3ctiCN
obsMJ17U0H469QGFu6W7
=5sDK
-----END PGP SIGNATURE-----

You might consider one of the routines which computes a distance between
two strings such as the following:

#===================================================

# LEVENSHTEIN DISTANCE

#==================================================
def dameraulevenshtein(seq1, seq2)
oneago = nil
thisrow = (1…seq2.size).to_a + [0]
seq1.size.times do |x|
twoago, oneago, thisrow = oneago, thisrow, [0] * seq2.size + [x

• 1]
seq2.size.times do |y|
delcost = oneago[y] + 1
addcost = thisrow[y - 1] + 1
subcost = oneago[y - 1] + ((seq1[x] != seq2[y]) ? 1 : 0)