Matrice dans la plage


18

Le défi

Étant donné une n>0sortie entière, une n+1 X n+1matrice contenant tous les entiers de 1à 2ncomme indiqué dans les cas de test ci-dessous

Cas de test

n=1  

1  2  
2  2

n=2

1   2   4  
2   3   4  
4   4   4

n=5  

1   2   3   4   5   10  
2   3   4   5   6   10  
3   4   5   6   7   10   
4   5   6   7   8   10  
5   6   7   8   9   10  
10  10  10  10  10  10  

n=10  

1   2   3   4   5   6   7   8   9   10  20  
2   3   4   5   6   7   8   9   10  11  20  
3   4   5   6   7   8   9   10  11  12  20  
4   5   6   7   8   9   10  11  12  13  20  
5   6   7   8   9   10  11  12  13  14  20  
6   7   8   9   10  11  12  13  14  15  20  
7   8   9   10  11  12  13  14  15  16  20  
8   9   10  11  12  13  14  15  16  17  20  
9   10  11  12  13  14  15  16  17  18  20  
10  11  12  13  14  15  16  17  18  19  20  
20  20  20  20  20  20  20  20  20  20  20  

Je pense que le modèle est assez facile, alors voyons qui donnera la réponse la plus courte en octets.
C'est du

Règles

L'entrée doit être un entier ( 1 indexé )

La sortie peut être une matrice (comme illustré dans les cas de test) ou une liste de listes

Réponses:


10

R , 53 octets

function(n)rbind(cbind(outer(1:n,1:n,`+`)-1,2*n),2*n)

Utilise le outer"produit" pour générer toutes les sommes de la gamme 1,...,nsous forme de matrice, soustrait1 de chacune, puis binds 2*nsous forme de colonne à droite et de ligne en bas, en recyclant selon les besoins et renvoie une matrice comme résultat.

Essayez-le en ligne!

R , 78 octets

Implémentation plus naïve.

function(n){m=matrix(2*n,n+1,n+1)
for(i in seq(n))m[1:n,i]=(0:(2*n))[1:n+i]
m}

Essayez-le en ligne!


+1 sympa, je pensais à l'extérieur mais je n'y
— étais

7

Mathematica, 61 46 octets

ArrayFlatten@{{##-1&~Array~{#,#},2#},{2#,2#}}&

merci @alephalpha pour -15 octets


ArrayFlatten@{{Array[+##-1&,{#,#}],2#},{2#,2#}}&
— alephalpha

+##-1&peut simplement être ##-1&et vous pouvez utiliser infix Array:ArrayFlatten@{{##-1&~Array~{#,#},2#},{2#,2#}}&
— ngenisis

5

MATL , 12 10 octets

:&+q,!GEYc

Essayez-le en ligne!

Explication

:       % Input n (implicit). Push range [1 2 ... n]
&+      % matrix of pairwise additions
q       % Subtract 1
,       % Do twice
  !     %   Transpose
  GE    %   Push 2*n
  Yc    %   Concatenate that value to all rows, thus extending the matrix
        % End (implicit). Display (implicit)




4

JavaScript (ES6), 64 octets

f=
n=>[...Array(n+1)].map((_,i,a)=>a.map((_,j)=>n-i&&n-j?i-~j:n+n))
<input type=number min=0 oninput="t.innerHTML=f(+this.value).map(a=>`<tr>${a.map(b=>`<td>${b}</td>`).join``}</tr>`).join``"><table id=t>


4

Java 8, 99 octets

Lambda de Integerà int[][](par exemple Function<Integer, int[][]>). Étonnamment résistant au golf.

n->{int p=n+1,o[][]=new int[p][p],i=0,r,c;while(i<p*p)o[r=i/p][c=i++%p]=r<n&c<n?r-~c:2*n;return o;}

Essayez-le en ligne

Lambda non golfé

n -> {
    int
        p = n + 1,
        o[][] = new int[p][p],
        i = 0,
        r, c
    ;
    while (i < p * p)
        o[r = i / p][c = i++ % p] =
            r < n & c < n ?
                r - ~c
                : 2 * n
        ;
    return o;
}

Remerciements

  • -1 octet grâce à Kevin Cruijssen

Vous pouvez jouer au golf un octet en commençant i=0et en mettant le ++at [c=i++%p].
— Kevin Cruijssen

3

Python 2 , 64 62 61 octets

-3 octets grâce à M. Xcoder.

lambda n:[range(i+1,i-~n)+[n*2]for i in range(n)]+[[n*2]*-~n]

Essayez-le en ligne!

Il me manque probablement un schéma clé.

Python 2 , 76 octets

lambda n:[[[n*2,i-~j][n-i and n-j>0]for j in range(n+1)]for i in range(n+1)]

Essayez-le en ligne!


1
Comme d'habitude, *(n+1)c'est *-~n.
— M. Xcoder


Si vous souhaitez également adopter la version Python 3, le plus court que j'ai pu obtenir est de 64 octets
— M. Xcoder

La version de 76 octets peut être réduite à 72 octets
— Halvard Hummel



2

R , 54 63 67 octets

function(n)cbind(rbind(sapply(1:n-1,'+',1:n),2*n),2*n)

Essayez-le en ligne!

Merci à @Guiseppe pour le pointeur pour sapply et les 9 octets


En fait, cette approche peut également être améliorée: sapply(1:n-1,'+',1:n)mais alors c'est seulement un octet de plus que l'utilisation outer(les guillemets sont évidemment des astuces)
— Giuseppe

1
\`fonctionne pour échapper aux backticks dans un bloc de code délimité par des backticks @Giuseppe
— Cyoce


2

Recursiva , 50 octets

  • Seulement 10 octets plus court que python et donc c'est officiel, Recursiva n'est pas du tout un langage de golf ... C'est un esolang cependant. :RÉ
|{Ba+++++'PJ"	"W+Z~}B+~}'Va'+}'Va'AD'VaJ"	"W*;aADa

Essayez-le en ligne!



1

Röda , 44 octets

f n{seq 1,n|[[seq(_,_1+n-1)]+2*n];[[2*n]*n]}

Essayez-le en ligne!

Explication:

f n{seq 1,n|[[seq(_,_1+n-1)]+2*n];[[2*n]*n]}
f n{                                       } /* Function f(n)         */
    seq 1,n                                  /* Sequence 1..n         */
           |                                 /* For each _1:          */
              seq(_,_1+n-1)                  /*   Sequence _1.._1+n-1 */
             [             ]                 /*   As list             */
                            +2*n             /*   Append 2*n          */
            [                   ]            /*   Push to the stream  */
                                   [2*n]     /* List [2*n]            */
                                        *n   /* Multiplied by n       */
                                  [       ]  /* Push to the stream    */

1

Dyalog APL, 29 octets

A besoin ⎕IO←0

{(S,⍨1+¯1 ¯1↓∘.+⍨⍳⍵+1)⍪S←2×⍵}

Essayez-le en ligne!

Comment?

  • 1+¯1 ¯1↓∘.+⍨⍳⍵+1 la partie supérieure gauche du tableau
  • (S,⍨...)⍪S←2×⍵ le coin

1

> <>, 84 + 2 octets

+2 pour le drapeau -v

Imprime avec des tabulations entre les valeurs et des sauts de ligne entre les lignes. Imprime également un onglet de fin sur la dernière ligne.

Essayez-le en ligne

1:r:&r&)?\0:r:&r&(?\~$:@2*nao1+!
a0./:r:0~<.17+1o9\ \$:@$:@+n9o1+
   \&r&)?;$:@2*n /

Pré-golf

1>:r:&r&)?\0>    :r:&r&(?\~$:@2*nao1+\
            \+1o9n+@:$@:$/
 \                                   /
          \~0>:r:&r&)?;$:@2*n9o1+\
             \                   /





0

Clojure, 153 135 octets

Liste de listes? Yay, Lisp

(fn[n](loop[r[] i 0 d (* 2 n)](if(= i n)(conj r(conj(repeat n d)d))(recur(conj r(conj(vec(map #(+ i %)(range 1(inc n))))d))(inc i)d))))

Non golfé:

(defn a[n]
  (loop [r[] i 0 d (* 2 n)]
    (if(= i n)
      (conj r(conj(repeat n d)d))
      (recur
        (conj r
            (conj (vec (map #(+ i %)(range 1(inc n)))) d))
        (inc i)
        d))))

Fonction anonyme qui prend l'entrée en argument et renvoie une liste de listes.

Sortie de n = 5:

[[1 2 3 4 5 10] [2 3 4 5 6 10] [3 4 5 6 7 10] [4 5 6 7 8 10] [5 6 7 8 9 10] (10 10 10 10 10 10)]

0

05AB1E , 17 octets

FLN+I·¸«ˆ}·¸I>.׈

Essayez-le en ligne!

Explication

F                   # for N in [0 ... input-1]
 L                  # push range [1 ... input]
  N+                # add N to each
    I·¸«            # append input*2
        ˆ           # add to global list
         }          # end loop
          ·¸        # push [input*2]
            I>.×    # repeat it input+1 times
                ˆ   # add to global list
                    # implicitly output global list

0

J, 29 octets

(}:@(][\1+i.@+:),]#+:),.>:#+:

non golfé

(}:@(] [\ 1+i.@+:) , ]#+:) ,. >:#+:

explication

(}:@(] [\ 1+i.@+:)                   NB. make the inner part of the matrix
          1+i.@+:                      NB. 1..2*n, where n is the input
    (] [\ 1+i.@+:)                     NB. fork: infixes (sliding window) of length n, over 1..2*n
(}:@                                   NB. remove last element
                   , ]#+:)           NB. add a row of 2*n to the end
                           ,. >:#+:  NB. add a column of 2*n to entire result above

Essayez-le en ligne!


Hé, ma réponse APL et votre réponse ont le même nombre d'octets! Pouvez-vous ajouter une explication s'il vous plaît?
— Zacharý

@ Zacharý, mis à jour. fwiw, cela pourrait probablement être joué au moins un peu plus loin par quelqu'un comme moi, et peut-être par 10+ octets supplémentaires par un expert J.
— Jonah

0

En fait , 23 octets

;;Rnkp@;r♀+@;τ;(♀q)@α@q

Essayez-le en ligne!

Explication:

;;Rnkp@;r♀+@;τ;(♀q)@α@q
;;                       two copies of input
  R                      range(1, input+1)
   n                     copy input times
    kp@                  push stack to list, remove first element
       ;r                push range(input)
         ♀+              pairwise addition (add the value in the range to each value in the corresponding list)
           @;            duplicate input again
             τ;          input*2, duplicate that
               (♀q)      append input*2 to each list
                   @α@q  append a row of input*2

0

Clojure v1.8, 97 octets

#(conj(mapv(fn[i](conj(vec(range i(+ % i)))(* 2 %)))(range 1(inc %)))(vec(repeat(inc %)(* 2 %))))

Essayez-le en ligne!

Explication

(range 1(inc %))                           Numbers from 1 to 'n'
(mapv ... (range 1(inc %)))                For each one of these numbers
(fn[i](conj(vec(range i(+ % i)))(* 2 %)))  Create the numbers from 'i' to (n+i-1), convert to vector and insert '2*n' to the vector
#(conj ... (vec(repeat(inc %)(* 2 %))))    Insert to the previous vector a vector of repeated '2*n's

En utilisant notre site, vous reconnaissez avoir lu et compris notre politique liée aux cookies et notre politique de confidentialité.
Licensed under cc by-sa 3.0 with attribution required.