Hello everybody,
Please shed some light on the following code.
I don’t understand at what point does the command get activated and also why is being incremented in two different places: once in the first while-loop and also towards the end of the code
Thanks!!!
# 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)
idx1 = 0
while idx1 < string.length
if string[idx1] != "a"
idx1 += 1
next
end
idx2 = idx1 + 1
while (idx2 < string.length) && (idx2 <= idx1 + 3)
if string[idx2] == "z"
return true
end
idx2 += 1
end
idx1 += 1
end
return false
end
hello,
At no point is a “command” executed (the word “command” in ruby source code context usually referrers to a command one might execute in a shell/terminal)
The code you have provided defines a “method” called nearby_az and that is never called.
If you want to execute the code in the method, trying out different strings, you could put these lines after the last end:
def nearby_az(string)
idx1 = 0
while idx1 < string.length
if string[idx1] != "a"
idx1 += 1
next
end
idx2 = idx1 + 1
while (idx2 < string.length) && (idx2 <= idx1 + 3)
if string[idx2] == "z"
return true
end
idx2 += 1
end
idx1 += 1
end
end
puts nearby_az('abcdz').inspect
puts nearby_az('abcz').inspect
puts nearby_az('abz').inspect
puts nearby_az('az').inspect
The above additional lines will print out the result of calling the method (the .inspect on the end ensures you see the result when it is printed (puts)
To run the code you can paste it into an irb
session (just type “irb” in a terminal)
@bugthing
Hello and thank you for your reply. I know how to run the code and test it. I just don’t understand the in the code.
Thank you.
V/r,
MC
ok, sorry I did kinda miss the point of the post…
I have commented the code, hope it helps
def nearby_az(string)
idx1 = 0
# outer loop that runs through the passed in string 1 character at a time.
while idx1 < string.length
# if the current character is NOT an 'a', then increment the counter variable and do the next loop of the outer loop
if string[idx1] != "a"
idx1 += 1
next
end
# at this point we know our counter is pointing at an 'a' in the string.
# here a new counter is created, pointing at the character next to the 'a'
idx2 = idx1 + 1
# start the inner loop using the 2nd counter, ensuring it does not go past the end of the string and not beyond 3 characters past the 'a'
while (idx2 < string.length) && (idx2 <= idx1 + 3)
# here we check if the 2nd counter is pointing at a character that is an 'z'
if string[idx2] == "z"
# .. if so, return true as we have seen an 'a' and within 3 characters there is a 'z'
return true
end
# here we need to increment the 2nd counter and loop to look at the next character
idx2 += 1
end
# increment the 1st counter as at this point we know there is no 'z' next to or within 3 characters of the 'a'
idx1 += 1
end
end
@bugthing Thank you so much for taking the time to explain the code. It helps a lot.
One more question What does NEXT do and at what point it gets executed?
Thanks,
MC
The next
command will jump to the next iteration of the most internal loop
You mean it will go to the next while-loop? Or…?
The code will return to top of the current loop
Imagine you’re running laps around a track. The next
is as if you stop part way through the run, but count the lap anyway and continue at the next lap (value of lap is incremented by one). Whatever has changed before the next
will still be changed, but what was after it was not run for that lap.