Crossing the road blindly

  • 3
  • 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.
Comments (27)
  • Popular
  • New
  • Old
You must be signed in
MBCLevel 11 , Apeldoorn
13 June, 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 10 , Glasgow
17 June, 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 11 , Apeldoorn
17 June, 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.
Nitin rajLevel 8 , Mumbai
11 June, 16:09
#guyzz help me out . #above code gives task condition not met
RomanLevel 41
12 June, 05:31
Please submit your code for review in the Help section.
SajanaLevel 8 , Dearborn
11 June, 04:56
Consider only the integer portion of the 'Double' value while performing MOD.
AhmedLevel 17 , Amsterdam
13 May, 09:19
Guys, don't overthink the problem. You don't have to right all the numbers! Just study the (remainder or Modulus Operator in Java) and you will fix this problem really easily!
KIN SOCHEATLevel 12 , Phnom Penh
13 May, 07:23
Thanks resolved
Deji OlarinoyeLevel 10 , Lagos
11 May, 20:08
Modulo Arithmetic -Best solved using recursion.
KIN SOCHEATLevel 12 , Phnom Penh
11 May, 13:09
I test this code the result is ok. but it still not pass. someone can tell why? BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); String slight = reader.readLine(); double t = Double.parseDouble(slight); if (0 <t && t<=60){ if (t == 4 || t == 8 || t == 12 || t == 16 | t == 20 || t == 24 || t == 28 || t == 32 || t == 36 || t == 40 || t == 44 || t == 48 || t == 52 || t == 56 || t == 60){ System.out.println("red"); }else if (t == 3 || t == 7 || t == 11 || t == 15 || t == 19 || t == 23 || t == 27 || t== 31 || t == 35 || t == 39 || t == 43 || t == 47 || t == 51 || t == 55 || t == 59){ System.out.println("yellow"); } else System.out.println("green"); }else System.out.println("not Correct number"); } }
RomanLevel 41
13 May, 06:04
Please submit your code for review in the Help section. p.s. t can be more than 60 ...
Lex MedeirosLevel 17 , San Jose
20 March, 21:27
Below is my code. When I run it in ItelliJ IDEA and I input the numbers, I get the correct color responses. But when I check it with CodeGym I get these errors. Any help is highly appreciated.
package com.codegym.task.task04.task0416;

/*
Crossing the road blindly

*/

import java.io.*;

public class Solution {
    public static void main(String[] args) throws Exception {
        //write your code here

        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        System.out.println("please enter number");
        String s = reader.readLine();
        Double t = Double.parseDouble(s);


        if((t%5.0 >= 0) && (t%5 < 3))
            System.out.println("green");
        else {
            if (t % 5.0 >= 3 && t % 5 < 4)
                System.out.println("yellow");
            else
                System.out.println("red");
        }
    }
}
RomanLevel 41
21 March, 07:23
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 codegym.cc/help.
Cinthya CabanzoLevel 22 , Bogota
25 April, 13:56
You can use else if to improve readability, I follow the tips that give the code style and help me to improve. Well, I thing that if you specify all the cases complety, will hepls, because the computer doesn't know which are the limits of your sample.
JobayerLevel 9 , Dhaka
4 July, 10:54
why have you added this line
System.out.println("please enter number");
Remove it & check whether it's working or not.
carlos oliveiraLevel 8 , Seattle
18 March, 09:28
Does anyone here knows the trick to pass Verify?. my code runs perfect with notepad. i think this compiler is very limited.
AlexLevel 41
18 March, 10:06
Please submit your code for review in the Help section.
carlos oliveiraLevel 8 , Seattle
18 March, 07:45
what is wrong with my code?