fhg
Volume 7, Number 43 -- December 12, 2007

Stuff I Didn't Publish This Year

Published: December 12, 2007

by Ted Holt


Note: The code accompanying this article is available for download here.


My wife and I recently visited our local Cracker Barrel, which is one of our favorite restaurants. While we were waiting for our food, I picked up the peg game that unfailingly occupies patrons at the tables of that establishment and, for the umpteenth time, failed to solve it.

The Peg Game

In case you're not familiar with a peg game, here's how it works. The board has 15 holes arranged in a triangle and 14 of the holes are occupied by pegs. To move, you jump a peg over an adjacent peg and land in the unoccupied hole on the other side, then remove the jumped peg from the board. The object of the game is to leave one peg on the board. It should be easy, but I can't figure it out. I always leave at least two or three pegs, which places me in the ignoramus category, I think.

As my wife and I sat there, trying to think of something to talk about other than the kids, it occurred to me that, while I'm too stupid to find a solution to the puzzle, I'm not too stupid to make a computer find a solution. A week or two later, I threw together an RPG program to print all possible solutions to the peg game. If you're interested, download it here.

With the number one hole left blank, here are four solutions.

  1. 040201
  2. 060504
  3. 010306
  4. 070402
  5. 100603
  6. 120805
  7. 130906
  8. 020509
  9. 030610
  10. 151006
  11. 060913
  12. 141312
  13. 111213
  1. 040201
  2. 060504
  3. 010306
  4. 100603
  5. 070402
  6. 130804
  7. 140905
  8. 020407
  9. 030508
  10. 110704
  11. 040813
  12. 121314
  13. 151413
  1. 060301
  2. 040506
  3. 110704
  4. 020407
  5. 130804
  6. 070402
  7. 151413
  8. 121314
  9. 100603
  10. 140905
  11. 010306
  12. 060504
  13. 020407
  1. 060301
  2. 151006
  3. 080910
  4. 100603
  5. 020509
  6. 140905
  7. 121314
  8. 070402
  9. 030508
  10. 010204
  11. 040813
  12. 141312
  13. 111213

Each move is a six-digit number made of a two-digit beginning position (From), a two-digit position to be jumped (Over), and a two-digit ending position (To). The first move of the first solution, 040201, means "the peg in hole 4 jumps the peg in hole 2 and lands in hole 1."

I numbered the holes one to 15, like this:

                   1
               2      3
             4     5     6
           7    8     9    10
         11   12   13    14   15

According to my program, there are 29,760 solutions to the peg game. I can't find even one of them. Ignoramus is too generous.

Indicators: *ON, *OFF, *NEITHER?

One of the first things I learned about RPG was that an indicator can be either off or on. This information was crucial because I was using the RPG II compiler on a System/3 Model 12. We didn't have any of these fancy IFxx, CABxx, CASxx, and DOxxx op codes or *INxx variables.

Fast-forward to the 21st century and witness how much we have progressed. Now a variable can be off, on, or neither off nor on. A three-valued indicator? Have we arrived or what? Here's how it's done.

Declare a two-byte character variable.

D SomeVar         s              2a   inz(*off)

SomeVar is *ON if it holds two ones, *OFF if it contains two zeros, and is neither *ON nor *OFF if it has one one and one zero. Now you can code expressions like these:

if SomeVar = *on;
   DoWhatever();
endif;

if SomeVar = *off;
   DoThis();
endif;

if SomeVar <> *on and SomeVar <> *off;
   DoSomething();
else; 
   DoSomethingElse();
endif;

if SomeVar = *on or SomeVar = *off;
   DoSomething();
else; 
   DoSomethingElse();
endif;

Don't those last two ifs look weird? Believe it or not, it's possible for the else branches to execute. And no, before you ask, I've never found a use for this technique.

Store a Binary Tree in an Array

Years ago, when I was earning my computer science degree, I used linked lists to implement binary trees in Pascal programs. Since linked lists don't work too well in RPG, it may be better to use an array instead. "How does that work?" I hear you ask.

Consider the following binary tree:

         Joe
        /   \
       /     \
      /       \
     /         \
   Bob         Sam
   / \         / \
  /   \       /   \
 /     \     /     \
Jim   Bud   Don    Abe

The same can be stored as an array. The left leaf of node nis in element 2n. The right leaf is in element 2n + 1.

Joe

Bob

Sam

Jim

Bud

Don

Abe

To find the parent of a node, divide the element number by two and throw away the remainder.

Logical Shortcuts

I have known people who took pride in writing a program as concisely as possible. If memory serves me correctly, the old Data Network magazine, the forerunner to Midrange Computing, often presented programming problems in which the object was to achieve the desired output with as few lines of RPG or OCL as possible.

I still like to keep programs short, but not at the expense of readability. Some shortcuts are clearly better, at least in my opinion. For example, I much prefer this:

/free
   *in25 = not *in25;

To this:

/free
   if *in25;
      *in25 = *off;
   else;
      *in25 = *on;
   endif;

I offer the following logical shortcuts with a healthy dose of skepticism that they are better than their longer equivalents.

Problem 1: Verify that an order is ready for shipment (i.e., all lines of an order are complete). A line is complete if the status code is E.

Long way:

ReadyToShip = *on;
dow '1';
   read OrdLine;
   if %eof();
      leave;
   endif;

   if Status <> 'E';
      ReadyToShip = *off;
   endif;

enddo;

Short way:

ReadyToShip = *on;
dow '1';
   read OrdLine;
   if %eof();
      leave;
   endif;

   ReadyToShip = ReadyToShip and Status = 'E';

enddo;

One line has replaced three lines. Here's a similar example.

Problem 2: Is at least one line of an order ready to ship?

Long way:

OneLineIsReady = *off;
dow '1';
   read OrdLine;
   if %eof();
      leave;
   endif;

   if Status = 'E';
      OneLineIsReady = *on;
   endif;

enddo;

Short way:

OneLineIsReady = *off;
dow '1';
   read OrdLine;
   if %eof();
      leave;
   endif;

   OneLineIsReady = OneLineIsReady or Status = 'E';

enddo;

What do you think? Better or worse?

While I'm on the subject of logic, here's a bit of code from a program I worked on recently. Do you see the logic error?

C     SOMEKEY    CHAIN     SOMEFILE                97 
C     *IN97      IFEQ      '0'                                   
C     *IN97      DOWEQ     '0'                                   
 ... more stuff 
C                ENDDO
C                ENDIF

Enjoy the Holidays

My family and I will soon be celebrating Christmas. I used to muddle through the Christmas season with a bit of cynicism, relating to the words of Henry Wadsworth Longfellow.

And in despair, I bowed my head:
"There is no peace on earth," I said,
"For hate is strong, and mocks the song
Of peace on earth, good will to men."

But I finally figured out that misery, cynicism, and other negative emotions don't solve any problems. Just the opposite is true. There is enough misery in the world without my choosing to be miserable, too.

So now I enjoy Christmas. I enjoy the special songs and carols, the lights and decorations, the excitement in my children's faces, the gatherings with family and friends. I receive more pleasure from giving than from getting. These days I think on other words from Longfellow:

Then pealed the bells more loud and deep:
"God is not dead, nor doth He sleep;
The wrong shall fail, the right prevail,
With peace on earth, good will to men."

Whatever holidays you celebrate, if any, I wish for you a season of happiness and peace. I hope that 2008 will be the year when everyone on earth becomes as happy as I am.

Answer to the logic question:

Since do-while is a top-tested loop, the if is unnecessary.




                     Post this story to del.icio.us
               Post this story to Digg
    Post this story to Slashdot


Sponsored By
PRODATA COMPUTER SERVICES

Push-Pull-Synchronize Data TODAY!

RDB Connect gives you easy access to remote databases from your System i programs.

Share real time data across platforms NOW!

Use Remote Database Connect today.....download a free trial today.

Order today and SAVE $$$!

800.228.6318

sales@prodatacomputer.com

www.prodatacomputer.com


Senior Technical Editor: Ted Holt
Technical Editors: Howard Arner, Joe Hertvik, Shannon O'Donnell, Kevin Vandever
Contributing Technical Editors: Joel Cochran, Wayne O. Evans, Raymond Everhart,
Bruce Guetzkow, Brian Kelly, Marc Logemann, David Morris
Publisher and Advertising Director: Jenny Thomas
Advertising Sales Representative: Kim Reed
Contact the Editors: To contact anyone on the IT Jungle Team
Go to our contacts page and send us a message.

Sponsored Links

looksoftware:  Present your core System i applications in Outlook, Google and Notes
COMMON:  Join us at the annual 2008 conference, March 30 - April 3, in Nashville, Tennessee
NowWhatJobs.net:  NowWhatJobs.net is the resource for job transitions after age 40


 

IT Jungle Store Top Book Picks

The System i RPG & RPG IV Tutorial and Lab Exercises: List Price, $59.95
The System i Pocket RPG & RPG IV Guide: List Price, $69.95
The iSeries Pocket Database Guide: List Price, $59.00
The iSeries Pocket Developers' Guide: List Price, $59.00
The iSeries Pocket SQL Guide: List Price, $59.00
The iSeries Pocket Query Guide: List Price, $49.00
The iSeries Pocket WebFacing Primer: List Price, $39.00
Migrating to WebSphere Express for iSeries: List Price, $49.00
iSeries Express Web Implementer's Guide: List Price, $59.00
Getting Started with WebSphere Development Studio for iSeries: List Price, $79.95
Getting Started With WebSphere Development Studio Client for iSeries: List Price, $89.00
Getting Started with WebSphere Express for iSeries: List Price, $49.00
WebFacing Application Design and Development Guide: List Price, $55.00
Can the AS/400 Survive IBM?: List Price, $49.00
The All-Everything Machine: List Price, $29.95
Chip Wars: List Price, $29.95


 
The Four Hundred
Database Tool Maker Joins the System i Market

State of the System i: Other Software Makers Weigh In

IDC Says Server Buyers Weigh Economy and Power in Q3

As I See It: What's Past Is Prologue

The Linux Beacon
AMD Stalled by a Bug in Barcelona Opterons

Red Hat Goes Grid and Real Time with Enterprise MRG Distro

IDC Says Server Buyers Weigh Economy and Power in Q3

As I See It: What's Past Is Prologue

Four Hundred Stuff
Above Security Takes i5/OS Log Aggregation to Heart

Shield's Remote Journal-Based DR Solution Matures at V2R1

Putting the 'i' Back Into PCI

Pat Townsend and BalaBit Pair Up to Cover System i Logs

Big Iron
Sine Nomine Shows Off Solaris on System z

Top Mainframe Stories From Around the Web

Chats, Webinars, Seminars, Shows, and Other Happenings

System i PTF Guide
December 8, 2007: Volume 9, Number 49

December 1, 2007: Volume 9, Number 48

November 24, 2007: Volume 9, Number 47

November 17, 2007: Volume 9, Number 46

November 10, 2007: Volume 9, Number 45

November 3, 2007: Volume 9, Number 44

The Windows Observer
Windows Anti-Piracy Program Gets Stronger, Weaker with Vista SP1

Exchange Server 2007 SP1 Goes RTM

SAP-Microsoft Mega-Merger Rumor Surfaces, Then Dies

Be My Guest

The Unix Guardian
Sine Nomine Shows Off Solaris on System z

Q&A with Jim Herring: The View from the Top

Sun to Release xVM Virtualization Under GPL v3 License

Be My Guest

Four Hundred Monitor
Four Hundred Monitor's
Full iSeries Events Calendar

THIS ISSUE SPONSORED BY:

Help/Systems
ProData Computer Services
DRV Technologies


Printer Friendly Version


TABLE OF CONTENTS
Use SQL User-Defined Functions to Avoid Data Repetition Problems

Stuff I Didn't Publish This Year

Admin Alert: Getting Started with Trial Capacity on Demand, Part 2

Four Hundred Guru

BACK ISSUES

From the IT Jungle Forums
Choose Logical File Format with SQL

IBM 6400 on LPT1 prints junk

Reallocate disk space from one LPAR to another

How to retrieve a workstation ID

Finding *OUTFILE Template Files





 
Subscription Information:
You can unsubscribe, change your email address, or sign up for any of IT Jungle's free e-newsletters through our Web site at http://www.itjungle.com/sub/subscribe.html.

Copyright © 1996-2008 Guild Companies, Inc. All Rights Reserved.
Guild Companies, Inc., 50 Park Terrace East, Suite 8F, New York, NY 10034

Privacy Statement