Created
November 18, 2013 16:34
-
-
Save bricejlin/7530904 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
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
In regards to the following:
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.