Doubt when returning the amount of different letters

Asked

Viewed 49 times

-1

I would like to return the amount of different letters between the two strings, in this case the different letters are "TACG" and "ARGC", soon would have to return 4.

ruby def letras(string_1, string_2)
  contador = 0
  string_1 = string_1.split("")
  string_2 = string_2.split("")
  for letra in string_1
    for letra_1 in string_2
       if letra != letra_1
          contador += 1
       end
    end
  end
  return contador
end
puts letras 'GGTACGCAB', 'GGARGCAB'

1 answer

0

If you wanted to see if it is the same letter at each position (index) of the String:

def letras(string_1, string2)
  contador = 0
  string_1.split.each_with_index do |letra, index|
    counter += 1 if letra != string_2.split[index]
  end
  return contador
end

Browser other questions tagged

You are not signed in. Login or sign up in order to post.