Qualche tempo fa c'era una sfida sulla moltiplicazione delle stringhe. Ci ha mostrato come possiamo moltiplicare non solo i numeri, ma anche le stringhe. Tuttavia, non possiamo ancora moltiplicare un numero per una stringa correttamente. C'è stato un tentativo di farlo, ma questo è ovviamente sbagliato. Dobbiamo sistemarlo!
Il tuo compito:
Scrivi una funzione o un programma che moltiplica due input, una stringa e un numero intero. Per moltiplicare (correttamente) una stringa per un numero intero, dividere la stringa in caratteri, ripetere ogni carattere un numero di volte uguale all'intero, quindi incollare nuovamente i caratteri. Se il numero intero è negativo, utilizziamo il suo valore assoluto nel primo passaggio, quindi invertiamo la stringa. Se l'ingresso è 0, non viene emesso nulla (qualsiasi cosa moltiplicata per 0 è uguale a nulla).
Ingresso:
Una stringa composta esclusivamente da caratteri ASCII e newline stampabili e un numero intero (possibile negativo).
Produzione:
La stringa moltiplicata per l'intero.
Esempi:
Hello World!, 3 --> HHHeeellllllooo WWWooorrrlllddd!!!
foo, 12 --> ffffffffffffoooooooooooooooooooooooo
String, -3 --> gggnnniiirrrtttSSS
This is a fun challenge, 0 -->
Hello
World!, 2 --> HHeelllloo
WWoorrlldd!!
punteggio:
Questo è code-golf , vince il conteggio di byte più basso!