DEV Community

Discussion on: Daily Challenge #173 - Pandemia

Collapse
 
cipharius profile image
Valts Liepiņš

Ruby with heavy use of regexp and functional paradigm:

def infectionPercentage str
    total = str.scan(/\d/).length
    return 0 if total == 0
    infected = str.gsub(/\d*1\d*/) { |x| x.split("").map {1}.join }.scan(/1/).length
    "#{infected / total.to_f * 100}%"
end