Skip to main content
Code Review

Return to Answer

Challenge guarantees that the input digits are given in nondescending order; added 3 characters in body
Source Link
200_success
  • 145.6k
  • 22
  • 190
  • 479
  • 0,9,5,5,5,5,5,5,5,9Impossible (should be 09:55:55)
    2,0,2,5,5,5,5,5,5,5Impossible (should be 20:55:55)

    Due to tests like if hrs[0], if hrs[1], if mins[1], and if secs[1], hours, minutes, or seconds is a multiple of 10, or if the hour starts with 0.

  • 2,2,4,4,4,4,4,4,724:47:44 (should be 22:47:44)

    The challenge states that 24:00:00 is the upper bound. The correct answer should be, 22:47:44. Note that this means, demonstrates that a naïve greedy algorithm is not sufficient to solve this problem. You might optimistically select 24 for the hour (which would work for input such as 0,0,0,0,2,4,9,9,9). However, for the input 2,2,4,4,4,4,4,4,7, you should then find that there is no valid minute if you pick 24 as the hour, and you would need to backtrack and try 22 for the hour.

  • 0,9,5,5,5,5,5,5,5Impossible
    2,0,5,5,5,5,5,5,5Impossible

    Due to tests like if hrs[0], if hrs[1], if mins[1], and if secs[1], hours, minutes, or seconds is a multiple of 10, or if the hour starts with 0.

  • 2,2,4,4,4,4,4,4,724:47:44

    The challenge states that 24:00:00 is the upper bound. The correct answer should be 22:47:44. Note that this means that a naïve greedy algorithm is not sufficient to solve this problem. You might optimistically select 24 for the hour (which would work for input such as 0,0,0,0,2,4,9,9,9). However, for the input 2,2,4,4,4,4,4,4,7, you should then find that there is no valid minute if you pick 24 as the hour, and you would need to backtrack and try 22 for the hour.

  • 0,5,5,5,5,5,5,5,9Impossible (should be 09:55:55)
    0,2,5,5,5,5,5,5,5Impossible (should be 20:55:55)

    Due to tests like if hrs[0], if hrs[1], if mins[1], and if secs[1], hours, minutes, or seconds is a multiple of 10, or if the hour starts with 0.

  • 2,2,4,4,4,4,4,4,724:47:44 (should be 22:47:44)

    The challenge states that 24:00:00 is the upper bound. The correct answer, 22:47:44, demonstrates that a naïve greedy algorithm is not sufficient to solve this problem. You might optimistically select 24 for the hour (which would work for input such as 0,0,0,0,2,4,9,9,9). However, for the input 2,2,4,4,4,4,4,4,7, you should then find that there is no valid minute if you pick 24 as the hour, and you would need to backtrack and try 22 for the hour.

Added advice for beginners
Source Link
200_success
  • 145.6k
  • 22
  • 190
  • 479
  • 0,9,5,5,5,5,5,5,5Impossible
    2,0,5,5,5,5,5,5,5Impossible

    Due to tests like if hrs[0], if hrs[1], if mins[1], and if secs[1], hours, minutes, or seconds is a multiple of 10, or if the hour starts with 0.

  • 2,42,54,54,54,54,54,54,5724:5547:5544

    The challenge states that 24:00:00 is the upper bound. The correct answer should be 22:47:44. Note that this means that a naïve greedy algorithm is not sufficient to solve this problem. You might optimistically select 24 for the hour (which would work for input such as 0,0,0,0,2,4,9,9,9). However, for the input 2,2,4,4,4,4,4,4,7, you should then find that there is no valid minute if you pick 24 as the hour, and you would need to backtrack and try 22 for the hour.

Educational tips

The solution above uses some rather advanced techniques, and may be overwhelming for a beginner. However, I would point out an essential next step for improving your coding skills.

It is critical to learn to write functions, each with a clearly defined purpose, that accept parameters, return results, and use no global variables. Document the purpose, parameters, and outputs in a docstring, as I've done. That will force you to package your code into small, reusable chunks. For example, can you write a function that accepts a string (or list) of characters, and that returns a tuple with two items — the best possible hour that can be formed from those characters, and all the leftover characters that were not used to form the hour?

Defining a function is necessary to solve this problem correctly, because, as I've pointed out above, a correct solution requires some trial and error. If you are unable to reuse the code that performs the trials, then you will end up writing if-else statements to handle all of the failure cases, and it's going to be a huge mess!

  • 0,9,5,5,5,5,5,5,5Impossible
    2,0,5,5,5,5,5,5,5Impossible

    Due to tests like if hrs[0], if hrs[1], if mins[1], and if secs[1], hours, minutes, or seconds is a multiple of 10, or if the hour starts with 0.

  • 2,4,5,5,5,5,5,5,524:55:55

    The challenge states that 24:00:00 is the upper bound.

  • 0,9,5,5,5,5,5,5,5Impossible
    2,0,5,5,5,5,5,5,5Impossible

    Due to tests like if hrs[0], if hrs[1], if mins[1], and if secs[1], hours, minutes, or seconds is a multiple of 10, or if the hour starts with 0.

  • 2,2,4,4,4,4,4,4,724:47:44

    The challenge states that 24:00:00 is the upper bound. The correct answer should be 22:47:44. Note that this means that a naïve greedy algorithm is not sufficient to solve this problem. You might optimistically select 24 for the hour (which would work for input such as 0,0,0,0,2,4,9,9,9). However, for the input 2,2,4,4,4,4,4,4,7, you should then find that there is no valid minute if you pick 24 as the hour, and you would need to backtrack and try 22 for the hour.

Educational tips

The solution above uses some rather advanced techniques, and may be overwhelming for a beginner. However, I would point out an essential next step for improving your coding skills.

It is critical to learn to write functions, each with a clearly defined purpose, that accept parameters, return results, and use no global variables. Document the purpose, parameters, and outputs in a docstring, as I've done. That will force you to package your code into small, reusable chunks. For example, can you write a function that accepts a string (or list) of characters, and that returns a tuple with two items — the best possible hour that can be formed from those characters, and all the leftover characters that were not used to form the hour?

Defining a function is necessary to solve this problem correctly, because, as I've pointed out above, a correct solution requires some trial and error. If you are unable to reuse the code that performs the trials, then you will end up writing if-else statements to handle all of the failure cases, and it's going to be a huge mess!

added 40 characters in body
Source Link
200_success
  • 145.6k
  • 22
  • 190
  • 479
  • 0,9,5,5,5,5,5,5,5Impossible
    2,0,5,5,5,5,5,5,5Impossible

    Due to tests like if hrs[0], if hrs[1], if mins[1], and if secs[1], hours, minutes, or seconds is a multiple of 10, or if the hour starts with 0.

  • 2,4,5,5,5,5,5,5,524:55:55

    The challenge states that 24:00:00 is the upper bound.

  • 2,0,5,5,5,5,5,5,5Impossible

    Due to tests like if hrs[0], if hrs[1], if mins[1], and if secs[1], hours, minutes, or seconds is a multiple of 10, or if the hour starts with 0.

  • 2,4,5,5,5,5,5,5,524:55:55

    The challenge states that 24:00:00 is the upper bound.

  • 0,9,5,5,5,5,5,5,5Impossible
    2,0,5,5,5,5,5,5,5Impossible

    Due to tests like if hrs[0], if hrs[1], if mins[1], and if secs[1], hours, minutes, or seconds is a multiple of 10, or if the hour starts with 0.

  • 2,4,5,5,5,5,5,5,524:55:55

    The challenge states that 24:00:00 is the upper bound.

Source Link
200_success
  • 145.6k
  • 22
  • 190
  • 479
Loading
lang-py

AltStyle によって変換されたページ (->オリジナル) /