Binary Branches

15

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.

Okx

Posted 2017-04-09T20:12:22.933

Reputation: 15 025

40 < length of input < 11 is 11 decimal or binary? :P – ETHproductions – 2017-04-11T00:33:26.500

@ETHproductions Decimal :P – Okx – 2017-04-11T10:08:26.343

Answers

4

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

Jonathan Allan

Posted 2017-04-09T20:12:22.933

Reputation: 67 804

5

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.

Neil

Posted 2017-04-09T20:12:22.933

Reputation: 95 035

I only count 149 bytes.

– Engineer Toast – 2017-04-10T16:02:50.297

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. – Alex A. – 2017-04-11T03:23:38.340

4

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

HyperNeutrino

Posted 2017-04-09T20:12:22.933

Reputation: 26 575

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+' ') – Leo – 2017-04-10T05:17:42.147

Weird, still seems 172 bytes for me. – programmer5000 – 2017-04-10T17:50:28.557

@programmer5000 Sorry, that would be because I forgot to update the code itself. – HyperNeutrino – 2017-04-10T19:29:08.563

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') – L3viathan – 2017-04-11T13:50:47.757

@L3viathan Can you check the syntax on that? It's giving me a syntax error. – HyperNeutrino – 2017-04-11T13:53:16.087

Are you on Python 3.6? – L3viathan – 2017-04-11T13:53:44.000

@L3viathan No, 3.4.3. – HyperNeutrino – 2017-04-11T13:54:49.473

Well, in the latest version of Python (3.6+), it's not a syntax error, as f-strings were introduced then. – L3viathan – 2017-04-11T14:19:01.960

@L3viathan Oh okay. I'll take your word for it, I'll install 3.6 at some point in time. Thanks! – HyperNeutrino – 2017-04-11T14:19:35.483

{b}{b} can become {b*2} or {b+b}. – L3viathan – 2017-04-11T14:58:45.740

4

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'))

Arnauld

Posted 2017-04-09T20:12:22.933

Reputation: 111 334

why not [n,n,s.replace(/./g,' '),s,'\\'][n]? – tsh – 2017-04-10T03:01:03.417

@tsh That would require to search for /\d/g rather than /./g to ignore non-numeric characters. – Arnauld – 2017-04-10T08:35:28.870

4

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

officialaimm

Posted 2017-04-09T20:12:22.933

Reputation: 2 739

1

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

– Jonathan Allan – 2017-04-10T16:23:44.053

3

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!

Steadybox

Posted 2017-04-09T20:12:22.933

Reputation: 15 798

1Rather than printing a / or \ padded to width n+1, why not print a space, and then a / or \ padded to width n? – Neil – 2017-04-09T21:41:00.857

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? – Neil – 2017-04-10T08:13:20.470

3

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!

Jörg Hülsermann

Posted 2017-04-09T20:12:22.933

Reputation: 13 026

3

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.

xnor

Posted 2017-04-09T20:12:22.933

Reputation: 115 687

2

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.

Conor O'Brien

Posted 2017-04-09T20:12:22.933

Reputation: 36 228

2

///, 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.

Comrade SparklePony

Posted 2017-04-09T20:12:22.933

Reputation: 5 784

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. – Ørjan Johansen – 2017-04-10T23:33:12.837

Or wait, it's not totally hopeless since there's an input length limit of 11. – Ørjan Johansen – 2017-04-10T23:38:42.023

Something like /*/\/y0\/ y\/\/y1\/ y\//**********/y///s/yx/ and then you get spacing with s. – Ørjan Johansen – 2017-04-10T23:44:09.820

@ØrjanJohansen Oops, forgot about spacing... thanks. How would I incorporate your code into the answer? – Comrade SparklePony – 2017-04-11T00:00:25.067

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. – Ørjan Johansen – 2017-04-11T00:01:19.580

Put it after the first substitution, and otherwise use s where you need variable spacing of the length of the input. – Ørjan Johansen – 2017-04-11T00:02:30.107

Hm just testing the short version, it's not quite right. Oh duh you need \\\ inside y0 and y1. – Ørjan Johansen – 2017-04-11T00:06:03.757

Not very deliberately, but (1) Your mention of my BCT interpreter was what drove me to join this site (2) We seem to have similar tastes (3) As a result of (1) and (2), I now do notice when your nick is on the front page. – Ørjan Johansen – 2017-04-11T00:13:33.907

@ØrjanJohansen Fair... On the other hand, what is the completed spacing fix? "Inside" could mean a lot if things. – Comrade SparklePony – 2017-04-11T00:21:10.220

Sample use, replace the xsx at then end as needed: /x/01001100//*/\/y\\0\/ y\/\/y\\1\/ y\//**********/y///s/yx/xsx. – Ørjan Johansen – 2017-04-11T00:23:48.503

1

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.

Keerthana Prabhakaran

Posted 2017-04-09T20:12:22.933

Reputation: 759

3your output does not adjust as per the length of the string. – officialaimm – 2017-04-10T07:58:58.113

1...and the question specifies "0 < length of input < 11". – Jonathan Allan – 2017-04-10T16:53:35.477

1@officialaimm oh yeah. Just noticed. Thanks. Will update my answer! Jonathan.. that was a typo. Thanks I corrected it. – Keerthana Prabhakaran – 2017-04-10T17:37:50.470

0

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.

Neil

Posted 2017-04-09T20:12:22.933

Reputation: 95 035