第一个,最后一个以及之间的所有内容


33

给定两个整数,则输出两个整数,然后输出它们之间的范围(不包括两个整数)。

范围的顺序必须与输入的顺序相同。

例子:

 Input        Output
 0,  5   ->   [0, 5, 1, 2, 3, 4]
-3,  8   ->   [-3, 8, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7]
 4,  4   ->   [4, 4]
 4,  5   ->   [4, 5]
 8,  2   ->   [8, 2, 7, 6, 5, 4, 3]
-2, -7   ->   [-2, -7, -3, -4, -5, -6]

我想我们不能按预定顺序输入?
凯文·克鲁伊森

@KevinCruijssen,不,输出顺序取决于输入顺序
TFeld

@StewieGriffin,输出顺序必须与输入顺序相同
TFeld

这个可接受的输出格式?请注意换行符
Luis Mendo

2
@KevinCruijssen可接受任何合理的I / O。
TF

Answers:


14

R39 33 30字节

c(a<-scan(),setdiff(a:a[2],a))

在线尝试!

感谢为user2390246和J.Doe保存的字节。


通过将输入作为向量而不是两个单独的整数,可以节省一些字节
user2390246 '18

是的,这是合理的,实际上,作为一个完整程序而不是功能,它甚至变得更短。
Kirill L.

您可能会滥用以下事实::操作员使用两个args的第一个元素
占用




8

Perl 6的26 22个字节

{|@_,|[...^](@_).skip}

在线尝试!

说明

{                    }
 |@_,   # Slip args a,b into result
      [...^](@_)  # Reduce args a,b with ...^ operator, same as a...^b
                .skip  # Skip first element
     |  # Slip into result

7

Python 2,40个字节

lambda x,y:[x,y]+range(x,y,-(y<x)|1)[1:]

在线尝试!


真的很喜欢-(y<x)|1。很酷,但我不知道为什么会起作用!有机会您可以解释一下吗?
ElPedro '18年

2
@ElPedro基本上,y<x检查是否y严格小于,否则x返回。在此之后,一元适用于它,它转换到和到。最后一步是将此数字与进行按位“或”运算。这显然使()不受影响,也使()不受影响(已设置的符号位,因此将其保持不变)。但是,它转换到,所以不会对我用抱怨的。TrueFalse-True-1False0110b1-1-0b1-101rangestep0
Erik the Outgolfer

那真的很酷,也很聪明。如果我能投票两次,我会。非常感谢您的解释。
ElPedro


6

Japt,8个字节

cUr!õ kU

在这里尝试

             :Implicit input of array U
c            :Concatenate
 Ur          :  Reduce U by
   !õ        :   Inclusive range
      kU     :  Remove all elements in original U

6

JavaScript(ES6),51个字节

将输入作为(a)(b)

a=>g=(b,c=b)=>(b+=b<a|-(b>a))-a?[...g(b,c),b]:[a,c]

在线尝试!

已评论

a =>                // main function, taking a
  g = (             // g = recursive function
    b,              //     taking b
    c = b           // we save a backup of the original value of b into c
  ) =>              //
    (b +=           // add to b:
      b < a |       //   +1 if b is less than a
      -(b > a)      //   -1 if b is greater than a
    )               //   (or 0 if b = a)
    - a ?           // if the updated value of b is not equal to a:
      [             //   generate a new array:
        ...g(b, c), //     prepend all values generated by a recursive call
        b           //     append the current value of b
      ]             //
    :               // else:
      [a, c]        //   stop recursion and return the first 2 values: a and c

6

Python 2中47个 41 40字节

lambda a,b:[a,b]+range(a,b,a<b or-1)[1:]

在线尝试!

这是我的,现在已经发布了许多其他Python答案

-6个字节,多亏了GB


在无效范围内利用空范围是处理前向或后向列表的明智方法。我可以看到它非常有用,并且知道存在是一个很好的技巧。
akozi

2
使用单个范围的41个字节:range(a,b,(a <b)* 2-1)
GB

a<b or-1对于第三个范围参数,该值较短。我得到的最短的是lambda x,y:[x,y]+range(x+(x<y or-1),y,x<y or-1)
mbomb007

5

Java 10, 109 108 104 102 93 62个字节

使用以空格分隔的字符串:

b->a->{var r=a+" "+b;for(;a<b?++a<b:--a>b;)r+=" "+a;return r;}

在线尝试。

使用清单:

b->a->{var r=new java.util.Stack();for(r.add(a),r.add(b);a<b?++a<b:--a>b;)r.add(a);return r;}

在线尝试。

a<b?++a<b:--a>b可以使用++a<b||(a-=2)>b相同的字节数:在线尝试使用String在线尝试使用List。)


老(109 108 104 102使用阵列101个字节)的答案:

a->b->{int s=a<b?1:-1,i=a!=b?(b-a)*s+1:2,r[]=new int[i];for(r[0]=a,r[1]=b;i>2;)r[--i]=b-=s;return r;}

-7个字节,感谢@nwellnhof

在线尝试。

说明:

a->b->{                // Method with 2 int parameters & int-array return-type
  int s=               //  Step integer, starting at:
        a<b?1          //   1 if the first input is smaller than the second
        :-1;           //   -1 otherwise
      i=               //  Array-index integer, starting at:
        a!=b?          //   If the inputs aren't equal:
         (b-a)*s+1     //    Set it to the absolute difference + 1
        :              //   Else:
         2,            //    Set it to 2
      r[]=new int[i];  //  Result-array of that size
  for(r[0]=a,          //  Fill the first value with the first input
      r[1]=b;          //  And the second value with the second input
      i>2;)            //  Loop `i` downwards in the range [`i`,2):
    r[--i]=            //   Decrease `i` by 1 first with `--i`
                       //   Set the `i`'th array-value to:
           b-=s;       //    If the step integer is 1: decrease `b` by 1
                       //    If the step integer is -1: increase `b` by 1
                       //    And set the array-value to this modified `b`
  return r;}           //  Return the result-array

Java的标准库中没有用于生成整数范围的内容吗?还是太冗长而无法使用?
Οurous

@Οurous确实太冗长了:a->b->{var L=java.util.stream.IntStream.range(a,b).boxed().collect(java.util.Collectors.toList());L.add(0,b);L.add(0,a);return L;}(130字节)
Kevin Cruijssen

是Java 8还是Java 10?由于“ var” ^^'
Neyt

1
@Neyt Ah,已修复。我最初的版本与下面的数组不使用var,这就是为什么我通常将它们设置为8,而将其实际使用var为10(以及将其使用String.repeat为11)的原因。:)添加列表和字符串答案后忘记更新它,现在应该更正。谢谢。
凯文·克鲁伊森

5

APL(Dyalog扩展),5字节

匿名中缀函数。

,,…~,

在线尝试!

, 第一个和最后一个(照亮参数的串联)

, 和(串联)

 范围

~ 没有

, 第一个和最后一个(照亮参数的串联)


很好,所以我假设您从现在开始将在所有高尔夫活动中使用它?
扎卡里

@Zacharý只有在代码明显更短或更简单的情况下才可能。
亚当


4

果冻,4字节

,œ|r

在线尝试!

怎么运行的

,œ|r  Main link. Left argument: a. Right argument: b

,     Pair; yield [a, b].
   r  Range; yield [a, ..., b].
 œ|   Perform multiset union.

4

J,26个字节

,,[|.@]^:(>{.)<.+1}.i.@|@-

在线尝试!

说明:

二进位动词(带有左右参数)

                         -    subtracts the arguments
                       |@     and finds the absolute value
                    i.@       and makes a list 0..absolute difference
                 1}.          drops the fist element
                +             adds to the entire list
              <.              the smaller of the arguments
   |.@]                       reverses the list
       ^:                     only if
  [                           the left argument
         (>{.)                is greater than the first item of the list
 ,                            appends the list to
,                             the right argument appended to the left one

1
,,[:}.@}:<.+i.@-@(+*)@-长度为23个字节,并且在相对参数顺序上没有特殊的大小写(而是:它隐藏在signum内部*)。我觉得这可能跌到20岁以下,但我很累。
乔纳

@Jonah谢谢!Btw FrownyFrog的解决方案比我的解决方案好得多,因此,我不再进一步介绍它了。
Galen Ivanov '18


4

J,13个字节

,,<.+i.@-~-.=

在线尝试!

     i.@-~       range [0 .. |difference|-1], reverse if the difference is positive
          -.=    remove the zero (either "=" is 0 or there’s nothing to remove)
  <.+            to each element add the smaller of the args
,,               prepend args

不错的解决方案!我完全忘i.了否定论点。
Galen Ivanov

1
这太好了!
乔纳

3

批处理,107字节

@echo %1
@echo %2
@for %%s in (1 -1)do @for /l %%i in (%1,%%s,%2)do @if %1 neq %%i if %%i neq %2 echo %%i

将输入作为命令行参数。说明:

@echo %1
@echo %2

输出两个整数。

@for %%s in (1 -1)do

尝试上升和下降范围。

@for /l %%i in (%1,%%s,%2)do

在包含范围内循环。

@if %1 neq %%i if %%i neq %2

排除两个整数。

echo %%i

输出当前值。


3

Pyth,5个字节

+QtrF

输入是一个包含两个元素的列表[input 1, input 2]在此处在线尝试,或在此处一次验证所有测试用例。

+QtrFQ   Implicit: Q=eval(input())
         Trailing Q inferred
   rFQ   Generate range [input 1 - input 2)
  t      Discard first element
+Q       Prepend Q

从现在开始,我绝对会使用F而不是.*2元素列表。
hakr14






3

PHP(102字节)

function t($a,$b){count($r=range($a,$b))>1?array_splice($r,1,0,array_pop($r)):$r=[$a,$b];print_r($r);}

沙盒

不幸的是(对于高尔夫)PHP具有相当冗长的函数名称,这对长度造成了很大的影响。但是基本思想是创建一个范围,然后弹出最后一个元素并将其缝合在偏移量1处。例如,4,4我必须添加的示例中添加count($r=range($a,$b))>1?...:$r=[$a,$b];了很多内容,不幸的array_splice()是,通过引用它使我遭受了更多打击字节($r= and a ;)。都是因为这种“边缘情况”,大声笑。

好吧反正享受!


我认为这不是代码高尔夫的正确方法。检查这个 function t($a,$b){$o=array($a,$b);for($i=$a+1;$i<$b;$i++)$o[]=$i;print_r($o);}
th3pirat3

或类似的东西function t($a,$b){echo $a.$b;for($i=$a+1;$i<$b;$i++)echo $i};
th3pirat3

1
它必须是一个函数,并且必须输出一个数组。如果您有更好的答案,欢迎发布。
ArtisticPhoenix

我进行了编辑,现在是有效提交吗?我应该把它作为新答案还是什么?
th3pirat3

这完全取决于您,我只想无循环执行...大声笑
ArtisticPhoenix

3

Clojure,61字节

(fn[[a b]](def s(if(> a b)-1 1))(list* a b(range(+ a s)b s)))

匿名函数,将2向量作为输入并返回列表。

在线尝试!

说明

(fn [[a b]] ; An anonymous function that accepts a 2-vector as input, and destructures it to a and b
  (def s (if (> a b) -1 1)) ; If a > b assigns -1 to s and assigns 1 to s otherwise. This determines the order of the elements of the output list.
  (list* a b ; Creates a list with a and b as the first two elements. The remaining elements will be appended from the following range:
    (range (+ a s) b s))) ; A range starting at a+s and ending at b with step s

3

D,85字节

T[]f(T)(T a,T b){T[]v=[a,b];T c=2*(b>a)-1;for(T i=a+c;a!=b&&b!=i;i+=c)v~=i;return v;}

在线尝试!

@HatsuPointerKun的C ++答案的端口到D。


3

TI-BASIC,35 34字节

来自Misha Lavrov的 -1个字节

Prompt A,B
Disp A,B
cos(π(A>B
For(I,A+Ans,B-Ans,Ans
Disp I
End

2
1-2(A>B再用替换一个字节cos(π(A>B
米莎·拉夫罗夫

@MishaLavrov seq(不会为投入工作,其中AB都是一样的,不幸的是:(
kamoroso94

是的-另外,我省略了的论据seq(,因此我不再相信它会更小。不过,这个cos(技巧应该会有所帮助。
米莎·拉夫罗夫

2

木炭,15字节

IE²NI…⊕θηI⮌…⊕ηθ

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

IE²N

在单独的行上打印输入。

I…⊕θη

打印升序范围(如果有)。

I⮌…⊕ηθ

打印反向升序反向范围(如果有)。


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.