From 093db3a8d0c9e3404999bb9512b4fa0fcaba7d56 Mon Sep 17 00:00:00 2001 From: AAshGray <193518051+AAshGray@users.noreply.github.com> Date: Thu, 13 Feb 2025 00:56:36 +0000 Subject: [PATCH 1/4] input validation --- archive/r/ruby/josephus-problem.rb | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 archive/r/ruby/josephus-problem.rb diff --git a/archive/r/ruby/josephus-problem.rb b/archive/r/ruby/josephus-problem.rb new file mode 100644 index 000000000..13d9a8f06 --- /dev/null +++ b/archive/r/ruby/josephus-problem.rb @@ -0,0 +1,25 @@ +def josephus_problem(array, k, index) + +end + +def error() + puts "Usage: please input the total number of people and number of people to skip." +end + +if ARGV.length != 2 + error() +end + +ARGV.each do |arg| + if arg.match(/[a-zA-Z]/) || arg.empty? || + error() + else + num1 = ARGV[0].to_i + num2 = ARGV[1].to_i + + array = [*1..num1] + + josephus_problem(array, num2, 0) + end +end + From 0429fe03e7589563f0091ab7277062fbaee71d73 Mon Sep 17 00:00:00 2001 From: AAshGray <193518051+AAshGray@users.noreply.github.com> Date: Thu, 13 Feb 2025 01:37:04 +0000 Subject: [PATCH 2/4] Josephus function attempt WIP --- archive/r/ruby/josephus-problem.rb | 46 +++++++++++++++++++----------- 1 file changed, 30 insertions(+), 16 deletions(-) diff --git a/archive/r/ruby/josephus-problem.rb b/archive/r/ruby/josephus-problem.rb index 13d9a8f06..56ecbf7fe 100644 --- a/archive/r/ruby/josephus-problem.rb +++ b/archive/r/ruby/josephus-problem.rb @@ -1,25 +1,39 @@ -def josephus_problem(array, k, index) +def josephus_problem(array, k, start) + if (array.length == 1) + # if the array is one item left, return the survivor value + return array[0] + else + # set the index position (start + (k - 1)) and set wrap with % + index = (start + k - 1) + wrap = index % array.length + array.delete_at(wrap) + return josephus_problem(array, k, wrap) + end end def error() - puts "Usage: please input the total number of people and number of people to skip." + puts "Usage: please input the total number of people and number of people to skip." end +# check for incorrect number of arguments if ARGV.length != 2 - error() -end - -ARGV.each do |arg| - if arg.match(/[a-zA-Z]/) || arg.empty? || - error() - else - num1 = ARGV[0].to_i - num2 = ARGV[1].to_i + error +else + # check for letters or empty strings in each argument + ARGV.each do |arg| + if arg.match(/[a-zA-Z]/) || arg.empty? + error + else + # convert the arguments to integers + n = ARGV[0].to_i + k = ARGV[1].to_i - array = [*1..num1] - - josephus_problem(array, num2, 0) - end -end + # create an array with a length of argument n + array = [*1..n] + # pass our array to the function with k and our start index (0) + josephus_problem(array, k, 0) + end + end +end \ No newline at end of file From 87b740b6f37ad1f62b250a3304b2883a599312f4 Mon Sep 17 00:00:00 2001 From: AAshGray <193518051+AAshGray@users.noreply.github.com> Date: Thu, 13 Feb 2025 16:53:31 +0000 Subject: [PATCH 3/4] corrected bad loop and adjusted verification section --- archive/r/ruby/josephus-problem.rb | 29 +++++++++++------------------ 1 file changed, 11 insertions(+), 18 deletions(-) diff --git a/archive/r/ruby/josephus-problem.rb b/archive/r/ruby/josephus-problem.rb index 56ecbf7fe..efde48ea2 100644 --- a/archive/r/ruby/josephus-problem.rb +++ b/archive/r/ruby/josephus-problem.rb @@ -1,8 +1,7 @@ def josephus_problem(array, k, start) - if (array.length == 1) # if the array is one item left, return the survivor value - return array[0] + puts array[0] else # set the index position (start + (k - 1)) and set wrap with % index = (start + k - 1) @@ -17,23 +16,17 @@ def error() end # check for incorrect number of arguments -if ARGV.length != 2 - error +# .any? loops over the elements of ARGV to check for letters or empty strings in each argument +if ARGV.length != 2 || ARGV.any? { |arg| arg.match(/[a-zA-Z]/) || arg.empty? } + error() else - # check for letters or empty strings in each argument - ARGV.each do |arg| - if arg.match(/[a-zA-Z]/) || arg.empty? - error - else - # convert the arguments to integers - n = ARGV[0].to_i - k = ARGV[1].to_i + # convert the arguments to integers + n = ARGV[0].to_i + k = ARGV[1].to_i - # create an array with a length of argument n - array = [*1..n] + # create an array with a length of argument n + array = [*1..n] - # pass our array to the function with k and our start index (0) - josephus_problem(array, k, 0) - end - end + # pass our array to the function with k and our start index (0) + josephus_problem(array, k, 0) end \ No newline at end of file From bac35ee9f26bc22b3f32f0cdd2650d21fc06f455 Mon Sep 17 00:00:00 2001 From: AAshGray <193518051+AAshGray@users.noreply.github.com> Date: Fri, 14 Feb 2025 23:49:50 +0000 Subject: [PATCH 4/4] updated validation to check for non-digit characters --- archive/r/ruby/josephus-problem.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/archive/r/ruby/josephus-problem.rb b/archive/r/ruby/josephus-problem.rb index efde48ea2..c84059c50 100644 --- a/archive/r/ruby/josephus-problem.rb +++ b/archive/r/ruby/josephus-problem.rb @@ -16,8 +16,8 @@ def error() end # check for incorrect number of arguments -# .any? loops over the elements of ARGV to check for letters or empty strings in each argument -if ARGV.length != 2 || ARGV.any? { |arg| arg.match(/[a-zA-Z]/) || arg.empty? } +# .any? loops over the elements of ARGV to check if the argument contains non-digit characters or empty strings +if ARGV.length != 2 || ARGV.any? { |arg| arg.match(/[^0-9]/) || arg.empty? } error() else # convert the arguments to integers