8
\$\begingroup\$

The aim is simple: given the string of resistance values, draw the part of "electrical circuit" of those resistors. Examples follow.

  1. Input: 3. Output:

    --3--
    
  2. Input: 1,2,3. Output:

    --1----2----3--
    

Next, the resistors can be joined parallel (by 2 or 3 resistors per join), but the required depth of parallel joins is only 1.

  1. Input: 1|2. Output:

     |--1--|
    -|     |-
     |--2--|
    
  2. Input: 1|2|3. Output:

     |--1--|
    -|--2--|-
     |--3--|
    

    Note, that --2-- resistor is now centered.

  3. Input: 1|2,3|4|5. Output:

     |--1--|  |--3--|
    -|     |--|--4--|-
     |--2--|  |--5--|
    

Combined joins:

  1. Input: 1,2,3|4,5,6|7|8,9. Output:

               |--3--|       |--6--|
    --1----2---|     |---5---|--7--|---9--
               |--4--|       |--8--|
    

If it would be more convenient, the input can be a two-dimensional array. I.e. the input for the last example would look like this: [1,2,[3,4],5,[6,7,8],9].

Some notes:

  • No other forms of (in|out)puts are allowed.
  • The resistance of each resistor can vary in the range from 1 to 9. Other values, like -, 42 or 0 are disallowed.
  • The joins like 1|2|3|4 are invalid. As already mentioned, max 3 per parallel join. Empty joins, i.e. ...,|,... or ...,,... are invalid.
  • Oh, and this is code golf :)
\$\endgroup\$
  • \$\begingroup\$ Thanks for the correction, it's a fixed numbers of dashes. The separate resistors are preceded and followed by two dashes. \$\endgroup\$ – nicael Dec 25 '15 at 15:13
  • \$\begingroup\$ Can the first and third line be padded with spaces to even the length of the lines? \$\endgroup\$ – TFeld Dec 25 '15 at 18:45
  • \$\begingroup\$ @Tfeld Trailing/leading spaces are allowed before/after the whole output, but not between the resistors. \$\endgroup\$ – nicael Dec 25 '15 at 18:54
  • \$\begingroup\$ Are leading/trailing empty lines allowed when there are no parallel resistors? \$\endgroup\$ – PurkkaKoodari Dec 26 '15 at 3:40
  • \$\begingroup\$ @Pietu Hm, let it be yes :) \$\endgroup\$ – nicael Dec 26 '15 at 5:31
2
\$\begingroup\$

Python 2, 195 bytes

a=b=c=''
B=' '*5
H='--'
S=' |'+H;E=H+'| '
for x in input():
 if x>9:a+=S+`x[0]`+E;c+=S+`x[-1]`+E;b+='-|'+[B,H+`x[1]`+H][len(x)>2]+'|-'
 else:b+=H+`x`+H;a+=B;c+=B
print[b,a+'\n'+b+'\n'+c]['|'in a]

Takes input as:

[1,2,[3,4],5,[6,7,8],9]

Output:

           |--3--|       |--6--|      
--1----2---|     |---5---|--7--|---9--
           |--4--|       |--8--|      
| improve this answer | |
\$\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.