Descrivi le parole con le loro lettere


14

Ai fini della sfida attuale di "delineare" una parola significa circondarla successivamente con le proprie lettere, iniziando con l'ultima, e infine sostituire la parola originale al centro con spazi:

       oooooo 
       onnnno 
on ->  on  no 
       onnnno
       oooooo

Compito:

Dato un elenco di parole, composto solo da lettere inglesi minuscole e / o maiuscole, delinea ogni parola e visualizza orizzontalmente tutti i blocchi risultanti uno accanto all'altro, separati da una colonna di singolo spazio, allineati verticalmente al centro dei blocchi.

È possibile scrivere un programma completo o una funzione.

Ingresso:

Un elenco di parole o, se preferite, una stringa delimitata da spazi o altri simboli

Produzione:

La rappresentazione ASCII dei blocchi per le parole delineate. Sono consentiti spazi bianchi iniziali / finali.

Casi test:

Input 1: ["code", "golf"] (or "code golf")
Output 1:

    cccccccccccc gggggggggggg
    cooooooooooc goooooooooog
    coddddddddoc gollllllllog
    codeeeeeedoc golfffffflog
    code    edoc golf    flog
    codeeeeeedoc golfffffflog
    coddddddddoc gollllllllog
    cooooooooooc goooooooooog
    cccccccccccc gggggggggggg

Input 2: ["I", "am", "just", "a", "man"]  (or "I am just a man")
Output 2: 

           jjjjjjjjjjjj
           juuuuuuuuuuj     mmmmmmmmm
    aaaaaa jussssssssuj     maaaaaaam
III ammmma justtttttsuj aaa mannnnnam
I I am  ma just    tsuj a a man   nam  
III ammmma justtttttsuj aaa mannnnnam
    aaaaaa jussssssssuj     maaaaaaam 
           juuuuuuuuuuj     mmmmmmmmm 
           jjjjjjjjjjjj

Criteri vincenti:

Vince il codice più breve in byte in ogni lingua. Apprezzerò molto se commenterai / spiegherai il tuo codice e il tuo approccio.


Possiamo presumere che ci sia almeno una parola?
PurkkaKoodari,

@ Pietu1998 Sì, c'è sempre almeno una parola
Galen Ivanov,

1
@Kevin Cruijssen Transpose?
Galen Ivanov,

Risposte:


7

Tela , 22 20 byte

l *;±21*{;l└*e⟳} ]r⤢

Provalo qui!

Spiegazione:

{                 ]    map over the inputs
 l *                     array of length spaces - the canvas of the current word
    ;                    get the word back on top
     ±                   reverse it
      21*                repeat each character twice
         {      }        for each character
          ;l└              push the height of the item below (the canvas)
             *             repeat the character that many times vertically
              e            and encase the canvas in that char column
               ⟳           and rotate it clockwise for encasing the next time
                 ∙      push another space as the separator of words
                   r   center the words
                    ⤢  and transpose the final output (as everything was built vertically)

5

Carbone , 35 byte

FA«≔LιθMθ↑Fθ«B⁻׳θ⊗κ⊕⊗⁻θκ§ικ↘»M⊕⊗θ→

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

FA«

Scorri l'elenco di input.

≔Lιθ

Ottieni la lunghezza della parola corrente.

Mθ↑

Sposta nell'angolo in alto a sinistra del contorno risultante.

Fθ«

Ripeti una volta per ogni personaggio.

B⁻׳θ⊗κ⊕⊗⁻θκ§ικ

Disegna una casella di altezza, larghezza e carattere appropriati.

↘»

Passa all'angolo in alto a sinistra della casella successiva.

M⊕⊗θ→

Passa alla struttura successiva.


4

Haskell , 188 183 174 171 167 byte

-9 -13 byte grazie a Laikoni .

e=[]:e
c#a=k c<$>k(c<$a!!0)a
k c s=c:s++[c]
f w=foldr(#)[p w]w
p=(' '<$)
s w=unlines.map unwords.foldr(zipWith(:))e$until(\a->all((p a>=).p)$f<$>w)(k=<<p.head)<$>f<$>w

Provalo online!


\a->and[p a>=p x|x<-f<$>w]può essere \a->all((p a>=).p)$f<$>we k c=(++[c]).(c:)può essere k c s=c:s++[c].
Laikoni,

3

Pyth, 34 33 byte

Jsm+;uCjR*2HG_.iddm\ dQjCm.[lJd;J

Provalo online.

Scarica un sacco di spazio extra, ma ciò è consentito dalla sfida.

Spiegazione

  • m... Qfa quanto segue per ogni parola dnell'input Q:

    • m\ dmappa la parola con x => " ", essenzialmente creando la lista [" ", ..., " "]con tutti gli elementi quante la parola ha lettere.
    • .iddinterlaccia la parola con se stessa, ripetendo due volte le lettere della parola. _inverte questa stringa. worddiventa ddrrooww.
    • uinizia con G= la matrice di spazi e applica quanto segue con ogni lettera nella stringa interlacciata inH :
      • *2H ripete il personaggio due volte.
      • jR... Gmette ogni stringa Gtra la coppia di caratteri.
      • Cscambia righe e colonne. Quando questi tre passaggi vengono eseguiti due volte con lo stesso personaggio H, questo delinea le linee Gcon quel personaggio.
    • Ora abbiamo le colonne per la parola delineata d. +;antepone una colonna spaziale.
  • sappiattisce la matrice di colonne per ogni parola e la Jsalva nella variabile J.
  • m... Jfa quanto segue per ogni colonna dell'output:
    • .[lJd;riempie entrambi i lati della colonna con spazi in modo che la lunghezza della colonna sia uguale al numero di colonne. Questo è sempre un'imbottitura sufficiente per allineare le colonne verticalmente.
  • Ctrasforma le colonne in righe e junisce le righe con nuove righe.

Soluzione alternativa, 33 byte

j.tsm.[L\ l+dsQ+;uCjR*2HG_.iddm\ 

Provalo online.

Nota che c'è uno spazio finale. Principalmente lo stesso algoritmo, tranne i pad delle colonne in alto e quindi traspone con riempimento dello spazio.


3

R , 189 byte

function(x,S=32,`+`=rbind,`*`=cbind)cat(intToUtf8(Reduce(`+`,Map(function(s,K=utf8ToInt(s),o=S-!K){for(i in rev(K))o=i+i*o*i+i
for(j in(0:(max(nchar(x))-nchar(s)))[-1])o=S*o*S
o+S},x))+10))

Provalo online!

Una collaborazione tra digEmAll e me stesso in chat .

function(x){
 S <- 32			# space
 `+` <- rbind			# alias for rbind
 `*` <- cbind			# alias for cbind
 outlineWord <- function(s){	# function to construct the outline for each word
  chars <- utf8ToInt(s)		# convert to code points
  output <- S - !chars		# replace each char with 32 (space)
  for(i in rev(chars))
   o <- i + i * o * i + i	# o <- rbind(i,cbind(i,o,i),i)
  for(j in(0:(max(nchar(x))-nchar(s)))[-1])
   o <- S * o * S		# pad with spaces
   o + S}			# return with an additional row of spaces between words
 outlines <- Map(outlineWord,x)	# apply outlineWord to each element of x
 outlines <- Reduce(`+`,outlines)# reduce by rbind
 outlines <- outlines+10	# add row of newlines
 cat(intToUtf8(outlines))	# convert back to strings and print
}

187 con un evidente alias
J.Doe,

@ J.Doe è il wiki della community, quindi sentiti libero di modificarlo in :-)
Giuseppe,



1

05AB1E , 46 byte

εg©;ò<Uyη央∍«®>∍}y𫩪®Xиª˜».º.∊}¶«».C.B€SζJ»

Non troppo contento, ma sono contento che funzioni.

Provalo online o verifica tutti i casi di test .

Spiegazione:

ε                             # Map `y` over the (implicit) input-list
 g                            #  Take the length of the current item
  ©                           #  Store it in the register (without popping)
   ;                          #  Halve it
    ò                         #  Ceil and cast to integer at the same time
     <                        #  Decrease it by 1
      U                       #  Pop and store it in variable `X`
 yη                           #  Take the prefixes of the current string `y`
   ε       }                  #  Map over these prefixes:
    ¤                         #   Take the last character of the string
     ®×                       #   Increase it to a size equal to the length from the register
       «                      #   Append it to the current prefix
        ®>                    #   Take the length from the register, and add 1
                             #   Shorten the string to that size
 y                            #  Push the string `y` again
  ð«                          #  Append a space
    ©                         #  Store it in the register (without popping)
     ª                        #  Append it at the end of the list of modified prefixes
      ®                       #  Push the string with space from the register again
       Xи                     #  Repeat it `X` amount of times
         ª                    #  Append them to the list
          ˜                   #  Flatten to remove the empty appended list if `X` was 0
           »                  #  Join by newlines
            .º.∊              #  Intersect mirror both horizontally and vertically
                }             # Close outer map
                 ¶«           # Append a newline after each (for the space delimiters)
                   »          # Join everything by newlines
                    .C        # Centralize it horizontally
                              # (too bad a centralize vertically isn't available..)
                      .B      # Split on newlines again
                        S    # Convert each line to a list of characters
                          ζ   # Zip, swapping rows/columns (with space filler by default)
                           J  # Join the loose characters of every line to a string again
                            » # Join the lines by newlines (and output implicitly)
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.