Postado Agosto 25, 2016 8 anos Description Text reflow means to break up lines of text so that they fit within a certain width. It is useful in e.g. mobile browsers. When you zoom in on a web page the lines will become too long to fit the width of the screen, unless the text is broken up into shorter lines. *: Podem fazer em qualquer linguagem Input You will be given a text with a maximum line width of 80 characters. Output Produce the same text with a maximum line width of 40 characters Challenge Input Quote In the beginning God created the heavens and the earth. Now the earth was formless and empty, darkness was over the surface of the deep, and the Spirit of God was hovering over the waters. And God said, "Let there be light," and there was light. God saw that the light was good, and he separated the light from the darkness. God called the light "day," and the darkness he called "night." And there was evening, and there was morning - the first day. Challenge Output Quote In the beginning God created the heavens and the earth. Now the earth was formless and empty, darkness was over the surface of the deep, and the Spirit of God was hovering over the waters. And God said, "Let there be light," and there was light. God saw that the light was good, and he separated the light from the darkness. God called the light "day," and the darkness he called "night." And there was evening, and there was morning - the first day. Bonus Let's get rid of the jagged right margin of the text and make the output prettier. Output the text with full justification; Adjusting the word spacing so that the text is flush against both the left and the right margin. Bonus Output Quote In the beginning God created the heavens and the earth. Now the earth was formless and empty, darkness was over the surface of the deep, and the Spirit of God was hovering over the waters. And God said, "Let there be light," and there was light. God saw that the light was good, and he separated the light from the darkness. God called the light "day," and the darkness he called "night." And there was evening, and there was morning - the first day. Finally This challenge is posted by /u/slampropp Also have a good challenge idea? Consider submitting it to /r/dailyprogrammer_ideas Link do topico original
Postado Dezembro 7, 2016 8 anos Fiz em JavaScript, usei JQUERY só para usar a função .text(), a lógica mesmo é em Javascript Puro... Fiquei com preguiça no final de reanalizar as linhas e adcionar mais espaços... hehehehe $(function(){ var texto = $('#input').text(); var limite = 40; var out = ''; var paragrafos =['']; var indice=0; texto.split("\n").forEach(function(elm,ind, arr){ paragrafos[indice] += elm; if(elm.lastIndexOf('.') == elm.length -1){ indice++; paragrafos[indice]=''; } }); paragrafos.forEach(function(elm,ind, arr){ out += limitaTXT(elm, limite); }); function limitaTXT(entrada, limite){ var out=''; while(entrada.length > limite){ var entrada_1 = entrada.substr(0, limite); var fim = entrada_1.lastIndexOf(' '); if(fim === -1){ fim = limite;} // não encontrou espaço, logo será enviado os 40 primeiros characteres out += entrada_1.substr(0,fim+1)+'\n'; entrada = entrada.substr(fim+1); } if(entrada.length > 0){ out += entrada+'\n'; } return out; } $('#output').text(out); }); Aqui está o código funcionando para quem quiser continuar o desafio e colocar os espaços... https://jsbin.com/sijajesezu/edit?html,js,output
Postado Dezembro 30, 2016 8 anos Python é tão gostosinho *com justificação* def reflow(text, width): line, *words = text.split() for word in words: if len(line) + len(word) >= width: while len(line) < width: line = line.replace(' ', ' ', width - len(line)) print(line) line = word else: line = ' '.join([line, word]) print(line) if __name__ == '__main__': import argparse parser = argparse.ArgumentParser() parser.add_argument('-w', '--width', default=80, type=int) args = parser.parse_args() import sys reflow(' '.join(sys.stdin.readlines()), args.width) Editado Dezembro 30, 2016 8 anos por Lordfire (veja o histórico de edições)
Participe da conversa
Você pode postar agora e se cadastrar mais tarde. Se você tem uma conta, faça o login para postar com sua conta.