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

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

Closed
wants to merge 3 commits into from
Closed
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
22 changes: 22 additions & 0 deletions .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// For format details, see https://aka.ms/devcontainer.json. For config options, see the
// README at: https://github.com/devcontainers/templates/tree/main/src/ruby
{
"name": "Ruby",
// Or use a Dockerfile or Docker Compose file. More info: https://containers.dev/guide/dockerfile
"image": "ruby:3.3.5-bullseye"

// Features to add to the dev container. More info: https://containers.dev/features.
// "features": {},

// Use 'forwardPorts' to make a list of ports inside the container available locally.
// "forwardPorts": [],

// Use 'postCreateCommand' to run commands after the container is created.
// "postCreateCommand": "ruby --version",

// Configure tool-specific properties.
// "customizations": {},

// Uncomment to connect as root instead. More info: https://aka.ms/dev-containers-non-root.
// "remoteUser": "root"
}
65 changes: 63 additions & 2 deletions lib/sg_strange_calendar.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,70 @@
require 'date'

class SgStrangeCalendar
def initialize(year, today = nil)
# write your code here
@year = year
@today = today
end

def generate(vertical: false)
# write your code here
builder = vertical ? VerticalBuilder : HorizontalBuilder
builder.new(@year, @today).build
end

class Builder
WDAYS = %w[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].freeze
MONTHS = %w[Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec].freeze
BLANK = ' '.freeze

private_constant :WDAYS, :MONTHS, :BLANK

def initialize(year, today)
@year = year
@today = today
end

def build
calendar = rows.map.with_index do |line, i|
[ths[i].ljust(4), *line].join(' ').gsub(/\s(\[\d+\])(?:\s|\z)/) { $1 }.rstrip
end
[header.join(' '), *calendar].join("\n")
end

private

def days_in(month)
begining_of_month, end_of_month = Date.new(@year, month, 1), Date.new(@year, month, -1)
array = [*begining_of_month..end_of_month].map do |date|
date == @today ? "[#{date.day}]".rjust(length + 2) : date.day.to_s.rjust(length)
end
forward_blanks = Array.new(begining_of_month.wday, BLANK.rjust(length))
array.unshift(*forward_blanks)
array.fill(BLANK.rjust(length), array.size...WDAYS.size)
end

def header = raise NotImplementedError
def ths = raise NotImplementedError
def length = raise NotImplementedError
def rows = raise NotImplementedError
end

class HorizontalBuilder < Builder
private

def header = @header ||= [@year.to_s, *WDAYS.dup]
def ths = @ths ||= MONTHS.dup
def length = 2
def rows = @rows ||= [*1..12].map(&method(:days_in))
end

class VerticalBuilder < Builder
private

def header = @header ||= [@year.to_s, *MONTHS.dup]
def ths = @ths ||= WDAYS.dup
def length = 3
def rows = @rows ||= [*1..12].map(&method(:days_in)).transpose
end

private_constant :Builder, :HorizontalBuilder, :VerticalBuilder
end
9 changes: 0 additions & 9 deletions test/sg_strange_calendar_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ def test_level_1_for_2025
end

def test_level_2_for_2024_01_01
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 +65,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 +86,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 +107,6 @@ def test_level_2_for_2025_03_31
end

def test_level_2_all
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 All @@ -123,7 +119,6 @@ def test_level_2_all
end

def test_level_3_for_2024
skip "レベル2およびレベル3にチャレンジする人はこの行を削除してください"
expected = <<~TXT.chomp
2024 Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
Su 1 1
Expand Down Expand Up @@ -169,7 +164,6 @@ def test_level_3_for_2024
end

def test_level_3_for_2024_01_01
skip "レベル2およびレベル3にチャレンジする人はこの行を削除してください"
expected = <<~TXT.chomp
2024 Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
Su 1 1
Expand Down Expand Up @@ -216,7 +210,6 @@ def test_level_3_for_2024_01_01
end

def test_level_3_for_2024_12_09
skip "レベル2およびレベル3にチャレンジする人はこの行を削除してください"
expected = <<~TXT.chomp
2024 Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
Su 1 1
Expand Down Expand Up @@ -263,7 +256,6 @@ def test_level_3_for_2024_12_09
end

def test_level_3_for_2025_03_31
skip "レベル2およびレベル3にチャレンジする人はこの行を削除してください"
expected = <<~TXT.chomp
2025 Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
Su 1
Expand Down Expand Up @@ -310,7 +302,6 @@ def test_level_3_for_2025_03_31
end

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