Roman Lishtaba
BAN USER
Comments (4)
Follows (1)
Reputation 0
Page:
1
Comment hidden because of low score. Click to expand.
Comment hidden because of low score. Click to expand.
0
of 0 vote
You can do it easily with pattern matching. Here is example using Ruby:
"aaabbaacccd".gsub(/([[:alpha:]])\1*/) do |group|
"#{group.size}#{group[0]}"
end
// this will result in "3a2b2a3c1d"
Comment hidden because of low score. Click to expand.
0
of 0 vote
restricted = [2,3,5,9,199,200,344].freeze
def generate(restricted)
number = rand(1000)
number += 4 if number < 1 || restricted.include?(number)
number
end
Comment hidden because of low score. Click to expand.
0
of 0 vote
Is this question regarding Levenshtein algorithm? Here is an example:
def levenshtein(a, b)
case
when a.empty? then b.length
when b.empty? then a.length
else
[(a[0] == b[0] ? 0 : 1) + levenshtein(a[1..-1], b[1..-1]),
1 + levenshtein(a[1..-1], b),
1 + levenshtein(a, b[1..-1])].min
end
end
Page:
1
CareerCup is the world's biggest and best source for software engineering interview preparation. See all our resources.
Here is example using Ruby:
I haven't compressed "aa" to "a2" since it does not save any space and task does not require to do it.
- Roman Lishtaba January 18, 2016