图片输出视觉图


22

编写一个程序,为画框的肖像输入绘画的尺寸,消光宽度和画框宽度。程序应使用符号将图形输出X为绘画,+消光和#框架。这些符号必须以空格分隔。只要输出在视觉上符合条件,尾随空格就可以。输入可以是0

输入:3 2 1 2(宽度,高度,遮罩宽度,框架宽度)

输出:

前3和2是绘画的宽度和高度。 1是其周围的遮罩宽度。 2是整个框架的宽度。

以文本形式:

# # # # # # # # #
# # # # # # # # #
# # + + + + + # #
# # + X X X + # #
# # + X X X + # #
# # + + + + + # #
# # # # # # # # #
# # # # # # # # #

获胜代码以尽可能少的字节完成条件。


2
好挑战!对于未来的挑战,您可能需要使用The Sandbox
MilkyWay90

2
您介意输入的顺序不同吗?
vityavv

1
我们可以返回字符串列表吗?
MilkyWay90

5
任何输入都可以为零吗?
Laikoni

1
每行的末尾可以有一个空格吗?
路易斯·门多

Answers:


5

Python 2,98个字节

w,h,a,b=input()
a*='+'
b*='#'
for c in b+a+h*'X'+a+b:print' '.join(min(c,d)for d in b+a+w*'X'+a+b)

在线尝试!

严格按照规范打印以空格分隔的网格。我发笑*=用于转换ab从数字到字符串。

Python 3可以避免' '.join使用来节省一些字节,使用f字符串和赋值表达式可以节省更多的字节。感谢Jo King提供-2个字节。

Python 3,93字节

def f(w,h,a,b):a*='+';b*='#';[print(*[min(c,d)for d in b+a+w*'X'+a+b])for c in b+a+h*'X'+a+b]

在线尝试!


我已经出气了!出色的工作,似乎打得很好
MilkyWay90

高尔夫不错!非常聪明。
乔治·哈里斯

4

的JavaScript(ES6), 118个113  107字节

(w,h,M,F)=>(g=(c,n)=>'01210'.replace(/./g,i=>c(i).repeat([F,M,n][i])))(y=>g(x=>'#+X'[x<y?x:y]+' ',w)+`
`,h)

在线尝试!

已评论

(w, h, M, F) => (       // given the 4 input variables
  g = (                 // g = helper function taking:
    c,                  //   c = callback function returning a string to repeat
    n                   //   n = number of times the painting part must be repeated
  ) =>                  //
    '01210'             // string describing the picture structure, with:
    .replace(           //   0 = frame, 1 = matte, 2 = painting
      /./g,             // for each character in the above string:
      i =>              //   i = identifier of the current area
        c(i)            //   invoke the callback function
        .repeat         //   and repeat the result ...
        ([F, M, n][i])  //   ... either F, M or n times
    )                   // end of replace()
)(                      // outer call to g:
  y =>                  //   callback function taking y:
    g(                  //     inner call to g:
      x =>              //       callback function taking x:
        '#+X'           //         figure out which character to use
        [x < y ? x : y] //         according to the current position
        + ' ',          //         append a space
      w                 //       repeat the painting part w times
    )                   //     end of inner call
    + '\n',             //     append a line feed
  h                     //   repeat the painting part h times
)                       // end of outer call

3

MATL,24字节

&l,ithYaQ]'#+X'w)TFX*cYv

输入为:高度,宽度,遮罩宽度,框架宽度。

在线尝试!

说明

&l      % Take height and width implicitly. Push matrix of that size with all
        % entries equal to 1
,       % Do twice
  i     %   Take input
  th    %   Duplicate, concatenate: gives a 1×2 vector with the number repeated
  Ya    %   Pad matrix with those many zeros vertically and horizontally
  Q     %   Add 1 to each entry 
]       % End
'#+X'   % Push this string
w)      % Index into the string with the padded matrix
TF      % Push row vector [1 0]
X*      % Kronecker product. This inserts columns of zeros
c       % Convert to char again. Char 0 is will be displayed as space
Yv      % Remove trailing spaces in each line. Implicitly display


2

木炭48 47 44字节

≔×NXθ≔×NXηFE+#×Nι«≔⁺ι⁺θιθ≔⁺ι⁺ηιη»Eη⪫⭆θ⌊⟦ιλ⟧ 

在线尝试!链接是详细版本的代码。注意:尾随空格。编辑:现在使用@xnor的算法。说明:

≔×NXθ≔×NXη

输入宽度和高度并将其转换为Xs的字符串。

FE+#×Nι

循环遍历字符+#,将其转换为其余两个输入所给定长度的字符串。然后循环这两个字符串。

«≔⁺ι⁺θιθ≔⁺ι⁺ηιη»

给绘画加上前缀和后缀,以用于抠图和取景。

Eη⪫⭆θ⌊⟦ιλ⟧ 

循环遍历字符串,使用最少的水平和垂直字符,然后对行进行两次间隔设置,以隐式方式在每行上打印每行。


2

05AB1E(旧版) / 05AB1E --no-lazy32 31 字节

и'Xׄ+#vyI©×UεX.ø}®FDнgy×.ø]€S»

按顺序接受输入height, width, matte, frame。如果挑战中指定的输入顺序严格(仍在等待OP进行验证),s则可以为+1字节添加(交换)。

--no-lazy在新版本的05AB1E中,需要Elixir编译器标志,因为由于对嵌套映射/循环的惰性求值,Elixir具有某些奇怪的行为(此处为无此标志的结果)。

在旧版05AB1E中在线尝试。
在带有--no-lazy标记的新版本05AB1E中在线尝试。

说明:

и              # Repeat the second (implicit) input the first (implicit) input amount of
               # times as list
 'X×          '# Repeat "X" that many times
„+#v           # Loop `y` over the characters ["+","#"]:
    y          #  Push character `y`
     I         #  Push the next input (matte in the first iteration; frame in the second)
      ©        #  And store it in the register (without popping)
       ×       #  Repeat character `y` that input amount of times
        U      #  Pop and store that string in variable `X`
    εX.ø}      #  Surround each string in the list with string `X`
    ®F         #  Inner loop the value from the register amount of times:
      Dнg      #   Get the new width by taking the length of the first string
         y×    #   Repeat character `y` that many times
             #   And surround the list with this leading and trailing string
   ]           # Close both the inner and outer loops
    S         # Convert each inner string to a list of characters
      »        # Join every list of characters by spaces, and then every string by newlines
               # (and output the result implicitly)



1

R,119字节

function(w,h,m,f)write(Reduce(function(x,y)rbind(y,cbind(y,x,y),y),rep(c("+","#"),c(m,f)),matrix("X",w,h)),1,w+2*f+2*m)

在线尝试!


1

蟒3.8(预发布)116个 115 113字节

lambda a,b,c,d:"\n".join((g:=['#'*(a+2*c+2*d)]*d+[(h:='#'*d)+'+'*(a+c*2)+h]*c)+[h+'+'*c+'X'*a+'+'*c+h]*b+g[::-1])

在线尝试!

第一次尝试打高尔夫球,很快就会得到改善。 a是宽度,b是高度,c是无光泽宽度,d是框架宽度。

-1个字节,使用:=运算符定义he * d

-2个字节,感谢Jo King建议我删除e和f参数

说明:

lambda a,b,c,d:          Define a lambda which takes in arguments a, b, c, and d (The width of the painting, the height of the painting, the padding of the matte, and the padding of the frame width, respectively).
    "\n".join(                       Turn the list into a string, where each element is separated by newlines
        (g:=                         Define g as (while still evaling the lists)...
            ['#'*(a+2*c+2*d)]*d+       Form the top rows (the ones filled with hashtags)
            [(h:='#'*d)+'+'*(a+c*2)+h]*c Form the middle-top rows (uses := to golf this section)
        )+
        [h+'+'*c+'X'*a+'+'*c+h]*b+       Form the middle row
        g[::-1]                      Uses g to golf the code (forms the entire middle-bottom-to-bottom)
    )

删除e分配会节省您两个字节,f分配并不会为您节省任何费用
Jo King

@JoKing哇,我发现快捷方式后忘了删除ef任务g
MilkyWay90

0

Javascript,158个字节

(w,h,m,f)=>(q="repeat",(z=("#"[q](w+2*(m+f)))+`
`)[q](f))+(x=((e="#"[q](f))+(r="+"[q](m))+(t="+"[q](w))+r+e+`
`)[q](m))+(e+r+"X"[q](w)+r+e+`
`)[q](h)+x+z)

可以稍微修剪一下

f=

(w,h,m,f)=>(q="repeat",(z=("# "[q](w+2*(m+f))+`
`)[q](f))+(x=((e="# "[q](f))+(r="+ "[q](m))+(t="+ "[q](w))+r+e+`
`)[q](m))+(e+r+"X "[q](w)+r+e+`
`)[q](h)+x+z)

console.log(f(3,2,1,2))


0

Perl 6、98字节

{map(&min,[X] map (($_='#'x$^d~'+'x$^c)~'X'x*~.flip).comb,$^a,$^b).rotor($b+2*($c+$d)).join("\n")}

在线尝试!

这是xnor的Python答案的一部分

Perl 6、115字节

->\a,\b,\c,\d{$_=['#'xx$!*2+a]xx($!=c+d)*2+b;.[d..^*-d;d..^a+$!+c]='+'xx*;.[$!..^*-$!;$!..^a+$!]='X'xx*;.join("
")}

在线尝试!

利用Perl 6的多维列表分配,大致打高尔夫球的匿名代码块。例如,@a[1;2] = 'X';将分配'X'索引为1的列表中索引为2 @a[1,2,3;3,4,5]='X'xx 9;的元素,并将索引3,4,5为的列表中所有具有索引的元素替换1,2,3'X'

说明:

首先,我们初始化列表作为a+2*(c+d)通过b+2*(c+d)的矩形# s。

$_=['#'xx$!*2+a]xx($!=c+d)*2+a;
State:
# # # # # # # # #
# # # # # # # # #
# # # # # # # # #
# # # # # # # # #
# # # # # # # # #
# # # # # # # # #
# # # # # # # # #
# # # # # # # # #

然后我们分配的内部矩形 + s

.[d..^*-d;d..^a+$!+c]='+'xx*;
State:
# # # # # # # # #
# # # # # # # # #
# # + + + + + # #
# # + + + + + # #
# # + + + + + # #
# # + + + + + # #
# # # # # # # # #
# # # # # # # # #

最后是Xs 的最里面的矩形。

.[$!..^*-$!;$!..^a+$!]='X'xx*;
# # # # # # # # #
# # # # # # # # #
# # + + + + + # #
# # + X X X + # #
# # + X X X + # #
# # + + + + + # #
# # # # # # # # #
# # # # # # # # #

0

果冻35 31 28 24字节

Dịx@“#+X+#”
⁽-Fç«þ⁽-ȥç$G

在线尝试!

在订单框中输入输入,遮罩,宽度,高度;逗号分隔。输出带有帧和遮罩的ASCII艺术图片。如果输入顺序很严格,则需要添加更多字节(按照我的原始帖子)。

根据@EriktheOutgolfer的答案进行的几次高尔夫比赛;我已经意识到字符是ASCII顺序的,但是没有想到如何最好地利用它,因此忘记了G。不过,他的答案仍然更好!


我从不使用Jelly编程,但是可以压缩43134,43234吗?编辑:我需要学习阅读,您提到它们确实可以每个4字节进行编码。但是输入顺序与这些数字是否可以编码有什么关系?:S
凯文克鲁伊森

@KevinCruijssen可以使用两个字节的语法编码的最大整数为32250;因为两者都超过了我无法保存字节。现在,我假设我可以交换一切,如果不允许的话可以还原!
尼克·肯尼迪

好的,我知道了。43134将需要3个编码字符,其中包括一个前导/尾随字符,以指示其已编码为5个字节。而且,由于第二个数字比第一个数字大100,Jelly也许有某种复制品吗?不确定动作是否可以在果冻中推入43134,重复,推入100,加号,配对以及更短?
凯文·克鲁伊森

@KevinCruijssen我最初尝试使用+0,100来保存任何内容。我想我可以使用尼拉德链来使用这样的事实:尼拉德³是100,但是如果允许我重新排序输入,则基数250的整数更好
尼克·肯尼迪


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.