水平对齐的ASCII艺术


20

您的任务是接受两个“ ASCII Art”作为输入,并水平对齐每个艺术品。

例如,假设您有两个字符串,"abc\ndef"并且"123\n456"。您需要将它们水平对齐以产生字符串"abc123\ndef456“。我称其为“水平对齐”,因为在打印时输入看起来像这样:

abc
def

和:

123
456

打印后的输出将如下所示:

abc123
def456

请注意如何将一个输入放置在另一个输入的旁边。


输入值

  • 输入将是字符串,并且可以作为两个单独的参数或字符串序列。
  • 艺术人物的十进制代码范围为32-126(含)。
  • 可以支持任意数量的艺术来对齐,而不是仅支持两种(但是显然您必须支持至少两种)。
  • 您可以假设每种艺术品的尺寸都相同,并且至少包含一行。
  • 您必须至少能够支持100x100的角色艺术。

  • 为了与站点上的约定保持一致,参数顺序无关紧要。左边或右边是哪种艺术都没有关系。


输出量

  • 输出将是如上所述的对齐艺术,返回或输出到标准输出。

  • 可选的任何结尾空格。

  • 对齐的艺术之间不得有视觉分隔符。


输入和输出艺术必须是\n\r字符串分隔。允许2D阵列将太琐碎。

提交的内容可能是功能或完整程序。

测试用例:

"abc\ndef", "123\n456" -> "abc123\ndef456".

"qwertyuiop\n asdfghjkl", "Some other\nTextFiller" -> "qwertyuiopSome other\n asdfghjklTextFiller"

"  *  \n *** \n*****\n *** \n  *  \n", "  +  \n  +  \n+++++\n  +  \n  +  \n" -> "  *    +  \n ***   +  \n*****+++++\n ***   +  \n  *    +  \n"

1
我们可以使用自定义分隔符代替换行符吗?即"|"" "
Rɪᴋᴇʀ

10
我要说的是,鉴于这将破坏任何艺术。
Carcigenicate

我们可以\r代替使用\n吗?
亚当

@Adám当然。我将更新措辞。
Carcigenicate

前导空格可以吗?
亚当

Answers:


20

6
因为谁只是对此有一个内置的?:P
caird coinheringaahing

5
那甚至不公平。;-;
–totalhuman

3
@KevinCruijssen在PPCG上,通常允许以您想要的任何顺序进行输入,对于基于堆栈的语言,当前的顺序比反向更有意义。
dzaima

5
@KevinCruijssen虽然我只记得我有一个内置的反向添加功能,所以我更新了帖子:p
dzaima

2
那么Canvas有两个内置函数吗?好啊,为什么不?
Caird coinheringaahing


6

Python 2,59个字节

lambda y:'\n'.join(map(str.__add__,*map(str.splitlines,y)))

在线尝试!


也可以通过删除空间来缩短它。:P
完全人类的

如果要像以前一样输入和输出行列表,可以将其减少到30:在线尝试!

我坐在那里大约5分钟,试图决定是否允许这样做。正如Haskell答案所显示的那样,它将把挑战减少到更微不足道的程度。我想不过过于琐碎的解决方案不会那么受欢迎。
Carcigenicate




3

Bash + coreutils,14年

  • @DavidFoerster节省了4个字节。
paste -d "" $@

输入以两个文件名作为命令行参数给出。

在线尝试


您可以保存4个字节:paste -d "" $@
David Foerster

@DavidFoerster谢谢!很奇怪-我之前尝试过,但是没有用。编辑-我现在看到了- -d ""我尝试了代替而不是尝试了-d"",这当然与-d
Digital Trauma


2

APL(Dyalog Unicode),9 字节SBCS

完整程序。提示(STDIN)以- \r分隔的字符串的任何长度列表。字符串可能参差不齐,并且宽度不同,只要它们的行数相同即可。打印(STDOUT)生成的ASCII艺术作品。

⊃,/⎕FMT¨⎕

在线尝试!

 提示输入评估值

⎕FMT¨ 每种格式(评估所有控制字符和返回字符矩阵)

,/ 水平合并它们(减少分类)

 披露(因为减少的排名从1降低到0)


2

Java 8,100 84 78字节

a->b->{for(int i=0;;)System.out.println(a.split("\n")[i]+b.split("\n")[i++]);}

ArrayIndexOutOfBoundsException将结果打印到STDOUT后,以退出至STDERR,这是允许的

-6个字节,感谢@OlivierGrégoire

说明:

在线尝试。

a->b->{                        // Method with two String parameters and no return-type
  for(int i=0;;)               //  Loop over the substrings of the first input
    System.out.println(        //   Print:
     a.split("\n")[i]          //    The substring of the first input
     +b.split("\n")[i++]);}    //    plus the same-indexed substring of the second input

1
a->b->{for(int i=0;;)System.out.println(a.split("\n")[i]+b.split("\n")[i++]);}78个字节 没有任何额外的副作用。因此,我们可以简单地计数直到发生异常。
奥利维尔·格雷戈尔

@OlivierGrégoire谢谢!在将所有内容打印到STDOUT之后,确实允许退出并返回STDERR错误。
凯文·克鲁伊森

2

红宝石,48个字节

->a,b{$;=$/;a.split.zip(b.split).map(&:join)*$/}

在线尝试!

一个lambda接受两个字符串并返回一个字符串。将默认split定界符设置为newline $;=$/;不会保存任何字节,但是会使其余部分看起来更好。

Ruby,49个字节(任意多个字符串)

->s{s.map{|a|a.split$/}.transpose.map(&:join)*$/}

在线尝试!

只是为了好玩。事实证明,我们可以接受一个字符串数组,而仅需支付1个字节即可。


2

JavaScript(ES6),51个字节

f=
(a,b)=>a.replace(/.+/g,a=>a+b.shift(),b=b.split`
`)
;document.write("<pre>"+f("abc\ndef", "123\n456")+"</pre>")


2

不可思议,21字节

->#oN.zip#++.-> <>"
"

用法示例:

(->#oN.zip#++.-> <>"
")["abc#ndef" "abc#ndef"]

#n 用于代替 \n换行符。

说明

详细版本:

(map #oN) . (zip #con) . (map split "#n")

沿换行符分割输入数组中的每个字符串,并用字符串串联zip,然后输出每个项目。





1

JavaScript(ES6),52个字节

以currying语法接受输入(a)(b)

a=>b=>a.split`
`.map((s,i)=>s+b.split`
`[i]).join`
`

在线尝试!



1

PowerShell51 49字节

param($a,$b)$a-split"
"|%{$_+($b-split"
")[$i++]}

在线尝试!

将输入作为带换行符的文字字符串。您也可以使用`n(在PowerShell中使用换行符,而不是\n) instead.

我们首先-split在换行符上输入左侧的输入字符串,这将创建一个数组,并循环遍历该数组|%{...}。每次迭代时,我们将字符串与正确的输入字符串连接在一起,并再次在换行符上分割,索引并递增。

这些保留在管道上Write-Output,完成时的隐式给我们输出为字符串数组,并在字符串之间打印换行符。




1

Japt -R8 7字节

·íV· m¬

试试吧


说明

             :Implicit input of strings U & V
·            :Split U on newlines
  V·         :Split V on newlines
 í           :Interleave
     m       :Map
      ¬      :  Join
             :Implicitly join with newlines and output

另类

·Ë+V·gE

试试吧

             :Implicit input of strings U & V
·            :Split U on newlines
 Ë           :Map over each element at index E and rejoin with newlines
   V·        :  Split V on newlines
     gE      :  Get the element at index E
  +          :  Append to the current element
             :Implicitly join with newlines and output

1

重击,92字节

a=();for b;do c=;while IFS= read -r d;do a[c++]+=$d;done<<<"$b";done;printf '%s\n' "${a[@]}"

在线尝试!

取消高尔夫:

array=()                             # Initialize the array
for argument in "${@}"; do           # Loop over the arguments list
  index='0'                          # Reset the index
  while IFS='' read -r 'line'; do    # Loop over every line of the current argument
    array[index]+="${line}"          # Append the line to its corresponding place
    (( index++ ))                    # Increment the index
  done <<< "${argument}"             # End while loop
done                                 # End for loop
printf '%s\n' "${array[@]}"          # Print array's content

例子:

$ foo $'abc\ndef' $'123\n456'
abc123
def456

$ foo $'qwertyuiop\n asdfghjkl' $'Some other\nTextFiller'
qwertyuiopSome other
 asdfghjklTextFiller

$ foo \
>   $'  *  \n *** \n*****\n *** \n  *  \n' \
>   $'  +  \n  +  \n+++++\n  +  \n  +  \n'
  *    +  
 ***   +  
*****+++++
 ***   +  
  *    +  


# https://gist.github.com/nxnev/dad0576be7eb2996b860c320c01d0ec5
$ foo "$(< input1)" "$(< input2)" "$(< input3)" > output

我也有一个较短的,但如果第二个失败 read语句,语句返回一个非零值,。

Bash,55个字节

while IFS= read -r a;IFS= read b<&3;do echo "$a$b";done

注意:<&3tio.run上似乎不起作用

此代码使用文件描述符(13)代替参数:

$ foo <<< $'qwertyuiop\n asdfghjkl' 3<<< $'Some other\nTextFiller'
qwertyuiopSome other
 asdfghjklTextFiller

1

木炭,8字节

PθM⌕θ¶→η

在线尝试!链接是详细版本的代码。说明:

 θ          First input
P           Print without moving the cursor
    θ       First input
     ¶      Literal newline
   ⌕        Find index
  M   →     Move that many squares right
       η    Implicitly print second input

添加2个字节以接受多个输入:

FA«PιM⌕ι¶→

在线尝试!链接是详细版本的代码。说明:

 A          Input
F «         Loop over all entries
   Pι       Print current entry
     M⌕ι¶→  Move to next entry

添加4个字节以接受未填充的输入:

PθM⌈E⪪θ¶Lι→η

在线尝试!链接是详细版本的代码。说明:

      θ         First input
       ¶        Literal newline
     ⪪          Split
    E           Map over each string
         ι      Current string
        L       Length
   ⌈            Maximum
  M       →     Move that many squares right



1

斯威夫特 4,119字节

func f(s:[String])->String{return s[0].split{$0=="\n"}.enumerated().map{$0.1+s[1].split{$0=="\n"}[$0.0]+"\n"}.joined()}

说明

func f(s: [String]) -> String {
    return s[0].split{ $0=="\n" }       //splitting the first string after every \n
    .enumerated()                       //create a tuple of offsets and elements
    .map {
        $0.1 +                          //current element
        s[1].split{$0 == "\n"}[$0.0] +  //splitting the second string + indexing
        "\n"                            //new line after every line
     }
     .joined()
}

在线尝试!

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.