Perl golf

The exercises are from Ton Hospel's Perl golf pages.

Other exercises can be found on the original Perl Golf homepage.

Minigolf 1

You get on stdin 10 strings of length 10, where each character is either space or x, so each line matches /^[ x]{10}\n\z/. Print "1\n" to stdout if there is a path of x's from top to bottom (the path may twist and turn), "0\n" otherwise. (only vertical and horizontal contacts count, NOT diagonal). Nothing should appear on stderr. Multiple paths, dead ends, loops and disconnected pieces are possible.

example input:

x x     x 
x x xxx xx
xxx x x x 
  xxx x   
  xxxxx x 
  x   xxx 
     xx x 
xxx xxx   

Clearly there is at least one path of x's from top to bottom (e.g. the one in red), so the result should be:


There is a testprogram provided by Rick Klement.

Minigolf 2

You get on stdin one integer per line. The first integer is the target. Find in the rest the number closest to the target and print it to stdout followed by a newline, nothing should go to stderr. No values will be equally close to the target. Each line matches /^[1-9]\d{0,3}\n\z/. Input will be at least two lines.

example input:


here 8 is the target number, 6 is closest (distance two), so the output should be:


Perl Minigolf 3

You get on STDIN a sequence of integers, each representing the size of a stack of boxes. Each stack is lower than 10, but can not be empty, so the input lines match /^[1-9]\n\z/. Print on STDOUT how many there boxes are at each level starting from above, each number properly terminated with a \n. Nothing should be printed on STDERR. There will be between 1 and 1000 (inclusive) boxes.

example input:


So the stacks look like:

|  |                                      <- 1 box
|  |                                      <- 1 box
+--+      +--+                +--+
|  |      |  |                |  |        <- 3 boxes
+--+      +--+                +--+
+--+      +--+                +--+
|  |      |  |                |  |        <- 3 boxes
+--+      +--+                +--+
+--+      +--+      +--+      +--+
|  |      |  |      |  |      |  |        <- 4 boxes
+--+      +--+      +--+      +--+
 5         3         1         3

Therefore the output should be:


Check your solution using the test program

Perl Minigolf 4

You get on the commandline a set of natural numbers (integers greater than or equal to zero). Find the smallest natural number not on the list, and print it to STDOUT followed by a newline. Nothing should appear on STDERR, the returncode does not matter. There will be 0 to 30 arguments, matching /^0\z|^[1-9]\d{0,5}\z/. The numbers can appear in any order and there can be duplicates.

example input:

3 7 13 15 0 1 0 4 21 5 13 13 13 9 10 12 8 6
Some missing numbers in this sequence are 2, 11, 14 ... The smallest of these is 2, so the output should be:

Check your solution using the version 3 test program

Perl Maxigolf 1

Consider a 2x2 square where each position can be filled (represented by a O (that's not a zero)), or empty (represented by a .). You can then look how many elements are filled on each row and column, for example:

 OO  -> 2 filled
 .O  -> 1 filled
1  2   filled
Which we could write in several ways. The two we will consider are: ol>
  • 21 (row sums, top first) 12 (column sums, left first)
  • 12 (column sums, left first) 21 (rwo sums, top first)
  • Your program will get as commandline arguments the row and column sums in the form /^[012][012]\z/. You are to output exactly one pattern that has these row and column sums or nothing if there is no way to satisfy the input. Nothing should go to STDERR. The output should match /^([O.][O.]\n){2}\z/ or /^\z/.

    Example arguments: 11 11

    Example Output:

    You are free to interpret the arguments as row-sums first or column-sums first. but you should be consistent. If your program assumes one order for a certain input, you must assume that order for all inputs

    Perl minigolf

    The exercises are from Terje Kristensen's pages.

    LED digits

    Given a number composed of 1 to 20 digits (matching /^\d{1,20}\z/) as argument, output on STDOUT the same number in a "LED" format where each digit is 3 cells wide and 5 cells high (matching /^([# ]{3}( [# ]{3}){0,19}\n){5}\z/).

    Digits should be single space separated and no trailing spaces should be present after the last digit, except the ones filling the blank cells within the digit itself. If no argument is given you are to print nothing.

    So if the input is:


    The output should be:

    ###  # ### ### # # ### ### ### ### ### ###
    # #  #   #   # # # #   #     # # # # #   #
    # #  # ### ### ### ### ###   # ### ### ###
    # #  # #     #   #   # # #  #  # #   # #
    ###  # ### ###   # ### ### #   ### ### ###

    If the first digit is 1 you are to print the 2 leading spaces that compose the "LED" digit.


    Output this to STDOUT:


    It's the printable characters of an ASCII table.


    The Soundex Index System was developed so that similar sounding names or names with similar spelling could be encoded for easy retrieval. It has been used by the U.S. Bureau of the Census, and some States use it to help encode your drivers. license number. Your task is to read a sequence of names, one at a time and one per line, compute and print the corresponding soundex code (one line of output per name).

    Names will consist of 1 to 20 uppercase letters (matching /^[A-Z]{1,20}\n\z/).

    A Soundex Code always consists of a letter followed by three digits. Here are the rules for soundex encoding:

    1. The first letter of a name appears as the first and only letter in the soundex code.
    2. The letters A, E, I, O, U, Y, W and H are never encoded, but do break successive code sequences (see next rule).
    3. All other letters are encoded EXCEPT when they immediately follow a letter (including the first letter) that would be encoded with the same code digit.
    4. Since the first letter is in the code, the first letter and all immediately following letters with the same code digit shall not be encoded.
    5. The soundex code guide is:
      Code Key Letters and Equivalents
      1 B, P, F, V
      2 C, S, K, G, J, Q, X, Z
      3 D, T
      4 L
      5 M, N
      6 R
    6. Trailing zeros are appended to short codes so all names are encoded with a letter followed by three digits.
    7. Longer codes are truncated after the third digit.

    Example: SCHAEFER. First letter is S so the code will start with S. SC is not coded (rule 4), HAE is not coded (rule 2), F => 1, E is not coded (rule 2), R = 6. This leaves S16, and with zero padding this gives the soundex code of S160.

    Crossing (s)words

    Input consists of two lines on STDIN, each contain an odd length word completely in uppercase (so STDIN matches /^([A-Z]{L}\n){2}\z/ with L an odd number).

    These words have no letters in common except for the letter exactly in the middle.

    The lengths of the two words are identical, 3 ≤ L ≤ 19 (and L % 2 == 1).

    You must output these two words to STDOUT, the first word vertical and the second horizontal so that they intersect on the common middle letter.

    You should use leading spaces to position the vertical word, but there should be no trailing spaces.

    So STDOUT will match /^( {H}[A-Z]\n){H}[A-Z]{L}\n( {H}[A-Z]\n){H}\z/ where H=(L-1)/2.

    Sample input


    Sample output


    Rough flying

    While flying on Kiwi Air (motto "we soar like the Kiwi bird") I have noticed that every time the plane is jolted downwards I lose 20% of my drink and every time the plane is jolted upwards I lose 10% of my drink. Read as a command line argument the proportion of drink remaining, and print out the number of upjolts and downjolts.

    Input will be a one 6-place decimal number (matching /^0\.\d{6}\z/) as a command line argument, representing the proportion of drink left. The number of jolts will always be such that the proportion left can be exactly represented in 6 places of decimals.

    Output (STDOUT) will be the number of downjolts followed by the number of upjolts, separated by a single space (matching /^\d \d\n\z/).

    Example Input


    Example Output

    3 2