Difficulty:
Easy
Easy
Tags:
strings
strings
Instructions:
One of the challenges in implementing Rubeque is performing string substitution. See how well you can do.
One of the challenges in implementing Rubeque is performing string substitution. See how well you can do.
Problem and Solution:
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
# Solution to 'Simple String Substitution' on rubeque.com | |
# by jakthegr8 | |
# http://www.rubeque.com/problems/simple-string-substitution | |
class String | |
def simple_sub(pattern, replace) | |
gsub(/#{Regexp.escape(pattern)}/ ) {replace} | |
end | |
end | |
subject = "?? == ??" | |
assert_equal subject.simple_sub("==", "<="), "?? <= ??" | |
assert_equal subject.simple_sub("abc", "def"), "?? == ??" | |
assert_equal subject.simple_sub("??", "fun??"), "fun?? == fun??" | |
naruse_answer = "Date.parse(date.sub(/(\d+)[-\/](\d\d).(\d{4})/,'\\3-\\1-\\2')).strftime('%b %d, %Y')" | |
assert_equal subject.simple_sub("??", naruse_answer), "#{naruse_answer} == #{naruse_answer}" |
No comments:
Post a Comment