Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re: Re: (Golf) Cryptographer's Tool #1

by srawls (Friar)
on Jun 20, 2001 at 02:17 UTC ( [id://89843]=note: print w/replies, xml ) Need Help??


in reply to Re: (Golf) Cryptographer's Tool #1
in thread (Golf) Cryptographer's Tool #1

That doesn't work when 0 is in the 'alphabet.' Here's a modified solution that handles 0 properly, which is only 8 more char than yours: It adds the null character to each string, which shouldn't print on most machines (it does on my dos, not on my unix).
sub c { ($_,$a)=@_;s!.!$h{$&}||=($a=~/./g,"$&\0")!ge;y/\0//;$_ }

Update:Fixed code as per chipmunk's comment. It's 54 chars now.

The 15 year old, freshman programmer,
Stephen Rawls

Replies are listed 'Best First'.
Re: Re: Re: (Golf) Cryptographer's Tool #1
by chipmunk (Parson) on Jun 20, 2001 at 02:19 UTC
    Adding extra characters to the result is not a valid solution. They may be invisible when you print them out, but they're still there.
Re: Re: Re: (Golf) Cryptographer's Tool #1
by sean (Beadle) on Jun 20, 2001 at 05:08 UTC
    nice, srawls... here it is down to 51:
    sub c{ ($_,$a)=@_;s!.!chr($h{$&}||=($a=~/./g,ord$&))!ge;$_ #23456789_123456789_123456789_123456789_123456789_1 }

    It doesn't work with null either, but as i read it, thats fine.

    update: oops, here's one that resets %h and is reusable, as per the given example, at 54:
    sub c{ ($_,$a,%h)=@_;s!.!chr($h{$&}||=($a=~/./g,ord$&))!ge;$_ #23456789_123456789_123456789_123456789_123456789_1234 }
    --sean
Re: Re: Re: (Golf) Cryptographer's Tool #1
by MeowChow (Vicar) on Jun 20, 2001 at 03:31 UTC
    The fix fails if a null character is present in the alphabet.
       MeowChow                                   
                   s aamecha.s a..a\u$&owag.print
      Yes it does, but the this is straight from the specification:

      Given: a word, and an 'alphabet' string (and to be exact about this latter part, each character in the word and the alphabet can be represented in 7 bits, eg the printable ASCII set).

      The 15 year old, freshman programmer,
      Stephen Rawls

        Well, let's go pickin' nits... the latin abbreviation, "eg" means "for example". So according to the spec, any 7 bit ASCII character is possible; printable ASCII characters mearly represent examples of possible 7-bit characters. If, however, Masem had written "ie", which means "that is to say", then you would be correct.

        /me ducks and covers...

           MeowChow                                   
                       s aamecha.s a..a\u$&owag.print

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://89843]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (4)
As of 2024-04-19 05:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found