• 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.
Ilie Babcenco Java Developer at Amdaris
3 November 2022, 20:41
hint: a % 5
22 August 2021, 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
Enrique Del Valle Backend Developer
5 January 2022, 01:10
this tip helped me alot, thank you
Toka
Level 11 , Dugo Selo, Croatia
15 July 2021, 15:29
I thought I would fail but I did it :)
Aron
Level 18
2 July 2021, 11:17
Challenging task, took a while to solve, worth it in the end 🙂
Hemel
Level 7 , London, United Kingdom
12 June 2021, 11:23
Toka
Level 11 , Dugo Selo, Croatia
14 July 2021, 15:50
Because user input is double
Anonymous #11180958
Level 6 , Alicante, Spain
4 December 2022, 15:54
i did it using float, the think is u are working with decimals Not sure if using double or float makes any difference in this case? or wether to use one or another
Level 22 , Chandler, United States
15 April 2021, 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 2021, 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..
EvelynSt
Level 10 , Italy
3 October 2022, 18:53
i would like to see the code with for loop
KARMA GURUNG
Level 5 , Wyken, United Kingdom
2 February 2021, 10:52
input no. % 5 does the job
Seth Barker
Level 5 , Eureka Springs, United States
1 February 2021, 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 :)