带E的字母E


19

给定五个输入,您的任务是显示以下字母“ E”形的ASCII艺术作品。

例子:

输入:(7,2,+,|,-注意:您不必遵循此确切的输入格式,如果不使用它,则必须说明自己的输入格式是如何工作的)

说明:

  • 7 总宽度,包括左右边缘字符。

  • 2 垂直字符数。

  • + 应该显示在边缘的字符。

  • | 在边缘之间垂直显示的字符。

  • - 应该水平显示的字符。

上面示例的输出:

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


其他例子:

输入: 7,2,@,|,-

输出:

@-----@
|
|
@-----@
|
|
@-----@


输入: 7,2,+,|,#

输出:

+#####+
|
|
+#####+
|
|
+#####+


输入: 8,3,+,|,#

输出:

+######+
|
|
|
+######+
|
|
|
+######+


输入: 8,3,+,@,#

输出:

+######+
@
@
@
+######+
@
@
@
+######+


输入: 9,4,^,$,!

输出:

^!!!!!!!^
$
$
$
$
^!!!!!!!^
$
$
$
$
^!!!!!!!^


不允许作弊和标准漏洞。

您的代码不得将任何内容打印到STDERR。

您的代码可以接受您选择的任何字符编码作为输入,但是您选择的任何字符编码必须至少支持所有95个可打印ASCII字符。

成功完成此挑战的最短代码(以字节为单位)是获胜代码。

排行榜


27
我不喜欢“没有输入”的特殊情况。在我看来,它并没有增加任何挑战,并且使事情变得毫无意义。
Yttsi'9

3
标题是因为您可以提供E输入内容,并E使用Es 做标题吗?
trichoplax

3
尽管我不喜欢它,“无输入”的真正含义是什么?如果您不输入任何内容,我的Floroid解决方案将永远挂起。为了使我的解决方案具有竞争力,您必须传递5个零,并且我必须检查输入是否为零并相应地对待...?-1。
Yytsi'9

4
@TheBitByte请参阅我的第二条评论,以查看我认为确实存在问题。现在,您已经将其编辑了,+ 1。
Yytsi

1
@Kevin是 我只是对“无输入”的解释有些困惑。我认为空字符串不等于“无输入”。而且我很习惯在解释器中运行代码,我实际上在上面等待输入的输入,因此这就是我将其扔到那里的原因。
Yytsi

Answers:


6

05AB1E16 14字节

Í×s.ø©|`×`»D®»

说明

Í×                  # create a string of the correct nr of horizontal chars
  s.ø               # add the corner char on both sides
     ©              # save in register while keeping it on the stack
      |`            # push the remaining inputs to the top of the stack
        ×`          # push the correct nr of vertical chars on the stack
          »         # join on newline (joining the top vertical and horizontal sections)
           D        # duplicate this
            ®       # push the horizontal part again
             »      # join everything on newline

在线尝试!

感谢Adnan,节省了4个字节。


1
出于好奇,您为什么使用©寄存器:p?
阿德南

1
@Adnan:好收获!我打算去,但最终没有使用它,忘了删除它:)
Emigna

1
@muddyfish:从问题不明显的问题中读取了解释,所以我错过了。谢谢你让我知道!
Emigna '16

1
还有哪些作品在这里特殊的环绕命令:Í×s.øU×S»X»D»Xr»
阿德南

2
@Adnan:对。好想法!我以前从未使用过它:)
Emigna '16

6

Python,53 51 55字节

lambda a,b,c,d,e:d.join("\n"*-~b).join([c+e*(a-2)+c]*3)

+4字节感谢@nimi

匿名lambda函数,要调用它,请f=在它之前编写。例:

>>> print f(4,1,"€","|","-")
€--€
|
€--€
|
€--€

替代,53字节

lambda a,b,c,d,e:((c+e*a+c+"\n"+(d+"\n")*b)*3)[:-b*2]

没有输入特殊情况的旧版本,69 65 63字节

是的,要在挑战中期改变需求...

lambda a=1,b=1,(c,d,e)="+|-":d.join("\n"*-~b).join([c+e*a+c]*3)

2
小错误:第一个参数(2在您的示例中)是线的总长度,包括角,因此形状的正确输入为f(4,1 ...)
nimi 2016年

@nimi感谢您的提示。(+4字节:()OP的第一个示例有点令人困惑。(其他一些答案也包含此错误)
KarlKastor


3

Ruby,54 45 42字节

->x,y,c,v,h{[c+h*~-~-x+c+$/]*3*((v+$/)*y)}

这是一个匿名函数,它将输入的不同部分作为单独的参数,并将结果作为完整的字符串返回。

例如,

f=->x,y,c,v,h{[c+h*~-~-x+c+$/]*3*((v+$/)*y)}
puts f[6, 2, 'Ø', 'V', '>']

版画

Ø>>>>Ø
V
V
Ø>>>>Ø
V
V
Ø>>>>Ø

3

Javascript(ES6),64个字节

(h,v,e,V,H)=>(v=(h=e+H.repeat(h-2)+e)+`
${V}`.repeat(v)+`
`)+v+h

let f =
(h,v,e,V,H)=>(v=(h=e+H.repeat(h-2)+e)+`
${V}`.repeat(v)+`
`)+v+h

console.log(f(8,3,'+','@','#'))


这可以处理无输入的特殊情况吗?
科纳·奥布莱恩

{ "message": "Script error.", "filename": "", "lineno": 0, "colno": 0 }内联运行时。
noɥʇʎԀʎzɐɹƆ

@AgentCrazyPython-在IE上运行?它没有支持.repeat()
阿尔诺

@Arnauld瑙,野生动物园
noɥʇʎԀʎzɐɹƆ

哦,这是ES6 ...
noɥʇʎԀʎzɐɹƆ


3

R,80字节

相当重复:

function(h,v,a,b,c)cat(t<-c(a,rep(c,h),a,"\n"),d<-rep(c(b,"\n"),v),t,d,t,sep="")

松散

function(h,v,a,b,c)

cat(t<-c(a,rep(c,h),a,"\n"),
    d<-rep(c(b,"\n"),v),
    t,d,t,
    sep="")


2

Pyth,19个字节

jP*3,++Jw*-E2wJj*Ew

该程序在角点字符,水平字符数,水平字符,垂直字符数和垂直字符的STDIN上使用换行符分隔的输入,并打印结果。

在线尝试

怎么运行的

jP*3,++Jw*-E2wJj*Ew  Program.
       Jw            Get the corner character. Store in J
           E         Get the number of horizontal characters
          - 2        -2
         *   w       Get the horizontal character and repeat it that many times
      +              Add J at the beginning of that
     +         J     and at the end
                 E   Get the number of vertical characters
                * w  Get the vertical character and repeat it that many times
               j     Join the above on newlines
    ,                Construct a 2-element list from the horizontal and vertical strings
  *3                 Repeat it 3 times
 P                   Everything except the last element
j                    Join the above on newlines
                     Implicitly print

2

MATLAB,95 92 91 85 81字节

MATLAB'E'函数。(编辑:在八度上不起作用)

function a=e(n,m,c,h,v);a(1:n)=h;a=[c a c];a(2:m+1,1)=v;a=[a;a;a];a=a(1:3+2*m,:);

和无高尔夫球场:

function a=e(n,m,c,h,v); %Declare the function
a(1:n)=h;                %Initialise return with top line excluding the corners
a=[c a c];               %Then add corner pieces
a(2:m+1,1)=v;            %Next add the first vertical part
a=[a;a;a];               %Repeat three times vertically to get an E with a tail
a=a(1:3+2*m,:);          %And then lop off the tail

该函数应按如下方式调用:

e(5,2,'*','-','|')

哪个会返回:

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

可能可以简化一点,我会继续努力。我不喜欢使用整个函数声明来获取输入,因此将查看我是否可以对此进行改进。


  • 通过简化第一行的生成来节省3个字节,以首先使该行没有角,然后添加角,因为这减少了需要索引的次数。

  • 从第一个角开始保存的另一个字节。

  • 通过将repmat(a,3,1)call 替换为6个字节[a;a;a]

  • 通过使用a而无需特定的初始化(已在函数声明中声明)节省了4个字节-谢谢@LuisMendo


1
@LuisMendo有趣。我最初a=[c a c]在那里,但是将其删除以缩小内容,因为通常您无法索引访问不存在的变量并在此过程中创建它。我忘记了这是一个函数,因此a已经在函数声明中声明为返回值。谢谢:)
汤姆·卡彭特

实际上,即使没有在函数中,您也可以执行此操作-索引不存在的变量会创建它。每天学习新事物。
汤姆·卡彭特

2

Perl,40 + 1(-n)= 41字节

感谢@Ton Hospel节省了14个字节并允许该程序使用大于10的条目。

/.$/;$,=$/.<>x<>;say+($`.$&x(<>-2).$`)x3

需要-n以及-E(或-M5.010)运行。例如 :

perl -nE '/.$/;$,=$/.<>x<>;say+($`.$&x(<>-2).$`)x3' <<< '^$
!
4
9'

1
@TheBitByte好,很好,谢谢。
达达

2
这已经非常好了。但是您可以使用输入格式,并< 10通过放入/(.)(.)/;$,=$' x<>;say+($1.$2x(<>-2).$1.$/)x3一个文件(因为使用$')并使用perl -M5.010 -n prog.pl <<< '^!S\n4\n9'(使用实际的换行符)调用占48个字节(额外2个)的文件来解决挑战不会说重复的缺点。障碍,因为它不能与-e)相结合
Ton Hospel

@TonHospel谢谢。是的,我什至没有注意到这不适用于数字>10..非常适合输入格式,谢谢。
达达

这几乎是在滥用选择输入格式的自由,但是:perl -nE '/.$/;$,=$/.<>x<>;say+($`.$&x(<>-2).$`)x3' <<< '^$\n!\n4\n9'是41个字节(没有更多$'),并且还摆脱了虚假的尾随换行符
Ton Hospel

@TonHospel确实,我是在您提出第一个建议之后提出了该解决方案的,但是由于输入格式的原因,我对此不太满意...由于我们都考虑过这一点,因此我更改了答案,但我仍然觉得这有点作弊...
Dada

2

Dyalog APL31 29 字节

按此顺序提示输入水平字符,宽度,交界字符,高度,垂直字符。

h↓⊃⍪/3/⊂↑(⍞⍴⍨h←⎕),⊂⍞{∊⍺⍵⍺}⎕⍴⍞

⎕⍴⍞输入水平字符并重复输入宽度时间(

⍞{... }输入结字,这将是的功能...

∊⍺⍵⍺ 展平[[junction],[horizo​​ntals],[junction]]

封装,使其可以成为列表的一部分

(... ),前置...

h←⎕ 输入高度

⍞⍴⍨ 输入垂直字符并重复多次

将字符串列表放入字符表

封装(因此可以整体重复)

3/ 重复三遍

    
┗━ ┗━ ┗━

⍪/ 将三块垂直连接


┣━
┣━
┗━

(这也封装了它们,因此我们需要...)

去除封装

h↓删除第一个h(行)

┏━
┣━
┗━

在线尝试APL!


2

C,130字节

#define p putchar
#define E for(o=0;o++<O;p(10))p(S);
#define W for(p(P),D=0;D++<C-2;)p(_);p(P);p(10);
f(C,O,P,S,_,D,o){W E W E W}

用法:

main(){f(7,2,'+','|','-');}

输出量

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

删除defines中的分号并将其添加为f(C,O,P,S,_,D,o){W;E;W;E;W;}将保存一个字节。
betseg

2

C#,108个字节

(m,n,e,v,h)=>{string x=e+new string(h,m-2)+e+"\n",y=new string(v,n).Replace(v+"",v+"\n");return x+y+x+y+x;};

匿名函数,它生成每个水平和垂直线并生成最终输出。

非高尔夫功能:

(m,n,e,v,h)=>
{
    string x = e + new string(h, m - 2) + e + "\n",
        y = new string(v, n).Replace(v + "", v + "\n");
    return x + y + x + y + x;
};

完整的测试用例程序:

using System;

namespace LetterEWithoutE
{
    class Program
    {
        static void Main(string[] args)
        {
            Func<int,int,char,char,char,string>f= (m,n,e,v,h)=>{string x=e+new string(h,m-2)+e+"\n",y=new string(v,n).Replace(v+"",v+"\n");return x+y+x+y+x;};

            Console.WriteLine(f(7,2,'+','|','-'));
            Console.WriteLine(f(7,2,'@','|','-'));
            Console.WriteLine(f(7,2,'@','|','#'));
            Console.WriteLine(f(8,3,'+','|','#'));
            Console.WriteLine(f(8,3,'+','@','#'));
            Console.WriteLine(f(9,4,'^','$','!'));
        }
    }
}

2

MATL,15字节

感谢@muddyfish的更正

2-Y"yv!iiY"!yyy

在线尝试!

说明

为了清楚起见,使用质询中的第一个示例来说明每个步骤之后的堆栈内容。

2-    % Implicitly input number of repetitions of the char of the horizontal line.
      % Subtract 2
      %   STACK: 5
Y"    % Implicitly input char of the horizontal line. Apply run-length decoding
      %   STACK: '-----' (string)
y     % Implicitly input (from below) the char of the corners. Duplicate onto the top
      %   STACK: '+', '-----', '+'
v!    % Concatenate all the stack horizontally. We now have the horizontal line
      % including the corners
      %   STACK: '+-----+'
iiY"  % Take two inputs: char of the vertical line and number of repetitions
      %   STACK: '+-----+', '||'
!     % Transpose. This tranforms the string into a vertical char array, which
      % gives the vertical line
      %   STACK: '+-----+', ['|';'|'] (vertical char array)
y     % Duplicate from below: this pushes a new copy of the horizontal line
      % onto the top of the stack
      %   STACK: '+-----+', ['|';'|'], '+-----+'
y     % Duplicate from below: this pushes a new copy of the vertical line
      % onto the top of the stack
      %   STACK: '+-----+', ['|';'|'], '+-----+', ['|';'|'],
y     % Duplicate from below: this pushes a new copy of the horizontal line
      % onto the top of the stack
      %   STACK: '+-----+', ['|';'|'], '+-----+', ['|';'|'], '+-----+'
      % Implicitly display

1

Bash + coreutils,105个字节

printf -- "$3`printf -- "$4%.0s" $(seq $1)`$3`printf "\n$5%.0s" $(seq $2)`%.0s\n" {1..3}|sed -n 1,$(($2*2+3))p

假设存储该文件的文件名为A.sh,则用法为:

bash A.sh <Horizontal Segment Length w/out Edge Chars> <Vertical Segment Length> '<Left/Right Edge Char>' '<Char Between Edges>' '<Vertical Char>'

如果--字符输入之一恰好是,则需要-,并且printf显然不能在没有双破折号的情况下很好地处理字符串开头的破折号,则需要使用。

说明

假设输入是5 2 + * |...

  1. $3printf -- "$4%.0s" $(seq $1)$3printf "\n$5%.0s" $(seq $2)

    一起创建第一个水平线段和垂直线段。这将导致:

    +*****+
    |
    |
    
  2. printf -- "$3printf -- "$4%.0s" $(seq $1)$3printf "\n$5%.0s" $(seq $2)%.0s\n" {1..3}

    重复先前创建的零件3。现在导致:

    +*****+
    |
    |
    +*****+
    |
    |
    +*****+
    |
    |
    
  3. printf -- "$3printf -- "$4%.0s" $(seq $1)$3printf "\n$5%.0s" $(seq $2)%.0s\n" {1..3}|sed -n 1,$(($2*2+3))p

    最后,sed通过仅输出的第一<Vertical Segment Length>*2+3行,将先前的输出传递到,以摆脱最后2行的线段E。我们终于得到了E我们想要的:

    +*****+
    |
    |
    +*****+
    |
    |
    +*****+
    

1

PowerShell v2 +,60 59字节

param($a,$b,$c,$d,$e)(,($x="$c$($e*($a-2))$c")+,$d*$b)*2;$x

将输入作为单独的命令行参数。构造水平字符串,将其存储$x为以后使用,然后使用逗号操作符将其形成数组,。执行$d公式化为元素数组的数组串联(即,在末尾添加元素)$b。依次将其与另一个逗号运算符配制成两个元素的数组,并留在管道中。然后,将水平线$x留在管道上。滥用默认格式Write-Output在元素之间放置换行符。

PS C:\Tools\Scripts\golfing> .\the-letter-e-without-e.ps1 5 3 "z" "v" "d"
zdddz
v
v
v
zdddz
v
v
v
zdddz

1

Python 3,60个字节

功能

def p(a,b,c,d,e):q=c+e*(a-2)+c;return(q+'\n'+(d+'\n')*b)*2+q

测试用例

>>> print(p(8,2,'+','|','#'))
+######+
|
|
+######+
|
|
+######+

您的输出中有两个破折号
Blue

1

Brainf * ck,147个字节

,>,>++++++++[<------<------>>-]<<-->>>,>,>,>+++>++>++++++++++<<[-<<<.<<<[->>+>>>.<<<<<]>>[-<<+>>]>.>>>>>.<[-<<<<<<[->+>>.>>>>.<<<<<<<]>[<+>-]]>>>>]

从标准输入中输入输入的前5个字符。前两个从其ASCII码中减去48,因此0-9的行为符合预期。对于大于9的数字,请在数字后加上48,并使用相应的字符。其他三个字符是在挑战中指定的。

我敢肯定这不是最佳解决方案,但是寿命太短了,无法为高尔夫而烦恼。

有评论:

[
    Input: number number corner vertical horizontal
    Numbers are single digits; add 48 and use the ASCII character corresponding
    to the number you want for numbers > 9.
    First number is the number of characters across. Second is the number down.

    Layout: {first number-2} {second number} {temp} {a} {b} {c}
]

,>,>++++++++[<------<------>>-]<<-->>>,>,>,
now we should have the first five cells with the specified layout
the 6th will hold 3 as a counter and the 7th 2 and the 8th 10 '\n'

>+++>++>++++++++++<<
[  while the 6th cell is not 0
    -
    <<<.    print corner
    <<<[->>+>>>.<<<<<]  print horizontal characters
    >>[-<<+>>]         copy temp back to 1st cell
    >.>>>>>.           print corner and newline
    <
    [ If the second counter is not zero
        -
        <<<<<<[->+>>.>>>>.<<<<<<<]  print vertical and newline n times
        >[<+>-]           copy temp back to 2nd cell
    ]
    >>>>
]

示例运行:

sean@SEANSBOX:~/Dropbox/Code/BF$ ./bf E.b
94^$!
^!!!!!!!^
$
$
$
$
^!!!!!!!^
$
$
$
$
^!!!!!!!^

1

PHP,97字节

list(,$w,$h,$c,$v,$r)=$argv;echo$b=str_pad($a=str_pad($c,++$w,$r)."$c\n",--$h*2+$w,"$v\n"),$b,$a;

没有循环,只有内置函数。

用运行php -r '<code>' <parameters>


1

Java 7中,205个 129字节

String c(int w,int h,String a,char b,char c){String r=a,n="\n",l="";for(;w-->2;r+=c);r+=a+n;for(;h-->0;l+=b+n);return r+l+r+l+r;}

-76个字节,感谢一个匿名陌生人。
PS:下次不要去编辑其他人的帖子。如果您喜欢打高尔夫球,请留下评论,或者如果使用的是完全不同的方法,您可以自己做出答案。不过,仍然要感谢您打败了所有这些字节-无论您是谁。

非高尔夫球和测试用例:

在这里尝试。

class M {
    static String c(int w, int h, String a, char b, char c){
        String r = a,
               n = "\n",
               l = "";
        for(; w-- > 2; r += c);
        r += a+n;
        for( ;h-- > 0; l += b+n);
        return r+l+r+l+r;
    }

    public static void main(String[] a) {
        System.out.print(c(7, 2, "+", '|', '-'));
        System.out.print(c(9, 4, "?", '¡', '¿'));
    }
}

输出:

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

?¿¿¿¿¿¿¿?
¡      
¡      
¡      
¡      
?¿¿¿¿¿¿¿?
¡      
¡      
¡      
¡      
?¿¿¿¿¿¿¿?

0

球拍124字节

(λ(w h a b c)(for((i 3))(display a)(for((i w))(display c))(display a)(when(< i 2)(displayln "")(for((j h))(displayln b)))))

更具可读性的形式:

(define(f w h a b c)
  (for((i 3))
    (display a)
    (for((i w))
      (display c))
    (display a)
    (when(< i 2)
      (displayln "")
      (for((j h))
        (displayln b)))))

测试:

(f 7 2 "+" "|" "-" )

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

0

C ++,121字节

#import<string>
#import<iostream>
#define f(w,h,C,W,H){std::string s(w,W),t;s=C+s+C+"\n";for(int i=h;i--;)t=t+H+"\n";std::cout<<s+t+s+t+s;}

取消高尔夫:

#import<string>
#import<iostream>
#define f(w,h,C,W,H){
    std::string s(w,W),t;  //define two strings, one empty, one with horizontal characters
    s = C+s+C+"\n";        //assemble a horizontal bar
    for(int i=h;i--;) 
        t=t+H+"\n";        //assemble a vertical bar
    std::cout<<s+t+s+t+s;  //print
}

在C ++中,不允许像在C中那样声明没有类型的函数。但是,像函数一样的宏是完全可能的。还请注意,除非您在宏的最后一行中每行都添加一个“ \”,否则将取消编译版本。您可以通过删除{}来节省两个额外的字节,但是您不能连续两次使用宏。

用法:

int main() {
    f(4,2,'+','-','|')
    f(2,1,'@','#','i')
    return 0;
}

输出:

+----+
|
|
+----+
|
|
+----+
@##@
i
@##@
i
@##@

在线尝试


0

CJam,23个字节

riri)N*r2*\r*\@r**a3*\*

在线尝试!

输入按给定顺序进行,但应以空格分隔,而不要使用逗号。一些困难是为CJam的加入操作*获得正确的输入顺序;为了比较,重新安排输入可以节省4个字节

如果输入被配音,A B C D E则程序将执行以下操作:

ri     e# get A as integer
ri)N*  e# create B+1 newlines
r2*    e# create 2 Cs
\r*    e# join newlines with D (hereafter ND)
\@     e# bring A & Cs to the front
r*     e# create A Es
*      e# join, puts Es between Cs (hereafter CEC)
a3*    e# makes 3 copies of CEC strings
\*     e# join, puts NDs between CECs

0

Lua(5.2),144字节

k,a,p,q=loadstring,arg,io.write,print l,d=k"for i=3,a[1]do p(a[5])end",k"for i=1,a[2]do q(a[4])end"b=a[3]p(b)l()q(b)d()p(b)l()q(b)d()p(b)l()p(b)

在线尝试!(编码地面)

它现在应该输出类似的内容:

+@@@@@+
l
l
+@@@@@+
l
l
+@@@@@+

自己的输入: 7 2 + l @

您可以在project-> compile选项中更改输入,然后更改值,每个值均与示例中相同,但不以逗号分隔,而是以空格分隔。


0

QBIC,44个字节

::;;;X=A[q,a-2|X=X+C]X=X+A ?X[1,2|[1,b|?B]?X

说明

::;;;                 Get the input parameters, 2 numbers and 3 strings
X=A[q,a-2|X=X+C]X=X+A Build the horizontal string: The corner string, x-2 times the filler string and another corner
?X                    Print the horizontal string
[1,2|                 Do the next thing twice
[1,b|?B]?X            Print the right number of vertical strings, then the horizontal string.

0

PHP,94字节

<?list($v,$h,$p,$d,$r)=$_GET[a];for($h++;$i<=2*$h;)echo$i++%$h?$d:str_pad($p,$v-1,$r).$p,"\n";

输入格式与建议字符串相同的数组


如果使用,"\n"而不是."\n",则可以删除三元组的parens。
泰特斯(Titus)

for($h++;$i<=2*$h;)$i++%$h保存另一个字节。
泰特斯

$v-1只为给出3个水平字符[5,2,+,|,-]水平字符数,不包括左右边缘字符
Titus

@Titus是正确的,我已回滚您的编辑。5水平表示3个具有2个边的字符=5。看一下问题。而对于其他的想法谢谢
约尔格Hülsermann
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.