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 (56)
  • Popular
  • New
  • Old
You must be signed in to leave a comment
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 15, 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 7, Moscow, Russian Federation
21 October 2020, 06:14
i used this construction: double b=a.nextFloat(); double c= b-5*Math.floor(b/5);
Maria Level 15, Moscow, Russian Federation
6 October 2020, 13:41
Great task! Thank you! I did it =)
Joe M Level 9, East Coast, United States
27 September 2020, 17:56
Learning to use Buffered Reader! Make a template using Preferences/Editor/Live Templates. type the full line in the big box and the abbreviation(buff) in the small box, now when you enter buff + TAB it will type the line for you.
Joe M Level 9, East Coast, United States
26 September 2020, 14:36
Nine lines of code using Scanner. It uses modulus.
Joe M Level 9, East Coast, United States
26 September 2020, 14:33
I used Scanner. Worked fine, very simple output. I know I am supposed to be learning Buffered Reader though. Couldn't get it to work using BufferedReader.
Je Level 19, Philippines
15 September 2020, 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 Till Level 7, San Jose, United States
24 July 2020, 23:22
5 is key
Mihai Bone Level 8, Bucharest, Romania
30 September 2020, 00:01
and %