I would like to count the number of times "red"
is followed by "green"
in this array:
["red", "orange", "green", "red", "yellow", "blue", "green"]
If it is another color, the code should ignore it and proceed to the next item in the array.
event_type.each_slice(2) do |red, green|
break unless green
count = count + 1
end
p "The count is #{count}"
Step 1:
Look for red
Step 2:
IF not last item
Compare with next item on array
ELSE Go to Step 4
Step 3:
IF green, count = count + 1
Go to Step 1
ELSE Go to Step 2
Step 4:
Print Count
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…