Created
September 29, 2017 20:23
-
-
Save barbchoy/219fb1638f2667fd98722cb35c4808f2 to your computer and use it in GitHub Desktop.
nearby-AZ created by barbchoy - https://repl.it/Br7R/3300
This file contains hidden or 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
# 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. | |
def nearby_az(string) | |
i=0 | |
a_pos=0 | |
while i<string.length | |
if string[i]=="a" | |
a_pos=i | |
if string[a_pos+1]=="z"||string[a_pos+2]=="z"||string[a_pos+3]=="z" | |
return true | |
end | |
end | |
i+=1 | |
end | |
return false | |
end | |
# These are tests to check that your code is working. After writing | |
# your solution, they should all print true. | |
puts("\nTests for #nearby_az") | |
puts("===============================================") | |
puts('nearby_az("baz") == true: ' + (nearby_az('baz') == true).to_s) | |
puts('nearby_az("abz") == true: ' + (nearby_az('abz') == true).to_s) | |
puts('nearby_az("abcz") == true: ' + (nearby_az('abcz') == true).to_s) | |
puts('nearby_az("a") == false: ' + (nearby_az('a') == false).to_s) | |
puts('nearby_az("z") == false: ' + (nearby_az('z') == false).to_s) | |
puts('nearby_az("za") == false: ' + (nearby_az('za') == false).to_s) | |
puts("===============================================") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment