태그 보관물: code-golf

code-golf

배열 열거 의미합니다 ). 출력은

양의 정수

주어지면

n

객체의 모든 배열이 생성 됩니다.

세부

  • 변위는 고정 점이없는 순열입니다. (이는 모든 배치 번호에서
    i

    i

    번째 항목에 있을 수 없음을 의미합니다 ).

  • 출력은 숫자의 배열
    (1,2,,n)

    (또는 대안 적으로

    (0,1,2,,n1)

    )로 구성되어야합니다.

  • 다른 방법으로 항상
    (n,n1,,1)

    (또는 각각

    (n1,n2,,1,0)

    배열을 인쇄 할 수 있지만 반드시 지정해야합니다.

  • 출력은 결정 론적 이어야합니다. 즉, 주어진
    n

    을 입력으로 하여 프로그램을 호출 할 때마다 출력이 같아야합니다 (배열 순서가 동일하게 유지되어야 함을 포함하여). 매번 유한 한 시간 (확률 1로 충분하지 않음).

  • n 2 라고 가정 할 수 있습니다
    n2

  • 주어진 대해 모든 배열을 생성하거나 대안 으로 인덱스 역할을하는 다른 정수 를 가져 와서 번째 배열을 선택한 순서대로 인쇄 할 수 있습니다 .
    n

    k

    k

배치 순서가 여기에 나열된 순서와 같을 필요는 없습니다.

n=2: (2,1)
n=3: (2,3,1),(3,1,2)
n=4: (2,1,4,3),(2,3,4,1),(2,4,1,3), (3,1,4,2),(3,4,1,2),(3,4,2,1), (4,1,2,3),(4,3,1,2),(4,3,2,1)

OEIS A000166 은 배치 수를 계산합니다.



답변

젤리 , 6 바이트

Œ!=ÐṂR

정수 목록을 생성하는 양의 정수를 허용하는 모나드 링크.

온라인으로 사용해보십시오!

어떻게?

Œ!=ÐṂR - Link: integer, n
Œ!     - all permutations of (implicit range of [1..n])
     R - range of [1..n]
   ÐṂ  - filter keep those which are minimal by:
  =    -   equals? (vectorises)
       -   ... i.e. keep only those permutations that evaluate as [0,0,0,...,0]


답변

Brachylog , 9 바이트

⟦kpiᶠ≠ᵐhᵐ

온라인으로 사용해보십시오!

이것은 [0, …, n-1]주어진 하나의 배열을 출력하는 생성기입니다 n.

우리가 그것을 포장하는 경우 ᶠ - findallmetapredicate, 우리는 발전기에 의해 교란의 가능한 모든 세대를 얻을.

설명

⟦           The range [0, …, Input]
 k          Remove the last element
  p         Take a permutation of the range [0, …, Input - 1]
   iᶠ       Take all pair of Element-index: [[Elem0, 0],…,[ElemN-1, N-1]]
     ≠ᵐ     Each pair must contain different values
       hᵐ   The output is the head of each pair


답변

자바 스크립트 (V8) , 85 바이트

0부터 시작하는 모든 범위를 인쇄하는 재귀 함수.

f=(n,p=[],i,k=n)=>k--?f(n,p,i,k,k^i&&!p.includes(k)&&f(n,[...p,k],-~i)):i^n||print(p)

온라인으로 사용해보십시오!

댓글

f = (                   // f is a recursive function taking:
  n,                    //   n   = input
  p = [],               //   p[] = current permutation
  i,                    //   i   = current position in the permutation
  k = n                 //   k   = next value to try
) =>                    //         (a decrementing counter initialized to n)
  k-- ?                 // decrement k; if it was not equal to 0:
    f(                  //   do a recursive call:
      n, p, i, k,       //     leave all parameters unchanged
      k ^ i &&          //     if k is not equal to the position
      !p.includes(k) && //     and k does not yet appear in p[]:
        f(              //       do another recursive call:
          n,            //         leave n unchanged
          [...p, k],    //         append k to p
          -~i           //         increment i
                        //         implicitly restart with k = n
        )               //       end of inner recursive call
    )                   //   end of outer recursive call
  :                     // else:
    i ^ n ||            //   if the derangement is complete:
      print(p)          //     print it


답변

루비 , 55 바이트

->n{[*0...n].permutation.select{|x|x.all?{|i|i!=x[i]}}}

온라인으로 사용해보십시오!

모든 0 기반 정렬 생성


답변

05AB1E , 9 바이트

Lœʒāø€Ë_P

온라인으로 사용해보십시오!

설명

L           # push [1 ... input]
 œ          # get all permutations of that list
  ʒ         # filter, keep only lists that satisfy
   āø       # elements zipped with their 1-based index
     €Ë_P   # are all not equal


답변

Wolfram Language (Mathematica) , 55 바이트

Select[Permutations[s=Range@#],FreeQ[Ordering@#-s,0]&]&

온라인으로 사용해보십시오!


답변

Japt , 8 바이트

0 기반

o á fÈe¦

시험해보십시오 (Footer는 테스트 케이스와 쉽게 비교할 수 있도록 모든 요소를 ​​증가시킵니다)

o á fÈe¦     :Implicit input of integer
o            :Range [0,input)
  á          :Permutations
    f        :Filter
     È       :By passing each through this function
      e      :  Every element of the permutation
       ¦     :  Does not equal its 0-based index