29
\$\begingroup\$

A time in the format hhMMss is represented by six numbers in the range 0..9 (e.g.100203 for 3 seconds after 2 minutes after 10am (10:02.03), or 155603 for three seconds after 56 minutes after 3pm (15:56.03).

Treating these times as integers, these numbers are therefore in the range 000000 to 235959; but not all numbers in that range are valid times.

Normally, though, integers aren't represented with leading 0s, right?

So, this challenge is to take a numeric input (without leading 0s), and say whether it represents a proper time or not when the leading 0s are put back.

Input

Any integer, as a string or an integer type, in the range 0..235959 inclusive. all numbers as strings will be input with no leading 0s (e.g. 2400, not 002400). The time 000000 maps to 0; or exceptionally as . Inputs outside of this range should return Falsy, but there is no requirement that they are supported.

Output

Truthy/Falsy value - by which I mean there must be a consistent distinction in the output between True and False - e.g. True could be output as 1 and False could be any other output (or even a variable output) - as long as it can be documented how to tell what is True and what is not.

More Challenge Details

Given the input integer, figure out if the number represents a time (Truthy) or not (Falsy).

A number represents a time if a time (hhMMss) with leading 0s removed is the same as the number.

e.g. 00:00.24 is represented by 24
e.g. 00:06.51 is represented by 651
e.g. 00:16.06 is represented by 1606
e.g. 05:24.00 is represented by 52400
e.g. 17:25.33 is represented by 172533

There are therefore some numbers that can't represent times:

e.g. 7520 - this can't represent hhMMss because 00:75:20 isn't a time

As a general rule, the valid numbers fall into the pattern:

trimLeadingZeros([00..23][00..59][00..59]);

The following numbers are the entire set of inputs and the required answers for this challenge

Seconds only (e.g. 00:00.ss, with punctuation and leading 0s removed, -> ss)
0 to 59 - Truthy
60 to 99 - Falsy

Minutes and seconds (e.g. 00:MM.ss, with punctuation and leading zeros removed, -> MMss)
100 to 159 - Truthy
160 to 199 - Falsy
etc, up to:
2300 to 2359 - Truthy
2360 to 2399 - Falsy
2400 to 2459 - Truthy
2460 to 2499 - Falsy
etc, up to:
5900 to 5959 - Truthy
5960 to 9999 - Falsy

Hours 0..9, minutes and seconds (e.g. 0h:MM.ss with punctuation and leading zeros removed -> hMMss)

10000 to 10059 - Truthy
10060 to 10099 - Falsy
etc, up to:
15800 to 15859 - Truthy
15860 to 15899 - Falsy
15900 to 15959 - Truthy
15960 to 19999 - Falsy

20000 to 20059 - Truthy
20060 to 20099 - Falsy
20100 to 20159 - Truthy
20160 to 20199 - Falsy
etc, up to:
25800 to 25859 - Truthy
25860 to 25899 - Falsy
25900 to 25959 - Truthy
25960 to 25999 - Falsy
etc, up to:
95800 to 95859 - Truthy
95860 to 95899 - Falsy
95900 to 95959 - Truthy
95960 to 99999 - Falsy

Hours 10..23, minutes and seconds (e.g. hh:MM.ss with punctuation and leading zeros removed -> hhMMss)

100000 to 100059 - Truthy
100060 to 100099 - Falsy
100100 to 100159 - Truthy
100160 to 100199 - Falsy
etc, up to:
105800 to 105859 - Truthy
105860 to 105899 - Falsy
105900 to 105959 - Truthy
105960 to 109999 - Falsy

This pattern is then repeated up to:

235900 to 235959 - Truthy
(236000 onwards - Falsy, if supported by program)

Leading 0s must be truncated in the input, if strings are used.

Code golf, so least bytes wins - usual rules apply.

\$\endgroup\$
  • 2
    \$\begingroup\$ I just can't find the left-pad built-in ... \$\endgroup\$ – user92069 Feb 25 at 14:40
  • \$\begingroup\$ Related (the portion about verifying whether a 6-digit number is a valid time) \$\endgroup\$ – Kevin Cruijssen Feb 25 at 15:04
  • 5
    \$\begingroup\$ You say input is in the range 0..235959 inclusive, but then you have a test case for 236000 onwards. You should clarify what's the actual range of inputs we need to support (I'd suggest 0..999999). \$\endgroup\$ – Grimmy Feb 25 at 15:05
  • 9
    \$\begingroup\$ It's probably too late now, but I think this challenge would be more fun if the range could be beyond the 235959 and possibly even beyond the 999999. Now it simply boils down to (in pseudo-code) max(hh,mm,ss)<60 and we can just ignore the check on hours, since it's guaranteed to be valid. \$\endgroup\$ – Kevin Cruijssen Feb 25 at 16:42
  • 3
    \$\begingroup\$ This is assuming a day with no leap second, or possibly TAI. For example, 2016-12-31 18:59:60 US/Eastern is a valid time. \$\endgroup\$ – aschepler Feb 26 at 6:37

41 Answers 41

1
2
1
\$\begingroup\$

JavaScript (Babel Node), 71 52 bytes

t=i=>{[,,m,,s]=(i+'').padStart(6,0);return m<6&&s<6}

Try it online!

| improve this answer | |
\$\endgroup\$
1
\$\begingroup\$

C (gcc), 30 bytes

f(n){n=n%100<60&n/100%100<60;}

Doesn't check hours.

Try it online!

C (gcc), 39 bytes

f(n){n=n%100<60&n/100%100<60&n/1e4<24;}

Checks hours.

Try it online!

| improve this answer | |
\$\endgroup\$
1
\$\begingroup\$

Python 3, 90 85 bytes

lambda t:{t}&s
s={0}
import time
while len(s)<86400:s|={int(time.strftime("%H%M%S"))}

Try it online!

The idea is to simply create a set of all valid time (as int), then check if the given number is in the set. The set is created by repeatedly adding the current time to the set until all 86400 distinct values are added. Returns: non-empty set if t is valid time, otherwise empty set.

| improve this answer | |
\$\endgroup\$
1
\$\begingroup\$

JavaScript (ES6),  27 26  21 bytes

Takes input as an integer in hhmmss format. Returns \$0\$ or \$1\$.

n=>n%100<60&n%1e4<6e3

Try it online!

How?

n=>n%100<60&n%10000<6000

is just as long as:

n=>n%100<60&n/100%100<60

But from there we can use the scientific notation to save 3 bytes:

n%10000<6000
n%1e4<6e3
| improve this answer | |
\$\endgroup\$
1
\$\begingroup\$

Jelly, 7 bytes

ṚHÐoṀ<6

Try it online!

How?

ṚHÐoṀ<6 - Link: integer, n                 e.g.  236059
Ṛ       - (implicit digits of n) reversed        [9  , 5  , 0  , 6  , 3  , 2  ]
  Ðo    - apply to odd indices:
 H      -   halve                                [4.5, 5  , 0  , 6  , 1.5, 2  ]
    Ṁ   - maximum                                6
      6 - six                                    6
     <  - less than?                             0

Alternative 7:

ṚḊm2Ṁ<6 - reverse, dequeue, modulo-2-slice, maximum, less than 6?
| improve this answer | |
\$\endgroup\$
1
\$\begingroup\$

MUMPS, 74 70 64 40 bytes

t(t) s l=$l(t)-5 f f=l:2:7 q:$e(t,f,f+1)>$s(f-l:59,1:23)
 q f>5

Ah, I didn't consider that you only need to handle values up to 239999. That drops it from 64 to 40.

t(t) f f=$l(t)-3:2:7 q:$e(t,f)>5
 q f>5
| improve this answer | |
\$\endgroup\$
1
\$\begingroup\$

PowerShell, 56 bytes

param($a)!(1-in(0..2|%{$a%100-ge60;$a/=100})-or$a-ge.24)

Try it online.

| improve this answer | |
\$\endgroup\$
1
\$\begingroup\$

ArnoldC, 676 bytes

IT'S SHOWTIME
HEY CHRISTMAS TREE n
YOU SET US UP 0
GET YOUR ASS TO MARS n
DO IT NOW
I WANT TO ASK YOU A BUNCH OF QUESTIONS AND I WANT TO HAVE THEM ANSWERED IMMEDIATELY
HEY CHRISTMAS TREE s
YOU SET US UP 0
GET TO THE CHOPPER s
HERE IS MY INVITATION n
I LET HIM GO 100
ENOUGH TALK
HEY CHRISTMAS TREE m
YOU SET US UP 0
GET TO THE CHOPPER m
HERE IS MY INVITATION n
HE HAD TO SPLIT 100
I LET HIM GO 100
ENOUGH TALK
HEY CHRISTMAS TREE o
YOU SET US UP 0
GET TO THE CHOPPER o
HERE IS MY INVITATION 60
LET OFF SOME STEAM BENNET m
ENOUGH TALK
GET TO THE CHOPPER o
HERE IS MY INVITATION 60
LET OFF SOME STEAM BENNET s
KNOCK KNOCK o
ENOUGH TALK
TALK TO THE HAND o
YOU HAVE BEEN TERMINATED

Try it online!

| improve this answer | |
\$\endgroup\$
1
\$\begingroup\$

[JavaScript (Node.js)], 26 bytes

f=>!/[6-9].(..)?$/.test(f)

Simple Javascript Regular Expression

JavaScript (Node.js) – Try It Online

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ You've got a stray space after the >, I think you can use . instead of \d, and you can probably simplify the condition, e.g. by using test. \$\endgroup\$ – Neil Mar 2 at 10:54
  • \$\begingroup\$ Thanks @Neil, I used test, but changed the logic. \$\endgroup\$ – kanine Mar 2 at 17:24
  • \$\begingroup\$ You don't use recursive so you can omit t= \$\endgroup\$ – l4m2 Apr 22 at 9:40
0
\$\begingroup\$

Perl 5, 42 39 +1 (-p) = 40 bytes

$_=0|!($_%100>59|$_/1e4%10>5|$_/1e5>23)

Saved 3 bytes by changing to exponent notation (1000 => 1e4)

Try it online!

| improve this answer | |
\$\endgroup\$
0
\$\begingroup\$

05AB1E, 6 bytes

твà60‹

Only works for the range \$[0, 235959]\$.

Try it online or verify all test cases.

Explanation:

тв      # Convert the (implicit) input-integer to base-100 as list
  à     # Pop and push the maximum
   60‹  # Check if it's smaller than 60
        # (after which the result is output implicitly)
| improve this answer | |
\$\endgroup\$
1
2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.