# List all times in the day at a half hour rate

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'

• Is this 24 hour time? Could you please provide the entire output? – xnor May 7 '15 at 8:21
• Must the output be sorted? – orlp May 7 '15 at 8:30
• What if it takes my program 23.5 hours to run? – tfitzger May 7 '15 at 13:59
• I can't see why that would be a negative. – Espen Schulstad May 7 '15 at 14:00
• 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. – Logic Knight May 16 '15 at 11:21

# Pyth, 26 bytes

Pjbm%"'%02d:%s0',"d*U24"03


Demonstration.

We start with the cartesian product of range(24) (U24) with the string "03".

Then, we map these values to the appropriate string formating substitution (m%"'%02d:%s0',"d).

Then, the resultant strings are joined on the newline character (jb).

Finally, we remove the trailing comma (P) and print.

• Looks like we have a new contender for the prize. Author of pyth, that's almost cheating ;) – Espen Schulstad May 7 '15 at 18:54

# Befunge-93, 63 bytes

0>"'",:2/:55+/.55+%.":",:2%3*v
^,+55,","<_@#-*86:+1,"'".0. <


# Bash: 5847 46 characters

h=echo \'{00..23}:{0,3}0\'
echo "${h// /, }"  • I like this solution :) – Espen Schulstad May 7 '15 at 16:35 • Anonymous user suggested echo \'{00..23}:{0,3}0\'|sed 's/ /,\n/g' of 40 characters. Nice. Thanks. But I prefer to make use of bash's own strength. – manatwork May 8 '15 at 9:58 • printf "'%s',\n" {00..23}:{0,3}0 – izabera May 9 '15 at 12:22 • @manatwork oh i didn't read the question well, sorry... – izabera May 9 '15 at 12:27 • printf "'%s'\n" {00..23}:{0,3}0|sed$\!s/$/,/ is 45 bytes – izabera May 9 '15 at 12:31 # CJam, 31 30 29 bytes 24,[U3]m*"'%02d:%d0', "fe%~7<  This is pretty straight forward using printf formatting: 24, e# get the array 0..23 [U3] e# put array [0 3] on stack m* e# do a cartesian product between 0..23 and [0 3] array e# now we have tuples like [[0 0], [0 3] ... ] etc "'%02d:%d0', "fe% e# this is standard printf formatting. What we do here is e# is that we format each tuple on this string ~7< e# unwrap and remove comma and new line from last line e# by taking only first 7 characters  Try it online here • Looks like this is going to take the gold. We'll give it until tomorrow ;) – Espen Schulstad May 7 '15 at 16:38 • @EspenSchulstad I wish it would actually give me some gold. sigh – Optimizer May 7 '15 at 16:39 • Remember, it's not only viritual gold, but also honor. What we do in life echoes in eternity. – Espen Schulstad May 7 '15 at 16:40 # Python 2, 58 56 bytes for i in range(48):print"'%02d:%s0',"[:57-i]%(i/2,i%2*3)  Like sentiao's answer but using a for loop, with slicing to remove the comma. Thanks to @grc for knocking off two bytes. • Would this work: "'%02d:%s0',"[:57-i] – grc May 7 '15 at 14:16 • @grc Ahaha of course, that's much better – Sp3000 May 7 '15 at 15:03 • Better than I could do! – Tim May 7 '15 at 18:15 # Java - 119 bytes I started with Java 8's StringJoiner, but that means including an import statement, so I decided to do it the old way: void f(){String s="";for(int i=0;i<24;)s+=s.format(",\n'%02d:00',\n'%02d:30'",i,i++);System.out.print(s.substring(2));}  Perhaps this can be improved by getting rid of the multiple occuring String and System keywords. • a version in groovy perhaps :) – Espen Schulstad May 7 '15 at 10:24 • You can shorten this to 159: Remove the space before a, move the i increment, lose the for braces, and move the comma/newline to the beginning (which lets you use the shorter substring and get rid of length()). Since functions are allowed by default, you can get make it even shorter by eliminating boilerplate: void f(){String s="";for(int i=0;i<24;)s+=String.format(",\n'%02d:00',\n'%02d:30'",i,i++);System.out.print(s.substring(2));} Even a bit more if you make it just return the string instead of printing it, but that seems to go against the spirit, if not the letter. – Geobits May 7 '15 at 13:00 • Damn, that's brilliant! I got to keep those tricks in mind, thanks! – Sander May 7 '15 at 13:53 • Oh, another few: change String.format to s.format. Your compiler/IDE may complain about it, but it works ;) – Geobits May 7 '15 at 13:55 • Nice one! format's a static method, so it should be accessed by its class, but indeed, using it this way also works! – Sander May 8 '15 at 6:18 # Ruby, 946156 51 $><<(0..47).map{|i|"'%02d:%02d'"%[i/2,i%2*30]}*",
"


Thanks to @blutorange (again) for his help in golfing!

• You can reduce that to 61 bytes: puts (0..47).to_a.map{|h|"'%02d:%02d'"%[h/2,h%2*30]}.join"," (there's a newline after the last comma) – blutorange May 7 '15 at 14:37
• Thank you again! I really didn't try it much... – rorlork May 7 '15 at 14:42
• Ruby needs some love. 58 bytes ;) puts Array.new(48){|i|"'%02d:%02d'"%[i/2,i%2*30]}.join',' – blutorange May 7 '15 at 16:57
• @blutorange @rcrmn you can reduce 4 more bytes (and get to my solution below :)) ) by replacing .join with * :) – Tomáš Dundáček May 8 '15 at 5:54

# Perl, 525048 45B

$,=", ";say map<\\'$_:{0,3}0\\'>,"00".."23"


With help from ThisSuitIsBlackNot :)

## JAVA 95 94 bytes

I love the fact that printf exists in Java:

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


Ungolfed

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


EDIT Replaced the ',' with 44

• 24.times{printf("'%02d:00',\n'%02d:30'%c\n",it,it,(it<24)?44:0)}​ in groovy :) same solution, just that it's down to 65 chr – Espen Schulstad May 7 '15 at 16:24
• @EspenSchulstad Would that be considered a standalone function, or is there other boilerplate that would be needed to get it to run? – tfitzger May 7 '15 at 16:28
• If you have groovy, you could either run that in groovysh or just as a groovy script in a .groovy-file. then you don't need no function. – Espen Schulstad May 7 '15 at 16:30
• groovyconsole.appspot.com/edit/24001 – Espen Schulstad May 7 '15 at 16:33
• @EspenSchulstad Interesting. I've only done minimal work with Groovy. – tfitzger May 7 '15 at 16:56

# Pyth, 32 31 bytes

I golfed something in python but it turned out to be exactly the same as Sp3000's answer. So I decided to give Pyth a try:

V48<%"'%02d:%d0',",/N2*3%N2-54N


It's a exact translation of Sp3000 answer:

for i in range(48):print"'%02d:%d0',"[:57-i]%(i/2,i%2*3)


It's my first go at Pyth, so please do enlighten me about that 1 byte saving.

• Nicely done, and welcome to Pyth. – isaacg May 7 '15 at 20:25

# PHP, 109 bytes

foreach(new DatePeriod("R47/2015-05-07T00:00:00Z/PT30M")as$d)$a[]=$d->format("'H:i'");echo implode(",\n",$a);


# Ruby, 54 51 bytes

puts (0..23).map{|h|"'#{h}:00',
'#{h}:30'"}.join",
"

• You can reduce 3 bytes by changing \n to actual newlines and removing the space between join and ". On the other hand, take note that the specified output has leading zeros for the hours. – rorlork May 7 '15 at 16:13

~@n=0,47w(S.q("'%02d:%d0'%c",n/2,n%2*3,n<47&44|0))$ Using ascii 44 = , and 0 a space saves a character. An ungolfed Lua version would be for h=0,47 do print(string.format("'%02d:%d0'%c",h/2,h%2*3, if h<47 and 44 or 0)) end  The if statement is much like the ternary operator a > b ? 44 : 0. # C# - 120 bytes class P{static void Main(){for(var i=0;i<24;i++)System.Console.Write("'{0:00}:00',\n'{0:00}:30'{1}\n",i,i==23?"":",");}}  # Python, 60 58 64 bytes for i in range(24):print("'%02d:00,\n%02d:30'"%(i,i)+', '[i>22])  Ungolfed: for i in range(24): if i <23: print( ('0'+str(i))[-2:] + ':00,\n' + str(i) + ':30,') else: print( ('0'+str(i))[-2:] + ':00,\n' + str(i) + ':30')  Try it online here. • Why not put it on 1 line, and save another 2 bytes. – isaacg May 7 '15 at 18:15 • I don't think this works - no leading zero on the hour. – isaacg May 7 '15 at 18:17 • @isaacg fixed that! – Tim May 7 '15 at 18:25 • The output is not the same as in the original question. – sentiao May 8 '15 at 8:51 • @sentiao what is different? – Tim May 8 '15 at 11:41 # 𝔼𝕊𝕄𝕚𝕟, 39 chars / 67 bytes (non-competing) ↺;Ḁ⧺<Ḱ;)ᵖ'⦃Ḁ<Ḕ?0:⬯}⦃0|Ḁ/2}:⦃Ḁ%2*3}0',”  Try it here (Firefox only). Not a single alphabetical character in sight... # PHP, 69 70 62 bytes for($x=-1;++$x<47;)printf("'%02d:%d0', ",$x/2,$x%2*3)?>'23:30'  Try it online Outputting '23:30' at the end is a bit lame, and so is closing the php context using ?> without opening or re-opening it. An cleaner alternative (but 65 bytes) would be: for($x=-1;++$x<48;)printf("%s'%02d:%d0'",$x?",
":'',$x/2,$x%2*3);


Try it online

Thank you @Dennis for the tips. Alternative inspired by the contribution of @ismael-miguel.

• Welcome to Programming Puzzles & Code Golf! Your code prints a null byte at the end. I'm not sure if that is allowed. – Dennis Jan 11 '16 at 5:44
• @Dennis Thank you, you're right... I assumed PHP would see it as end-of-string. Posted a new version. – mk8374876 Jan 11 '16 at 18:43
• <?...?>'23:30' saves three bytes. Also, you can replace \n with an actual newline. – Dennis Jan 11 '16 at 19:07

# Swift, 74 bytes

Updated for Swift 2/3...and with new string interpolation...

for x in 0...47{print("'\(x<20 ?"0":"")\(x/2):\(x%2*3)0'\(x<47 ?",":"")")}

• The final line is specified to not have a comma & your code prints it. – Kyle Kanos May 7 '15 at 13:32

# Javascript, 89 bytes

for(i=a=[];i<24;)a.push((x="'"+("0"+i++).slice(-2))+":00'",x+":30'");alert(a.join(",\n"))

• Quick question: how many arguments Array.push() supports? ;) – manatwork May 7 '15 at 14:31
• You're right. That is a polyad. Thanks, I'll make that change after I finish testing my entry for another challenge. – SuperJedi224 May 7 '15 at 15:09
• A few more characters can be removed with some elementary reorganizations: for(i=a=[];i<24;)a.push((x=("0"+i++).slice(-2))+":00",x+":30");alert(a.join(",\n")) – manatwork May 7 '15 at 15:28
• There, I fixed it. – SuperJedi224 May 7 '15 at 15:38
• That reuse of variable x is quite ugly coding habit. If you change the loop control variable to something else (as in my suggestion at 2015-05-07 15:28:25Z), then you can add the opening single quotes to x's value to reduce the two "'"+ pieces to one: for(i=a=[];i<24;)a.push((x="'"+("0"+i++).slice(-2))+":00'",x+":30'");alert(a.join(",\n")) – manatwork May 7 '15 at 15:58

## Python 2: 64 bytes

print ',\n'.join(['%02d:00,\n%02d:30'%(h,h) for h in range(24)])

• The output is not the same as in the original question. – sentiao May 8 '15 at 8:51
• I don't know what happened to the edit I made. here is the correct version also 64 chars: print',\n'.join("'%02d:00',\n'%02d:30'"%(h,h)for h in range(24)) – Alan Hoover May 8 '15 at 14:04

Ruby - 52 bytes

puts (0..47).map{|i|"'%02d:%02d'"%[i/2,i%2*30]}*",
"

• This seems like my solution with just the change of the .join for *... It's common courtesy to instead of just posting a new answer with a minor improvement, to suggest the improvement to the original poster. See meta.codegolf.stackexchange.com/questions/75/… – rorlork May 8 '15 at 8:13
• @rcrmn I agree I made a mistake and I apologize for it - should've had looked for other ruby answers first. Great work though in your answer with \$><<! – Tomáš Dundáček May 8 '15 at 8:45
• Don't worry about it, you are new here: I was advising you so that you could avoid this in the future. – rorlork May 8 '15 at 8:55

# Python 2, 74 65 bytes

We generate a 2 line string for each hour, using text formatting:

print',\n'.join("'%02u:00',\n'%02u:30'"%(h,h)for h in range(24))


This code is fairly clear, but the clever indexing and integer maths in the answer by Sp3000 gives a shorter solution.

• no reason to use formatting for 00 and 30, save 9 bytes with "'%02u:00',\n'%02u:30'"%(h,h) – DenDenDo May 7 '15 at 10:42
• you could save some bytes by having too loops : print',\n'.join("'%02u:%02u'"%(h,i)for h in range(24)for i in[0,30])` – dieter May 7 '15 at 11:01
• Thanks DenDenDo. I used this to save some bytes. Dieter, I can see where your idea may help, but I could save more bytes with that idea from DenDenDo. – Logic Knight May 8 '15 at 11:25