Mais Text Munger

Whitespace, punctuation, numbers — anything that isn’t a word — should also remain unchanged.

Ou seja, minha implementação anterior está fundamentalmente errada ;)

E eu só li essa parte do enunciado depois do comentário do Rodolpho.

Mas é fácil de resolver, usando o shlex mesmo:

import sys
import shlex

from random import shuffle

def munge(text):
    t = shlex.shlex(text, posix=False)
    t.quotes = "’"
    t.whitespace = ""
    #print(list(t))
    s = (munge_word(word) for word in t)
    print("".join(s))

def munge_word(aword):
    s = list(aword)
    first = s.pop(0)
    try:
      last = s.pop()
    except IndexError:
        return aword
    shuffle(s)
    return first + .join(s) + last

try:
  s = open(sys.argv[1]).read()
  munge(s)
except IndexError:
  print("Usage: munge.py file")
 

Esta entrada foi publicada em Geek life, Python e marcada com a tag , . Adicione o link permanente aos seus favoritos.

Deixe uma resposta

O seu endereço de email não será publicado Campos obrigatórios são marcados *

Você pode usar estas tags e atributos de HTML: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">