How do I write a method which takes in a string and returns an array of
pairs each pair contains the next distinct letter in the string, and the
number consecutive repeats?
Thank you
How do I write a method which takes in a string and returns an array of
pairs each pair contains the next distinct letter in the string, and the
number consecutive repeats?
Thank you
Sounds like fun, I’ll have a go since I’ve never done something like
that before…
Here’s one way to do it. If this is homework then you’d better be able
to explain to your teacher how it works; so I recommend doing some
studying of the objects and methods involved: “String#scan”, “Regexp”,
“Capturing Groups”, “Enumerable#map”, “Array”, and “String#length”
‘moossse’.scan(/(\w)(\1*)/).map{|a,b|[a,b.length+1]}
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.
Sponsor our Newsletter | Privacy Policy | Terms of Service | Remote Ruby Jobs