6
\$\begingroup\$

Introduction

enter image description hereToday is my birthday,

Yes my parents spend a lot time at home during the cold December holidays as many others.

So I also want my cake.

But I have a problem: I just sent the invitations today so I'm not sure how many friends will come. Also I don't want to order a big cake because I have been on a diet since last year and don't want to keep temptation leftovers in the fridge.

Challenge

Write a full program to output the cake design so my baker prepares just enough cake for everyone.

Input Integer 1 <= n <= 999. Worst case I eat my cake alone.

  • Each slice will have a unique label number 1 .. n sequenced left to right, then top to bottom.
  • Write in the middle line and align center the best way you can.
  • Slice size is a square of 3x3 (this is why upper limit is 999).
  • * indicate where to cut each slice.

Output

n = 1 => Side = RoundUP(sqrt(1)) = 1

*****
*   *
* 1 *
*   *
*****

n = 3 => Side = RoundUP(sqrt(3)) = 2

*********
*   *   *
* 1 * 2 *
*   *   *
*********
*   *
* 3 *
*   *
*****

n = 5 => Side = RoundUP(sqrt(5)) = 3

*************
*   *   *   *
* 1 * 2 * 3 *
*   *   *   *
*************
*   *   *
* 4 * 5 *
*   *   *
*********

n = 10 => Side = RoundUP(sqrt(10)) = 4

*****************
*   *   *   *   *
* 1 * 2 * 3 * 4 *
*   *   *   *   *
*****************
*   *   *   *   *
* 5 * 6 * 7 * 8 *
*   *   *   *   *
*****************
*   *   *  
* 9 * 10*
*   *   *
*********

Winning

Shortest code in bytes wins.

\$\endgroup\$
8
  • 14
    \$\begingroup\$ Happy Birthday! \$\endgroup\$
    – Dennis
    Sep 15, 2015 at 19:28
  • 8
    \$\begingroup\$ Oh God, what have I started?! D: We need a cake tag now ;) \$\endgroup\$
    – Beta Decay
    Sep 15, 2015 at 19:30
  • \$\begingroup\$ What can I say september have a lot of bdays :) \$\endgroup\$ Sep 15, 2015 at 19:31
  • 2
    \$\begingroup\$ Do the numbers have to be as shown: 1 (and presumably 3) digits centred, and 2 digits right of centre? Or can they be justified in whatever way is easiest? \$\endgroup\$ Sep 15, 2015 at 19:55
  • 12
    \$\begingroup\$ Worst case I eat my cake alone. - I consider this the best case scenario. \$\endgroup\$
    – user42643
    Sep 15, 2015 at 20:44

1 Answer 1

1
\$\begingroup\$

Python 2, 241 239 237 bytes

i=input()
s=int(round(i**.5+.5))
q="*"*5;p="*   *"
v=lambda x,y:"\x1b[%d;%dH".join(["",q,p,"*{}*",p,q])%tuple(i for j in zip(range(x*4+1,x*4+6),[y*4+5]*5)for i in j)
print"".join([v(j/s,j%s).format(`j+1`.center(3," "))for j in range(i)])

Lots of ANSI goodness...

Defines a function for drawing a box at a position on the screen.

Then calls it with the relevant positions that are in the cake.

Finally adds the numbers using str.format() and str.center

NOTE: If your terminal isn't big enough to display everything at once, it will get 'cropped' and you won't be able to see anything past the last row.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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