星星造星


17

介绍

这个想法是使用星号(星号)*在指定的尺寸上显示一个ascii-art星。维度是一个大于或等于1个的输入数字,用于指定星形的上点的高度。这里的星星是六颗尖的星星,从图片的角度看,它们的大小更大。

在所有情况下,恒星的ascii-art表示都应显示为两个重叠的三角形,如以下示例所示。

参数化

下图和数据表描述了星形的前七个尺寸的属性。每个参数随着ñ增加以算术级数增长,除了ñ=1个不同

在此处输入图片说明

在此处输入图片说明

例子

对于输入1(简并的情况),程序输出应如下所示:

  *
*****
*****
  *

输入2:

    *
   ***
*********
 *******
*********
   ***
    *

(3)

       *
      ***
     *****
***************
 *************          
  ***********
 *************
***************
     *****
      ***
       *

(5)

             *
            ***
           *****
          *******
         *********
***************************
 *************************
  ***********************
   *********************
    *******************
   *********************
  ***********************
 *************************
***************************
         *********
          *******
           *****
            ***
             *

挑战

您的任务是创建一个将接受数字N作为输入的函数或程序,然后仅使用字符和*字符输出适当大小的星形。

  • 您可以假设输入值始终为正整数。
  • 输出行上的尾随空白可以。
  • 程序算法对于任何ñ输入都应足够通用,以产生星图输出。当然,由于显示器输出尺寸的原因,存在实际限制。
  • 输出应打印到STDOUT。
  • 禁止出现标准漏洞
  • 这是因此所有常规高尔夫球规则都适用。

计分

这是因此字节数最短的代码胜出!


5
您还说它可以是一个函数,但是必须“打印到STDOUT”。这是故意的吗?
发布Rock Garf Hunter,

5
是的,一般的构造规则会很棒...例如,我不确定输入是否均匀,例如N = 4 ...
digEmAll

7
请不要说“产生星星的算法是编程挑战的一部分。” 这不是大多数高尔夫球手在进行ASCII艺术挑战赛时喜欢的东西,并且听起来像是在尝试抚平指定者的责任。就是说,我很惊讶人们对这里的结构不清楚。就像挑战说的那样,这是两个重叠的三角形。明确说明三角形的大小和偏移量是否有帮助?
xnor19年

9
@TimPederick关于N = 1的好建议是不同的。我写了一个便条,这样求解者就不会错过这一点。我认为,如果没有这种特殊情况,挑战会更好。
xnor19

4
@xnor:因为n = 1是不同的,所以我无法推断出通用规则...并且IMO规则应始终为ASCII艺术指定,否则我完全可以打印超出定义范围的任何内容例子;)
digEmAll

Answers:


7

05AB1E,21字节

3*s≠-L·<sÅ0«Âø€à'*×.C

在线尝试! 或作为测试套件

说明

3*                     # multiply input by 3
  s≠-                  # subtract 1 if the input isn't 1
     L                 # push range [1 ... (3*n-(n!=1))]
      ·<               # multiply by 2 and subtract 1 to get odd numbers
        sÅ0«           # append n zeroes
            Âø         # zip with a reversed copy
              ۈ       # get the largest number in each pair
                '*×    # repeat "*" for each number in the list
                   .C  # format centered

1
您的输出星星看起来正确。
迈克尔·卡拉斯

1
我可以找到很多3*s≠-L·<类似like 6*s≠·-ÅÉ或的替代方案≠3/-6*ÅÉ,但不幸的是,没有其他替代方案更短。:)
凯文·克鲁伊森

xs3*<ŸRsLì'*×.º.C.∊19 :)。我撒谎了,这对1.无效。
魔术章鱼缸

7

Haskell,114个字节

建立一个g接受数字并生成IO单子的函数,该单子将星星打印到STDOUT。我认为可以。

f=replicate
a%b=mapM_(\n->putStrLn$f(a-n)' '++f(2*n-3)'*')$zipWith max<*>reverse$[2..a]++f b 0
g 1=4%1
g a=(3*a)%a

在线尝试!

说明

首先让我们谈谈lambda。

\n->putStrLn$f(a-n)' '++f(2*n-1)'*'

这需要一个数字n来绘制为星形。我们先打印出两倍的星星,然后再打印1颗,然后将其填充到图像尺寸的右边。我们将其填充在右侧a空格以使星线居中。我们可以使用此lambda绘制每条线。

从这个lambda中我们创建(%)(%)首先从mapM_我们的lambda做起,将轮廓变成形状。

现在,我们要做的只是列出星星的个人资料列表。我们可以这样做:首先用制作一个三角形[1..a],然后用一些零填充++replicate b 0。如果我们取三角形的轮廓并将其反转,我们将获得恒星的另一半。为了强加它们,我们只需要创建一个新的轮廓,其中每个条目都是两个三角形中的最大值。这是zipWith max

然后,我们在以下两种方法之一调用这个:作为3%1用于输入1(3*a-1)%a其他。

从这里开始,我们需要对一些值进行修整以剃一些字节。由于3*a-1时间较长,因此我们将其他一些值偏移了1,以便所有内容都取消,并且得到了预期的行为3*a。即我们在开始我们的名单2,而不是1和做2*n-3的,而不是2*n-1来弥补的变化。

替代版本,114字节

这个构建了一个无点功能 (%)<*>min 2

f=replicate
a%b=mapM_(\n->putStrLn$f(3*a-n)' '++f(2*(n-b)+1)'*')$zipWith max<*>reverse$[b..3*a]++f a 0
(%)<*>min 2

在线尝试!

ñ>1个

f=replicate
g a=mapM_(\n->putStrLn$f(3*a-n)' '++f(2*n-3)'*')$zipWith max<*>reverse$[2..3*a]++f a 0

在线尝试!

ñ=1个

f=replicate
g a=mapM_(\n->putStrLn$f(3*a-n)' '++f(2*n-1)'*')$zipWith max<*>reverse$[1..3*a]++f a 0

在线尝试!


您的输出现在看起来不错。
Michael Karas

6

[R 125个 107 101字节

function(n,S=3*n+!n-1,P=pmax(I<-c(2:S*2-3,!1:n),rev(I)),`~`=strrep)write(paste0(' '~S-1-P/2,'*'~P),1)

在线尝试!

  • -24感谢@Giuseppe

先前(不同)的方法:

[R 150个 148 136 135 130 128字节

function(n,G=n<2,K=4*n-1+G)for(i in 1:K-1)cat(`[<-`(rep(' ',W<-3*n-2+G),1+W+c(-i:i*(i<K-n),-(j=K-i-1):j*(i>=n)),'*'),sep='','
')

在线尝试!

  • -14感谢@Kirill L.
  • -1感谢@ t-clausen.dk
  • -7感谢@Giuseppe

是的,我也不喜欢那些重复的S [] =分配,看起来像这样的作品
基里尔L.

太好了!我在想类似的东西...但是我在吃晚饭:P
digEmAll

似乎您可以保存一个字节:i> n-1可以重写为i> = n
t-clausen.dk

@ t-clausen.dk:是的,谢谢!
digEmAll

1
较新版本上的117个字节
Giuseppe

5

Python 2101 99 97字节

n=input()
x=2*(n>1)
for i in range(1,8*n,2):print('*'*[i,8*n-i-x][i+x>n*6or i/n/2%2]).center(6*n)

在线尝试!

-2个字节,感谢Lynn


我可以肯定地说,选择器表达式中不需要括号,因此i+x>n*6or可以节省两个字节。
林恩

@Lynn谢谢:)
TF

你甚至可以去i+x>~i/n/2%2*6*n或类似的东西i+x>3*n*(~i/n&2)(包括96个字节。)
林恩

5

的JavaScript(V8)  101  108个字节

编辑:+ 7字节以打印到STDOUT

n=>print((k=3*n+!~-n,g=y=>++y<k+n?`
`.padEnd(w=k-Math.max(y>n&&n-y+k,y<k&&y)).padEnd(2*k+~w,'*')+g(y):'')``)

在线尝试!

已评论(无print

n => (                    // n = input
  k =                     // k is half the maximum width of the star + 1.5
    3 * n + !~-n,         // k = 3n if n > 1 or 4 if n = 1
  g = y =>                // g = recursive function taking y
    ++y < k + n ?         //   increment y; if y is less than k + n:
      `\n`                //     append a line feed
      .padEnd(            //     append w - 1 leading spaces:
        w =               //       where w is defined as
          k -             //       k minus
          Math.max(       //       the maximum of:
            y > n         //         - true (coerced to 1) if y > n
            && n - y + k, //           or n - y + k otherwise (bottom triangle)
            y < k &&      //         - true (coerced to 1) if y < k
            y             //           or y otherwise (top triangle)
          )               //       end of Math.max()
      )                   //     end of padEnd()
      .padEnd(            //     append 2 * (k - w) - 1 stars
        2 * k + ~w,       //       by padding to 2 * k - w - 1
        '*'               // 
      ) +                 //     end of padEnd() 
      g(y)                //     append the result of a recursive call
    :                     //   else:
      ''                  //     stop recursion
)``                       // initial call to g with y = [''] (zero-ish)

提防这个挑战似乎需要将您的输出直接打印到控制台。从技术上讲,这不符合该要求。
发布Rock Garf Hunter,

@ SriotchilismO'Zaic感谢您告诉我。现在,此问题已“修复”。
Arnauld

3

果冻,21 字节

×3’+ỊR;Ṭ»Ṛ$”*ẋz⁶ṚZŒBY

接受打印到STDOUT的正整数的完整程序。

在线尝试!或见一个测试套件

怎么样?

×3’+ỊR;Ṭ»Ṛ$”*ẋz⁶ṚZŒBY - Main Link: integer, n   e.g. 3
 3                    - three                        3
×                     - multiply (n by)              9
  ’                   - decrement                    8
    Ị                 - insignificant (abs(n)<=1)?   0
   +                  - add                          8
     R                - range                        [1,2,3,4,5,6,7,8]
       Ṭ              - un-truth (n)                 [0,0,1]
      ;               - concatenate                  [1,2,3,4,5,6,7,8,0,0,1]
          $           - last two links as a monad:
         Ṛ            -   reverse                    [1,0,0,8,7,6,5,4,3,2,1]
        »             -   maximum (vectorises)       [1,2,3,8,7,6,7,8,3,2,1]
           ”*         - an asterisk character        '*' 
             ẋ        - repeat (vectorises)          ["*","**",...]
               ⁶      - a space character            ' '
              z       - transpose with filler        ["***********"," ********* ",...]
                Ṛ     - reverse                      ["   *   *   ","   ** **   ",...]
                 Z    - transpose                    ["       *","      **",...]
                  ŒB  - bounce (vectorises)          ["       *       ","      ***      ",...]
                    Y - join with newline characters "       *       \n      ***      \n..."
                      - implicit print


2

果冻,21字节

×’»ɗ3”*xⱮz⁶ɓ⁶x;»Ṛ$ŒBY

在线尝试!

单子链接接受单个整数作为其左参数,并返回以星号作为其输出的换行符分隔的Jelly字符串。当以完整程序运行时,会将星号隐式打印到STDOUT。

说明

   ɗ3                 | Last three links as a dyad with 3 as right argument:
×                     |   Multiply (by 3)
 ’                    |   Decrease by 1
  »                   |   Maximum of this (and 3)
     ”*xⱮ             | An implicit range from 1 to this many asterisks
         z⁶           | Zip with space as filler
           ɓ          | Start a new dyadic chain with the input as left argument and the list of asterisks as right argument
            ⁶x        | Input many spaces
              ;       | Concatenated to the asterisk list
                 $    | Last two links as a monad:
               »Ṛ     |   Maximum of this list and its reverse
                  ŒB  | Bounce each list (i.e. mirror it without duplicating the middle entry)
                    Y | Join with newlines

1
我感谢您×3’»3以相同的长度^ _ ^ 编写时尚的开始
Lynn

1
@Lynn在我这样做的时候,我以为我将要使用另一种快速方式来合并多个链接,这意味着我可以在允许的最大4个范围内这样做。但是,一旦决定使用ɓ它就没关系,但我保留了它,因为我仍然喜欢它!
尼克·肯尼迪

2

木炭,25字节

G↙θ←⁺⊗θ¬⊖θ↑⊗θ↘⊕θ*‖O¬C⁰¬⊖θ

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

G↙θ←⁺⊗θ¬⊖θ↑⊗θ↘⊕θ*

绘制一个不规则的五边形,代表恒星的右上角四分之一,但使用特殊的外壳1使该行增加一列。

‖O¬

反思完成明星。

C⁰¬⊖θ

更多特殊的外壳使星星1多排几颗。

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

∧¬⊖θ*G↗↓⊖׳N*‖O‖OO↓∧⊖θ⊖⊗θ

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

∧¬⊖θ*

*对于的情况,请多打印一些1

G↗↓⊖׳N*

绘制一个适当大小的三角形的左半部分。

‖O

反射以完成三角形。

‖OO↓∧⊖θ⊖⊗θ

用反射将其重叠,除非是 1,在这种情况下只需对其进行反射即可。

14个字节,不带特殊框1

G<⊖׳N*‖OO↑⊖⊗θ

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

G<⊖׳N*

画一个适当大小的三角形。

‖OO↑⊖⊗θ

将其与反射重叠。


2

Perl 6,74个字节

{$_ Z~|[R,] $_}o{.&{|((' 'x--$+$_*3~'*'x$++*2+1)xx$_*3-($_>1)),|($ xx$_)}}

在线尝试!

从字面上创建一个具有正确比例的三角形,并使用字符串或运算符将其与上下颠倒的副本重叠(~|)。输出为带有前导行和尾随行空白的行列表。

说明:

                {.&{                                                     }  # Anonymous code block
                      (                        )xx$_*3-($_>1)   # Repeat n*3-(n!=1) times
                       ' 'x--$+$_*3      # With a decreasing indentation
                                   ~'*'x$++*2+1  # Append an increasing triangle
                                       # This creates the triangle
                                                            ,|($ xx$_)  # And add some padding lines
{             }o   # Pass the triangle to the combining function
    Z~|            # Zip string bitwise or
 $_                # The list
       [R,] $_     # With its reverse

2

J53 50字节

' *'{~[:(+.|.),.@#&0,~[:(|.,}.)"1*@<:>:/~@i.@-~3*]

在线尝试!

不打高尔夫球

' *' {~ [: (+. |.) ,.@#&0 ,~ [: (|. , }.)"1 *@<: >:/~@i.@-~ 3 * ]

怎么样

使用函数表(如3年级时间表)通过使用>:(大于或等于)函数来构造三角形的一半。然后颠倒每一行,砍最后一列,并将两侧缝合在一起以获得完整的三角形(但由1和0组成)。n在底部添加零行。最后反转整个过程,并使用boolean或+.将其覆盖在原始对象上。然后将1转换为*,将0转换为空格。


真好!这是我的解决方案-长度相同,方法不同:在线尝试!
Galen Ivanov

1
谢谢。确实感觉可以打更多,但我尝试了其他几种方法,但没有做到。
乔纳

2

T-SQL,194个字节

@ 是输入值

@c 处理顶部三角形的宽度

@d 处理宽度底部三角形

@e包含@c或输出@d-这样可以节省一些字节

@f处理1的特殊情况作为输入。@c*@=3确定何时使用@f。比写便宜5个字节@c=3and @=1

DECLARE @ INT=8

,@c INT=1,@e INT=1,@d INT,@f INT=0SET @d=@*8-3r:PRINT
space(@*3-@e/2+@f/2)+replicate('*',@e-@f)SELECT
@c=nullif(@c,@*6-3)+2,@f=iif(@c*@=3,2,0),@d-=2-@f,@e=iif(@c>@d
or @c/2<@,@c,@d)IF @d>0goto r

在线尝试


1

杰普特 -R,25个字节

+5个字节 n=1

õ cUon3*U-´UÎ)®ç* êÃê!U û

尝试一下

õ cUon3*U-´UÎ)®ç* êÃê!U û     :Implicit input of integer U
õ                             :Range [1,U]
  c                           :Concatenate
   Uo                         :  Range [0,U)
     n                        :  Subtract each from
      3*U-                    :    Multiply U by 3 and subtract
          ´U                  :      Decrement U
            Î                 :      Get sign
             )                :End concat
              ®               :Map each Z
               ç*             :  Repeat "*" Z times
                  ê           :  Palindromise
                   Ã          :End map
                    ê!U       :If decremented U is 0, append reverse, else, palindromise
                        û     :Centre pad each line with spaces to the length of the longest
                              :Implicitly join with newlines and output
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.