465安排


24

这是挑战。编写一些代码以输出范围内的所有整数。听起来很简单,但这是棘手的部分。它将以最低的编号开始,然后是最高的编号。然后是数组中尚未存在的最低数字。然后是尚未出现的最高点。

例:

让我们以1到5为起点

数字是[1、2、3、4、5]。

我们取第一,所以[1]。剩余数字为[2、3、4、5]。我们以最后一个新数组为[1,5]。剩余数字为[2、3、4]。我们以第一个新数组为[1、5、2]。剩余数字为[3,4]。我们采用最后一个新数组为[1、5、2、4]。剩余数字为[3]。我们以第一个新数组为[1、5、2、4、3]。没有剩余的数字,我们完成了。输出[1、5、2、4、3]

规则:

  • 这是代码高尔夫球,用任何语言用最少的字节写。
  • 没有标准漏洞。
  • 请链接到在线口译员吗?(例如https://tio.run/
  • 两个输入,均为整数。范围的下限和范围的上限。
  • 我不在乎输出的数据类型是什么,但是它必须以正确的顺序显示数字。

例子

低:4高:6结果:4 6 5


低:1高:5结果:1 5 2 4 3


低:-1高:1结果:-1 1 0


低:-1高:2结果:-1 2 0 1


低:-50高:50结果:-50 50 -49 49 -48 48 -47 47 -46 46 -45 45 -44 44 -43 43 -42 42 -41 41 -40 40 -39 39 -38 38 -37 37 -36 36 -35 35 -34 34 -33 33 -32 32 -31 31 -30 30 -29 29 -28 28 -27 27 -26 26 -25 25 -24 24 -23 23 -22 22 -21 21- 20 20 -19 19 -18 18 -17 17 -16 16 -15 15 -14 14 -13 13 -12 12 -11 11 -10 10 -9 9 -8 8 -7 7 -6 6 -5 5 -4 4 -3 3 -2 2 -1 1 0


打高尔夫球快乐!


2
几乎是重复的(不同之处在于,此合并需要在合并前将后半部分反转)。
彼得·泰勒

输入总是按照低端,高端的顺序进行吗?
Sumner18

1
@ Sumner18是的。这里的社区在输入验证方面陷入僵局,我没有要求提供逆序输入,因此我们可以假设输入始终为低-高。
AJFaraday

1
@ Sumner18这些挑战通常如何起作用是因为我们不在乎如何处理无效输入。仅通过处理有效输入的方式(即,它们都是整数,第一个均小于第二个),才能判断您的代码是否成功
AJFaraday

1
@AJFaraday:您应该在主帖子上添加一条注释,指示X始终严格低于Y(即X!= Y),我错过了此评论;)
digEmAll

Answers:


15

R38 37 36字节

function(a,b)rbind(a:b,b:a)[a:b-a+1]

在线尝试!

  • -1个字节感谢@ user2390246
  • -1个字节感谢@Kirill L.

利用R逐列存储矩阵的事实


使用rbind比我的方法好得多,但是使用[seq(a:b)]代替可以节省1个字节unique
user2390246

您是对的,我错过了指定a <b(从不等于)的注释,因此我们可以使用seq(a:b)
digEmAll

@digEmAll我的解决方案本质上是对谜题的字面解释,我什至从未想到要做这样的事情。令人印象深刻,请投票!
Sumner18



8

R65 64 61 60字节

-1个字节,感谢RobertS。

-4多亏了digEmAll

x=scan();z=x:x[2];while(sum(z|1)){cat(z[1],"");z=rev(z[-1])}

在线尝试!


您可以替换length(z)使用sum(z|1),以节省1个字节:)
罗伯特S.

我不知道它是如何工作的,但我想是可以的。sum(z | 1)似乎总是等于至少1,这将导致while循环无限循环。但显然不是
Sumner18

4
z是一个向量。该向量的每个元素都|用1表示。总是等于1。取和时,您有一个用TRUEs 填充的向量,因此结果等于向量的长度。如果向量为空,则|与无关,因此输出向量为logical(0)。当您拿走那笔款项时,它是0
OganM



5

PowerShell59 48字节

param($a,$b)(($z=0..($b-$a))|%{$a+$_;$b-$_})[$z]

在线尝试!

(似乎很长...)

取得输入$a$b,构建范围0 .. ($b-$a),存储该入$z,然后通过在该范围循环。通过该范围的循环仅用作计数器,以确保获得足够的迭代次数。每次迭代,我们把$a$b与加法/减法流水线。这给了我们喜欢的东西1,5,2,4,3,3,4,2,5,1,所以我们需要切片成从0$b-$a原数组(即计数),所以我们只用适当的元素离开。剩下的就在管道上,输出是隐式的。

-11个字节感谢mazzy。



@mazzy啊,我喜欢这个$b-$a把戏-这很聪明!
AdmBorkBork

5

05AB1E,6个字节

ŸDvć,R

在线尝试!

说明

Ÿ        # push range [min ... max]
 D       # duplicate
  v      # for each element in the copy
   ć,    # extract and print the head of the original list
     R   # and then reverse it

Ÿ2ä`R.ι使用交错非迭代,但这仍然好得多。
魔术章鱼缸

1
@MagicOctopusUrn:我首先尝试了一个非迭代的解决方案,但由于我不知道该问题,结果甚至更糟;)
Emigna

与我的想法相似,因此很明显地获得了+1。我也喜欢通过@MagicOctopusUrn替代7轮。:)
Kevin Cruijssen

1
@KristianWilliams:似乎为我工作
Emigna

1
@KevinCruijssen:我改用一对,因为这样反而更直观:)
Emigna



4

R,51字节

function(x,y,z=x:y)matrix(c(z,rev(z)),2,,T)[seq(z)]

在线尝试!

说明:对于x:y长度序列N,请创建一个由N组成的2×N矩阵,该矩阵由顶行的x:y和底行的y:x组成matrix(c(z,rev(z)),2,,T)。如果我们选择N矩阵的第一个元素[seq(z)],则会按列选择它们,并提供所需的输出。

由digEmAll超越


1
我只是在您30秒钟前发布了一种非常类似的方法:D
digEmAll

@digEmAll是的,但是您的要好很多!
user2390246

4

cQuents,19个字节

#|B-A+1&A+k-1,B-k+1

在线尝试!

请注意,由于TIO的解释器不是最新的,因此它现在在TIO上不起作用。

说明

#|B-A+1&A+k-1,B-k+1
                      A is the first input, B is the second input
#|B-A+1               n = B - A + 1
       &              Print the first n terms of the sequence
                      k starts at 1 and increments whenever we return to the first term
        A+k-1,         Terms alternate between A + k - 1 and
              B-k+1     B - k + 1
                       increment k



4

JVM字节码(OpenJDK asmtools JASM),449个字节

enum b{const #1=Method java/io/PrintStream.print:(I)V;static Method a:(II)V stack 2 locals 4{getstatic java/lang/System.out:"Ljava/io/PrintStream;";astore 3;ldc 0;istore 2;l:iload 2;ldc 1;if_icmpeq t;aload 3;iload 0;invokevirtual #1;iinc 0,1;iinc 2,1;goto c;t:aload 3;iload 1;invokevirtual #1;iinc 1,-1;iinc 2,-1;c:aload 3;ldc 32;i2c;invokevirtual java/io/PrintStream.print:(C)V;iload 0;iload 1;if_icmpne l;aload 3;iload 0;invokevirtual #1;return;}}

脱开(略清洁)

 enum b {    
    public static Method "a":(II)V stack 5 locals 4 {
        getstatic "java/lang/System"."out":"Ljava/io/PrintStream;";
        astore 3;
        ldc 0;
        istore 2;
    loop:
        iload 2;
        ldc 1;
        if_icmpeq true;
    false:
        aload 3;
        iload 0;
        invokevirtual "java/io/PrintStream"."print":"(I)V";
        iinc 0,1;
        iinc 2,1;
        goto cond;
    true:
        aload 3;
        iload 1;
        invokevirtual "java/io/PrintStream"."print":"(I)V";
        iinc 1,-1;
        iinc 2,-1;
        goto cond;
    cond:
        iload 0;
        iload 1;
        if_icmpne loop;
        aload 3;
        iload 0;
        invokevirtual "java/io/PrintStream"."print":"(I)V";
        return;
    }
}

独立功能,需要从Java调用b.a(num1,num2)

说明

此代码将方法参数用作变量,并使用本地#3中的布尔值决定要输出的数字。每次循环迭代都向左或向右输出,并且该数字向左递增或向右递减。循环继续直到两个数字相等,然后输出该数字。

...我有种明显的感觉,我在字节数上被大大超越了


4

Wolfram语言(Mathematica)56 54字节

这是我第一次打高尔夫球!

f[a_,b_]:=(c=a~Range~b;Drop[c~Riffle~Reverse@c,a-b-1])

在线尝试!

使用中缀符号保存2个字节。

说明:

f[a_,b_]:=                                   \function of two variables
c=a~Range~b;                                 \list of integers from a to b 
                           Reverse@c         \same list in reverse
                  c~Riffle~Reverse@c         \interleave the two lists
             Drop[c~Riffle~Reverse@c,a-b-1]  \drop last |a-b-1| elements (note a-b-1 < 0)

或者,我们可以使用Take[...,b-a+1]相同的结果。

测试:

f[4, 6]
f[1, 5]
f[-1, 1]
f[-1, 2]

输出:

{4, 6, 5}
{1, 5, 2, 4, 3}
{-1, 1, 0}
{-1, 2, 0, 1}

“在线试用”链接返回403。“抱歉,您无权访问此项目。”
罗希特(Rohit Namjoshi)

@RohitNamjoshi我更新了链接

请注意,在TIO上,可以将页眉和页脚代码放在实际代码框上方和下方的文本框中。这使代码看起来更简洁,也使您可以利用PPCG答案格式化程序(esc-sg)。在线尝试!
乔金

@JoKing非常感谢,我以前从未使用过它!


3

Japt,7个字节

将输入作为数组。

rõ
ÊÆÔv

尝试运行所有测试用例

         :Implicit input of array U=[low,high]
r        :Reduce by
 õ       :  Inclusive, reversed range (giving the range [high,low])
\n       :Reassign to U
Ê        :Length
 Æ       :Map the range [0,Ê)
  Ô      :  Reverse U
   v     :  Remove the first element

3

MATL,8字节

&:t"1&)P

在线尝试!

说明

&:      % Take two inputs (implicit). Two-input range
t       % Duplicate
"       % For each
  1&)   %   Push first element, then an array with the rest
  P     %   Reverse array
        % End (implicit). Display (implicit)


3

第四(gforth),52字节

: f 2dup - 1+ 0 do dup . i 2 mod 2* 1- - swap loop ;

在线尝试!

说明

从0循环到(结束-开始)。将“结束”和“开始”放在堆栈顶部。

每个迭代:

  • 输出当前号码
  • 从当前数字加(或减)1
  • 用其他号码切换当前号码

代码说明

: f           \ start new word definition
  2dup -      \ get the size of the range (total number of integers)
  1+ 0        \ add 1 to the size because forth loops are [Inclusive, Exclusive) 
  do          \ start counted loop from 0 to size+1
    dup .     \ output the current top of the stack
    i 2 mod   \ get the index of the loop modulus 2
    2* 1-     \ convert from 0,1 to -1,1
    -         \ subtract result from top of stack (adds 1 to lower bound and subtracts 1 from upper)
    swap      \ swap the top two stack numbers 
  loop        \ end the counted loop
;             \ end the word definition


3

Haskell,30个字节

l%h=l:take(h-l)(h:(l+1)%(h-1))

用法: 3%7给出`[3,7,4,6​​,5]

对于输入,l, h该函数使用input递归调用l+1, h-1,并将其添加l,h到开始中。该代码没有使用任何暂停条件,而是take(h-l)将序列缩短为正确的长度(否则,序列将是无限个递增和递减的序列)。


3

Brachylog,15个字节

⟦₂{∅|b↔↰T&hg,T}

输入是2元素列表[lo,hi]。请注意,下划线用于负数。在线尝试!

说明

⟦₂               2-argument inclusive range: [1,5] -> [1,2,3,4,5]
  {           }  Call this recursive predicate to calculate the output:
   ∅               Base case: the input is empty list; nothing to do
    |              Otherwise (recursive case):      [1,2,3,4,5]
     b             Behead the input list            [2,3,4,5]
      ↔            Reverse                          [5,4,3,2]
       ↰           Call the predicate recursively   [5,2,4,3]
        T          Label the result T
         &         Also, with the input list,
          h        Take the head                    1
           g       Wrap it in a list                [1]
            ,T     Append T from earlier            [1,5,2,4,3]

3

MathGolf,6个字节

↨_x^─▀

在线尝试!

解释 (1, 5)

↨        inclusive range from a to b    [1, 2, 3, 4, 5]
 _       duplicate TOS                  [1, 2, 3, 4, 5], [1, 2, 3, 4, 5]
  x      reverse int/array/string       [1, 2, 3, 4, 5], [5, 4, 3, 2, 1]
   ^     zip top two elements on stack  [[1, 5], [2, 4], [3, 3], [4, 2], [5, 1]]
    ─    flatten array                  [1, 5, 2, 4, 3, 3, 4, 2, 5, 1]
     ▀   unique elements of string/list [1, 5, 2, 4, 3]

之所以可行,是因为输出中的所有元素都应该是唯一的,因此唯一元素运算符将滤除数组的后半部分,从而生成正确的输出。






2

Cubix,16个字节

;w(.II>sO-?@;)^/

在这里尝试

集中化

    ; w
    ( .
I I > s O - ? @
; ) ^ / . . . .
    . .
    . .

说明

基本上,这会使两个边界一次靠近一步,直到它们相遇。每次循环时,我们都会s拍打边界,输入O,取差,然后根据符号递增)或递减(


2

Pyth,10 8字节

{.iF_B}F

在这里尝试

说明

{.iF_B}F
      }FQ  Generate the range between the (implicit) inputs.
 .iF_B     Interleave it with its reverse.
{          Deduplicate.
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.