simple-exercises/ruby-exercises/easy-6.rb
# Write a method, #build_power_plants, that will take in two arguments. The
# first of which is the number of cities that need to be powered. The cities
# are arranged in a line and are equidistant from eachother.
#   The second argument will be the range of each powerplant (the number of cities
# in each direction a plant can reach and power). So, a range of 1 means that each
# plant can only reach the cities on either side of it. A range of 0 means that the
# plants can only power the city they are built in.
#
#   Your method should dictate which cities' powerplants need to be built such
# that all of the cities have power, and the least number of cities have powerplants
# as possible.
#
#   Output an array of the cities, with 0 representing a city without a powerplant
# and 1 representing a city with one.
#
# For example:
# build_power_plants(3, 2) means that there are three cities, and each powerplant
# will have a range of two cities. Therefore your method should output the array
# { 0, 1, 0 ]
Write a method, #build_power_plants, that will take in two arguments. The
first of which is the number of cities that need to be powered. The cities
are arranged in a line and are equidistant from eachother.
The second argument will be the range of each powerplant (the number of cities
in each direction a plant can reach and power). So, a range of 1 means that each
plant can only reach the cities on either side of it. A range of 0 means that the
plants can only power the city they are built in.

Your method should dictate which cities' powerplants need to be built such
that all of the cities have power, and the least number of cities have powerplants
as possible.

Output an array of the cities, with 0 representing a city without a powerplant
and 1 representing a city with one.

For example:
build_power_plants(3, 2) means that there are three cities, and each powerplant
will have a range of two cities. Therefore your method should output the array
{ 0, 1, 0 ]
Write a method #build_power_plants that will take in two arguments . The first of which is the number of cities that need to be powered . The cities are arranged in a line and are equidistant from eachother . The second argument will be the range of each powerplant ( the number of cities in each direction a plant can reach and power ) . So a range of 1 means that each plant can only reach the cities on either side of it . A range of 0 means that the plants can only power the city they are built in . Your method should dictate which cities ' powerplants need to be built such that all of the cities have power and the least number of cities have powerplants as possible . Output an array of the cities with 0 representing a city without a powerplant and 1 representing a city with one . For example : build_power_plants ( 3 2 ) means that there are three cities and each powerplant will have a range of two cities . Therefore your method should output the array { 0 1 0 ]
def build_power_plants(number_of_cities, plant_range)
  if number_of_cities == 0
    return []
  end
  if plant_range == 0
    return Array.new(number_of_cities, 1)
  end
  results = Array.new(number_of_cities, 0)
  diameter = 2 * plant_range + 1
  row = 0
  while true
    first_index = row * diameter
    if first_index >= number_of_cities
      return results
    end
    current_plant = first_index + plant_range
    if current_plant >= number_of_cities
      results[-1] = 1
      return results
    end
    results[current_plant] = 1
    row += 1 
  end
end
def build_power_plants ( number_of_cities , plant_range ) if number_of_cities == 0 return [ ] end if plant_range == 0 return Array . new ( number_of_cities , 1 ) end results = Array . new ( number_of_cities , 0 ) diameter = 2 * plant_range + 1 row = 0 while true first_index = row * diameter if first_index >= number_of_cities return results end current_plant = first_index + plant_range if current_plant >= number_of_cities results [ - 1 ] = 1 return results end results [ current_plant ] = 1 row += 1 end end
Write a method, #build_power_plants, that will take in two arguments.
Write a method #build_power_plants that will take in two arguments .
# [1, 1, 1...] # plant_range = 3, # cities = 17 # diameter = 2 * plant_range + 1 #  0,  1,  2,  3,  4,  5,  6   => first_index   = 0 * 7 #              1               => current_plant = 0 + 3 = 3 #  7,  8,  9, 10, 11, 12, 13   => first_index   = 1 * 7 #             1                => current_plant = 7 + 3 = 10 # 14, 15, 16                   => first_index   = 2 * 7 = 14 #         1                    => current_plant = 17 >= #cities # so current_plant = #cities - 1 = 16(last index) # row doesnt exist # plant spot doesn't exist # put plant on the last city instead # next row
number_of_cities
<not_specific>
plant_range
<not_specific>