15
\$\begingroup\$

Given a binary number, your task is to create a 'branch' of that number, with a depth of 2.

For example, given 0 as input, you should output exactly this:

     /000
  /00
 /   \001
0
 \   /010
  \01
     \011

This should be fairly self explanatory of how the branches should be created. Depth 2 means we calculate branches for numbers of up to 2 numbers longer. We also calculate the branches in order, with zeroes at the top and ones at the bottom.

More test cases:

0

     /000
  /00
 /   \001
0
 \   /010
  \01
     \011

1

     /100
  /10
 /   \101
1
 \   /110
  \11
     \111

00

       /0000
   /000
  /    \0001
00
  \    /0010
   \001
       \0011

01

       /0100
   /010
  /    \0101
01
  \    /0110
   \011
       \0111

10

       /1000
   /100
  /    \1001
10
  \    /1010
   \101
       \1011

11

       /1100
   /110
  /    \1101
11
  \    /1110
   \111
       \1111

Rules

  • You will never receive characters in the input other than 1 and 0.
  • 0 < length of input < 11.
  • Trailing whitespace allowed at the end of lines.
\$\endgroup\$
2
  • 4
    \$\begingroup\$ 0 < length of input < 11 is 11 decimal or binary? :P \$\endgroup\$ Apr 11, 2017 at 0:33
  • \$\begingroup\$ @ETHproductions Decimal :P \$\endgroup\$
    – Okx
    Apr 11, 2017 at 10:08

12 Answers 12

5
\$\begingroup\$

Batch, 178 170 159 bytes

@set/pb=
@set s=%b:0= %
@set s=%s:1= %
@set e=@echo %s%
%e%  %s% /%b%00
%e% /%b%0
%e%/ %s% \%b%01
@echo %b%
%e%\ %s% /%b%10
%e% \%b%1
%e%  %s% \%b%11

Edit: Saved 11 bytes thanks to @ConorO'Brien.

\$\endgroup\$
2
  • \$\begingroup\$ I only count 149 bytes. \$\endgroup\$ Apr 10, 2017 at 16:02
  • \$\begingroup\$ I assume Neil is counting line breaks as the Windows-style CRLF whereas TIO is counting them as LF. I'm not sure whether LF works for Batch on Windows. \$\endgroup\$
    – Alex A.
    Apr 11, 2017 at 3:23
4
\$\begingroup\$

Jelly, 39 38 bytes

L⁶ẋ,W;“/0¶\1 ”ṃ@“ð&ẏ{×ẏĊfẏȷ®ỤṪ⁻ʠaƇGⱮȷ’

Try it online!

How?

The art to be printed is:

L  L /N00
L /N0
L/ L \N01
N
L\ L /N10
L \N1
L  L \N11

Where N is the input string and L is a string of spaces of the length of the input string.

As such it is comprised of eight components (L, N, /, 0, the newline character, \, 1, and the space character) and hence may be stored as a base-8 number ( which may be compressed as a base-250 number in Jelly). The atom combines base conversion and indexing into a list (effectively one may define arbitrary digits to be used).

L⁶ẋ,W;“/0¶\1 ”ṃ@“ð&ẏ{×ẏĊfẏȷ®ỤṪ⁻ʠaƇGⱮȷ’ - Main link: binary string s  e.g. "100"
 ⁶                                     - space character
  ẋ                                    - repeat by:
L                                      -     length(s)                    [' ',' ',' ']
    W                                  - wrap s in a list                 [['1','0','0']]
   ,                                   - pair               [[' ',' ',' '],['1','0','0']]
      “/0¶\1 ”                         - char list: ['/','0',<newline>,'\',','1',' ']
              
     ;                                 - concatenate        [[' ',' ',' '],['1','0','0'],'/','0',<newline>,'\',','1',' ']
                “ð&ẏ{×ẏĊfẏȷ®ỤṪ⁻ʠaƇGⱮȷ’ - base 250 number: 91531517467460683226372755994113932025707662527
              ṃ@                       - base decompression [reversed @arguments]
                                        -     this uses the concatenated list above as
                                        -     the 8 digits of that number in base 8.
                                        - implicit print
\$\endgroup\$
4
\$\begingroup\$

JavaScript (ES6), 112 bytes

s=>`22   /300
2 /30
2/2  4301
3
242  /310
2 431
22   4311`.replace(/./g,n=>[s.replace(/./g,' '),s,'\\'][n-2]||n)

Demo

let f =

s=>`22   /300
2 /30
2/2  4301
3
242  /310
2 431
22   4311`.replace(/./g,n=>[s.replace(/./g,' '),s,'\\'][n-2]||n)

console.log(f('0'))
console.log(f('11'))
console.log(f('101'))

\$\endgroup\$
2
  • \$\begingroup\$ why not [n,n,s.replace(/./g,' '),s,'\\'][n]? \$\endgroup\$
    – tsh
    Apr 10, 2017 at 3:01
  • \$\begingroup\$ @tsh That would require to search for /\d/g rather than /./g to ignore non-numeric characters. \$\endgroup\$
    – Arnauld
    Apr 10, 2017 at 8:35
4
\$\begingroup\$

Python 3, 117 109 bytes

lambda k:'ll   /g00\nl /g0\nl/l  \g01\ng\nl\l  /g10\nl \g1\nll   \g11'.replace('l',' '*len(k)).replace('g',k)

Try it online!

The format string when printed looks like:

ll   /g00
l /g0
l/l  \g01
g
l\l  /g10
l \g1
ll   \g11

This looks good already for string of length of 1. All we got to do is replace l by spaces of length equal to that of g and, of course, g is to be replaced by the original string

\$\endgroup\$
1
  • 1
    \$\begingroup\$ You can save a byte using an unnamed lambda, which also means you can get rid of the print (since returning the string should be acceptable) and save another seven bytes. You can then save two more by using a multiline string getting you down to 107... TIO \$\endgroup\$ Apr 10, 2017 at 16:23
4
\$\begingroup\$

Python 3.6, 172 153 128 bytes

Literally does not get more straightforward than this... This is actually shorter than my original attempt at generating it with an algorithm. How sad.

k=input()
l=len(k)
b=' '*l
print(f'{b*2}   /{k}00\n{b} /{k}0\n{b}/ {b}\\{k}01\n{k}\n{b}\\ {b}/{k}10\n{b} \\{k}1\n{b*2} \\{k}01')

-19 bytes thanks to @Leo
-25 bytes thanks to @L3viathan

\$\endgroup\$
10
  • \$\begingroup\$ I think it would be shorter to drop a, c, and d, and use only b and spaces in the final string. (a is b*2+' ') \$\endgroup\$
    – Leo
    Apr 10, 2017 at 5:17
  • \$\begingroup\$ Weird, still seems 172 bytes for me. \$\endgroup\$
    – user58826
    Apr 10, 2017 at 17:50
  • \$\begingroup\$ @programmer5000 Sorry, that would be because I forgot to update the code itself. \$\endgroup\$
    – hyper-neutrino
    Apr 10, 2017 at 19:29
  • \$\begingroup\$ Save 26 characters with format strings: print(f'{a}/{k}00\n{b} /{k}0\n{b}/ {b}\\{k}01\n{k}\n{b}\\ {b}/{k}10\n{b} \\{k}1\n{b*2} \\{k}01') \$\endgroup\$
    – L3viathan
    Apr 11, 2017 at 13:50
  • \$\begingroup\$ @L3viathan Can you check the syntax on that? It's giving me a syntax error. \$\endgroup\$
    – hyper-neutrino
    Apr 11, 2017 at 13:53
3
\$\begingroup\$

C, 170 168 bytes

Thanks to @Neil for saving two bytes!

n;f(char*s){n=strlen(s);printf("%*c%s00\n%*c%s0\n %*c%*c%s01\n%s\n %*c%*c%s10\n%*c%s1\n%*c%s11",2*n+4,47,s,n+2,47,s,n,47,n+3,92,s,s,n,92,n+3,47,s,n+2,92,s,2*n+4,92,s);}

Try it online!

\$\endgroup\$
2
  • 1
    \$\begingroup\$ Rather than printing a / or ` padded to width n+1, why not print a space, and then a /` or \ padded to width n? \$\endgroup\$
    – Neil
    Apr 9, 2017 at 21:41
  • \$\begingroup\$ Ugh, let me try that again. Rather than printing a / or \ padded to width n+1, why not print a space, and then a / or \ padded to width n? \$\endgroup\$
    – Neil
    Apr 10, 2017 at 8:13
3
\$\begingroup\$

Python 3, 96 bytes

lambda s:"""   /00
 /0
/  \01

\  /10
 \1
   \11""".translate([s,' '*len(s),s])

Try it online! The unprintable characters do not display correctly; the string format is the same as officialaimm's, but with \x01 for l and \x02 for g.

ll   /g00
l /g0
l/l  \g01
g
l\l  /g10
l \g1
ll   \g11

Uses string substitution with Python 3's flexible translate. The translate list [s,' '*len(s),s] maps \x01 to ' '*len(s) and \x02 to s. Any larger characters are unchanged because they give indices that are out-of-bounds for the list. \x00 could not be used because a null byte is read as a program end, so the first entry is wasted.

\$\endgroup\$
3
\$\begingroup\$

PHP, 128 bytes

Only a simple Output

<?=$b=str_pad("",strlen($a=$argn)),"$b   /{$a}00\n$b /{$a}0\n$b/$b  \\{$a}01\n$a\n$b\\$b  /{$a}10\n$b \\{$a}1\n$b$b   \\{$a}11";

Try it online!

\$\endgroup\$
2
\$\begingroup\$

Stacked, 81 bytes

{!n#'' '*@s's  s /n00
s /n0
s/ s \n01
n
s\ s /n10
s \n1
s  s \n11' '\l'$#~1/repl}

Try it online!

Not very interesting, unfortunately. Here's the most interesting part:

'\l'$#~1/repl
         repl     replace all
'\l'              letters
    $#~           by evaluating
       1/         over one argument (otherwise, it would evaluate the "last" thingy)

This is basically string interpolating, but 10 bytes shorter than the builtin.

\$\endgroup\$
2
\$\begingroup\$

///, 116 bytes

/[/\\\///x///*/[y\\0[ y\/\/y\\1[ y\//**********/y///s/yx//~/  /~ ss[x00
 s[x0
s[~s\\x01
x
s\\~s[x10
 s\\x1
~ ss\\x11

Try it online!

Input is as follows:

/[/\\\///x/INPUT HERE!!!!!!!!//*/[y\\0[ y\/\/y\\1[ y\//**********/y///s/yx//~/  /~ ss[x00
 s[x0
s[~s\\x01
x
s\\~s[x10
 s\\x1
~ ss\\x11

Works by using a basic template, and adding spaces and characters where needed.

The byte count went up because Ørjan Johansen realized that it did not handle spacing at first. But the problem is know fixed.

\$\endgroup\$
10
  • \$\begingroup\$ I gave you an upvote before checking that it worked - but you're not adjusting the spacing for length. I don't see a succinct way to do that with such a literal input format. \$\endgroup\$ Apr 10, 2017 at 23:33
  • \$\begingroup\$ Or wait, it's not totally hopeless since there's an input length limit of 11. \$\endgroup\$ Apr 10, 2017 at 23:38
  • \$\begingroup\$ Something like /*/\/y0\/ y\/\/y1\/ y\//**********/y///s/yx/ and then you get spacing with s. \$\endgroup\$ Apr 10, 2017 at 23:44
  • \$\begingroup\$ @ØrjanJohansen Oops, forgot about spacing... thanks. How would I incorporate your code into the answer? \$\endgroup\$
    – sporkl
    Apr 11, 2017 at 0:00
  • \$\begingroup\$ FWIW /00/0|0//01/0|1//10/1|0//11/1|1//|/<\\y>//z/<y>x//<y>0/ //<y>1/ //<\\y\>///s/z/ can handle arbitrary length. \$\endgroup\$ Apr 11, 2017 at 0:01
1
\$\begingroup\$

Python 2, 101,91 bytes113 bytes

lambda y:'   ++/_00\n +/_0\n+/  +\\_01\n_\n+\\  +/_10\n +\\_1\n   ++\\_11'.replace('_',y).replace('+',' '*len(y))

Try it online!

Input is a string of 0's and 1's of length 1 or 2! That is 0,01,10 or 11!

+12 bytes - corrected the spacing in \ for length two input.

\$\endgroup\$
3
  • 3
    \$\begingroup\$ your output does not adjust as per the length of the string. \$\endgroup\$
    – 0xffcourse
    Apr 10, 2017 at 7:58
  • 1
    \$\begingroup\$ ...and the question specifies "0 < length of input < 11". \$\endgroup\$ Apr 10, 2017 at 16:53
  • 1
    \$\begingroup\$ @officialaimm oh yeah. Just noticed. Thanks. Will update my answer! Jonathan.. that was a typo. Thanks I corrected it. \$\endgroup\$ Apr 10, 2017 at 17:37
0
\$\begingroup\$

Charcoal, 34 bytes

P<³←⮌θF²«J³⁻×⁴ι²θP<²Iι↗F²«P⁺⁺θικ↓↓

Try it online! Link is to verbose version of code. Explanation:

P<³

Print the left pairs of /s and \s.

←⮌θ

Print the input right-justified at the current position.

F²«

Loop through the branches.

J³⁻×⁴ι²

Move the the position of the branch. We can do this because the root was printed right-justified so that the middle branch is always at the same absolute position.

θ

Print the input.

P<²

Print the right pair of / and \.

Iι

Print the branch suffix.

Move to the first leaf.

F²«

Loop through the leaves.

P⁺⁺θικ

Print the input and the branch and leaf suffix.

↓↓

Move to the next leaf. Note: If trailing whitespace was acceptable then F²⁺⁺⁺θι궶 would save a byte.

\$\endgroup\$

Your Answer

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

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