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.
Comments (63)
  • Popular
  • New
  • Old
You must be signed in to leave a comment
scriptKing madDog
22 August, 22:32
I spent hours on this it was hell, hot tip is use factors of 5 and % symbol to make a variable to work off
Toka
Level 10 , Dugo Selo, Croatia
15 July, 15:29
I thought I would fail but I did it :)
Aron
Level 16
2 July, 11:17
Challenging task, took a while to solve, worth it in the end 🙂
Hemel
Level 7 , London, United Kingdom
12 June, 11:23
I've solved the task but trying to work out why I had to do it in a certain way. BufferedReader min = new BufferedReader(new InputStreamReader(System.in)); double a = Double.parseDouble(min.readLine()); I had to use double instead of int even though int was giving the correct answer. Can someone please explain why the following couldn't be used in this situation? int a = Integer.parseInteger(min.readLine()); Thanks!
Toka
Level 10 , Dugo Selo, Croatia
14 July, 15:50
Because user input is double
Jaime Padilla
Level 7 , Tempe, United States
15 April, 22:35
I thought the modulus doesn't work correctly when using decimals? As the remainder would either be zero or start a repeating number? For instance, I receive: 5.3 % 5 = 0.2999999999999998 Shouldn't it be, 5.3 % 5 = 0.3? When only going to 1 decimal place? It took me trial and error to pass this. Maybe I'm just tired and completely overlooking something, but if you have an answer, please reply.
Miriam Kretschmann
Level 5 , Bielefeld, Germany
8 April, 12:08
I used a for loop for defining the ranges in which the lights appear (like, "green" is between "a = i*5" and "b = a+1") and created an if/else statement accordingly. Took me a while but it worked :) Tried my luck with modulo first but got stuck..
KARMA GURUNG
Level 5 , Wyken, United Kingdom
2 February, 10:52
input no. % 5 does the job
Seth Barker
Level 5 , Eureka Springs, United States
1 February, 23:05
I had to break it down on paper to really see what the numbers would do when a modulator was applied. Then I created the range.
remote87
Level 18 , Sofia, Bulgaria
27 November 2020, 17:34
Well, I may be stupid, but I did not used modulus and so on, I did not find the logic ( stupid as I said ). This is how I thought it out - yellow is 3, red is 4. I have a cycle ( green, yellow, red, green, yellow etc ), so in every iteration I add a number to yellow and red so that I can get my next numbers, where the light corresponds to that specific number. So far so good! I needed a validation if the input equals the number that I'm currently on, but the user can input a decimal number as well... so, what? I am sure that I can find a build in method to round the number down to it's integer ( because for example 2.9 is green, and 3 is already yellow ). If my validation passes, I break out of the loop and print. Fine, but how to check if the light is not green with so many options and iterations? Hmmm, if only I had an empty spot to keep the result of my loop...than, if the validation in the loop has passed, I can keep my result in that empty spot, break out of my loop, check if the spot is empty or not - if not empty, than that's my light, if empty - oh, well, than it's green and I can pass :) I hope that I helped someone, who's not so smart to check modulo on decimal and blah blah blah ( and I hope that I did not spoiled the fun out of it ) ...my solution works like a charm! After spending couple of hours, thinking how to solve it, I am happy that I figured it out by my self and I did not used some other people's solution just to pass the test :)
Erextor
Level 9 , Moscow, Russian Federation
21 October 2020, 06:14
i used this construction: double b=a.nextFloat(); double c= b-5*Math.floor(b/5);