31
\$\begingroup\$

Shortest answer wins.

It must be sorted, and in 24hr time. The final line does not have a comma.

Output should be as follows:

'00:00',
'00:30',
'01:00',
'01:30',
'02:00',
'02:30',
'03:00',
'03:30',
'04:00',
'04:30',
'05:00',
'05:30',
'06:00',
'06:30',
'07:00',
'07:30',
'08:00',
'08:30',
'09:00',
'09:30',
'10:00',
'10:30',
'11:00',
'11:30',
'12:00',
'12:30',
'13:00',
'13:30',
'14:00',
'14:30',
'15:00',
'15:30',
'16:00',
'16:30',
'17:00',
'17:30',
'18:00',
'18:30',
'19:00',
'19:30',
'20:00',
'20:30',
'21:00',
'21:30',
'22:00',
'22:30',
'23:00',
'23:30'
\$\endgroup\$
  • 6
    \$\begingroup\$ Is this 24 hour time? Could you please provide the entire output? \$\endgroup\$ – xnor May 7 '15 at 8:21
  • 1
    \$\begingroup\$ Must the output be sorted? \$\endgroup\$ – orlp May 7 '15 at 8:30
  • 44
    \$\begingroup\$ What if it takes my program 23.5 hours to run? \$\endgroup\$ – tfitzger May 7 '15 at 13:59
  • 9
    \$\begingroup\$ I can't see why that would be a negative. \$\endgroup\$ – Espen Schulstad May 7 '15 at 14:00
  • 6
    \$\begingroup\$ Just a small suggestion about upvotes. I try to upvote every valid answer to a challenge I create as a small reward for taking the effort. I have plenty of rep and don't really need an upvote here, but please consider other answerers who may give up responding if their answers are ignored after lots of hard thinking and debugging. \$\endgroup\$ – Logic Knight May 16 '15 at 11:21

47 Answers 47

1
\$\begingroup\$

PHP 89 88 87 85 bytes

<?for($i=0;$i<24;$i++){$a=sprintf("%02d",$i);echo"'$a:00',\n'$a:30'",$i<23?",\n":'';}

I tried but it isn't the shortest in PHP.

Old code:

<?for($i=0;$i<24;$i++){($i<10?$a="0$i":$a=$i);echo"'$a:00',\n'$a:30'".($i==23?'':",\n");}
\$\endgroup\$
  • \$\begingroup\$ Why the parenthesis around the whole first ternary expression? And reverse the last ternary to use the shorter < operator instead of ==: $i<23?",\n":''. \$\endgroup\$ – manatwork May 8 '15 at 13:25
  • \$\begingroup\$ @manatwork So it adds the 0 in front of every number lower than 10, without the parenthesis it wouldn't work. With the < it would add a comma at the end \$\endgroup\$ – Timo May 8 '15 at 13:29
  • \$\begingroup\$ Works for me. But anyway, you are using the ternary in a strange way. Usually the assignment is done outside. And that expression could also be rewritten to compare against 9 instead of 10, sparing another 1 character: <?php for($i=0;$i<24;$i++){$a=$i>9?$i:"0$i";echo"'$a:00',\n'$a:30'".($i<23?",\n":'');} \$\endgroup\$ – manatwork May 8 '15 at 13:34
  • \$\begingroup\$ Ah yes, I had it the other way around. Thank you. \$\endgroup\$ – Timo May 8 '15 at 13:42
  • \$\begingroup\$ One more thing: the parenthesis around the last (now only) ternary is required just because you concatenate it to the other piece of string. As echo supports multiple parameters, just enumerate them separated with comma and you can spare the parentheses. \$\endgroup\$ – manatwork May 8 '15 at 13:43
1
\$\begingroup\$

PowerShell, 69 Bytes

$d=date 0;(0..47|%{"'{0:HH:mm}'"-f$d;$d=$d.AddMinutes(30)})-join",`n"

I love how it's shorter to use actual date functions than simply try to brute force the numbers with .ToString operators to pad zeroes and logic to decide if we're in a 00 or a 30 output.

Sets a new date $d to be 00:00:00 Jan 1 0000, then goes into a loop 48 times with 0..47|%{..}. Each time in the loop we generate our output using the -f operator. The HH:mm format for dates gives us 24-hour time with minutes, just as we want. Then, we addMinutes(30) to prep us for the next go-round. Outside the loop, we -join everything together with a comma and a newline.

\$\endgroup\$
1
\$\begingroup\$

Java, 93 bytes

void t(){for(int i=0;i<24;)System.out.printf("'%02d:00',\n'%02d:30'%s",i,i,i++<23?",\n":"");}

Ungolfed

void t()
{
    for(int i = 0; i < 24; i++)
    {
        System.out.printf("'%02d:00',\n", i);
        System.out.printf(   "'%02d:30'", i);
        System.out.printf( i < 23 ?",\n":"");
    }
}
\$\endgroup\$
1
\$\begingroup\$

TreeHugger, 1433 Bytes

TreeHugger is a bf variant that uses a binary tree instead of a linear tape. Read about it here and you can run scripts on the implementation here. As for how this program works, here is a rough run down:

  • Store the characters needed
    • In the first loop, get within 5 of the desired value
    • Manually adjust to the correct value
  • Walk through the tree and print the proper characters in the proper order
    • WARNING: This step is a little tedious
++++++++++[>++++++>+++++>+++++>++++>++++>+^^^^^<+++++>+++++^^^<++++>+++++>+++++^<+++++^^<+++++>+++++>++++++^<++++++^^<+++++>++++++^<+++++^^^^-]>-->+>-->->++++>^^^^^<+++>++^^^<->->+++^<++^^<-->+>---^<----^^<>-----^<++++^^^^<.<..^^>.>>.[.>]^^^^^^^^<.<..^^>[.>]^^^^^^^^<.<.^>.^^>.>>.[.>]^^^^^^^^<.<.^>.^^>[.>]^^^^^^^^<.<.<.^^^>.>>.[.>]^^^^^^^^<.<.<.^^^>[.>]^^^^^^^^<.<.>.^^^>.>>.[.>]^^^^^^^^<.<.>.^^^>[.>]^^^^^^^^<.<.^><.^^^>.>>.[.>]^^^^^^^^<.<.^><.^^^>[.>]^^^^^^^^<.<.^>>.^^^>.>>.[.>]^^^^^^^^<.<.^>>.^^^>[.>]^^^^^^^^<.<.<<.^^^^>.>>.[.>]^^^^^^^^<.<.<<.^^^^>[.>]^^^^^^^^<.<.<>.^^^^>.>>.[.>]^^^^^^^^<.<.<>.^^^^>[.>]^^^^^^^^<.<.><.^^^^>.>>.[.>]^^^^^^^^<.<.><.^^^^>[.>]^^^^^^^^<.<.>>.^^^^>.>>.[.>]^^^^^^^^<.<.>>.^^^^>[.>]^^^^^^^^<.>.^<.^^>.>>.[.>]^^^^^^^^<.>.^<.^^>[.>]^^^^^^^^<.>..^^>.>>.[.>]^^^^^^^^<.>..^^>[.>]^^^^^^^^<.>.^<<.^^^>.>>.[.>]^^^^^^^^<.>.^<<.^^^>[.>]^^^^^^^^<.>.^<>.^^^>.>>.[.>]^^^^^^^^<.>.^<>.^^^>[.>]^^^^^^^^<.>.<.^^^>.>>.[.>]^^^^^^^^<.>.<.^^^>[.>]^^^^^^^^<.>.>.^^^>.>>.[.>]^^^^^^^^<.>.>.^^^>[.>]^^^^^^^^<.>.^<<<.^^^^>.>>.[.>]^^^^^^^^<.>.^<<<.^^^^>[.>]^^^^^^^^<.>.^<<>.^^^^>.>>.[.>]^^^^^^^^<.>.^<<>.^^^^>[.>]^^^^^^^^<.>.^<><.^^^^>.>>.[.>]^^^^^^^^<.>.^<><.^^^^>[.>]^^^^^^^^<.>.^<>>.^^^^>.>>.[.>]^^^^^^^^<.>.^<>>.^^^^>[.>]^^^^^^^^<.<<.^.^^>.>>.[.>]^^^^^^^^<.<<.^.^^>[.>]^^^^^^^^<.<<.^^>.^^>.>>.[.>]^^^^^^^^<.<<.^^>.^^>[.>]^^^^^^^^<.<<..^^^>.>>.[.>]^^^^^^^^<.<<..^^^>[.>]^^^^^^^^<.<<.^>.^^^>.>>.[.>]^^^^^^^^<.<<.^>.^^^>.>.>.>.

Un-Golfed

The three digit numbers are the ASCII values we're aiming for in the approximation and adjustment phases. The numbers in the print phase, indicate the hour that we are printing.

# Approximation
+++++ +++++ [
058 > +++++ +
051 > +++++
048 > +++++
039 > ++++
044 > ++++
010 > +
^^^^^ 
053 < +++++ 
052 > +++++
^^^
039 < ++++
049 > +++++
053 > +++++
^
052 < +++++
^^
048 < +++++
051 > +++++
057 > +++++ +
^
056 < +++++ + 
^^
050 < +++++ 
055 > +++++ +
^
054 < +++++
^^^^ -
]
# Adjustment
058 > --
051 > +
048 > --
039 > -
044 > ++++
010 >
^^^^^ 
053 < +++ 
052 > ++
^^^
039 < -
049 > -
053 > +++
^
052 < ++
^^
048 < --
051 > +
057 > ---
^
056 < ----
^^
050 < 
055 > -----
^
054 < ++++ 
^^^^
# Print
0
<.<..^^     >.>>.[.>] ^^^^^ ^^^
<.<..^^     >[.>] ^^^^^ ^^^
1
<.<.^>.^^   >.>>.[.>] ^^^^^ ^^^
<.<.^>.^^   >[.>] ^^^^^ ^^^
2
<.<.<.^^^   >.>>.[.>] ^^^^^ ^^^
<.<.<.^^^   >[.>] ^^^^^ ^^^
3
<.<.>.^^^   >.>>.[.>] ^^^^^ ^^^
<.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] ^^^^^ ^^^
20
<.<<.^.^^      >.>>.[.>] ^^^^^ ^^^
<.<<.^.^^      >[.>] ^^^^^ ^^^
21
<.<<.^^>.^^  >.>>.[.>] ^^^^^ ^^^
<.<<.^^>.^^  >[.>] ^^^^^ ^^^
22
<.<<..^^^    >.>>.[.>] ^^^^^ ^^^
<.<<..^^^    >[.>] ^^^^^ ^^^
23
<.<<.^>.^^^    >.>>.[.>] ^^^^^ ^^^
<.<<.^>.^^^    >.>.>.>.

This can be golfed down more, I have a 1198 byte version that should work, but I think this implementation might have a bug. So stay tuned until I finish debugging my own implementation.

\$\endgroup\$
0
\$\begingroup\$

PHP, 82 77 bytes

Nothing fancy, just abusing PHP's string capacities.

<?for($H=-1;$H++<23;)echo$H?",
'":"'",$H=str_pad($H,2,0,0),":00',
'$H:30'";

Probably there's something that I can cut off.

Old version, 82 bytes:

<?foreach(range(0,23)as$H)echo$H?",
'":"'",$H=str_pad($H,2,0,0),":00',
'$H:30'";
\$\endgroup\$
0
\$\begingroup\$

JavaScript, 127 bytes

I'm not a very good golfer :s

var r='',i,m=":00',\n",q="'"
for(i=0;i<23;i++){
h=("0"+i).slice(-2)
r+=q+h+m+q+h+":30',\n"
}
r+=q+i+m+q+i+":30'"
console.log(r)

Java, 179 bytes

public class M{public static void main(String[] a){String r="",f="'%02d:%s',\n";for(int i=0;i<24;i++)r+=String.format(f+f,i,"00",i,"30");System.out.print(r.replaceAll(".$",""));}}

ungolfed

public class M {
    public static void main(final String[] a) {
        String r = "", f = "'%02d:%s',\n";
        for (int i = 0; i < 24; i++)r += String.format(f + f, i, "00", i, "30");
        System.out.print(r.replaceAll(".$", ""));
    }
}
\$\endgroup\$
  • \$\begingroup\$ don't be that modest! :) \$\endgroup\$ – Espen Schulstad May 7 '15 at 11:20
  • \$\begingroup\$ I'm not super good with javascript but I made a couple of improvements to bring it down to 104 bytes :) I removed some whitespace and moved the 23:** hour into the loop. This means you don't need the r+=q+i+m+q+i+":30'" line, but there is an extra comma, so you have to cut it off when you print it. That lets you save a lot of bytes, both from the removed line and variable initialization. You also might want to look into making this a 0-arg function, so you can use ES6 function syntax. There's a bit of boilerplate code you have to add, but usually returning > printing. \$\endgroup\$ – undergroundmonorail May 7 '15 at 13:13
0
\$\begingroup\$

C# - lots (but give away free with windows)

using System;
using System.Collections.Generic;
using System.Linq;
class P
{
    static void Main()
    {
        Console.Write(String.Join(",\n", Enumerable.Range(0, 48).Select(i => "'" + i / 2 + ":" + (i % 2) * 3 + "0'")));
    }
}
\$\endgroup\$
0
\$\begingroup\$

C++ - 104 bytes

I spend a while trying to find an obtuse way to remove the comma, but in the end the simple approach seemed to be by far the most efficient.

#include<cstdio>
int main(int c,char**v){for(c=-1;c++<47;printf("'%.2d:%d0'%s\n",c/2,c%2*3,c>46?"":","));}

I shouldn't need to initialize c to -1 but I couldn't get an inline postfix increment to work how I expected.

\$\endgroup\$
  • \$\begingroup\$ You can reduce one byte by #include<cstdio>. Also, one byte can go from the c=-1 by changing it to c=0 and moving the increment inside the printf like for(c=0;48>c;printf("'%.2d:%d0'%s\n",c/2,c%2*3,c++>46?"":","));. Note that with this, the actual output may depend on the compiler, the processor, etc, because of the unsequenced modification of c, and the compiler will give a warning accordingly. \$\endgroup\$ – rorlork May 8 '15 at 8:36
  • \$\begingroup\$ @rcrmn Thanks! Unfortunately, that for loop is what I had initially, but it doesn't behave as I would expect on MSVC++. Your code gives me 00:30 to 24:00, which indicates right-to-left argument evaluation, but if I move the ++ into c/2 I get the 00s and 30s swapped, which would indicate left-to-right evaluation. My guess is the compiler is attempting to optimize by doing c++/2 first and using the result to calculate c%2. \$\endgroup\$ – BMac May 8 '15 at 16:25
  • \$\begingroup\$ Oh I see. Using Apple LLVM version 6.0 (clang-600.0.57) here. I thought I was using gnu g++, but it seems apple likes to hide these things to the user. \$\endgroup\$ – rorlork May 8 '15 at 16:27
0
\$\begingroup\$

PostgreSQL: 97 characters

select string_agg(to_char(x*interval'30m','''hh24:mi'''),','||chr(10))from generate_series(0,47)x

PostgreSQL: 132 131 116 characters

Kept here to show PostgreSQL's cool way to generate timestamp series.

select string_agg(to_char(x,'''hh24:mi'''),','||chr(10))from generate_series('150511','150511 23:59',interval'.5h')x
\$\endgroup\$
0
\$\begingroup\$

Python, 145 bytes

import time as t;i=48;a=t.asctime;w=3600;s=t.sleep;s(24-int(a()[11:13])*w+w-60*int(a()[14:16]));
while i:print '\''+a()[11:16]+'\',';i-=1;s(w)

I guess the code is fairly readable (altough quite long). Once you run the program it waits until actual local time is 00:00h, then prints the time and waits for 30 min, time in which the program prints the hour again. It keeps repeating this process until 00:00h of the next day is reached. Therefore, with lots of patience and no incidents that force your computer to shutdown all the times get printed.

\$\endgroup\$
0
\$\begingroup\$

JavaScript (ES6), 81 77 bytes

Template strings work well for cutting down the size.

alert([...Array(48)].map((x,i)=>`'${i<20?0:''}${i/2|0}:${i%2*3}0'`).join`,
`)
\$\endgroup\$
  • \$\begingroup\$ Usually we call such code ECMAScript to emphasize the presence of experimental features. By the way, your code generates no output. (In JavaScript Standards for IO meta discussion we concluded that JavaScript should also use explicit output methods when output is requested.) \$\endgroup\$ – manatwork May 7 '15 at 19:51
  • \$\begingroup\$ @manatwork Thanks for that link! I figured there would be something like that, but couldn't find anything, and having seen other examples elsewhere on the site without explicit prints, I just went with it. I'll update the answer. Good to know on the ECMAScript detail as well! =) \$\endgroup\$ – Mwr247 May 7 '15 at 20:01
  • \$\begingroup\$ I don't think this works: es6fiddle.net/i9essmvf \$\endgroup\$ – royhowie May 7 '15 at 23:21
  • \$\begingroup\$ @royhowie For some reason, es6fiddle requires variables be declared with var (add it before the i=0 and you'll see it works). While usually a good idea to have, it is not required. \$\endgroup\$ – Mwr247 May 8 '15 at 5:46
  • \$\begingroup\$ It certainly works as is in Firefox. And 1 more character can be spared thanks to the template strings: instead of \n in join()'s parameter, make that string also a template string and include a literal newline. \$\endgroup\$ – manatwork May 8 '15 at 7:37
0
\$\begingroup\$

Ceylon, 101 bytes

shared void run(){print(",\n".join{for(h in 0..23)for(m in[0,3])"'``"``h``".pad(2,'0')``:``m``0'"});}

Formatted:

shared void run() {
    // original:
    print(",\n".join {
            for (h in 0..23)
                for (m in [0, 3])
                    "'``"``h``".pad(2, '0')``:``m``0'"
        });
}

This uses an iterable comprehension with two for-clauses, and nested string templates. "``h``" is a shorter way of writing h.string (we can't pad an integer directly). ",\n".join(iterable) puts the commas and new-lines between the strings (this way we can avoid the , in the last line).

Unfortunately Ceylon has no printf functionality, which would certainly make things shorter (no .pad or .string necessary).

After finding that "straightforward" solution, I experimented with some variations, which unfortunately result in slightly longer shrinked solutions:

shared void run() {
    print(",\n".join(
            (0..23)
                *.string
                *.pad(2, '0')
                .map((x) => "'``x``:00',\n'``x``:30'")
        ));
}

(102 bytes)

shared void run() {
    print(",\n".join {
            for (h in 0..23)
                let (x = "``h``".pad(2, '0'))
                    "'``x``:00',\n'``x``:30'"
        });
}

(104 bytes)

shared void run() {
    print(",\n".join(
            (0..23)
                .map((i) => "``i``".pad(2, '0'))
                .map((x) => "'``x``:00',\n'``x``:30'")));
}

(111 bytes)

\$\endgroup\$
0
\$\begingroup\$

Burlesque, 30 bytes

Burlesque is sadly rather weak when it comes to formatting because it doesn't have a printf (yet).

23rzm{2'0lp}":00 :30"wdcp)\[uN

Details see here.

\$\endgroup\$
0
\$\begingroup\$

Pyth, 24 bytes

V24V2%"'%02d:%d0',",N*3H

Run code

V24                     for loop 0-23, N counter
  V2                    for loop 0-1, H counter
    %                   format string
      "'%02d:%d0',"     source string
      ,                 begin list tuple
        N               first element, hour
        *3H             second element, minutes

Every second iteration of the second for loop, H is 1, which toggles the minutes digit between 0 and 3.

Interpreted code generated by the debugger:

for N in num_to_range(24):
 for H in num_to_range(2):
  imp_print(mod("'%02d:%d0',",[N,times(3,H)]))
\$\endgroup\$
0
\$\begingroup\$

PHP, 104 96 bytes

for($h=$m=0;$h<24;){echo($h||$m?",\n":"")."'".($h<10?0:"")."$h:".($m*3)."0'";if($m)$h++;$m=!$m;}
exploded view
for ($h = $m = 0; $h < 24; ) {
  echo ($h || $m ? ",\n" : "") . "'" . // echoes ",\n" on all but the first loop
       ($h < 10 ? 0 : "") . "$h:" .    // leading 0 for 0:00 - 9:30
       ($m * 3) . "0'";                // :00 if $m=0, :30 if $m=1
  if ($m) $h++;                        // increments $h if :30
  $m = !$m;                            // swaps :00 <-> :30
}
\$\endgroup\$
0
\$\begingroup\$

Java 7, 88 bytes

void c(){for(int i=0;i<48;)System.out.printf("'%02d:%s0'%s",i/2,i%2*3,i++<47?",\n":"");}

Shorter than the other three Java answers: 93 bytes & 94 bytes & 119 bytes

Explanation:

void c(){                  // Method
  for(int i=0;i<48;)       //  Loop from 0 to 48 (exclusive)
    System.out.printf(     //   Print with format:
      "'%02d:%s0'%s",      //    "'##:#0'X" (where `#` is a digit, and `X` is a String)
      i/2,                 //    (%02d) Current index divided by 2 and automatically floored
      i%2 * 3              //    (%s0) i%2 is either 0 or 1 (multiplied by 3 is either 0 or 3)
      i++<47 ? ",\n" : ""  //    (%s) A comma and new-line, unless it's the final line
                           //  End of loop
}                          // End of method

Try it here.

\$\endgroup\$
0
\$\begingroup\$

AHK, 96 bytes

t=20000101000000
Loop,48{
FormatTime,i,%t%,HH:mm
Send,'%i%',`n
EnvAdd,t,30,Minutes
}
Send,{BS 2}

AHK does not play nicely with time values so I have to store two variables: one to track the time in the format YYYYMMDDHH24MISS and another just to store the pretty text in the format HH:MI.

\$\endgroup\$

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.