ASCII冰淇淋


15

编写一个程序或函数,该程序或函数采用正整数N,并打印或返回N×N ASCII艺术字串,其上半部分是由组成的半圆(,下半部分是由组成的下指三角形V,并且用作填充的空格。

换句话说,制作一个ASCII冰淇淋蛋筒:(N = 17的输出)

      (((((      
    (((((((((    
  (((((((((((((  
  (((((((((((((  
 ((((((((((((((( 
 ((((((((((((((( 
(((((((((((((((((
(((((((((((((((((
VVVVVVVVVVVVVVVVV
 VVVVVVVVVVVVVVV 
  VVVVVVVVVVVVV  
   VVVVVVVVVVV   
    VVVVVVVVV    
     VVVVVVV     
      VVVVV      
       VVV       
        V        

例子

这是N = 1至5的输出。请注意,对于奇数N,三角形始终必须是较大的一半。

V

((
VV

(((
VVV
 V 

 (( 
((((
VVVV
 VV 

 ((( 
(((((
VVVVV
 VVV 
  V  

这是N = 101 pastebin。

这是一个非公开的Python 3参考实现:

N = int(input())
ic = [[' '] * N for _ in range(N)]
for y in range(N//2):
    for x in range(N):
        if (x - (N - 1) / 2)**2 + (y - (N - 1) / 2)**2 < (N / 2)**2:
            ic[y][x] = '('
for y in range(N//2, N):
    for x in range(y - N//2, N - (y - N//2)):
        ic[y][x] = 'V'
for line in ic:
    print(''.join(line))

细节

  • 从标准输入,命令行或函数参数获取输入。输出到stdout或类似文件,或者如果编写函数,则可以返回字符串。
  • 圆锥部分应与所有N的参考实现完全匹配。
  • 冰淇淋部分也没有必要精确的参考实现,只要它显然是一个半圆形的所有N.(这是使你不必有关在半圆由于舍入误差的细微差别担心匹配)
  • 不应有任何不必要的前导空格,但可能会有多余的尾随空格。
  • 输出可以选择包含尾随换行符。
  • 您可以选择使用任何其他3级不同的打印的ASCII到位的人物(V和空间。

计分

以字节为单位的最短提交获胜。决胜局(Tiebreaker)提交时间最久。


6
当我阅读标题时,我是唯一想到过“ IceCII ASCream”的人吗?
Sp3000

15
@ Sp3000 Jeez,我希望如此……
卡尔文的爱好

Answers:


7

CJam,46个字节

在线尝试。

{:Z{Z{Z(2./:R-zYR<):P#YR-zP#+Z2./P#>SP?}/N}fY}

我认为这目前正好模仿了最初的规范,这是我开始生成此答案时所必需的。通过使数学运算与原始规范的准确性降低,可能会节省一些字节,但是直到我发现这样做可以节省一个或两个以上字节的方法之前,我将保持原样。

说明

{               "Begin block";
  :Z{             "For each y from 0 to input-1";
    Z{              "For each x from 0 to input-1";
      Z(2./:R         "Calculate the radius as (input-1)/2.0";
      -z              "Calculate the horizontal distance from the center";
      YR<):P          "Calculate the power to raise distances to: (y<radius)+1
                       (This results in Euclidean distance being calculated for
                        the ice cream and Manhattan distance being calculated
                        for the cone)";
      #               "Raise the horizontal distance to the determined power";
      YR-zP#          "Calculate the vertical distance from the center and
                       raise it to the determined power";
      +               "Add the horizontal and vertical distances";
      Z2./P#          "Calculate the solid distance threshold and raise it to
                       the determined power";
      >SP?            "If the solid threshold is exceeded, produce a space;
                       otherwise, produce the determined power digit
                       (This results in ice cream being represented by the
                        digit '2' and the cone by the digit '1')";
    }/              "End x loop";
    N               "Produce a new line";
  }fY             "End y loop";
}               "End block";

这似乎是使用21而不是(V的?
Mark Reed

@MarkReed这是允许的。详细信息部分的最后一行。
2015年

3

印加2 129 123 121 111 107

这主要使用python示例中的公式,但是使用jot-dots和iotas而不是双循环。该i函数对该函数执行循环测试,该j函数在其上调用小写点。并且该k功能对该功能执行三角测试l。该c函数将j和的结果相加,l并将其整形为N×N。

编辑:-6将2个地图合并为1。
编辑:-2删除无用的缝隙。
编辑:更好的打字稿。
编辑:-10应用重复的表达式数组。
编辑:-4将重复表达式作为函数分解出来。
编辑:逐行注释。

q:y-(n-1)%2
i:[((n%2)^2)>+/(qx y)^2
j:(~[y%2)i.(~y)
k:2*[x>[|qy
l:(@1+~]y%2)k.(~y)
c:y y#((jn<y),ly){' (V' 

更详细地讲,入口点是一个c函数,它接受一个隐式命名为的参数y

c:y y#((jn<y),ly){' (V' 
         n<y            } assign y to 'n'
        jn<y            } call j(y)
              ly        } call l(y)
      ((    ),  )       } catenate the results
      (         ){' (V' } map 0 1 2 to ' ' '(' 'V'
  y y#                  } reshape to NxN

j函数接收与其y参数相同的输入值。

j:(~[y%2)i.(~y)
     y%2         } y divided by 2
    [            } floor
   ~             } iota. this generates the row indices 0..y/2
            ~y   } iota y. this generates the column indices 0..y
  (     )i.(  )  } jot-dot with the function i

这里的记号是双循环的。它i使用左右数组(0..n / 2和0..n)中元素的每种组合调用该函数。因此,i函数接收作为x所述ÿ表中的索引,并且它接收作为y所述X索引。这里的名字有点倒退:)。

i:[((n%2)^2)>+/(qx y)^2
     n%2                 } n divided by 2
    (n%2)^2              } squared
                 x y     } make a 2-element array (x,y)
                qx y     } call q on this array

哪里q

q:y-(n-1)%2
     n-1    } n minus 1
         %2 } divided by 2
  y-        } y minus that

回到 i

i:[((n%2)^2)>+/(qx y)^2
               (    )^2  } square the result from q(x,y)
             +/          } sum the two numbers
            >            } compare the left side (above) with the right (=> 0/1)
  [                      } floor

地板不需要。但是显然解释器中存在一个错误。

l功能与该j功能的相似之处在于使用了一个点。

l:(@1+~]y%2)k.(~y)
        y%2         } y divided by 2
       ]            } ceiling
      ~             } iota 0..ceil(y/2)-1
    1+              } add 1 => 1..ceil(y/2)
   @                } reverse => ceil(y/2)..1
               ~y   } iota y  0..y-1
  (        )k.(  )  } jot-dot using k

k函数产生一个按比例缩放的布尔值2,因此以后可以在映射中将其与冰淇淋值区分开。

k:2*[x>[|qy
     x       } k's left arg
         qy  } y-(n-1)%2
        |    } abs
       [     } floor
     x       } left-hand-side again
      >      } compare 
    [        } floor (should be unnecessary)
  2*         } scale by 2

tr实际上(通过删除REPL的提示删除制表符):

josh@Z1 ~/inca
$ ./inca2 <icecream | tr -d '\t'

c1
V

c2
((
VV

c3
(((
VVV
 V 

c4
 (( 
((((
VVVV
 VV 

c5
 ((( 
(((((
VVVVV
 VVV 
  V  

josh@Z1 ~/inca
$ 

2

Python 2中,193 192

不使用字符串,仅使用数学

N=input()
R=(N+1)/2;r=range(R)
s=lambda L,U:(10**U-10**L)/9
f=lambda N,m:s(0,N)+s(m,N-m)
g=lambda N,m:s(0,N)+s(m,N-m)*6
for i in r[1:]:print f(N,int(R-(2*R*i-i*i)**.5))
for i in r:print g(N,i)

s(L,U)返回形式为“- U最右边的L零,其余
f(N,m)数字为-”的数字,返回一个N位数字,其内部为21两边的m宽边框
g(N,m)相同,但7用于'颜色'内部截面的形状,因为它更紧密地匹配圆锥体的纹理

输出量

N=8         N=9
11122111    112222211
12222221    122222221
22222222    222222222
22222222    222222222
77777777    777777777
17777771    177777771
11777711    117777711
11177111    111777111
            111171111

非常独特的方法:)
卡尔文的爱好

如果我们也能看到冰淇淋的话,:P
Optimizer

2

Perl 6,175

非常简单的实现,不需要太多打高尔夫球,只是消除了多余的空格/标点符号:

sub MAIN($d){my$r=($d/2).Int;for 1..$r ->$n
{my$y=$n-$r;my$h=sqrt($r*$r-$y*$y).Int;my$w=2*$h+$d%2;say
' 'x($r-$h)~'('x$w};for 1..($d-$r) ->$y {say ' 'x($y-1)~'V'x($d-2*$y+2)}}
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.