园林建筑-ASCII风格


18

我们房子外面有一个10x10米的正方形花园。我们想种草,做一个露台。我们已经决定了如何划分花园,但是我们还没有确定草量与露台之间的比例。

我们需要可视化帮助,而ASCII艺术显然是实现此目的的最佳方法。


挑战:

取一个整数,范围为[0,100](或可选的十进制[0,1]),代表应该露台花园的百分比。

一平方米的露台用破折号-或条形表示|。一平方米的草用井号表示#

  • 如果露台的数量小于或等于50%,则应在花园的左下角开始用条形遮盖,然后垂直然后水平填充。
  • 如果梯田的数量超过50%,则我们希望以另一种方式(用虚线代替条形)进行装饰,并从左下角开始,然后水平填充,然后垂直填充。

例子:

N = 25%
||########
||########
||########
||########
||########
|||#######
|||#######
|||#######
|||#######
|||#######

N = 75%
##########
##########
-----#####
----------
----------
----------
----------
----------
----------
----------

N = 47%
||||######
||||######
||||######
|||||#####
|||||#####
|||||#####
|||||#####
|||||#####
|||||#####
|||||#####

N = 50%
|||||#####
|||||#####
|||||#####
|||||#####
|||||#####
|||||#####
|||||#####
|||||#####
|||||#####
|||||#####

N = 51%
##########
##########
##########
##########
-#########
----------
----------
----------
----------
----------

N = 0%
##########
##########
##########
##########
##########
##########
##########
##########
##########
##########

N = 100%
----------
----------
----------
----------
----------
----------
----------
----------
----------
----------

这是因此以字节为单位的最短代码获胜。有关I / O的标准规则。这是ASCII技术,因此输出应类似于上面的示例。即输出["|", "|" ...]不正确。

一如既往地鼓励解释:)


2
我的第一印象是这两种情况仅意味着解决两个单独的打高尔夫球任务,但是有一个共同的结构值得我们在它们之间共享代码。
xnor

Answers:


7

APL(Dyalog),34个字节

匿名前缀函数,期望范围为0–100的整数。假设⎕IOI ndex O rigin)为0,这在许多系统上都是默认的。

{'#-|'[⊖⍉⍣s10 10100↑⍵⍴1+s50≥⍵]}

在线尝试!

{} lambda;是参数:

'#-|[] 使用以下数组索引字符串:

50≥⍵ 如果50大于或等于参数则为1,否则为0

s← 存放在s(用于s购物中心)

1+ 增量

⍵⍴ 周期性ř ESHAPE到参数长度

100↑ 取其中的第一百,用零填充

10 10⍴[R ESHAPE十行十列

 得到的是(分离s10 10

⍉⍣s 如果小则转置

 上下翻转


1
{ '# - |'[⊖(⍉+⍨)⍣(⍵≤50)⊢⍵>⍎¨∘,⍨⎕d。]}
NGN

1
非常接近我的方法:{⊖⍉⍣c⊢10 10⍴(⍵/'-|'⊃⍨c←⍵≤50),100/'#'}
越野选手埃里克(Erik the Outgolfer)'17年

@EriktheOutgolfer你只需要99/'#'
NGN

@ngn哦,对了,
Erik the Outgolfer

1
@ngn完全不同。你为什么不张贴它?
亚当

5

J39,38 37字节

[:|.>&50|:'#-|'"0{~_10]\100{.]$1+51>]

怎么运行的:

                     _10]\100{.]$1+51>] - prepares a 10x10 array of 0, 1 or 2
                                 1+51>] - 1 if N<=50 otherwise 2
                               ]$       - list of N copies of the above (1 or 2)
                          100{.         - the above list filled to 100 items with 0
                     _10]\              - reshape the list to a 10x10 array
           '#-|'"0                      - constant array of chars
                   {~                   - replaces each digit 0, 1 or 2 with #, - or |     
     >&50                               - is N>50 ? 
         |:                             - if not, transpose the array
                                          (in fact |: here is rearrange axes
                                           0 - transpose
                                           1 - leave it intact)        
 |.@                                    - reverse the order ot the rows

在线尝试!


{.一个越界参数是一个很好的技巧。
乔纳

2
31个字节:(]|.@|:_10{&'#|-'\100{.1+$)>&50
FrownyFrog

@ FrownyFrog-好代码!
Galen Ivanov

@Jonah-是的,有时候非常方便。我也尝试过 _100{. 将填充物放在开头,但是后来我需要反转每一行,所以我放弃了。
Galen Ivanov

5

JavaScript(ES6),84个字节

将输入作为[0 ... 100]中的整数。

n=>(y=9,g=x=>~y?'|-#'[[x,y][k=n/51|0]*9+x+y<n?k:2]+[`
`[x-9]]+g(x++-9?x:!y--):'')(0)

测试用例

格式化和评论

n => (                          // given the terrace percentage n
  y = 9,                        // and starting with y = 9
  g = x =>                      // g = recursive function taking x:
    ~y ?                        //   if y is greater than or equal to 0:
      '|-#'[                    //     pick the relevant character:
        [x, y][k = n / 51 | 0]  //       using k = 1 if n > 50, 0 otherwise
        * 9 + x + y             //       and comparing either 10 * x + y or 10 * y + x
        < n ?                   //       with n; if we're located over the terrace area:
          k                     //         append either '|' or '-'
        :                       //       else:
          2                     //         append '#'
      ] +                       //     end of character insertion
      [`\n`[x - 9]] +           //     append a linefeed if x == 9
      g(x++ - 9 ? x : !y--)     //     update (x, y) and do a recursive call
    :                           //   else:
      ''                        //     stop recursion
)(0)                            // initial call to g with x = 0


4

果冻,23个字节

<©51ị⁾|-ẋḷ"”#ẋ³¤s⁵Z®¡ṚY

在线尝试!

Ç在页脚之前更改数字以更改输入。在没有命令行参数的情况下用作程序中的monadic链接,这是允许的


很好的答案+1。23个字节作为单ȷ2³
声道

我也设法获得了24个字节,以为这也可能是灵感的来源。
Xcoder先生17年

@ Mr.Xcoder我确实想到了这一点,但是我不确定我是否可以假设这样的事情(仅适用于尼拉德程序吗?嗯……)
Erik the Outgolfer

请参阅我与丹尼斯的讨论
Xcoder先生17年

3

SWI Prolog,249个字节

p(X):-write(X).
r(X,Y,G):-G=<50,10*X-Y+1=<G,p('|').
r(_,_,G):-G=<50,p('#').
r(X,Y,G):-(10-Y)*10+X>G,p('#').
r(_,_,_):-p('-').
l(_,11,_):-nl.
l(X,Y,G):-r(Y,X,G),Z is Y+1,l(X,Z,G).
a(10,G):-l(10,1,G).
a(Y,G):-l(Y,1,G),Z is Y+1,a(Z,G).
s(G):-a(1,G),!.

解决方案非常简单。过程a创建行,l将char写入行中的列,并r确定应打印什么字符。


2
G<51应该工作代替G<=50
Laikoni

3

MATL,26字节

'|-#'100:i>~o10eG50>?!E]P)

在线尝试!验证所有测试用例

说明

'|-#'     % Push this string
100:      % Push array [1 2 ... 100]
i         % Input a number and push it
>~        % Less than or equal (element-wise)? This transforms the
          % array into [true ... true false ... false]
o         % Convert to double. True becomes 1, false becomes 0
10e       % Rehaspe into 10-row matrix, in column-major order
G         % Push input
50>       % Greater than 50?
?         % If so
  !       %   Transpose
  E       %   Multiply by 2 (element-wise). So 0 remains as 0, and
          %   1 becomes 2
]         % End
P         % Flip vertically
)         % Index into string, modularly. So 1 corresponds to '|',
          % 2 to '-', and 0 to '#'
          % Implicitly display

3

Python 2,85个字节

T=j=10
n=input()+T
while j:print([(n-j)/T*'|',min(n-T*j,T)*'-'][n>60]+'#'*T)[:T];j-=1

在线尝试!

在这两种情况下,每行都在右边填充#长度10,这使我们可以在两种情况下共享该代码。数字10经常被使用,以致于别名可以T=10节省相当数量的字节。


无效!从输入51和之后,它都错过了一行。
暴民埃里克(Erik the Outgolfer)

@EriktheOutgolfer那些极端情况。
xnor

@EriktheOutgolfer谢谢,我认为这样可以解决问题?
xnor

看起来像是固定的。
暴民埃里克(Erik the Outgolfer)'17

2

红宝石92 82字节

->n{puts (r=0..9).map{|y|r.map{|x|n>(n>50?100-y*10+x:x*10+9-y)?"|-"[n/51]:?#}*''}}

在线尝试!

怎么运行的:

网格中的每个单元格都有一个从左下角开始并根据n的值水平或垂直进行的渐进数:

如果为n>50,则数字为100-y*10+x否则x*10+9-y


2

木炭,25字节

NθGTχ#↶F÷θχ⟦χ⟧﹪θχ¿›θ⁵⁰‖T↖

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

Nθ                          Input integer into q
  G                         Draw filled polygon
   T                        Directions Right, Down, Left
    χ                       Size 10
     #                      Filled with `#`
      ↶                     Rotate cursor left (now points up)
       F÷θχ                 Repeat q/10 times (integer divide)
           ⟦χ⟧              Print 10 `|`s and move to the next column
              ﹪θχ           Print (q mod 10) `|`s
                 ¿›θ⁵⁰      If q > 50
                      ‖T↖   Reflect diagonally

1
@StewieGriffin糟糕,对角线错误。很抱歉没有检查。
尼尔

实际上是25个字符,但61 个字节,不是吗?
ZeroOne

@ZeroOne木炭使用其自己的代码页
尼尔,2017年

哦,我懂了!感谢您的解释。:)
ZeroOne

2

外壳,24字节

↔?T†▼'-≤50⁰S↑C10+R⁰'|∞'#

在线尝试!

说明

↔?T†▼'-≤50⁰S↑C10+R⁰'|∞'#  Input is a number, say n=12
                     ∞'#  Infinite string of #s: "#######...
                +         Prepend to it
                   '|     the character |
                 R⁰       repeated n times: "||||||||||||####...
             C10          Cut to pieces of length 10: ["||||||||||","||##########","##..
           S↑             Take first 10 pieces.
 ?     ≤50⁰               If n is at most 50,
  T                       then transpose,
   †▼'-                   else take minimum with '-' for each character.
↔                         Reverse, implicitly print separated by newlines.

1

SOGL V0.12,21个字节

┐* #M*+Mm√H.M»>?H§┐┌ŗ

在这里尝试!

说明:

┐*                     push a vertical bar repeated input times
   #M*                 push "#" repeated 100 times
      +                add the two together
       Mm              mold to a length of 100
         √             convert to a square
          H            rotate clockwise
           .M»>?       if the input is greater than 50
                H        rotate the array clockwise again
                 §       reverse it horizontally
                  ┐┌ŗ    replace "|" with "-"

1

直流210个 197字节

[256r^1-255/]sx?dddI/dsT9r-sYI%ddIr-sqdsesm-I/sN[[lNlxx124*PIlN-lxx35*PIPlq1-dsq0<o]dsoxlN1+sNledsq0<oq]sJ50!<J[Ilxx35*PIPlY1-dsY0<E]sElY0<E[lmlxx45*PIlm-lxx35*PIP]sClTI>C[Ilxx45*PIPlT1-dsT0<Z]dsZx

在线尝试!


1

APL(Dyalog Classic),33字节

f←{'#-|'[⊖(⍉+⍨)⍣(⍵≤50)⊢⍵>⍎¨∘.,⍨⎕d]}

在线尝试!

根据阿达姆的回答

⎕d 是字符串 '0123456789'

∘., 笛卡尔积

与自己

⍎¨ 评估每个-得到0..99的10x10矩阵

⍵> 布尔矩阵的参数 较大的

充当分隔符

(⍉+⍨)⍣(⍵≤50)如果⍵≤50将矩阵加倍(+本身)并转置(

垂直反转

'#-|'[ ]'#-|'用矩阵的每个元素索引字符串


这个解释很好,恕我直言。
亚当

1

q,51个字节

{-1@'reverse$[i;::;flip]10 10#@[100#"#";til x;:;"|-"i:x>50];}

1

视网膜72 62字节

.+
$*|
T`|`-`.{51,}
$
100$*#
M!10`.{10}
O$s`(?<!-.*)\S
$.%`
O`

在线尝试!链接包括测试用例。编辑:在@MartinEnder的帮助下,保存了10个字节。说明:

.+
$*|

重复|给定次数

T`|`-`.{51,}

但是,如果输入至少为51,请将其更改为-s。

$
100$*#

追加100 #秒。

M!10`.{10}

分成10组,每组10个,丢弃任何剩余的东西。

O$s`(?<!-.*)\S
$.%`

如果输入至少为51,则转置结果。

O`

排序结果。

替代解决方案,也是62个字节:

.+
$*|
T`|`-`.{51,}
$
100$*#
M!10`.{10}
O`
O$^s`\S(?!.*-)
$.%`

转置之前进行排序可以在转置条件上节省一个字节,但要花费一个字节才能以正确的顺序获取结果。


您不需要#在第一O阶段,因为$.%`最多9。您还可以通过避免循环而节省一些字节,而不必在循环的最后进行另一个排序阶段,例如:tio.run / ## K0otycxL / ...甚至可能有一种更短的方法将该M阶段的结果重新排列为最终形状。
Martin Ender's

是的,例如,您可以在O舞台之后将平移的舞台移到右侧M,这样您就可以继续使用先行代替后退。
Martin Ender's

@MartinEnder感谢您的提示;我还可以打更多的字节。
尼尔,2017年


0

PHP,119 + 1字节

$r=str_pad("",100,"#");for($x=50<$n=$argn;$n--;)$r[90+($x?$n%10*2-$n:$n/10-$n%10*10)]="|-"[$x];echo chunk_split($r,10);

与管道一起运行-nR在线尝试


0

果冻,24字节

³<51
ȷ2Ḷ<s⁵ZḤ$Ç¡Ṛị“-|#”Y

在线尝试!

怎么运行的

我使用太多上标...

³<51 ~ Helper link.

³    ~ The input.
 <   ~ Is smaller than
  51 ~ 51?
     ~ Yields 1 for truthy, 0 for falsy.

ȷ2Ḷ<s⁵ZḤ$Ç¡Ṛị“-|#”Y ~ Main link.

ȷ2                  ~ 1e2 (i.e compressed 100).
  Ḷ                 ~ Lowered range. Yields [0, 100) ∩ ℤ.
   <                ~ Is smaller than the input? (element-wise).
    s⁵              ~ Split into sublists of length 10.
         Ç¡         ~ Repeat <last link as a monad> times (either 1 or 0 times).
      ZḤ$           ~ Zip (transpose) and unhalve element-wise.
           Ṛ        ~ Reverse.
            ị       ~ Modular, 1-based indexing into...
             “-|#”  ~ The literal string "-|#".
                  Y ~ Join by newlines.

0

R,102字节

n=scan();m=matrix("#",y<-10,y);m[0:n]="if"(n<51,"|","-");write("if"(n>50,m[,y:1],t(m[y:1,])),"",y,,"")

在线尝试!

n从stdin 读取并打印花园到stdout。

说明:

n=scan()               # read from stdin
m=matrix("#",10,10)               # create 10x10 matrix of "#"
m[0:n]="if"(n<51,"|","-")         # set the first n entries in m to the appropriate character
m="if"(n>50,                      # prepare for printing using write
       m[,10:1],                  # reverse m left to right
       t(m[10:1,]))               # flip m top to bottom and transpose
write(m,"",10,,"")                # write m to stdout in 10 columns with no separator

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.