Created
July 29, 2024 13:45
-
-
Save lain0/68b8d5329bb93aacfc62eea15a1d2080 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'rspec' | |
require 'date' | |
# Дано: день пребывания в отеле в рабочий день стоит 1000р, а в выходной 1500р. | |
# Задача: написать код который посчитает стоимость за пребывание в течении X дней(не включительно) с указанной даты. | |
# Можно пользоваться любой документацией, тут главное сам процесс разработки, | |
# и просто рабочий результат (не нужно перфекционизма) | |
# | |
# Можно использовать свой редактор, либо онлайн https://onecompiler.com/ruby | |
# # Friday 05.02.2021 = 1000 + 3000 | |
# puts calc(Date.new(2021, 2, 5), 3) | |
# # Monday 3000 | |
# puts calc(Date.new(2021, 2, 1), 3) | |
# # Friday 05.02.2021 = 1000 + 3000 + 2000 | |
# puts calc(Date.new(2021, 2, 5), 5) | |
module Calc | |
module_function | |
PRICE = 1_000 | |
WD_PRICE = 1_500 | |
def calc(date, days) | |
price = 0 | |
(date...date + days).map do |day| | |
week_day?(day) ? price += WD_PRICE : price += PRICE | |
end | |
price | |
end | |
def week_day?(day) | |
day.sunday? || day.saturday? | |
end | |
end | |
RSpec.describe Calc do | |
it { expect(Calc.calc(Date.new(2021, 2, 5), 3)).to eq 4000 } | |
it { expect(Calc.calc(Date.new(2021, 2, 1), 5)).to eq 5000 } | |
it { expect(Calc.calc(Date.new(2021, 2, 5), 5)).to eq 6000 } | |
end |
Author
lain0
commented
Jul 29, 2024
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment