Strings Pilish
Neste desafio, transforme uma string em uma série de palavras (ou sequências de caracteres) separadas por um único espaço, com cada palavra tendo o mesmo comprimento indicado pelos primeiros 15 dígitos da representação decimal de Pi:
3.14159265358979Se uma string contiver mais caracteres do que a quantidade total dada pela soma dos dígitos de Pi, os caracteres não utilizados serão descartados e você usará apenas os necessários para formar 15 palavras.
String = "HOWINEEDADRINKALCOHOLICINNATUREAFTERTHEHEAVYLECTURESINVOLVINGQUANTUMMECHANICSANDALLTHESECRETSOFTHEUNIVERSE"
Pi String = "HOW I NEED A DRINK ALCOHOLIC IN NATURE AFTER THE HEAVY LECTURES INVOLVING QUANTUM MECHANICS"
// Every word has the same length of the digit of Pi found at the same index ?
// "HOW" = 3, "I" = 1, "NEED" = 4, "A" = 1, "DRINK" = 5
// "ALCOHOLIC" = 9, "IN" = 2, "NATURE" = 6, "AFTER" = 5
// "THE" = 3, "HEAVY" = 5, "LECTURES" = 8, "INVOLVING" = 9
// "QUANTUM" = 7, "MECHANICS" = 9
// 3.14159265358979Além disso, se uma string contiver menos caracteres do que a quantidade total dada pela soma dos dígitos de Pi, você deverá, de qualquer forma, respeitar a sequência dos dígitos para obter as palavras.
String = "FORALOOP"
Pi String = "FOR A LOOP"
// Every word has the same length of the digit of Pi found at the same index ?
// "FOR" = 3, "A" = 1, "LOOP" = 4
// 3.14Se as letras contidas na string não corresponderem exatamente aos dígitos, você repetirá a última letra até que a palavra tenha o comprimento correto.
String = "CANIMAKEAGUESSNOW"
Pi String = "CAN I MAKE A GUESS NOWWWWWWW"
// Every word has the same length of the digit of Pi found at the same index ?
// "CAN" = 3, "I" = 1, "MAKE" = 4, "A" = 1, "GUESS" = 5, "NOW" = 3
// 3.14153 (Wrong!)
// The length of the sixth word "NOW" (3)...
// ...doesn't match the sixth digit of Pi (9)
// The last letter "W" will be repeated...
// ...until the length of the word will match the digit
// "CAN" = 3, "I" = 1, "MAKE" = 4, "A" = 1, "GUESS" = 5, "NOWWWWWWW" = 9
// 3.14159 (Correct!)Se a string fornecida estiver vazia, uma string vazia deverá ser retornada.
Dada uma string txt, implemente uma função que retorne a mesma string formatada de acordo com as instruções acima.
Exemplos
pilishString("33314444") ➞ "333 1 4444"
// 3.14
pilishString("TOP") ➞ "TOP"
// 3
pilishString("X")➞ "XXX"
// "X" has to match the same length of the first digit (3)
// The last letter of the word is repeated
pilishString("")➞ ""Notas
- Este desafio é um conceito simplificado inspirado no Pilish, um tipo peculiar de escrita restrita que utiliza todos os dígitos conhecidos de Pi. Um texto potencialmente infinito pode ser escrito permitindo pontuação e um conjunto de regras adicionais, o que permite evitar longas sequências de dígitos pequenos, substituindo-as por palavras com mais de 9 letras e fazendo com que a composição pareça mais semelhante a um poema de verso livre.
- O ponto que separa a parte inteira de Pi da parte decimal não deve ser considerado na função: ele está presente nas instruções e nos exemplos apenas para facilitar a leitura.