Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Golf - now for the Sentencing.

by grep (Monsignor)
on Apr 22, 2002 at 07:40 UTC ( #161011=perlmeditation: print w/ replies, xml ) Need Help??

Inspired by this node I thought a golf challenge of handling sentence case would be a lark.

Rules:
  • English sentences
  • must work on one or more sentences
  • capitalize the first word in a sentence
  • capitalize single I's
  • to keep it simple, only periods for ending punctuation
  • obviously proper names don't count
  • and to start it off

    00000000011111111112222222222333333333344444444445 12345678901234567890123456789012345678901234567890 $_=join'. ',map{ucfirst}split/\. /,lc;s/\bi\b/I/g;

    this is my first golf so hopefully I won't be too embarrassed with a score of 50



    grep
    Unix - where you can throw the manual on the keyboard and get a command

    Comment on Golf - now for the Sentencing.
    Download Code
    Re: Golf - now for the Sentencing.
    by Sidhekin (Priest) on Apr 22, 2002 at 08:32 UTC

      Of course, the real-world case is not this simple. So? ;-)

      I include a simple test harness, but otherwise follow your lead -- up to and including that trailing ";", with which my solution comes in at 25 strokes:

      # Test: $_='there is more than one way to do it. beware sentences followed by anything other than a single " ". just another golfer.'; # 3456789 123456789 12345 s/^.|\bi\b|\.\s*./\U$&/g; # Proof: print;

      The Sidhekin
      print "Just another Perl ${\(trickster and hacker)},"

        But this solution doesn't work when
        $_='tHere are CAPITAL leTters iNterSpersed';
        I believe grep wants everything to be lowercased other than beginning of sentences and single I's. Adding an lc and using your regex, this code
        # Test: $_='tHere is more than one way to do it. Also, i am just ANOTHER golfer.'; # 1 2 3 #234567890123456789012345678901 $_=lc;s;^.|\bi\b|\.\s*.;\U$&;g; # Proof print
        should output this
        There is more than one way to do it. Also, I am just another golfer.

          I believe grep wants everything to be lowercased other than beginning of sentences and single I's.

          Well, that's what I get for reading the rules only, and not his solution. Clearly, the intent of that code is to lowercase everything else. I cannot beat 31, and nor does japhy — his code is identical to yours, except for your trailing ";" and variant delimiters (and if he can ignore the test harness, so can you).

          (But I would have found the same! :-)

          Now, of course the lc version is shorter and therefore better, but I could not leave this without some code, now could I? This is at 35 strokes; the best remains at 31.

          # 3456789 123456789 123456789 12345 s/(^.|\.\s*.|\bi\b)|(.)/\U$1\L$2/g;

          The Sidhekin
          print "Just another Perl ${\(trickster and hacker)},"

    Re: Golf - now for the Sentencing.
    by japhy (Canon) on Apr 22, 2002 at 13:58 UTC
      I can grind out the following, at 30:
      #23456789012345678901234567890 $_=lc;s/^.|\bi\b|\.\s*./\U$&/g

      _____________________________________________________
      Jeff[japhy]Pinyan: Perl, regex, and perl hacker, who'd like a (from-home) job
      s++=END;++y(;-P)}y js++=;shajsj<++y(p-q)}?print:??;

    Re: Golf - now for the Sentencing.
    by PrakashK (Pilgrim) on Apr 23, 2002 at 00:56 UTC
      000000000111111111122222222223333333333 123456789012345678901234567890123456789 s/(.+?)(\.\s*)/\u\L$1$2/gs;s/\bi\b/I/gs
      39.

      /prakash

    Re: Golf - now for the Sentencing.
    by jmcnamara (Monsignor) on Apr 23, 2002 at 07:47 UTC

      Sidhekin's methodology is better. Nevertheless, here is my take. 33 chars
      #23456789_123456789_123456789_123 (33) s/[^.]+.\s*/\u\L$&/g;s/\bi\b/I/g; # Or for cases with leading whitespace #23456789_123456789_123456789_1234 (34) s/\s+|[^.]+./\u\L$&/g;s/\bi\b/I/g;

      --
      John.

    Re: Golf - now for the Sentencing.
    by oakbox (Chaplain) on Apr 23, 2002 at 14:57 UTC
      "Unix - where you can thrown the manual on the keyboard and get a command"

      After reading your sig, I immediately started dropping 'Essential System Administration' on my keyboard to see if, in fact, I could get a command.

      I tried several different manuals, just for completeness. After testing such works as 'Perl Cookbook', 'Advanced Perl Programming', 'Linux Unleashed', 'Perl 5 Unleashed', and several others. I can report that the GNU Emacs pocket reference gives the best results with 3 actual commands in 25 drops.

      Now to go and find a new keyboard.
      -oakbox

        grep probably meant:
        Perl - where you can throw the manual on the keyboard and get a program

        -- stefp -- check out TeXmacs wiki

    Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Node Status?
    node history
    Node Type: perlmeditation [id://161011]
    Approved by rob_au
    Front-paged by japh
    help
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others exploiting the Monastery: (14)
    As of 2014-10-20 18:18 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      For retirement, I am banking on:










      Results (87 votes), past polls