Skip to content

Instantly share code, notes, and snippets.

@shadoi
Last active August 29, 2015 14:15
Show Gist options
  • Save shadoi/cb1f8f11a3fffb6fb0a3 to your computer and use it in GitHub Desktop.
Save shadoi/cb1f8f11a3fffb6fb0a3 to your computer and use it in GitHub Desktop.
# Write a method that takes a string in and returns true if the letter
# "z" appears within three letters **after** an "a". You may assume
# that the string contains only lowercase letters.
#
# Difficulty: medium.
# may want to do max+1 as base-zero can be confusing
def x_after_y?(str, char1, char2, max)
range = Range.new(1, max)
str.slice!(str.chars.index(char1))
if str.length > 0
range.find {|n| true if str.chars.at(n) == char2}
end
end
---
[7] pry(main)> x_after_y?('asdz', 'a', 'z', 3)
=> 2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment