Nuova idea password: Word-walker


23

Ho pensato a un nuovo modo per generare le mie password, e anche se probabilmente non è molto intelligente a lungo termine, potrebbe comunque essere un divertente code-golf.

Prendendo una stringa di parole, la password viene generata in questo modo:

  • Scegli l' ennesimo carattere nell'ennesima parola
  • Se n è più grande della parola, continua a contare indietro

Esempio:

This is a fun task!
T     s a  u      !

T è il primo personaggio
s è il secondo
a è il primo, ma andando avanti e indietro è anche il terzo
u è il secondo ma a causa del conteggio all'indietro è anche il quarto
'!' è il quinto personaggio in "compito!" e quindi sarà incluso nella password finale,Tsau!

Regole

  • L'input sarà una stringa
  • Separare la stringa negli spazi, devono essere inclusi tutti gli altri caratteri
  • Le lettere maiuscole devono rimanere maiuscole, lo stesso con le lettere minuscole
  • In ogni parola fai n passi, dove n è il numero di parole che sono venute prima più una
  • Se n è più grande della parola, devi fare un passo indietro attraverso la parola, se premi l'inizio, vai avanti di nuovo finché non hai fatto un passo n volte
  • Il primo e l'ultimo personaggio vengono fatti una sola volta, quindi "divertente" in settima posizione come esempio va "funufun" e finisce con n, non "funnuff" e finisce con f
  • L'output deve essere una stringa

Esempi:

Input              Output
Once Upon A Time   OpAe
There was a man    Taaa
Who made a task    Waak
That was neat!     Taa
This is a long string to display how the generator is supposed to work  Tsagnoyotoipto

Vince il codice più breve in byte!


3
toè la dodicesima parola (0-indicizzata) nella stringa lunga, e quindi la lettera in codice dovrebbe essere t, non o.
Neil

@Neil <s> la sequenza è 1-indicizzata, altrimenti non puoi iniziare con la prima lettera della prima parola </s> (ho provato) il mio male, lo vedo ora
Troels MB Jensen

14
Tsau!è cinese perFuck!
sergiol

1
Anche il tuo piano di passaggio per la scelta del funufun rispetto al funnuff aumenterà la percentuale di vocali nell'output. Dal punto di vista crittografico, questo non è un potente generatore di password.
Criggie,

1
@ Criggie Non ho mai avuto intenzione di usarlo, ma come ho detto, sarebbe una sfida divertente, e sembra che i golfisti siano d'accordo
Troels MB Jensen,

Risposte:



7

05AB1E , 11 byte

#vyN©Fû}®è?

Provalo online!

Spiegazione

#             # split input on spaces
 vy           # for each word in input
   N©F        # N times do, where N is the current iteration
      û}      # palendromize the word
        ®è    # use N to index into the resulting word
          ?   # print


4

Java 10, 148 117 114 110 byte

s->{int i=-1,j;for(var a:s.split(" "))System.out.print(a.charAt((j=a.length()-1)>0*i++?i/j%2<1?i%j:j-i%j:0));}

-31 byte grazie a @SamYonnou creando una porta della risposta JavaScript di @ user71546 .
-4 byte grazie a @SamYonnou di nuovo, ottimizzando l'algoritmo per Java.

Provalo online.

Spiegazione:

s->{                            // Method with String parameter and no return-type
  int i=-1,                     // Step integer, starting at -1
      j;                        // Temp integer
  for(var a:s.split(" "))       // Loop over the parts split by spaces
    System.out.print(           // Print:
     a.charAt((j=a.length()-1)  //  Set `j` to the the length of the part minus 1
               >0               //  If the length of the part is larger than 1 (`j` > 0)
                 *i++?          //  (and increase `i` by 1 in the process with `i++`)
                i/j%2<1?        //   If `i` integer-divided by `j` is even:
                 i%j            //    Print the character at index `i` modulo-`j`
                :               //   Else:
                 j-i%j          //    Print the character at index `j` minus `i` modulo-`j`
               :                //  Else:
                0));}           //   Print the first (and only) character
                                //   (the >0 check is added to prevent divided-by-0 errors)

Non funziona per i casi di test 0, 2 e 5
TFeld

1
golfed fino a 117 "usando un approccio più aritmetico" simile a quello che sembra fare la versione di user71546:s->{int i=-1,j;for(var a:s.split(" ")){System.out.print(a.charAt(++i>(j=a.length()-1)?j>0?i/j%2==0?i%j:j-i%j:0:i));}}
SamYonnou,

1
@SamYonnou Grazie! E sono stato in grado di giocare a golf altri tre byte rimuovendo le parentesi e cambiando ==0in <1.
Kevin Cruijssen,

1
golf a 110 eliminando la ++i>(j=a.length()-1)condizione poiché la matematica funziona allo stesso modo indipendentemente dal risultato di tale condizione:s->{int i=-1,j;for(var a:s.split(" "))System.out.print(a.charAt(0<(j=a.length()+i-++i)?i/j%2<1?i%j:j-i%j:0));}
SamYonnou

1
@SamYonnou Grazie ancora! Ho leggermente modificato 0<(j=a.length()+i-++i)?in (j=a.length()-1)>0*i++?modo che la spiegazione fosse un po 'più facile da digitare (nessun byte salvato facendo così).
Kevin Cruijssen,

3

Carbone , 16 byte

⭆⪪S §⁺ι✂ι±²¦⁰±¹κ

Provalo online! Il collegamento è alla versione dettagliata del codice. Spiegazione:

  S                 Input string
 ⪪                  Split on spaces
⭆                   Map over words and join
      ι ι           Current word
       ✂ ±²¦⁰±¹     Slice backwards from 2nd last character to start exclusive
     ⁺              Concatenate
    §          κ    Cyclically index on current word index
                    Implicitly print

Spesso non riesco ad usare l'ultimo parametro di Slice.


Mi piace che Charcoal usi un glifo con le forbici
Giona,

3

JavaScript (Node.js) , 78 70 69 68 byte

-1 byte @Arnauld

x=>x.split` `.map((y,i)=>y[a=i%(l=y.length-1)|0,i/l&1?l-a:a]).join``

Provalo online!

Spiegazione

x=>
 x.split` `                    // Split the words by spaces
 .map((y,i)=>                  // For each word:
  y[                           //  Get the character at index:
                               //   A walk has cycle of length (2 * y.length - 2)
   a=i%(l=y.length-1)|0,       //   Calculate index a = i % (y.length - 1)
   i/l&1                       //   Check in which half the index i in
   ?l-a                        //   If in the second half of cycle, use y.length - 1 - a
   :a                          //   If in the first half of cycle, use a                  
  ]
 ).join``                      // Join back the letters

2

Rosso , 135 byte

func[s][n: 0 p: copy""foreach w split s" "[append/dup r: copy""append w
reverse copy/part next w back tail w n: n + 1 append p r/(n)]p]

Provalo online!

Leggibile:

f: func[s][
    n: 0
    p: copy ""
    foreach w split s "  "[
        r: copy ""
        append/dup r append w reverse copy/part next w back tail w n: n + 1
        append p r/(n)
    ]
    p
]



1

Pyth , 12 byte

s.e@+b_Ptbkc

Provalo online

s.e@+b_PtbkcQ   Final Q (input) implicit

           cQ   Split on spaces
 .e             Map the above with b=element, k=index
       Ptb        Remove 1st and last character
      _           Reverse
    +b            Prepend the unaltered element ('abcd' -> 'abcdcb')
   @      k       Get the kth character (0 indexed, wrapping)
s               Join on empty string, implicit output

1

Japt,, -P11 byte

¸Ëê ŪD gEÉ

Provalo

¸Ë+s1J w)gE

Provalo


spiegazioni

¸Ëê ŪD gEÉ
¸               :Split on spaces
 Ë              :Map over each element D at index E
  ê             :  Palindromise
    Å           :  Slice off the first character
     ªD         :  Logical OR with the original element (the above will return an empty string for single character words)
        g       :  Get the character at index
         EÉ     :  E-1
¸Ë+s1J w)gE
¸               :Split on spaces
 Ë              :Map over each element D at index E
   s1J          :  Slice off the first and last characters
       w        :  Reverse
  +     )       :  Append to D
         gE     :  Get the character at index E

1

C (gcc) , 148 byte (versione stringa), 114 byte (versione stampa)

Se devo restituire una stringa (versione lunga):

char c[99]={0};char*f(s,t,u,i,j,k)char*s,*t,*u;{for(u=c,i=0;t=strtok(s," ");s=0,i++)*u++=t[j=strlen(t),k=2*j-(j>1)-1,(i%k<j?i%k:k-i%k)%j];return c;}

Provalo online!

Altrimenti, stampo e non mi preoccupo di un buffer (versione corta):

f(s,t,i,j,k)char*s,*t;{for(i=0;t=strtok(s," ");s=0,i++)putchar(t[j=strlen(t),k=2*j-(j>1)-1,(i%k<j?i%k:k-i%k)%j]);}

Provalo online!


-(j>1)-1+~(j>1)penso che possa essere sostituito con 1 byte in meno.
Shieru Asakoto,

106 caratteri: putchar( t[ j=strlen(t)-1, k = i++ % (j ? j*2 : 1), k<j ? k : j+j-k ]); provalo online!
user5329483,

Versione con buffer: le variabili globali vengono azzerate implicitamente. Sostituisci *u++con c[i]e rimuovi u.
user5329483

Basandosi su @ user5329483 105 byte
ceilingcat il

1

AWK, 79 byte

Soprattutto perché sono curioso di vedere qualsiasi migliore soluzione awk o bash!

{for(i=1;i<=NF;i++){l=length($i);k=int(i/l)%2?l-i%l:k%l;printf substr($i,k,1)}}

Provalo online!



1

Haskell, 65 62 61 byte

zipWith(\i->(!!i).cycle.(id<>reverse.drop 1.init))[0..].words

Provalo online!

Richiede l'ultima versione di Preludecui presenta la <>funzione.

                   words    -- split the input string into a list of words
zipWith(\i->     )[0..]     -- zip the elements i of [0..] and the words pairwise
                            -- with the function      
      ... <> ...            --   call the functions with a word and concatenate
                            --   the results. The functions are
        id                  --     id: do nothing
        reverse.drop 1.init --     drop last and first element and reverse
    cycle                   --   repeat infinitely
(!!i)                       -- take the ith elemnt of  

Modifica: -3 byte grazie a @ user28667, -1 byte grazie a @B. Mehta


Sembra che funzioni zipWith(\i w->(cycle$id<>reverse.drop 1.init$w)!!i)[0..].wordsanche.
user28667

1
È possibile salvare un altro byte modificando il lambda in \i->(!!i).cycle.(id<>reverse.drop 1.init)factoring per la wmenzione esplicita (TIO)
B. Mehta

1

Stax , 9 byte

éñ~╗D¡┤Gq

Esegui ed esegui il debug

Disimballato, non golfato e commentato, sembra così.

j       split into words
{       start block for mapping
  cDrD  copy word; remove first and last character; reverse
  +     concatenate with original word
  i@    modularly (wrap-around) index using map iteration index
m       perform map

Esegui questo


1

PHP , 77 byte

while(ord($w=$argv[++$i]))echo($w.=strrev(substr($w,1,-1)))[~-$i%strlen($w)];

Provalo online!

  • -3 byte grazie a Kevin
  • -10 byte grazie a Tito

1
Bella risposta! Una piccola cosa da giocare a golf: è possibile eliminare le parentesi e un terzo byte aggiuntivo cambiando foreach(...){$c=...;echo$c[...];}in foreach(...)echo($c=...)[...];. Provalo online: 87 byte
Kevin Cruijssen

Puoi utilizzare l'elenco degli argomenti per dividere automaticamente in parole (-8 byte) e .=salvare due byte: while(ord($w=$argv[++$i]))echo($w.=strrev(substr($w,1,-1)))[~-$i%strlen($w)]; provalo online
Titus

Bello! Una domanda: ~ - $ i fa lo stesso di ($ i-1), giusto?
user2803033,

0

Powershell 208 186 170 byte

$args|%{$i=0;-join($_.Split()|%{$l=($b=($a=$_)).Length;if($l-gt2){$b=($a|%{-join$a[($l-2)..1]})}for($j=0;$a.Length-le$i;$j++){$a+=($b,$_)[$j%2]}$a.Substring($i,1);$i++})}

Ungolfed:

$args|%{
   $i=0;
    -join($_.Split()|%{
        $l=($b=($a=$_)).Length;
        if($l-gt2){
            $b=($a|%{-join$a[($l-2)..1]})
        }
        for($j=0;$a.Length-le$i;$j++){
            $a+=($b,$_)[$j%2]
        }
        $a.Substring($i,1);
        $i++
    })
}

Casi di prova di seguito o provalo online

@(
    "This is a fun task!",
    "Once Upon A Time",
    "There was a man",
    "Who made a task",
    "That was neat",
    "This is a long string to display how the generator is supposed to work"
)|%{$i=0;-join($_.Split()|%{$l=($b=($a=$_)).Length;if($l-gt2){$b=($a|%{-join$a[($l-2)..1]})}for($j=0;$a.Length-le$i;$j++){$a+=($b,$_)[$j%2]}$a.Substring($i,1);$i++})}

1
Ci sono molte cose che potresti abbreviare qui. Hai visto i suggerimenti per giocare a golf in PowerShell ?
Briantist,

Grazie! Avevo pensato di usare switch subito dopo la pubblicazione, ma il resto non mi era ancora venuto in mente.
Peter Vandivier,

Inoltre, un vero problema qui è che in realtà non prendi input da nessuna parte in questo frammento. Siamo abbastanza flessibili nel poter scrivere un programma o una funzione, ma il tuo ha un input implicito. Come primo passo potresti semplicemente rimpiazzare il tuo ""|%{con $args|%{, ma penso che puoi
giocarlo

1
Ecco una dimostrazione in TIO che mostra anche come utilizzare la funzione argomenti per casi di test . Mantenere il blocco di codice solo per il tuo codice ti consente anche di utilizzare il facile collegamento di TIO e il numero di byte per il tuo post!
Briantist,

0

J, 43 byte

[:(>{~"_1#@>|i.@#)[:(,}.@}:)&.>[:<;._1' '&,

ungolfed

[: (> {~"_1 #@> | i.@#) [: (, }.@}:)&.> [: <;._1 ' '&,
  • <;._1 ' '&, diviso negli spazi
  • (, }.@}:)&.> per ogni parola, uccidi il primo e l'ultimo olmo e aggiungi alla parola
  • #@> | i.@# prende il resto della lunghezza di ogni parola divisa nel suo indice
  • > {~"_1 prendi quel risultato e strappalo da ogni parola.

Provalo online!

Utilizzando il nostro sito, riconosci di aver letto e compreso le nostre Informativa sui cookie e Informativa sulla privacy.
Licensed under cc by-sa 3.0 with attribution required.