Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

変なカレンダー生成プログラムの作成(レベル2) #5

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 68 additions & 3 deletions lib/sg_strange_calendar.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,74 @@
class SgStrangeCalendar
require "date"
class SgStrangeCalendar
WEEKDAY_HEADER = "Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo"

def initialize(year, today = nil)
# write your code here
@year = year
@today = today
@calendar = create_calendar_hash(year) # {"Jan" => {1 => "Mo", 2=>"Tu", ... }, "Feb"=>{1=>"Th", ... }, ...}
end

def generate(vertical: false)
# write your code here
result = [@year.to_s + " " + WEEKDAY_HEADER]
@calendar.each do |month, date|
result.push(create_month_line(month, date))
end
adjust_space(result.join("\n"))
end

def create_calendar_hash(year)
result = {}

Date.new(year, 1, 1).step(Date.new(year, 12, 31)).each do |date|
month = date.strftime("%b") # ex: Jan
weekday = date.strftime("%a")[0, 2] # ex: Mo

result[month] ||= {}
result[month][date.day] = weekday
end

result
end

def create_month_line(month, date)
result = [month.ljust(4)]
target_date = 1

WEEKDAY_HEADER.split(" ").each do |w|
target_weekday = date[target_date]
if target_weekday.nil?
break
end

if w == target_weekday
if isToday(month, target_date)
result.push("[" + target_date.to_s + "]")
else
result.push(target_date.to_s.rjust(2))
end
target_date += 1
else
result.push(" ")
end
end

result.join(" ")
end

def isToday(targe_month, target_date)
[email protected]? && @today.strftime("%Y") == @year.to_s && @today.strftime("%b") == targe_month && @today.strftime("%d") == target_date.to_s.rjust(2, "0")
end
end

def adjust_space(target)
if @today.nil?
return target
end

@today.strftime("%d").to_i < 10 ? target.gsub("] ", "]") : target.gsub(" [", "[").gsub("] ", "]")

end

today = Date.new(2024, 12, 31)
c = SgStrangeCalendar.new(2024, today)
c.generate
6 changes: 2 additions & 4 deletions test/sg_strange_calendar_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def test_level_1_for_2025
end

def test_level_2_for_2024_01_01
skip "レベル2にチャレンジする人はこの行を削除してください"
# skip "レベル2にチャレンジする人はこの行を削除してください"
expected = <<~TXT.chomp
2024 Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo
Jan [1] 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
Expand All @@ -66,7 +66,6 @@ def test_level_2_for_2024_01_01
end

def test_level_2_for_2024_12_09
skip "レベル2にチャレンジする人はこの行を削除してください"
expected = <<~TXT.chomp
2024 Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo
Jan 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
Expand All @@ -88,7 +87,6 @@ def test_level_2_for_2024_12_09
end

def test_level_2_for_2025_03_31
skip "レベル2にチャレンジする人はこの行を削除してください"
expected = <<~TXT.chomp
2025 Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo
Jan 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
Expand All @@ -110,7 +108,7 @@ def test_level_2_for_2025_03_31
end

def test_level_2_all
skip "レベル2にチャレンジする人はこの行を削除してください"
# skip "レベル2にチャレンジする人はこの行を削除してください"
file_path = File.expand_path('level2.txt', File.dirname(__FILE__))
calendars = File.read(file_path).lines.each_slice(13).map(&:join).map(&:chomp)
from_date = Date.new(2025, 1, 1)
Expand Down
Loading