首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >运算符ASCII art

运算符ASCII art
EN

Code Golf用户
提问于 2016-11-07 15:45:59
回答 5查看 3.3K关注 0票数 22

挑战

给定下面列表中的ASCII运算符和一个数字n,使用该运算符作为字符,绘制出运算符的ASCII表示形式,该运算符的线段具有长度n。

输入

一个来自列表= + - x /的ASCII字符和一个整数n ( n >= 1 )。(我使用的是x而不是*/,而不是÷,但您可以使用任何一种,无论哪一种更简单)。对于+x,您只需处理奇数就可以避免对齐问题。

输出

由长度为n的字符组成的运算符的ASCII绘图。水平部分应在字符之间留有空格,以抵消垂直/水平距离的差异。

规则

这是密码-高尔夫,所以以字节为单位的最短代码将获胜。标准漏洞是被禁止的。

示例

输入:+ 3

代码语言:javascript
运行
复制
  +
+ + +
  +

输入:= 4

代码语言:javascript
运行
复制
= = = =
= = = =

输入:= 10

代码语言:javascript
运行
复制
= = = = = = = = = =
= = = = = = = = = =

输入:- 2

代码语言:javascript
运行
复制
- -

输入:/ 10

代码语言:javascript
运行
复制
         /
        /
       /
      /
     /
    /
   /
  /
 /
/

输入:x 7

代码语言:javascript
运行
复制
x     x
 x   x
  x x
   x
  x x
 x   x
x     x
EN

回答 5

Code Golf用户

发布于 2016-11-07 17:36:48

Scala,275个字节

代码语言:javascript
运行
复制
(i,c)=>if(c<44){val b=(" "*(i-1)+"+\n")*((i-1)/2)
b+"+ "*i+"\n"+b}else
if(c<46)"- "*i else
if(c<48)i-1 to(0,-1)map(" "*_+"/\n")mkString else
if(c<62)"= "*i+"\n"+"= "*i
else{val a=0 to i/2-1 map(x=>" "*x+"x"+" "*((i/2-x)*2-1)+"x"+" "*x+"\n")mkString;a+" "*(i/2)+"x"+a.reverse}

用法:

代码语言:javascript
运行
复制
val f:((Int,Char)=>String)=...
print(f(10, '/'))

解释:

代码测试char的ascii值,以选择生成映像的正确方式。所述运算符的ascii值为:('+' -> 43), ('-' ->45), ('/' -> 47), ('=' -> 61), ('x' -> 120)

代码语言:javascript
运行
复制
(i,c)=>                              //define a function
  if(c<44){                            //if c is a plus
    val b=(" "*(i-1)+"+\n")*((i-1)/2)    //define the top/bottom part b as (i-1)/2 times (i-1) spaces, a plus sign and a newlineine
    b+"+ "*i+"\n"+b                      //return b, i times a plus and a space, a newline and b
  }else if(c<46)                       //if c is a '-'
    "- "*i                               //return "- " repeated i times
  else if(c<48)                        //if c is a '/'
    i-1 to(0,-1)                         //create a range from i-1 to 0 in steps of -1
    map(" "*_+"/\n")                     //map each number to that number of spaces plus a "/" and a newline
    mkString                             //join them together
  else if(c<62)                        //if c is '='
    "= "*i+"\n"+"= "*i                   //return "= " repeated i times, a newline and "= " repeated i times again
  else{                                //if c if 'x'
    val a=                               //define a, which will be the top part, as...
      0 to i/2-1                         //a range from 0 to i/2-1
      map(n=>                            //map each number n to
        " "*n                              //n spaces
        +"x"                               //plus an "x"
        +" "*((i/2-n)*2-1)                 //plus ((i/2)-n)*2-1 spaces
        +"x"                               //plus an "x"
        +" "*n                             //plus n spaces
        +"\n"                              //and a newline
      )mkString;                         //join them
    a+" "*(i/2)+"x"+a.reverse          //return a, i/2 spaces, "x" and the reverse of a 
  }
票数 6
EN

Code Golf用户

发布于 2016-11-08 11:12:33

JavaScript (ES6),156个字节

代码语言:javascript
运行
复制
(c,n)=>[...Array(n--)].map((_,i,a)=>a.map((_,j)=>({'/':a=i+j-n,x:a&&i-j,'-':a=i+i-n,'+':a&&j+j-n,'=':a+2&&a-2}[c]?' ':c)).join(c=='='|c<'/'?' ':'')).join`\n`

其中\n表示文字换行符。

票数 5
EN

Code Golf用户

发布于 2016-11-08 14:50:15

Mathematica,191个字节

代码语言:javascript
运行
复制
e=#~Mod~2==1&;StringRiffle[Normal@SparseArray[{a_,b_}/;(a+b==d+1||a==b)[e@b&&a<3,,e@b&&(a==⌈d/2⌉||b==d),,e@b&&a<2,,a+b==d+1][[Last@ToCharacterCode@#~Mod~10]]:>#,{d=#2,2d-1}," "],"
"," "]&

匿名函数。接受字符串和数字作为输入,并返回字符串作为输出。到目前为止还不是最短的,但写起来还是很有趣的。

票数 3
EN
页面原文内容由Code Golf提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://codegolf.stackexchange.com/questions/98890

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档