<<   >>

47. Pyramigram

The pyramigram puzzle was posed by Eugene McDonnell in [128]:

 

The following puzzle originated with Linda Alvord, of Scotch Plains Fanwood High School in Scotch Plains, New Jersey. Ms. Alvord conducted APL contests for her students for several years and permitted nonstudents to enter the contests as well. An engaging thing about her contests was that she awarded an official-looking medal to the winner. I can remember how proud I was to receive my first medal. (To appreciate this story you should realize that at the time I had been using APL for a dozen years and was working in IBM’s prestigious APL design group.) Well, the day after I received the medal in the mail at home, my colleague Paul Berry was on the same commuter train to our office in Philadelphia. With great pride I showed my medal to Paul, who looked at it and said, “Oh yes, my son Michael has two of those.” Talk about put-downs!: Michael Berry (now in the Boston office of I.P. Sharp Associates) was then a high-school junior! In any event, here is the puzzle:

Write a function pg which takes a scalar integer argument from 1 to 26 and produce a character matrix containing a pattern like this:

         q
        w q
       q e w
      r q w e
     q e r t w

This example could have been generated by pg 5 , since it has 5 rows. In each row r appear r randomly selected and randomly ordered letters, once each. The letters are separated by single spaces, with equal numbers of spaces at either end of the row. The most interesting feature, however, is the sets of letters in adjacent rows differ only in one randomly selected letter.

A solution was provided in [129], translated into modern APL as follows:

pg←{
  d←⍵+⍵
  s←⍵×⍵
  t←(⍵,⍵)↑(⍵,d)⍴(1-d)↑'abcdefghijklmnopqrstuvwxyz'[⍵?26]
  p←⍋(s?s)+(-\⍵-⍨⌽⍳d)/s×⍳d
  (⌽⍳⍵)⌽((d-1)⍴1 0)\(⍵,⍵)⍴(,t)[p]
}

   ⎕rl←7*5    ⍝ to get reproducible random numbers

   pg¨ 4 5 6 7
┌───────┬─────────┬───────────┬─────────────┐
│   d   │    m    │     u     │      y      │
│  b d  │   v m   │    u v    │     y f     │
│ d b f │  m w v  │   x v u   │    f y e    │
│f d b h│ v m o w │  x t u v  │   e y f i   │
│       │m v w o j│ s t u x v │  y r i f e  │
│       │         │g s x v t u│ i r y f e o │
│       │         │           │w e i o r f y│
└───────┴─────────┴───────────┴─────────────┘