Altri tre numeri

Oct 09 2020

Dati tre numeri distinti da \$1\$a \$7\$, restituisce altri tre numeri distinti da \$1\$a \$7\$, ovvero non avere numeri in comune con i numeri originali. Il codice deve produrre un diverso set di output per ogni possibile set di input. Cioè, due input non possono produrre lo stesso output, trattandoli entrambi come insiemi non ordinati. Oltre a questo, puoi implementare qualsiasi mappatura desideri.

Più matematicamente, ti viene chiesto di dare una biiezione (funzione uno-a-uno) \$f:S \to S\$dove \$S\$consiste in sottoinsiemi di tre elementi di \$\{1,2,3,4,5,6,7\}\$, tale che \$f(s) \cap s = \emptyset\$per ogni \$s\in S\$. Come biiezione, questa mappatura deve essere invertibile, sebbene non sia necessario fornire la funzione inversa nel codice.

Ecco le 35 possibili triple (scritte separate da spazi ).

I / O

Il formato degli insiemi di tre elementi è flessibile. Puoi prendere gli input in ordine ordinato come tre numeri o un elenco / array / tupla di tre elementi o come un insieme. Tuttavia, non è possibile richiedere input ordinati in un ordine specifico diverso da quello ordinato. Puoi azzerare l'indice.

È inoltre possibile utilizzare una sequenza di sette bit di cui tre attivi. Questa sequenza di sette bit può anche essere rappresentata come numero decimale, byte o carattere.

L'output può essere fornito in uno qualsiasi di questi formati, con l'ulteriore vantaggio che gli output ordinati non devono essere ordinati.

Risposte

12 HyperNeutrino Oct 09 2020 at 19:46

Python 3 , 53 byte

def f(b):c=[*{*range(7)}-b];del c[-sum(b)%4];return c

Provalo online!

-3 byte grazie a FryAmTheEggman
-4 byte indicizzazione zero
-1 byte grazie a xnor

11 JonathanAllan Oct 09 2020 at 22:26

Python 3 , 43 byte

lambda s:([*{*range(7)}-s]*4)[-sum(s):][:3]

Provalo online!

8 JonathanAllan Oct 09 2020 at 21:39

Gelatina ,  9 8  7 byte

7RṚḟṙSḊ

Un collegamento monadico che accetta un elenco dei tre numeri, da \$[1,7]\$, in ordine ordinato che restituisce un elenco di altri numeri, da \$[1,7]\$, non necessariamente ordinati.

Provalo online! Oppure vedi tutti e 35 (ho ordinato i valori risultanti per un confronto più semplice).

Come?

7RṚḟṙSḊ - Link: list A                   e.g.  [2,4,7]
7R      - seven range                          [1,2,3,4,5,6,7]
  Ṛ     - reverse                              [7,6,5,4,3,2,1]
   ḟ    - filter discard (A) -> B              [6,5,3,1]
     S  - sum (A)                              13
    ṙ   - rotate (B) left by (that)            [5,3,1,6]
      Ḋ - remove the leftmost                  [3,1,6]
7 DominicvanEssen Oct 10 2020 at 12:24

R , 33 31 byte

Modifica: -2 byte utilizzando modulo -4(che restituisce il negativo di modulo 4)

(1:7)[v<--scan()][sum(v)%%-4-1]

Provalo online!

Trova le 4 cifre in 1..7 che non sono nell'input ed esclude quella corrispondente alla somma dell'input (a capo).

Il collegamento TIO verifica che gli output siano univoci per ogni input e mostra l'output per ogni input.

(1:7)                   # vector of digits 1..7
     [          ]       # select elements
         -scan()        # excluding (negative indexes) input
      v<-               # and define v as (negative) input
                        # (so up to here we have the 4 elements that aren't in the input)  
     [              ]   # from these, select elements
      -                 # excluding (negative index)
       (sum(v)%%4+1)    # the sum of input, modulo 4, plus 1
5 Shaggy Oct 09 2020 at 21:24

Japt , 14 byte

Come pubblicizzato in precedenza , sono un po 'brillo quindi questo potrebbe essere sbagliato e, anche se giusto, potrebbe probabilmente essere giocato un po'.

7õ kU k϶UxÍu4

Provalo o visualizza (quello che penso sia) la prova

5 Neil Oct 09 2020 at 21:47

Carboncino , 39 38 34 32 byte

NθI⁻¹²⁷⁺θX²⊟Φ⁷№ETXdhp﹪×℅λX²ι¹²⁷θ

Provalo online! Il collegamento è alla versione dettagliata del codice. L'I / O è un numero intero a 7 bit 7..112. Spiegazione: Gli ordinali della stringa TXdhphanno cinque schemi di bit che ho scelto arbitrariamente in modo tale che il risultato escluda 1. Vengono quindi ruotati ciclicamente fino a quando uno corrisponde all'ingresso, a quel punto ho determinato il bit escluso. Questo bit viene quindi aggiunto all'ingresso originale e infine 127viene stampata la differenza tra e la somma.

Nθ                                  Cast input to integer
             ⁷                      Literal 7
            Φ                       Filter on implicit range
                TXdhp               Literal string `TXdhp`
               E                    Map over characters
                        λ           Current character
                       ℅            Ordinal
                      ×             Multiplied by
                          ²         Literal 2
                         X          Raised to power
                           ι        Outer index
                     ﹪              Modulo
                            ¹²⁷     Literal 127
              №                     Count (i.e. contains)
                               θ    Input
           ⊟                        Pop matching value
          ²                         Literal 2
         X                          Raised to that power
       ⁺                            Added to
        θ                           Input
   ⁻                                Subtracted from
    ¹²⁷                             Literal 127
  I                                 Cast to string
                                    Implicitly print

Ho scelto arbitrariamente i seguenti cinque pattern di bit da escludere, 1ma qualsiasi cinque pattern anche ciclicamente distinti funzionerebbe.

T   1010100
X   1011000
d   1100100
h   1101000
p   1110000
4 Arnauld Oct 09 2020 at 20:00

JavaScript (ES6),  66  65 byte

Accetta l'input come una stringa di 3 cifre. Restituisce una stringa nello stesso formato.

f=(n,k=i=0)=>++k<8?(~n.search(k)||n*43%399%4==i++?'':k)+f(n,k):''

Provalo online!

3 JonathanAllan Oct 09 2020 at 23:32

05AB1E , 10 byte

7LsKsO(._¨

Provalo online!

Come?

7LsKsO(._¨ - (push the input)      e.g.: [2,4,7]
7          - push 7                      7,[2,4,7]
 L         - range                       [1,2,3,4,5,6,7],[2,4,7]
  s        - swap top two of the stack   [2,4,7],[1,2,3,4,5,6,7]
   K       - push a without bs           [1,3,5,6]
    s      - swap top two of the stack   [2,4,7],[1,3,5,6]  (implicit input swapped in)
     O     - sum                         13,[1,3,5,6]
      (    - negate                      -13,[1,3,5,6]
       ._  - rotate a left by b          [6,1,3,5]
         ¨ - remove rightmost            [6,1,3]
           - implicit print top of stack [6,1,3]
3 Noodle9 Oct 11 2020 at 23:06

C (gcc) , 84 83 byte

Risparmiato un enorme 16 19 23 byte grazie all'uomo stesso soffitto gatto !!!
Salvato un byte grazie a Neil !!!

p;i;f(m){for(p=i=0;(L"᰸ᨴᘬᤲᔪ"[p]>>i%7&m)-m;p+=++i%7<1);p=(64>>i%7)+m^127;}

Provalo online!

Accetta l'input come \$3\$bit impostati nel meno significativo- \$7\$-bit di un inte restituisce allo stesso modo gli altri tre numeri.

Spiegazione (prima di alcuni golf)

f(m){                                  // function taking an integer with  
                                       // 3 bits set in its 7 lsb  
                                       // representing the 3 input numbers  
     for(                              // loop over  
         p=L"ᔪᘬᤲᨴ᰸"                     // a sequence of 5 int values:
                                       //   5418,5676,6450,6708,7224
                                       // that are the 5 unique patterns of    
                                       // 3 set bits per 7 bits shifted and
                                       // repeated over 13 bits so that their  
                                       // 7th bit is unset:  
                                       //   5418 = 1010100101010
                                       //   5676 = 1011000101100  
                                       //   6450 = 1100100110010  
                                       //   6708 = 1101000110100  
                                       //   7224 = 1110000111000
                    ;;++p)             // no need to test for stopping  
                                       // since we must match one  
       for(i=7;i--;)                   // loop over shift values from 6 to 0    
         if((*p>>6-i&m)==m)            // if a shifted 7-bit slice of one of  
                                       // our patterns matches m we've found  
                                       // the correct bit to exclude from m's  
                                       // 4 unset bits
            return(1<<i)+m^127;        // add that bit to m and flip the 7   
                                       // lsb so the 3 other unset bits are  
                                       // now set to represent the 3 return
                                       // values  
 }
2 Razetime Oct 10 2020 at 04:02

Husk , 9 byte

hṙ_Σ¹`-ḣ7

Provalo online!

2 KjetilS. Oct 10 2020 at 22:03

Perl 5 , 49 byte

sub{@c=grep!/[@_]/,0..6;splice@c,-sum(@_)%4,1;@c}

Provalo online!

Solo una traduzione della risposta python da HyperNeutrino.

2 TomerShetah Oct 11 2020 at 17:19

Scala , 64 byte

b=>1.to(7).diff(b).zipWithIndex.filter(_._2!=b.sum*3%4)map(_._1)

Provalo online!

2 ellcs Oct 12 2020 at 17:41

Ruby, 38 byte

->s{n=s.sum;(([*1..7]-s)*9)[-n..-n+2]}

Provalo online!

Rubare la risposta di Eric che si basa sulla risposta di Jonathan . Avrei commentato Eric, ma non ho abbastanza reputazione.

La differenza effettiva: utilizzare un intervallo per ottenere la sezione a tre elementi.

1 EricDuminil Oct 10 2020 at 16:23

Ruby , 40 byte

->s{(([*1..7]-s)*9).last(s.sum).first 3}

Provalo online!

Porta Ruby della risposta di Jonathan .

Lyxal Oct 09 2020 at 22:55

05AB1E , 17 byte

7LIм{3.$IO(._Dg<£

Provalo online!

È una versione molto più lunga della risposta Jelly, quindi vota anche tu.

Graham Oct 10 2020 at 18:05

APL + WIN, 20 byte

Chiede di inserire un vettore di numeri interi

3↑(-+/¯2↑n)⌽n←(⍳7)~⎕

Provalo online! Grazie a Dyalog Classic

Ven Oct 12 2020 at 14:55

APL (Dyalog Unicode) , 13 byte SBCS

1∘↓+/⌽(⌽⍳7)∘~

Provalo online!

Questa è la cima di un treno

1∘↓+/⌽(⌽⍳7)∘~
      (⌽⍳7)∘~ ⍝ Right side of the atop
        ⍳7    ⍝ Range
       ⌽      ⍝ Reverse
           ∘  ⍝ Composed with...
            ~ ⍝ ...without (to remove our arguments)
     ⌽        ⍝ Rotated by...
   +/         ⍝ ...the sum of the arguments
1∘↓           ⍝ Left side of the atop
1∘↓           ⍝ Drop leftmost (drop curried with 1)