From 437f53b3f94b9833bf1a8c0cf0bdefcae0599acc Mon Sep 17 00:00:00 2001 From: rani-i3 Date: Tue, 29 Aug 2023 14:22:07 +0900 Subject: [PATCH 01/10] added calendar code --- 02.calendar/calendar.rb | 46 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 02.calendar/calendar.rb diff --git a/02.calendar/calendar.rb b/02.calendar/calendar.rb new file mode 100644 index 0000000000..d7cdcc7be6 --- /dev/null +++ b/02.calendar/calendar.rb @@ -0,0 +1,46 @@ +require 'date' +require 'optparse' + +# コマンドラインでparseする +options = {} +OptionParser.new do |opts| + opts.banner = "Usage: ruby calendar.rb [options]" + + opts.on("-m", "--month MONTH", Integer, "Specify the month (1-12)") do |month| + options[:month] = month + end + + opts.on("-y", "--year YEAR", Integer, "Specify the year") do |year| + options[:year] = year + end +end.parse! + +# 今の日付を取る +current_date = Date.today +year = options[:year] || current_date.year +month = options[:month] || current_date.month + +# date enumeratorを作る +date_range = Date.new(year, month, 1)..Date.new(year, month, -1) +dates_enum = date_range.to_enum + +# カレンダーを表示する +puts "\n#{Date::MONTHNAMES[month]} #{year}".center(20) +puts "日 月 火 水 木 金 土" + +# 最初の日に計算する +initial_offset = dates_enum.peek.wday + +# 見やすいのため +print " " * initial_offset + +# 1ヶ月分表示する +dates_enum.each do |date| + print date.day.to_s.rjust(2) + " " + + # 土曜日になったら新しいラインを作る + puts if date.saturday? +end + +puts "\n" + From 0de7d5ff8c40b5ea7bc92ac4ea2d9fe92500f38a Mon Sep 17 00:00:00 2001 From: rani-i3 Date: Wed, 30 Aug 2023 15:11:55 +0900 Subject: [PATCH 02/10] bowling code --- 03.bowling/bowling.rb | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 03.bowling/bowling.rb diff --git a/03.bowling/bowling.rb b/03.bowling/bowling.rb new file mode 100644 index 0000000000..de14b8d4de --- /dev/null +++ b/03.bowling/bowling.rb @@ -0,0 +1,41 @@ +require 'rubocop' + +score = ARGV[0] +scores = score.split(',') +shots = [] + +scores.each do |s| + if s == 'X' + shots << 10 + shots << 0 + else + shots << s.to_i + end +end + +frames = [] +shots.each_slice(2) do |s| + frames << s +end + +point = 0 +frame_index = 0 + +frames.each_with_index do |frame, index| + if frame_index >= 9 + point += frame.sum + else + if frame[0] == 10 + point += 10 + frames[index + 1][0] + frames[index + 1][1] + frame_index += 1 + elsif frame.sum == 10 + point += 10 + frames[index + 1][0] + frame_index += 1 + else + point += frame.sum + frame_index += 1 + end + end +end + +puts point From b0b0b90e6bdc84363f873fe1ab2b0a9a9d237477 Mon Sep 17 00:00:00 2001 From: rani-i3 Date: Wed, 30 Aug 2023 17:25:30 +0900 Subject: [PATCH 03/10] fixed issues --- 03.bowling/bowling.rb | 25 ++++++++++++++++++------- 1 file changed, 18 insertions(+), 7 deletions(-) diff --git a/03.bowling/bowling.rb b/03.bowling/bowling.rb index de14b8d4de..b1b3215d15 100644 --- a/03.bowling/bowling.rb +++ b/03.bowling/bowling.rb @@ -1,4 +1,5 @@ require 'rubocop' +require 'debug' score = ARGV[0] scores = score.split(',') @@ -17,23 +18,33 @@ shots.each_slice(2) do |s| frames << s end - +p frames point = 0 -frame_index = 0 frames.each_with_index do |frame, index| - if frame_index >= 9 + # binding.break + if index >=9 point += frame.sum +# elsif frame_index == 11 + else if frame[0] == 10 - point += 10 + frames[index + 1][0] + frames[index + 1][1] - frame_index += 1 + if frames[index + 1][0] == 10 + if frames[index + 2][0] == 10 + point += 30 + else + point += 20 + frames[index + 2][0] + end + + else + point += 10 + frames[index + 1].sum + end + + elsif frame.sum == 10 point += 10 + frames[index + 1][0] - frame_index += 1 else point += frame.sum - frame_index += 1 end end end From 7bcf1aea46e53093b144d6a1403af10771a32616 Mon Sep 17 00:00:00 2001 From: rani-i3 Date: Mon, 16 Oct 2023 11:01:00 +0900 Subject: [PATCH 04/10] modified ls code --- 04.ls/ls1.rb | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 04.ls/ls1.rb diff --git a/04.ls/ls1.rb b/04.ls/ls1.rb new file mode 100644 index 0000000000..17e39e394b --- /dev/null +++ b/04.ls/ls1.rb @@ -0,0 +1,28 @@ +# frozen_string_literal: true + +def list_files_in_columns(columns = 3) + current_path = Dir.pwd + folder_glob = "#{current_path}/*" + + files = Dir.glob(folder_glob) + + max_length = files.map { |file| File.basename(file).length }.max || 0 + + column_width = (max_length + 2) + + (files.length.to_f / columns).ceil + + column_arrays = Array.new(columns) { [] } + + files.each_with_index do |file, index| + column_arrays[index % columns] << File.basename(file) + end + + column_arrays.each do |row| + columns.times do |col| + end + puts + end +end + +list_files_in_columns From 7239a6c393281a5ce989be4d3fdc379539960af2 Mon Sep 17 00:00:00 2001 From: rani-i3 <132240853+rani-i3@users.noreply.github.com> Date: Mon, 16 Oct 2023 11:01:58 +0900 Subject: [PATCH 05/10] Delete 02.calendar/calendar.rb --- 02.calendar/calendar.rb | 46 ----------------------------------------- 1 file changed, 46 deletions(-) delete mode 100644 02.calendar/calendar.rb diff --git a/02.calendar/calendar.rb b/02.calendar/calendar.rb deleted file mode 100644 index d7cdcc7be6..0000000000 --- a/02.calendar/calendar.rb +++ /dev/null @@ -1,46 +0,0 @@ -require 'date' -require 'optparse' - -# コマンドラインでparseする -options = {} -OptionParser.new do |opts| - opts.banner = "Usage: ruby calendar.rb [options]" - - opts.on("-m", "--month MONTH", Integer, "Specify the month (1-12)") do |month| - options[:month] = month - end - - opts.on("-y", "--year YEAR", Integer, "Specify the year") do |year| - options[:year] = year - end -end.parse! - -# 今の日付を取る -current_date = Date.today -year = options[:year] || current_date.year -month = options[:month] || current_date.month - -# date enumeratorを作る -date_range = Date.new(year, month, 1)..Date.new(year, month, -1) -dates_enum = date_range.to_enum - -# カレンダーを表示する -puts "\n#{Date::MONTHNAMES[month]} #{year}".center(20) -puts "日 月 火 水 木 金 土" - -# 最初の日に計算する -initial_offset = dates_enum.peek.wday - -# 見やすいのため -print " " * initial_offset - -# 1ヶ月分表示する -dates_enum.each do |date| - print date.day.to_s.rjust(2) + " " - - # 土曜日になったら新しいラインを作る - puts if date.saturday? -end - -puts "\n" - From 8d00c4a4528dc1e988479736059095e5733d7659 Mon Sep 17 00:00:00 2001 From: rani-i3 <132240853+rani-i3@users.noreply.github.com> Date: Mon, 16 Oct 2023 11:02:18 +0900 Subject: [PATCH 06/10] Delete 03.bowling/bowling.rb --- 03.bowling/bowling.rb | 52 ------------------------------------------- 1 file changed, 52 deletions(-) delete mode 100644 03.bowling/bowling.rb diff --git a/03.bowling/bowling.rb b/03.bowling/bowling.rb deleted file mode 100644 index b1b3215d15..0000000000 --- a/03.bowling/bowling.rb +++ /dev/null @@ -1,52 +0,0 @@ -require 'rubocop' -require 'debug' - -score = ARGV[0] -scores = score.split(',') -shots = [] - -scores.each do |s| - if s == 'X' - shots << 10 - shots << 0 - else - shots << s.to_i - end -end - -frames = [] -shots.each_slice(2) do |s| - frames << s -end -p frames -point = 0 - -frames.each_with_index do |frame, index| - # binding.break - if index >=9 - point += frame.sum -# elsif frame_index == 11 - - else - if frame[0] == 10 - if frames[index + 1][0] == 10 - if frames[index + 2][0] == 10 - point += 30 - else - point += 20 + frames[index + 2][0] - end - - else - point += 10 + frames[index + 1].sum - end - - - elsif frame.sum == 10 - point += 10 + frames[index + 1][0] - else - point += frame.sum - end - end -end - -puts point From c87d885a946f026ce57304f8068a4b16574b2d47 Mon Sep 17 00:00:00 2001 From: rani-i3 Date: Tue, 17 Oct 2023 11:43:46 +0900 Subject: [PATCH 07/10] fixed code --- 04.ls/ls1.rb | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/04.ls/ls1.rb b/04.ls/ls1.rb index 17e39e394b..fb8a0cc1c3 100644 --- a/04.ls/ls1.rb +++ b/04.ls/ls1.rb @@ -1,28 +1,40 @@ # frozen_string_literal: true def list_files_in_columns(columns = 3) - current_path = Dir.pwd + current_path = Dir.pwd # Get the current working directory + folder_glob = "#{current_path}/*" + # Use Dir.glob to get the list of files matching the pattern files = Dir.glob(folder_glob) + # Calculate the maximum filename length max_length = files.map { |file| File.basename(file).length }.max || 0 + # Calculate the width of each column column_width = (max_length + 2) + # Calculate the number of rows (files.length.to_f / columns).ceil + # Initialize an empty array to store the columns column_arrays = Array.new(columns) { [] } + # Distribute the files into columns files.each_with_index do |file, index| column_arrays[index % columns] << File.basename(file) end + # Display the files in columns column_arrays.each do |row| columns.times do |col| + print row[col]&.ljust(column_width) + # file_index = row + col * rows + # print column_arrays[col][file_index].to_s.ljust(column_width) end puts end end +# Call the function to list files in columns list_files_in_columns From 80531e2d23d8cbfbc24536a745a37e75b111b574 Mon Sep 17 00:00:00 2001 From: rani-i3 Date: Tue, 17 Oct 2023 11:45:07 +0900 Subject: [PATCH 08/10] modified ls code to clean unecessesary comments --- 04.ls/ls1.rb | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/04.ls/ls1.rb b/04.ls/ls1.rb index fb8a0cc1c3..c049ecc90a 100644 --- a/04.ls/ls1.rb +++ b/04.ls/ls1.rb @@ -1,40 +1,38 @@ # frozen_string_literal: true def list_files_in_columns(columns = 3) - current_path = Dir.pwd # Get the current working directory + current_path = Dir.pwd folder_glob = "#{current_path}/*" - # Use Dir.glob to get the list of files matching the pattern + files = Dir.glob(folder_glob) - # Calculate the maximum filename length + max_length = files.map { |file| File.basename(file).length }.max || 0 - # Calculate the width of each column + column_width = (max_length + 2) - # Calculate the number of rows + (files.length.to_f / columns).ceil - # Initialize an empty array to store the columns + column_arrays = Array.new(columns) { [] } - # Distribute the files into columns + files.each_with_index do |file, index| column_arrays[index % columns] << File.basename(file) end - # Display the files in columns + column_arrays.each do |row| columns.times do |col| print row[col]&.ljust(column_width) - # file_index = row + col * rows - # print column_arrays[col][file_index].to_s.ljust(column_width) end puts end end -# Call the function to list files in columns + list_files_in_columns From 3e91009cd967e7cb78bce08ad370a267f7d2b3b8 Mon Sep 17 00:00:00 2001 From: rani-i3 Date: Wed, 18 Oct 2023 10:18:20 +0900 Subject: [PATCH 09/10] seperated into two methods --- 04.ls/ls1.rb | 31 ++++++++++++------------------- 1 file changed, 12 insertions(+), 19 deletions(-) diff --git a/04.ls/ls1.rb b/04.ls/ls1.rb index c049ecc90a..a5b3346834 100644 --- a/04.ls/ls1.rb +++ b/04.ls/ls1.rb @@ -1,31 +1,23 @@ # frozen_string_literal: true -def list_files_in_columns(columns = 3) - current_path = Dir.pwd - +def list_files + current_path = Dir.pwd folder_glob = "#{current_path}/*" - - files = Dir.glob(folder_glob) + files.map { |file| File.basename(file) } +end - - max_length = files.map { |file| File.basename(file).length }.max || 0 - - +def print_files_in_columns(columns = 3) + file_list = list_files + max_length = file_list.map { |file| file.length }.max || 0 column_width = (max_length + 2) - - - (files.length.to_f / columns).ceil - - + rows = (file_list.length.to_f / columns).ceil column_arrays = Array.new(columns) { [] } - - files.each_with_index do |file, index| - column_arrays[index % columns] << File.basename(file) + file_list.each_with_index do |file, index| + column_arrays[index % columns] << file end - column_arrays.each do |row| columns.times do |col| print row[col]&.ljust(column_width) @@ -34,5 +26,6 @@ def list_files_in_columns(columns = 3) end end +file_list = list_files -list_files_in_columns +print_files_in_columns From d23da38bd48f57a9731744cfe6db8c9f7457d0e1 Mon Sep 17 00:00:00 2001 From: rani-i3 Date: Thu, 19 Oct 2023 16:49:10 +0900 Subject: [PATCH 10/10] fixed opposite row and column --- 04.ls/ls1.rb | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/04.ls/ls1.rb b/04.ls/ls1.rb index a5b3346834..ebecdf2bfa 100644 --- a/04.ls/ls1.rb +++ b/04.ls/ls1.rb @@ -12,20 +12,18 @@ def print_files_in_columns(columns = 3) max_length = file_list.map { |file| file.length }.max || 0 column_width = (max_length + 2) rows = (file_list.length.to_f / columns).ceil - column_arrays = Array.new(columns) { [] } + column_arrays = Array.new(rows){[]} file_list.each_with_index do |file, index| - column_arrays[index % columns] << file + column_arrays[index % rows] << file end column_arrays.each do |row| - columns.times do |col| - print row[col]&.ljust(column_width) + row.each do |col| + print col&.ljust(column_width) end puts end end -file_list = list_files - print_files_in_columns