反向范围后继


21

给定正整数n,请执行以下操作(并输出每个阶段):

  1. 从包含n副本的列表开始n
  2. 执行以下n次数:
  3. i步骤th,逐渐减少i列表的th条目,直到到达i

因此,举例来说,如果给定的n4,那么你下手[4,4,4,4],然后在你的第一步[3,4,4,4][2,4,4,4][1,4,4,4]。在第二个步骤,你有[1,3,4,4][1,2,4,4]。第三步[1,2,3,4]。第四步什么都没有做。

所以您的输出是[[4,4,4,4],[3,4,4,4],[2,4,4,4],[1,4,4,4],[1,3,4,4],[1,2,4,4],[1,2,3,4]]


允许使用任何合理的输入/输出格式。


有标准漏洞。这是:字节数最小的答案胜出。


用于检查目的的Python实现


1
您可能要明确声明ith始终为1索引。
凯文·克鲁伊森

我们真的必须操纵数组吗?我得到一个简短的答案,而没有处理任何数组,产生了可接受的输出。
奥利维尔·格雷戈尔(OlivierGrégoire),

2
@OlivierGrégoire您不必遵循这些步骤,只需生成合理格式的输出即可。(即继续前进)
Leaky Nun

Answers:


6

果冻,9 个字节

r€⁸Œp»\QṚ

在线尝试!

怎么样?

r€⁸Œp»\QṚ - Link: integer, N    e.g. 4
 €        - for €ach of implicit range of N (i.e. for i in [1,2,3,...N])
  ⁸       -   with the chain's left argument, N on the right:
r         -     inclusive range (for i<=N this yields [i, i+1, ..., N]
          - ...leaving us with a list of lists like the post-fixes of [1,2,3,....,N]
          -                     e.g. [[1,2,3,4],[2,3,4],[3,4],[4]]
   Œp     - Cartesian product* of these N lists
          -                     e.g. [[1,2,3,4],[1,2,4,4],[1,3,3,4],[1,3,4,4],[1,4,3,4],[1,4,4,4],[2,2,3,4],[2,2,4,4],[2,3,3,4],[2,3,4,4],[2,4,3,4],[2,4,4,4],[3,2,3,4],[3,2,4,4],[3,3,3,4],[3,3,4,4],[3,4,3,4],[3,4,4,4],[4,2,3,4],[4,2,4,4],[4,3,3,4],[4,3,4,4],[4,4,3,4],[4,4,4,4]]
      \   - cumulative reduce with:
     »    -   maximum (vectorises)
          -                     e.g. [[1,2,3,4],[1,2,4,4],[1,3,4,4],[1,3,4,4],[1,4,4,4],[1,4,4,4],[2,4,4,4],[2,4,4,4],[2,4,4,4],[2,4,4,4],[2,4,4,4],[2,4,4,4],[3,4,4,4],[3,4,4,4],[3,4,4,4],[3,4,4,4],[3,4,4,4],[3,4,4,4],[4,4,4,4],[4,4,4,4],[4,4,4,4],[4,4,4,4],[4,4,4,4],[4,4,4,4]]
       Q  - de-duplicate        e.g. [[1,2,3,4],[1,2,4,4],[1,3,4,4],[1,4,4,4],[2,4,4,4],[3,4,4,4],[4,4,4,4]]
        Ṛ - reverse             e.g. [[4,4,4,4],[3,4,4,4],[2,4,4,4],[1,4,4,4],[1,3,4,4],[1,2,4,4],[1,2,3,4]]

*使用不同的输入可能更容易了解上面使用的笛卡尔积的情况:

the Cartesian product of [[0,1,2],[3,4],[5]]
is [[0,3,5],[0,4,5],[1,3,5],[1,4,5],[2,3,5],[2,4,5]]

您超出了不可取消的范围。
Leaky Nun

5

R83 82 74字节

N=rep(n<-scan(),n);while({print(N);any(K<-N>1:n)})N[x]=N[x<-which(K)[1]]-1

在线尝试!

代替双for循环,在while这里循环就足够了:我们找到列表大于索引的第一个索引,然后在该位置递减。

KTRUE地方N[i]>iwhich(K)回到真实的指数,我们采取先用[1]




2

APL + WIN,54个字节

提示屏幕输入整数

((⍴m)⍴n)-+⍀m←0⍪(-0,+\⌽⍳n-1)⊖((+/+/m),n)↑m←⊖(⍳n)∘.>⍳n←⎕

输出矩阵,每行代表每个步骤的结果,例如4:

4 4 4 4
3 4 4 4
2 4 4 4
1 4 4 4
1 3 4 4
1 2 4 4
1 2 3 4

2

果冻,11字节

x`’Jḟḣ1Ʋ¦ÐĿ

在线尝试!

怎么运行的

x`’Jḟḣ1Ʋ¦ÐĿ  Main link. Argument: n

x`           Repeat self; yield an array of n copies of n.
         ÐĿ  While the results are unique, repeatedly call the link to the left.
             Return the array of all unique results, including the initial value.
  ’     ¦      Decrement the return value at all indices specified by the chain
               in between.
       Ʋ         Combine the four links to the left into a monadic chain.
   J               Indices; yield [1, ..., n].
    ḟ              Filterfalse; remove all indices that belong to the return value.
     ḣ1            Head 1; truncate the result to length 1.

2

Python 3,91字节

n=int(input())
x=[n]*n;print(x)
for i in range(n):
    for j in[0]*(n-i-1):x[i]-=1;print(x)

在线尝试!


1个空间足以缩进python中的代码。删除不必要的空间并切换到python 2可节省10个字节:签出
Dead Possum,

@DeadPossum,即使我知道我可以在Python 2中做得更好,它很快就会过时了,所以我想尽可能多地练习我的Python 3技能。

2

Java(OpenJDK 8),135字节

a->{int r[]=new int[a],i=0;java.util.Arrays x=null;x.fill(r,a);for(r[0]++;i<a;r[i++]++)for(;--r[i]>i;System.out.print(x.toString(r)));}

在线尝试!

说明:

int r[]=new int[a],i=0;    //Initialize array and loop counter
java.util.Arrays x=null;    //reduces the number of of “Arrays” needed from 3 to 1
x.fill(r,a);    //Sets each value in array length n to int n
for(r[0]++;i<a;r[i++]++)    //Increment everything!
  for(;--r[i]>i;    //If decremented array element is larger than element number:
     System.out.print(x.toString(r)));}    //Print the array

信用:

-8个字节感谢Jonathan Frech

-16个字节感谢Kevin Cruijssen

-1字节感谢Okx


4
import java.util.*;是字节数恐怕的一部分。@JonathanFrech的代码可以通过在,i=0后面加上r[]并更改<-~a为来再打4个字节<=a。(在线试玩,144个字节)(和我改变~-i,以i-1使其更具可读性。)
凯文Cruijssen

1
import java.util.*;使用java.util.Arrays x=null;x.fill和摆脱139个字节x.toString(请注意,您当前的解决方案是155个字节,并带有必需的import java.util.*;。)
Kevin Cruijssen,

1
使用for(;r[i-1]>i;而不是来打高尔夫球for(;r[i-1]!=i;
Okx

2
@KevinCruijssen通过打高尔夫球++i<=a到可以保存另一个字节i++<a
乔纳森·弗雷希

1
另一个-2字节将最后一部分更改为for(r[0]++;i<a;r[i++]++)for(;--r[i]>i;System.out.print(x.toString(r)));。:) 在线尝试135个字节
Kevin Cruijssen,

2

Haskell,69 67 65 63字节

递归定义:

f 0=[[]]
f a=map(:(a<$[2..a]))[a,a-1..2]++[1:map(+1)x|x<-f$a-1]

感谢Laikoni 2个字节!


第二个map是两个较短的字节,并且具有列表理解功能:在线尝试!
Laikoni

2

PHP,153字节

在线尝试!

function f($n){
$a=array_fill(0,$n,$n);$r=json_encode($a)."\n";$p=0;while($p<$n)
{if($a[$p]!=$p+1){$a[$p]--;$r.=json_encode($a)."\n";}else{$p++;}}echo$r;}

尝试降低字节数或完成递归功能

说明

function f($n){
  $a=array_fill(0,$n,$n);          #start with $nlength array filled with $n
  $r=json_encode($a)."\n";         #pushed to the string to output
  $p=0;                            #first position
  while($p<$n){                    #on position $n ($n-1) we do nothing
    if($a[$p]!=$p+1){              #comparing the position+1 to the value
     $a[$p]--;                     #it gets decreased by 1
     $r.= json_encode($a)."\n";    #and pushed
   } else {
     $p++;                       #when position+1 = the value,
   }                               #position is changed ++
  }
   echo $r;
  }

似乎您有一些不必要的空格,所以应该为153个字节 -请注意,我不知道PHP。
朱塞佩

是的,请意识到,谢谢,现在编辑。
弗朗西斯科·哈恩




1

J, 17 15 bytes

+/\@,(#=)@i.&.-

Try it online!

Explanation

+/\@,(#=)@i.&.-  Input: n
              -  Negate n
          i.     Reverse of range [0, n)
       =           Identity matrix of order n
      #            Copy each row by the reverse range
              -  Negate
    ,            Prepend n
+/\              Cumulative sum of rows

1

Retina, 49 bytes

.+
*
_
$`_,$= 
.{*\`_+,(_+)
$.1
0`(\b(_+),\2)_
$1

Try it online! Explanation:

.+
*

Convert the input to unary.

_
$`_,$= 

Create a list of n copies of i,n where i is the index of the copy.

.

Don't print anything (when the loop finishes).

{

Loop until the pattern does not change.

*\`_+,(_+)
$.1

Temporarily delete the is and convert the ns to decimal and output.

0`(\b(_+),\2)_
$1

Take the first list entry whose value exceeds its index and decrement it.


1

Python 3, 70 67 65 bytes

def f(n):
 k=0;a=[n]*n
 while k<n-1:print(a);k+=a[k]==k+1;a[k]-=1

Try it online!

  • (67) Converting to function: -3 bytes
  • (65) Removing unneeded parentheses: -2 bytes

Ungolfed version:

def f(n):
    k = 0
    a = [n] * n             # create n-item list with all n's
    while k < n - 1:        # iterate through columns 0..n-1
        print(a)            # print whole list
        if a[k] == k + 1:   # move to the next column when current item reaches k+1
            k += 1
        a[k] -= 1           # decrement current item

0

C (clang), 131 141 bytes

i,j,k,m[99];p(){for(k=0;m[k];printf("%d ",m[k++]));puts("");}f(n){for(j=k=m[n]=0;k<n;m[k++]=n);p();for(;j<n;j++)for(i=1;i++<n-j;m[j]--,p());}

Try it online!

This will work for all n upto 99. TIO truncates output. It can support arbitrarily larger n by changing size of array m as memory permits.


Following is limited to n=1..9 but is significantly shorter

C (clang), 89 92 bytes

i,j;char m[12];f(n){j=!puts(memset(m,n+48,n));for(;j<n;j++)for(i=1;i++<n-j;m[j]--,puts(m));}

Try it online!

Updated: Modified to avoid dependence on static initialization


Your static/global initialization because multiple test cases is not allowed, as functions have to be callable more than once.
Jonathan Frech

@Jonathan Updated answers. I always wondered if this should be allowed, and couldn't make up my mind.
GPS

1
Here is the relevant meta post: codegolf.meta.stackexchange.com/a/4940/73111
Jonathan Frech

You could golf m[j]--,p() to p(m[j]--) and save a byte.
Jonathan Frech


0

Clojure, 132 bytes

#(loop[R[(vec(repeat % %))]j(- % 2)i 0](if(> i j)R(recur(conj R(update(last R)i dec))(if(= i j)(- % 2)(dec j))(if(= i j)(inc i)i))))

I was hoping this to be shorter...

Less stateful but longer at 141 bytes:

#(apply map list(for[i(range %)](concat(repeat(nth(cons 0(reductions +(reverse(range %))))i)%)(range % i -1)(if(>(dec %)i)(repeat(inc i))))))

0

Python 3, 101 bytes

def f(n):
 p=print;m=[n for_ in range(n)];p(m)
 for i in range(n):
    while m[i]>1+i:m[i]-=1;p(m)

I could probably golf more with the print, but I'm away from my computer and am not entirely sure of python 2's rules on setting a variable to print. I'll update later when I get to a computer or if someone clarifies in the comments.


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.