Crossing the road blindly

  • 8
  • Locked
Let's say that we are certain that at the beginning of every hour our traffic light is green for 3 minutes, yellow for a minute, and then red for another minute. Then the sequence repeats. Our program must determine what light is on now (where "now" is a real number that indicates the number of minutes that have elapsed since the beginning of the hour).
You can't complete this task, because you're not signed in.
  • Popular
  • New
  • Old
You must be signed in to leave a comment
JeLevel 6
15 September, 14:56
All conditions (using tm%4) were satisfied including numbers with decimal but I still get failed results. Would appreciate an explanation for it. Thanks!
M TillLevel 7 , San Jose
24 July, 23:22
5 is key
BarshaLevel 4 , Eluru
17 July, 06:58
In this task if t=9 or t=14 etc., then t%5=4 which is greater than 3, it will fail to meet the condition of green, i.e (t%5 >= 0) && (t%5 < 3), though the signal will be green when t=9 or t=14. I still passed the task. Please correct me if I am wrong here. Can someone let me know if we are considering t value only till 5?
Lucas HoageLevel 14 , Savannah
25 May, 15:40
This was very difficult for me to do. I've used modulus in the past exercises and outside of this for more simplified uses. I have never crossed over to using modulus for float or doubles before. This was a great lesson in how modulus operates with decimal numbers. A few hints: You don't need every minute in an hour. The range is determined by modulus If I say anymore, it would give it away. Honestly, I wonder if that second line gives it away. It's super obvious once you get that part. You already know how to do less than, greater than, equal to or a combination of those. Nothing changes with that. You still need to make conditions that work within the range
bobLevel 8 , Milan
18 May, 22:18
this is a great exercise, but i have to say i completely disagree with their solution set that 3 is yellow instead of green, 4 is red and 5 is green. this makes sense only as mod. works but not as in numbers of minutes. 3 should be green, 4 should be yellow, 5 should be red and 0 should be red. this is also how time and stopwatches work so decent enough model to follow. anyway, the program doesn't change you just swap around the comparisons but the data of the solution seems less relevant to the truth of the problem.
Lucas HoageLevel 14 , Savannah
25 May, 17:38
Consider this. 3 = yellow in the solution right? 2 minutes and 59 seconds does not equal 3 right? so if 2.59 < 3, then 2.59 is green right? if 3 is yellow, then 3 cannot be green. Remember that in real life we start counting from 0, not 1. So the beginning of the next set will always be the "0" in each minute. If we started counting from 1, when someone asked us to count to 10, we would begin at 2. Because 1 would effectively become our new "0". Open a stop watch and start it. Notice how it starts from 0 and increments in fractions of a second until it hits 1? Because 0 is where we begin. Just like in arrays. Just like how a computer thinks. So you already know what happens to 5 when using modulus. It becomes the new 0, not red. Because when we establish the conditions, which I shouldn't mention here, a condition of green is that it must be 0 . There is no difference between 0 and 5 in this scenario when it comes to minutes. 10 would also become the new 0. The same thing applies to 3 and 4. 3 becomes the "0" for 3 and 4 becomes the "0" for 4. Remember that the range is set by the value of modulus itself. Once we create the condition that modulus result 0 is for green, we are effectively saying 0,5,10,15,20,25, etc are the beginning of the timer again. It is correct in this exercise and in real life. Just remember that we are effectively taking an hour and downsizing it to a few minutes. In this case, 1/12th scale. Only in real life we would be utilizing electromagnetic sensors to determine if a car is there and if not, adjust current timer. (I won't delve into that too deeply, but that is why you'll see cross walk timers jump from say 37 seconds, down to 3 during low intersecting traffic).
Deborah WenrichLevel 7 , Pembroke Pines
25 April, 23:40
After 2 days of studying everybody's code in the help and display section and failing miserably at everything I tried. I found snippets of code that were missing from my code in various codes of yours. I put them together in mine and it worked.. Here it is in its entirety for those coming up.
RomanLevel 41
27 April, 05:49
Posting the solution in the comments is prohibited.
LiewLevel 10
16 April, 07:57
use modular operator, can be done in 5 lines.
11 April, 09:14
the last task doesn't seem to evaluate.

public class Solution {
    public static void main(String[] args) throws Exception {
        //write your code here
BufferedReader reader = new BufferedReader(new InputStreamReader(;
double time = Double.parseDouble(reader.readLine());
String light;
 if (time < 0){ light = "green";break;}
 if (time < 0){ light = "yellow";break;}
 time -=1;
 if (time <0){ light = "green";break;}
LiewLevel 10
16 April, 07:53
too many if statements,ur time is not decreasing at the right amount.Can consider using modular.
6 May, 22:39
Cz FerenczLevel 7 , Bucharest
25 November 2019, 20:41
After almost 2 hours of work in, i made it. :) With a bit of help from the help comments. Good luck to everyone. :)
Shoaib UmerLevel 20 , Bristol
17 November 2019, 23:18
Ny solution is working as per given conditions but not accepted. Plz point out my mistake.
RomanLevel 41
18 November 2019, 06:21
Please use the Help section for questions relating to tasks.