Created
November 20, 2024 20:06
-
-
Save erick8911/a64448669f2c393d443db711f692a25e to your computer and use it in GitHub Desktop.
Signage Gist
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
class Day | |
attr_accessor :day, :start_at, :end_at | |
def initialize(day, start_at, end_at) | |
@day = day | |
@start_at = start_at | |
@end_at = end_at | |
end | |
def same_time?(other_day) | |
return false if other_day.nil? | |
start_at == other_day.start_at && end_at == other_day.end_at | |
end | |
end | |
class Signage | |
attr_accessor :list | |
def initialize | |
@list = [] | |
end | |
def duplicated_days(today, tomorrow, index, name_days) | |
if today && tomorrow && today.same_time?(tomorrow) | |
name_days = duplicated_days(tomorrow, list[index + 2], index + 3, name_days << tomorrow.day) | |
end | |
return name_days | |
end | |
def pretty_print | |
printed_list = [] | |
looped_days = [] | |
list.each.with_index do |today, index| | |
tomorrow = list[index + 1] if index < list.length - 1 | |
name_days = duplicated_days(today, tomorrow, index, [today.day]) | |
if looped_days.flatten.include?(today.day) | |
next | |
end | |
looped_days << name_days | |
if name_days.length == 1 | |
printed_list << "#{today.day}: #{today.start_at} - #{today.end_at}" | |
elsif name_days.length == 2 | |
days = name_days.join(", ") | |
printed_list << "#{days}: #{today.start_at} - #{today.end_at}" | |
elsif name_days.length > 2 | |
days = "#{name_days.first} - #{name_days.last}" | |
printed_list << "#{days}: #{today.start_at} - #{today.end_at}" | |
end | |
end | |
printed_list | |
end | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment