The Friday Fragment

It’s Friday, and time for a new Fragment: my weekly programming-related puzzle.  For some background on Friday Fragments, see the earlier post.

This Week’s Fragment

Solve the following cryptogram:

Si spy net work, big fedjaw iog link kyxogy

This cryptogram is the dedication in the excellent book, Network Security, by Kaufman, Perlman, and Speciner.  Nothing fancy here; it’s just a simple substitution cipher.  So don’t bother writing or downloading a program to crack it, just sit down with a pencil and paper and plug at it, building up the substitution table as you go.

If you want to “play along”, post a solution as a comment or send it via email.  To avoid “spoilers”, simply don’t expand comments for this post.

Last Week’s Fragment – Solutions

Last week’s puzzle was this:

Write code to create a string of “fill data” of a given length, containing the digits 1-9, then 0, repeating.  For example, if given a length of 25, return “1234567890123456789012345″. For a length of 1, return “1″; for a length less than 1, return an empty string.

My son did it in Python, and a co-worker pointed me to a simple way he uses atAllPut: for fill data.  Here are some solutions, in three languages I often use:

In Smalltalk:

(1 to: length) inject: ” into: [ :str :ea | str, (ea \\ 10) printString ]

If a few extra temporary objects are a concern, do this:

ws := String new writeStream.
1 to: length do: [ :ea | ws print: (ea \\ 10) ].
ws contents

Here it is in Ruby:

(1..length).to_a.inject(”)  { | str, ea | str << (ea % 10).to_s() }

Although perhaps there’s a clever way to use join.

And, finally, in Java:

String s=””;
for (int i=1; i<=length; i++)
s += i % 10;

Easy enough, huh?  All it takes is a fragment.

Share This:
  • Print
  • Digg
  • StumbleUpon
  • del.icio.us
  • Facebook
  • Yahoo! Buzz
  • Twitter
  • Google Bookmarks
  • Google Buzz
  • RSS

One thought on “The Friday Fragment

  1. Your son

    Last week’s Friday Fragment, in python.

    Assuming variable length is defined:

    from operator import *
    output = ”.join(map(str, map(mod, range(length), [10]*length)))

    not quite two lines, since you have to import operator to get “mod” as a function rather than just an operator.

Comments are closed.