画一些扩大的箭头


25

这个挑战是关于打印一系列不断增长的ASCII艺术箭头。我将用语言描述模式,但是看一下本系列开始的内容可能会更容易:

>
<
->
<-
-->
<--
--->
<---
---->
<----
----->
<-----
------>
<------
...

长度为n的箭头包含箭头(<>)和n-1破折号(-)。朝右的箭头先是破折号,然后是>。左箭头以开头<,后接短划线。该系列包括一个长度为n向右的箭头,然后是长度为n的左向箭头,其中n从1到无穷大。

要完成挑战,请编写一个程序或函数,该程序或函数接受一个输入,一个整数i >= 1并输出第一个i箭头。箭头是单独的,不是左右成对的,因此i=3您应该输出:

>
<
->

您可以返回字符串列表,或一个接一个地打印它们。如果要打印,则箭头必须由一些一致的定界符来定界,而不必像示例中那样是换行符。

这是,因此最少的字节获胜。



每行之前/之后可以有空格吗?
奥利维尔·格雷戈尔

@OlivierGrégoire是的,尾随空格是可以的。
帕维尔

和标题空格?
奥利维尔·格雷戈尔

@OlivierGrégoire是的,很好。
帕维尔

Answers:



8

R,69字节

for(i in 1:scan()-1)cat('<'[i%%2],rep('-',i/2),'>'[!i%%2],'
',sep='')

在线尝试!

  • -5个字节,感谢@Giuseppe
  • -3个字节,感谢@RobertS。

strrep强制使用第二个参数,integer因此您可以/代替%/%
Giuseppe

您也可以a通过建立索引0...(n-1)来完全摆脱:在线试用!
朱塞佩

我是个白痴...谢谢!:D
digEmAll

@Giuseppe:我也注意到Robert S删除的问题。我可以使用rep代替strrep并保存3个字节...(facepalm)
digEmAll

8

Java(JDK),81个字节

n->{for(int i=0;i<n;)System.out.printf(i%2<1?"<%s%n":"%s>%n","-".repeat(i++/2));}

在线尝试!

说明

n->{                  // int-accepting consumer
 for(int i=0;i<n;)    //  for each i from 0 to n-1 included
  System.out.printf(  //   output on stdout with a pattern
   i%2<1              //    if i is even:
    ?"<%s%n"          //     use the left-arrow pattern
    :"%s>%n",         //    else: use the right-arrow pattern
   "-".repeat(i++/2)  //    fill the "%s" in the pattern with i/2 dashes, and increment i
  );                  // 
}                     //


@candied_orange这不是独立的。
OlivierGrégoire'18

这样做怎么
candied_orange

@candied_orange一样:计数中需要导入。
奥利维尔·格雷戈尔

为什么import java.util.function.*;不算?
candied_orange

8

Haskell,41个 40字节

(`take`g">")
g p=p:('<':init p):g('-':p)

在线尝试!

普通的旧递归:从string p= 开始">",collect p<在的最后一个字符之前位于pa,-然后在之前放置一个递归调用p。采取n此列表的第一项。

编辑:-1字节感谢@xnor。


1
奇怪的更改以节省一个字节。
xnor

6

Commodore BASIC V2(C64),94字节

0inputn:fOi=1ton:oniaN1gO1:?"<";
1on-(i<3)gO2:fOj=1.5toi/2:?"-";:nE
2on-nOiaN1gO3:?">";
3?:nE

不能完全确定字节数,这是基于键入有效程序的文本表示形式。由于BASIC V2使用程序的“令牌化”表示形式,因此在磁盘上的时间要短一些(91字节)。

在线演示

稍微“解开”:

0 inputn:fori=1ton:oniand1goto1:print"<";    :rem read n from user, loop to n, if odd skip "<"
1 on-(i<3)goto2:forj=1.5toi/2:print"-";:next :rem skip for i<3, print (i-1)/2 times "-"
2 on-notiand1goto3:print">";                 :rem if even skip ">"
3 print:next                                 :rem newline and next loop iteration

6

自修改Brainfuck,55字节

将输入作为字符代码。
仅支持最多255个输入。
使用空字符分隔行。

巧合的是,所有的箭头绘制字符都用作BF命令。不幸的是,它不保存任何字节(当前)。

>>,[<<[-<.>>+<]<<.>>.+>>-[<<<<<.>>>>[-<+<.>>].>-<]>]<>-

在线尝试!

说明

 Code  |              Memory         | Output | Comment
-------+-----------------------------+--------+--------------------------
       | '<' '>' '-' [0]  0   0   0  |        |
>>,    | '<' '>' '-'  0   0  [x]  0  |        |
[      |                             |        |
       | '<' '>' '-'  l   0  [x]  0  |        | l = arrow length
<<[-<  |                             |        | copy l to next cell
.>>+<] |                             |        | and print '-'
       | '<' '>' '-' [0]  l   x   0  | -----  | there are l '-'s
<<.    | '<' [>] '-'  0   l   x   0  | >      |
>>.+   | '<' '>' '-' [1]  l   x   0  | <null> |
>>-    | '<' '>' '-'  1   l  [y]  0  |        | y=x-1
[      |                             |        | execute if y>0
<<<<<. | [<] '>' '-'  1   l   y   0  | <      |
>>>>   | '<' '>' '-'  1  [l]  y   0  |        |
[-<+<. |                             |        |
>>]    | '<' '>' '-'  L  [0]  y   0  | -----  | L=l+1
.      | '<' '>' '-'  L  [0]  y   0  | <null> |
>-<]>] |                             |        | decrement y
<>-    |                             |        | do nothing, used as data


5

Pyth,17个字节

m_W%d2+*\-/d2@"><

输出是字符串列表。在这里在线尝试。

m_W%d2+*\-/d2@"><"dQ   Implicit: Q=eval(input())
                       Trailing "dQ inferred
m                  Q   Map [0-Q), as d, using:
          /d2            Floored division of d by 2
       *\-               Repeat "-" the above number of times
      +                  Append to the above...
             @"><"d      Modular index d into "><" - yields ">" for even d, "<" for odd
                         - examples: d=4 gives "-->", d=7 gives "---<"
 _W                      Reverse the above if...
   %d2                   ... (d % 2) != 0
                       Implicit print result of the map

5

PowerShell62 56 50字节

param($n)(0..$n|%{($j='-'*$_)+'>';"<$j"})[0..--$n]

在线尝试!

从循环0来输入$n,每次迭代创建两个箭头的字符串。然后将这些索引0..--$n以提取正确数量的元素。

KGlasier节省了6个字节。


弄乱自己的解决方案后,我发现了一种减少您的字节数的方法:通过将循环包装在方括号中并直接建立索引,可以节省4个字节。即param($n)(0..$n|%{($j='-'*$_++)+'>';"<$j"})[0..--$n]。因此,现在您不必写$x两次。
KGlasier '18

您也可以通过不使用++in 来节省两个字节,($j='-'*$_++)因为您$_在其他任何地方都不使用。
KGlasier

1
@KGlasier太棒了-感谢出色的高尔夫!:)
AdmBorkBork

5

Python 3,53个字节

我的第一个代码高尔夫答案。

lambda x:[i%2*"<"+i//2*"-"+~i%2*">"for i in range(x)]

-10个字节,感谢Jo King


5

哈斯克尔51) 44字节

-7个字节感谢xnor(使用iteratelist-comprehension)!

(`take`do b<-iterate('-':)"";[b++">",'<':b])

在线尝试!

解释/取消包装

使用do-notation可为我们节省一个concat,而使用infix-notation则可使用来实现无点功能take,如果取消这些功能,则会得到:

f n = take n $ concat [ [b++">", '<':b] | b <- iterate ('-':) "" ]

5

Japt -m16 15 13 12字节

感谢Shaggy,节省了1个字节

g<i>)iUUz ç-

在线测试

说明:

-m            // Map the program through [0...Input); U becomes the iterative number
g<i>)iUUz ç-  
 <i>          // ">" prepended with "<", creating "><"
g             //   Get the char at index U, with index-wrapping
    i         // Insert:
     U        //   At index U, with index-wrapping
         ç-   //   "-" repeated:
      Uz      //     U/2 times


@蓬松的哈!非常聪明,谢谢!
奥利弗


4

MathGolf17 15字节

由于Jo King和Kevin Cruijssen节省了2个字节

{ï½'-*'>ï¥╛Å⌡\n

在线尝试!

说明

15字节的方法与我原来的解决方案不同,我对实现不屑一顾。

{                 start block or arbitrary length
 ï                index of current loop, or length of last loop
  ½               pop a : push(a//2 if int else a/2)
   '-             push single character "-"
     *            pop a, b : push(a*b)
      '>           push single character ">"
        ï         index of current loop, or length of last loop
         ¥        modulo 2
          ╛       if without else
           Å      start block of length 2
            ⌡     decrement twice
             \    swap top elements
              n   newline char, or map array with newlines

if/elseMathGolf 中的工作方式如何?我知道if-without-else和else-without-if语句如何工作,但是如何在MathGolf中使用if创建一个if {...} else {...} ¿呢?(也许我应该在聊天室中而不是在这里发布。。但是,如果我可以修复if-else,我也许可以节省1个字节。)
Kevin Cruijssen 18/12/13

1
@KevinCruijssen我认为它可用于接下来的两个命令/块。例如,¿12如果为true,则将按1,否则,¿Å3*Å1+将为2;如果为true,则将添加一个,否则将下一个元素增加三倍
Jo King

@KevinCruijssen if / else从代码中弹出两个运算符或块。乔金是他的榜样是正确的,但你也可以做¿{"foo"}{"bar"}¿1{2}
maxb

@JoKing我将添加一个TODO来为切片运算符修复文档。
maxb

1
使用@KevinCruijssen解决方案的15个字节
Jo King

4

Japt -m,14个字节

"<>"¬hUUz ç-)q

在线尝试!

更新了全新的方法。

说明:

                  #Implicitly map over the range [0..input) as U
"<>"              #The string "<>"
    ¬             #Split into the array ["<",">"]
     hU     )     #Replace the element at index U with wrapping:
           -      # The character '-'
          ç       # Repeated a number of times equal to
       Uz         #  U integer divided by 2
             q    #Join the array to a string

1
ç自动将其第一个参数转换为字符串,因此您可以删除'
奥利弗

1
u借助索引包装,您不需要此方法,因此可以为14个字节。
毛茸茸的

4

C(gcc)80 77 76 74 71字节

g(n,i,j){--n&&g(n);for(j=n%2,i=n/=2;putchar(~n?n---i*j?45:62-j*2:0););}

在线尝试!

-3个字节,包含ASCII的想法

-1与 \0代替\n

-5重新排列零件


输出包括结尾\0

g(n,i,j){
    --n&&g(n);              //draw smaller arrows first (if n>1)
    for(j=n%2,i=n/=2;       //j:!(input parity); i:arrow len-1=ceil(input)/2-1
        putchar(~n          //if n>=0, arrow is not yet completed
                ? n---i*j   //if not first (j==1) or last (j==0) char of arrow:
                  ? 45      // output '-'
                  : 62-j*2  // otherwise, output the appropriate arrow head
                : 0););     //\0 after arrow complete. putchar returns 0; loop terminates
}

可能更清楚吗?idk
仅ASCII的


@only ASCII是的,即使字节数没有影响,也应该更清楚。至于第二点..谢谢你的想法!设法削减到78。
attinat


您仍然!n--在第一个代码块中拥有XD
仅ASCII的

3

JavaScript(ES6),58个字节

返回以空格分隔的字符串。

n=>(g=p=>n--?k++&1?`<${p} `+g(p+'-'):p+'> '+g(p):'')(k='')

在线尝试!




3

木炭,16字节

NθFθ«⊘ι↓>‖T»Fθ‖T

在线尝试!链接是详细版本的代码。在最终偶然发现这个问题之前,我有三个17字节的解决方案。说明:

Nθ

输入n

Fθ«

重复n次数,从0开始。

⊘ι

画一条-长度为索引一半的s 线(截断)。

↓>

绘制箭头并移至下一行。

‖T»

反射所有内容,翻转箭头。

Fθ‖T

上面的循环具有n反射,但是我们需要偶数个反射,因此请执行另一个n反射。


3

干净76 73字节

import StdEnv,StdLib
$n=take n[s\\i<-inits['--'..],s<-[i++['>'],['<':i]]]

在线尝试!

使用与节省点['-','-'..]相同的简洁事实['--'..]


3

JavaScript,49个字节

f=n=>--n?f(n,l='')+(n%2?`
<`+l:`
${l+='-'}>`):'>'

在线尝试!


哇,真酷
Limbo

...但是它抛出了10000,同时我的ES6解决方案仍然可以使用:D无论如何,您的解决方案非常酷)
Limbo

2

Powershell,51个字节

param($n)0..$n|%{'-'*$_+'>';'<'+'-'*$_}|?{$n---gt0}

2

6502机器代码(C64),49字节

00 C0 20 9B B7 A2 00 8A 4A A8 90 05 A9 3C 20 D2 FF A9 2D C0 00 F0 06 20 D2 FF 
88 D0 FA 8A 4A B0 05 A9 3E 20 D2 FF A9 0D 20 D2 FF E8 E4 65 D0 D7 60

仍然比BASIC短很多;)255由于计算机的自然整数只有8位,因此该位的最大数字范围最大。

在线演示

用法:(SYS49152,[n]例如SYS49152,3,来自挑战的示例)

已评论拆解

         00 C0       .WORD $C000        ; load address
.C:c000  20 9B B7    JSR $B79B          ; get unsigned byte from commandline
.C:c003  A2 00       LDX #$00           ; main loop counter
.C:c005   .loop:
.C:c005  8A          TXA                ; loop counter to accumulator
.C:c006  4A          LSR A              ; divide by 2, shift lowest bit to C
.C:c007  A8          TAY                ; result to Y
.C:c008  90 05       BCC .toright       ; C clear -> counter even, skip '<'
.C:c00a  A9 3C       LDA #$3C           ; load character '<'
.C:c00c  20 D2 FF    JSR $FFD2          ; output character
.C:c00f   .toright:
.C:c00f  A9 2D       LDA #$2D           ; load character '-'
.C:c011  C0 00       CPY #$00           ; counter/2 == 0 ? then no dashes
.C:c013  F0 06       BEQ .skipdashes
.C:c015   .printdashes:
.C:c015  20 D2 FF    JSR $FFD2          ; output character
.C:c018  88          DEY                ; decrement Y
.C:c019  D0 FA       BNE .printdashes   ; not 0 yet -> repeat
.C:c01b   .skipdashes:
.C:c01b  8A          TXA                ; loop counter to accumulator
.C:c01c  4A          LSR A              ; shift lowest bit to C
.C:c01d  B0 05       BCS .toleft        ; C set -> counter odd, skip '>'
.C:c01f  A9 3E       LDA #$3E           ; load character '>'
.C:c021  20 D2 FF    JSR $FFD2          ; output character
.C:c024   .toleft:
.C:c024  A9 0D       LDA #$0D           ; load newline character
.C:c026  20 D2 FF    JSR $FFD2          ; output character
.C:c029  E8          INX                ; next loop iteration
.C:c02a  E4 65       CPX $65            ; compare to command line argument
.C:c02c  D0 D7       BNE .loop          ; not reached yet -> repeat main loop
.C:c02e  60          RTS                ; exit


2

K(ngn / k)31 29字节

{"<->"x#2,x{(1=*x)_1,2-|x}\0}

在线尝试!

首先,我们生成的列表用0代替"<",1代替"-",2代替">"

{ } 带参数的功能 x

x{... }\0应用内部函数x时间,从的初始值开始0并保留中间结果

|x 相反

2- 将0替换为2,反之亦然,保持1不变

1, 前置1

(1=*x)_x等于1 的第一个?如果是,则删除一个元素,否则删除0个元素(不执行任何操作)

2,在初始">"箭头前面加上2

x#我们有一点点太多的名单,只所以采取第一x

"<->" use the lists' elements (0/1/2) as indices in this string


I would like to ask for an explanation (I haven't started learning K yet, I don't know which version to start with...)
Galen Ivanov

1
@GalenIvanov i tried to write an explanation, i hope it makes sense. thanks for your interest in my favourite language :) there are multiple implementations with different advantages and disadvantages (kx's original, kona, oK and i'm working on my own). would you like to join the apl chat room so i can give you more details?
ngn

Thank you, I'm already there
Galen Ivanov

2

05AB1E, 23 20 bytes

FNÉD„><è'-N;∍«s_iR},

Try it online!

First time using 05AB1E or any other golfing language for that matter. Any ideas welcome.

-3 from Kevin Cruijssen


1
Welcome to the world of 05AB1E, and nice first answer. +1 from me. :) "><" can be „>< to save a byte. There are builtins for 1, 2, and 3 char strings, being ', , and respectively. Here is a 18 bytes alternative I came up with, but perhaps it could be golfed a bit more. If you haven't seen it yet, we have a tips for golfing in 05AB1E page, and also feel free to ask anything in the chat.
Kevin Cruijssen

1
@KevinCruijssen Thanks so much for your ideas. I don't feel right just using your code, as it feels fairly different from mine, but I did use the idea of modulo 2 as checking for if a number is odd. I also use the two char string idea. I would not mind at all if you posted the 18 byte version on your own.
nedla2004

I've posted my answer in that case. :)
Kevin Cruijssen

2

C# (.NET Core), 90 bytes

a=>{for(int i=0;i<a;i++){var s=new String('-',i/2);Console.WriteLine(i%2<1?s+">":"<"+s);}}

Try it online!

Uses an Action delegate to pull in the input and not require a return.

Ungolfed:

a => {
    for(int i = 0; i < a; i++)          // from [0, a)
    {
        var s = new String('-', i / 2);     // create string of dashes of length (a / 2)
        Console.WriteLine(i % 2 < 1 ?       // write arrow
                                s + ">" :       // if i is even: dashes plus ">"
                                "<" + s);       // if i is odd: "<" plus dashes
    }
}


@EmbodimentofIgnorance Doesn't work, missing the first ">" output string.
Meerkat


2

ES6, 96 82 79 70 bytes

Try it online! (Thanks to @Oliver)

n=>[...Array(n)].map((_,i)=>(i%2?"<":"")+"-".repeat(i/2)+(i%2?"":">"))

1
Welcome to PPCG! By default, taking input as a variable is disallowed; you have to either make it a function (just stick a i=> in front of your code!) or from a command-line argument or STDIN or something.
HyperNeutrino

@HyperNeutrino okay, edited answer. However, the most voted answer contains only the body of the function, but ok. Anyway I'm outsider)
Limbo

Can you link it? I don't think any of them are invalid, at least not the top few.
HyperNeutrino

1
A few more bytes: Try it online
Oliver

1
A few more bytes if you re-arrange that last ternary operator and remove the center parenthesis: Try it online
Oliver

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.