Skip to content

Commit

Permalink
Reto mouredev#11 - ruby
Browse files Browse the repository at this point in the history
  • Loading branch information
test0n3 committed Mar 14, 2023
1 parent 8582f1a commit 94526dd
Showing 1 changed file with 43 additions and 0 deletions.
43 changes: 43 additions & 0 deletions Retos/Reto #11 - URL PARAMS [Fácil]/ruby/test0n3.rb
Original file line number Diff line number Diff line change
@@ -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"

0 comments on commit 94526dd

Please sign in to comment.