From 94526dd09d37f7dd88a720677e93207e2b27a46d Mon Sep 17 00:00:00 2001 From: Carlos A Date: Mon, 13 Mar 2023 21:45:57 -0500 Subject: [PATCH] Reto #11 - ruby --- .../ruby/test0n3.rb" | 43 +++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 "Retos/Reto #11 - URL PARAMS [F\303\241cil]/ruby/test0n3.rb" diff --git "a/Retos/Reto #11 - URL PARAMS [F\303\241cil]/ruby/test0n3.rb" "b/Retos/Reto #11 - URL PARAMS [F\303\241cil]/ruby/test0n3.rb" new file mode 100644 index 0000000000..c0d231f6b8 --- /dev/null +++ "b/Retos/Reto #11 - URL PARAMS [F\303\241cil]/ruby/test0n3.rb" @@ -0,0 +1,43 @@ +# frozen_string_literal: true + +def url_params(url) + return [] unless url.count('?') == 1 + + params = divide_string(url, '?') + values = [] + + params.split('&').each do |param| + values.push(divide_string(param, '=')) if param.match?(/\D+(={1})\w+/) + end + values +end + +def divide_string(string, char) + params_idx = string.rindex(char) + string.slice(params_idx + 1..string.size) +end + +TESTS = { 'input' => ['https://retosdeprogramacion.com?year=2023&challenge=0', + 'https://retosdeprogramacion.com/search?year=2023', + 'https://retosdeprogramacion.com/params/name/lastname', + 'retosdeprogramacion.com/search?', + ''], + 'output' => [%w[2023 0], + %w[2023], + [], + [], + []] }.freeze + +errors = 0 +TESTS['input'].each_with_index do |test, index| + resp = url_params(test) + expected = TESTS['output'][index] + next if resp == expected + + errors += 1 + puts "\n\noriginal: #{test}" + puts resp + puts "expected: #{expected}" +end + +puts "\nTests#{errors != 0 ? ' not ' : ' '}passed, #{errors} errors\n"