Skip to content

Instantly share code, notes, and snippets.

@bricejlin
Created November 18, 2013 16:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bricejlin/7530904 to your computer and use it in GitHub Desktop.
Save bricejlin/7530904 to your computer and use it in GitHub Desktop.
pattern = ',*,foo,bar,baz'
path = 'cat/foo/bar/baz'
# rules for match method:
# - return boolean value
# - asterisks are wildcards
# - compare pattern and path
# - matching letter rated higher over asterisk
# - if same # of asterisks, prefer wildcards that appear further to right
def match(pattern, path)
patt_arr = pattern.gsub(/,/, ' ').split
path_arr = path.gsub(/\//, ' ').split
if patt_arr.count == path_arr.count
while patt_arr.include?('*')
ast_index = patt_arr.index('*')
patt_arr.delete_at(ast_index)
path_arr.delete_at(ast_index)
end
patt_arr == path_arr
else
false
end
end
# asterisks are wildcards
p match('a,b,c', 'a/b/c') == true
p match('*,b,*', 'a/b/c') == true
puts
# ignores beg/end dashes and commas
p match('*,b,*', 'a/b/b/') == true
p match('*,b,*,', 'a/b/b') == true
p match('*,b,*', '/a/b/b/') == true
p match(',*,b,*', 'a/b/b') == true
puts
# false if array size isn't equal
p match('*,b,*', 'a/b/c/d') == false
p match('*,b,*', 'a/b') == false
@tobins
Copy link

tobins commented Nov 18, 2013

In regards to the following:

patt_arr = pattern.gsub(/,/, ' ').split
path_arr = path.gsub(/\//, ' ').split

What happens if the input contains spaces between '/' or ','? The Problem Description and Input Format does not note any thing about no white spaces being included in the input.

@tobins
Copy link

tobins commented Nov 18, 2013

Otherwise, looks good. Now how would you tie it all together?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment