11
\$\begingroup\$

I have a chocolate bar and I need help eating it so you'll be writing a program just to do that.

Explanation

The first line is the input. Examples are separated by an empty line.

7
   ____
 __|__|
|__|__|
|__|__|
|__|__|

6
_______
|__|__|
|__|__|
|__|__|

5
   ____
 __|__|
|__|__|
|__|__|

0


1
   ____
   |__|

Spec

Hopefully the examples clearly specify this challenge but to avoid any possible confusion, here's a spec:

You can have a single optional trailing/leading whitespace

An input of 0 is an empty output.

If the input is even, the top rows looks like:

_______
|__|__|

If it's odd, the top row is:

   ____
   |__|

If it's odd and greater than 1, the top rows are:

  ____
__|__|

The following rows are:

|__|__|

until the number of instances of |__| is the same as the input

\$\endgroup\$
  • \$\begingroup\$ What's the output for input 1? Does input of 0 need to be supported? \$\endgroup\$ – Doorknob Feb 6 '16 at 17:49
  • \$\begingroup\$ @Doorknob yes, I've updated the challenge \$\endgroup\$ – Downgoat Feb 6 '16 at 17:49
  • \$\begingroup\$ 1. Can we submit a function? 2. Can we take input from STDIN/function arguments? 3. Do we have to output ONE or ALL itterations? \$\endgroup\$ – YoYoYonnY Feb 6 '16 at 18:22
  • \$\begingroup\$ @YoYoYonnY yes, by default you can (this link answers your second question too). I'm not sure what you mean by iterations. Take a look at the examples, that's all you have to output \$\endgroup\$ – Downgoat Feb 6 '16 at 18:24
  • \$\begingroup\$ Are leading blank lines allowed? \$\endgroup\$ – Neil Feb 6 '16 at 18:29
1
\$\begingroup\$

Pyth, 55 bytes

K"|__|"IQ>7<*3%Q2r"3 7_"9?tQjb+XW%Q2J+KtKZd*/tQ2]J+*3dK

Try it online. Test suite.

This is really quick and dirty, written on my phone. Will get to more golfing later.

\$\endgroup\$
4
\$\begingroup\$

Haskell, 101 bytes

Haskell has never been great for code golfing...

b="\n|__|__|"
h="   ____\n "
f 1=h++"  |__|"
f 2="_______"++b
f 3=h++"__|__|"++b
f n=f(n-2)++b
f _=""
\$\endgroup\$
  • \$\begingroup\$ It depends on how you look at it. Java > Python > Haskell > GolfLang. I use python ;) \$\endgroup\$ – J Atkin Feb 6 '16 at 23:09
  • \$\begingroup\$ @JAtkin Well look at my PHP answer, then you see what's like worst \$\endgroup\$ – timmyRS Feb 6 '16 at 23:10
3
\$\begingroup\$

JavaScript ES6, 80 bytes

x=>(x%2?`   ____
 ${x<2?"  ":"__"}|__|`:x?"_______":"")+`
|__|__|`.repeat(x/2)

Uses the same technique as my Japt answer, and is surprisingly not much longer...

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

C, 104 102 bytes

f(x){x?x%2?printf("   ____\n%3s|__|\n",x<2?"":"__"):puts("_______"):0;for(x/=2;x>0;x--)puts("|__|__|");}

Maybe I can use some printf trickery to improve this...
Apparently I can

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

𝔼𝕊𝕄𝕚𝕟, 53 chars / 67 bytes

ï%2?`   ⟮__⟯Ⅰ
 ⦃ï<2?⍞  :⍞Ⅰ⦄|Ⅰ|`:ï?⍘_ď7:⬯⦄
|Ⅰ|Ⅰ|`ď ï/2

Try it here (Firefox only).

This is quite similar to the Javascript ES6 answer, even though I found it independently. Explanation to come when I finish golfing.

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

PHP, 134 chars

<?$i=$argv[1];if($i>0){echo($i%2===0?"_______\n|__":"   ____\n".($i>1?"___":"   "))."|__|\n";echo str_repeat("|__|__|\n",($i-1)/2);}?>

Notes:

  • PHP was not built for code golfing
  • I sure had FUN making this snippet
  • It throws an error if argv is undefined
  • Any help to short it is appreciated.
\$\endgroup\$
1
\$\begingroup\$

Retina, 69 bytes

r`(1?)1
$1__|__|¶
^__(?=\D{5}$)

^.
$0____¶ $0
^\D

^1
___
 ?1
|

Takes input in unary.

Try it online here.

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

Japt, 60 57 bytes

U%2?[S³'_²²RSU¥1?S²:'_²"|__|"]¬:U?'_p7 :P +"
|__|__|"pU/2

Just a basic answer. Can probably be improved. Test it online!

\$\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.