画一些ASCII框


19

取两个非负整数列表,并输出如下定义的ASCII框。

  • 角和交点为加号:+(ASCII代码43)
  • 垂直线是条形|(ASCII代码124)
  • 水平线是负号-(ASCII代码45)

第一个输入列表指定水平方向上每个加号之间的减号数。

第二个输入列表指定垂直方向上每个加号之间的条形数量。

举几个例子更容易解释:

0    // No minuses between each + sign
0    // No bars between + signs

++
++

------------------
1 2   // First a single minus, then two minuses 
1 2   // First  one bar, then two bars

+-+--+
| |  |
+-+--+
| |  |
| |  |
+-+--+


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

+-++---++--++
| ||   ||  ||
| ||   ||  ||
+-++---++--++
| ||   ||  ||
+-++---++--++
+-++---++--++
+-++---++--++

说明:

  • 输入顺序和格式是可选的
  • 仅应打印/显示这些框,但可以接受尾随空格或换行符。
  • 如果更方便,您可以选择将1添加到所有输入值。然后,第二个示例为:2 3; 2 3

这是代码高尔夫球,因此以字节为单位的最短代码获胜。


Answers:


5

MATL25 22 21字节

'|-+ '2:"1tiYsQ(]E!+)

使用1添加的输入(挑战允许)。

在线尝试!

说明

该代码最初构建一个数组1,该数组包含最终结果中非空格字符的列索引,0否则。因此,如果第一个输入为[2 1 4 1 3 1](将采用[1 0 3 0 2 0]从0开始的格式),则此数组将为

1 0 1 1 0 0 0 1 1 0 0 1 1

注意零游程的长度与输入如何相关。具体来说,此数组的构建如下:

  1. 将数组初始化为单个1
  2. 计算输入的累加总和1。在示例中,给出[3 4 8 9 12 13]
  3. 通过分配1具有步骤2给定的(从1开始)索引的条目,从步骤1扩展数组。中间条目自动设置为0

将为行构建类似的数组。第二个输入[3 2 1 1](或[2 1 0 0 ])给出

1 0 0 1 0 1 1 1

现在,第二个数组乘以2,转置并与广播相加到第一个。这给出了二维数组

3 2 3 3 2 2 2 3 3 2 2 3 3
1 0 1 1 0 0 0 1 1 0 0 1 1
1 0 1 1 0 0 0 1 1 0 0 1 1
3 2 3 3 2 2 2 3 3 2 2 3 3
1 0 1 1 0 0 0 1 1 0 0 1 1
3 2 3 3 2 2 2 3 3 2 2 3 3
3 2 3 3 2 2 2 3 3 2 2 3 3
3 2 3 3 2 2 2 3 3 2 2 3 3

索引到字符串中'|-+ ',将最终结果显示为2D char数组。由于索引是模块化且基于1的,因此索引0对应于最后一个元素(空格)。

'|-+ '                   % Push this string
      2:"       ]        % Do this twice
         1               % Push 1 (initial array)
          t              % Push another 1 (contents to be filled in)
           i             % Take input
            Ys           % Cumulative sum
              Q          % Add 1
               (         % Fill 1 into those entries of the array
                 E       % Multiply by 2
                  !      % Transpose
                   +     % Add, with broadcast
                    )    % Index (modular, 1-based) into the string

6

Python 2,117字节

def f(h,v):r="+"+"+".join("-"*i for i in h)+"+\n";print r+r.join(("|"+"|".join(" "*i for i in h)+"|\n")*i for i in v)+r

在ideone上尝试。

对此期望不高。真的很简单,只需使用python连接和字符串乘法即可将所有内容组合在一起。


6

JavaScript(ES6),83个字节

(a,b,g=(a,[s,t])=>t+a.map(n=>s.repeat(n)+t).join``+`
`)=>g(b,[g(a,` |`),g(a,`-+`)])

输出包括两个尾随换行符。


哇。因此,几乎同时发布了类似的答案。;)
Arnauld

(不过,您以2分2字节击败了我。)
Arnauld

@Arnauld您在演示上浪费了时间;-)
Neil

我猜就是这样。^^有趣的是,我的版本为81个字节,后跟两个换行符。
Arnauld


1

Pyth,45个字节

AQj.i*hlH]Js.i*hlG\+m*d\-G-mjb*d]XJ"+-""| "Hk

一个程序,该程序在STDIN上输入两个逗号分隔的列表,然后打印结果。

这里可能仍然会有打高尔夫球的事情。

在线尝试

解释稍后


1

Haskell,55个字节

f[a,b]x=a:do n<-x;(b<$[1..n])++[a]
g x=f[f"+-"x,f"| "x]

定义一个g接受两个输入列表并返回包含输出行的列表的函数


0

PowerShell v2 +,89字节

param($a,$b)($x="+$(($a|%{'-'*$_})-join'+')+")
$b|%{,"|$(($a|%{' '*$_})-join'|')|"*$_;$x}

堂,不要以为我能赶上JavaScript。

接受输入$a$b作为显式数组。$x根据循环$a和一些字符串连接,将变量设置为框的第一行。它被封装在括号中,因此被放置在管道中。然后,我们进行遍历$b,每次迭代将两个字符串放在管道上-相同的样式字符串,但带有空格,|而不是连字符和+,和$x。这些字符串都是Write-Output在程序完成时从管道中以隐式方式收集的,它们之间具有默认换行符。

例子

PS C:\Tools\Scripts\golfing> .\make-some-ascii-boxes.ps1 @(0) @(0)
++
++

PS C:\Tools\Scripts\golfing> .\make-some-ascii-boxes.ps1 @(1,0,3,0,2,0) @(2,1,0,0)
+-++---++--++
| ||   ||  ||
| ||   ||  ||
+-++---++--++
| ||   ||  ||
+-++---++--++
+-++---++--++
+-++---++--++

PS C:\Tools\Scripts\golfing> .\make-some-ascii-boxes.ps1 @(1,4,1) @(1,2,1)
+-+----+-+
| |    | |
+-+----+-+
| |    | |
| |    | |
+-+----+-+
| |    | |
+-+----+-+

0

Ruby,66个字节

->x,y{d=->c,m,z=x{c+z.map{|w|m*w}*c+c+$/}
d[d[?+,?-],d[?|,' '],y]}

0

果冻30 26 字节

Ḣị“+-“| ”ị@
1;+\Ṭ
Ç€,€/Ñ€Y

TryItOnline上进行测试

怎么样?

输入的输入是两个列表的列表,[vertical, horizontal]并且使用了递增选项
-因此示例3将[[3,2,1,1], [2,1,4,1,3,1]]
每个参数转换为分别指示rowType或rowCharacterType的布尔数组,例如[[1,0,0,1,0,1,1,1], [1,0,1,1,0,0,0,1,1,0,0,1,1]]
,通过将行从由rowType和rowCharacterType组合标识的字符-即rowType标识"+-"或,"| "而rowCharacterType标识这两个字符之一。

Ḣị“+-“| ”ị@ - Link 1, create a row: [rowType, [rowCharaterTypes]]
Ḣ           - head - get the rowType
  “+-“| ”   - list of strings ["+-", "| "]
 ị          - index into (if rowType is 1 "+-"; if rowType is 0 "| ")
         ị@ - index into with reversed operands (index into that from rowCharaterTypes)
                (replace each 1 in rowCharaters with "+" or "|" and each 0 with "-" or " ")

1;+\Ṭ - Link 2, create the Type lists from the inputs: int[] nCharacters
1;    - 1 concatenated with the input
  +\  - reduce with addition (accumulation provides the indices)
    Ṭ - boolean array with 1s at those indices
            As an example take the vertical of example 3:
            [3,2,1,1] -> [1,3,2,1,1] -> [1,4,6,7,8] -> [1,0,0,1,0,1,1,1]
            each of which will be passed as a rowType for Link 1

Ç€,€/Ñ€Y - Main link, takes one argument: [Vertical, Horizontal] (incremented option)
Ç€       - call the last link (2) for each of the two lists in the input
  ,€/    - pair each and reduce (making a list of [rowtype [rowCharacterTypes]])
     р  - call the next link (1) for each
       Y - join on line feeds
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.