Crossing the road blindly

  • 8
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
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 19 , 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.
MecoSLevel 14
19 September 2019, 17:22
My solution is a bit verbouse but run and response the correct light.. I didn't understand why is not accepted. Sorry for my english...
RomanLevel 41
15 October 2019, 05:32
If you need help, something isn't right in your code, the server won't accept your solution (even if you are 100% sure that it is correct). Describe your question/issue in the HELP section at
Carlos CortázarLevel 17 , Madrid
23 August 2019, 15:34
My program Solution runs for all the cases, but when i press Verify the program says not verified. Anyone can figure out why?
AlexLevel 41
24 August 2019, 13:15
Please check this in the Help section. Don't forget to attach your code with the question.
Michael BreyLevel 7 , Dresden
13 October 2019, 18:03
Hello Carlos, your solution is absolutely smart and perfect. I did not find any better way to do the task, especially with such a concise and transparent logic. Thank you!! On the other hand, I also do not understand, why the code does not validate. :-(
MBCLevel 14 , Apeldoorn
13 June 2019, 17:51
Does anyone know why we have to declare the variable t with 5.0 as a modulus operation? I have looked the modulus operator up online and understand that it gives the remainder, but I do not get how that relates to the hour and minutes passing from t?
GCPixLevel 15 , Glasgow
17 June 2019, 11:49
It doesn't have anything to do with hours. The lights cycle in a 5 minute block, the first 3 minutes are green, minute 4 is yellow and minute 5 is red. At that point it goes back to the start. Because this adds to 5, and the time runs 0-1 minutes, 1-2, 2-3,3-4,4-5 then modulus 5 will always reduce the numbers to the same sequence. If that makes any sense?
MBCLevel 14 , Apeldoorn
17 June 2019, 13:54
Yes it does make sense now, I think I didn't identify the 5 minute cycle for all of the lights, that's why.