-1
a = 'rrgb'
b = 'rggb'

I have these two strings. I am trying to count the correct amount of duplicates. The result should be three, b has 3 of the correct letters. But everything I find online returns 4 since technically they're the same letters. What is the best way to do this?

  • 1
    Pretty unclear what you mean with "duplicates" and "correct" and how you're counting. – Stefan Pochmann Feb 24 '18 at 22:45
  • 3
    I agree, the question is very ambiguously worded. If `a = "xyz"` and `b = "zyx"`, then should your result be `1` or `3`? If `a = "XYZ"` and `b = "xyz"`, then should your result be `0` or `3`? If `a = "x y z"` and `b = "x y z"`, then should your result be `3` or `5`? If `a = "xxy"` and `b = "xxy"`, then should your result be `2` or `3`? You need to be more specific than *"count the correct amount of duplicates ... of the correct letters"*, because the answers below have taken different interpretations of this vague sentence and will yield different results. – Tom Lord Feb 25 '18 at 01:02
  • 1
    When you post a question it's a good idea to check back from time-to-time in case--as here--readers ask for clarification. – Cary Swoveland Feb 25 '18 at 02:13

1 Answers1

4
a = 'rrgb'
b = 'rggb'

p a.chars.zip(b.chars).count{|c1, c2| c1 == c2} # => 3
steenslag
  • 79,051
  • 16
  • 138
  • 171