DEV Community

Discussion on: Daily Challenge #173 - Pandemia

Collapse
nijeesh4all profile image
Nijeesh Joshy • Edited on

RUBY

#TESTS
require 'minitest'

class IsDivisibleTest < MiniTest::Test
  def test_calculate_infected_percentage
    assert_equal  100 * 11.00 / 15.0 , calculate_infected_percentage('01000000X000X011X0X')
    assert_equal 100 * 8.00 / 11, calculate_infected_percentage('01X000X010X011XX')
    assert_equal 100.0, calculate_infected_percentage('000001XXXX0010X1X00010')
    assert_equal 100 * 6.00 / 14, calculate_infected_percentage('X00X000000X10X0100')
  end

  def test_no_peoples
    assert_equal 0, calculate_infected_percentage('XXXXX')
  end

  def test_none_infected
    assert_equal 0, calculate_infected_percentage('00000000X00X0000')
  end

  def no_islands
    assert_equal 100.0, calculate_infected_percentage('0000000010')
  end
end

#============CODE==================

def calculate_infected_percentage(world='')
  return 0 if !world.include?('0') || !world.include?('1')
  continents = world.split('X')
  total_population = 0
  infected = 0
  continents.each do  |continent|
    total_population += continent.length
    infected+= continent.length if continent.include?('1')
  end

  (infected * 100.00) / total_population
end


> MiniTest.run

Run options: --seed 34491

# Running:

...

Finished in 0.002091s, 1434.6125 runs/s, 3347.4292 assertions/s.

4 runs, 7 assertions, 0 failures, 0 errors, 0 skips
=> true