You have 8 cards (pieces of paper) numbered from 1 to 8. The first 4 are all white, and the last 4 are all black. On each one is written:
#1: The next two cards are black.
#2: The next two cards are of different colors.
#3: The previous card has the same color as the next.
#4: There are the same number of black cards before and after this one.
#5: The previous card is of the same color as the next.
#6: The previous card is white.
#7: The next two cards are of the same color.
#8: The previous card is black.
Arrange the 8 cards in a row so that all the sentences result truthfully.
(In reply to re: Logical solution
Since you mention "the time of Cobol...", I'm assuming that was the old cobol, before COBOL II, when it still didn't have an END-IF, but just a period(.) to terminate all and any nested IFs. That, and the lack of substring capability were the worst things about that language for a sensible programmer. It also allowed insensible programmers to use an ALTER xxx TO PROCEED TO xxx to make debugging impossible. And of course any programming allowing GOTO allows the insensible programmer to wreak havoc, but sometimes a GOTO is the best way to handle something, when used in moderation.
Quick Basic, the language I most often used, and its similar QBasic and Visual Basic (versions 6 and under) all have similar syntax and include the needed END IF, as well as substring handling (as MID$). There is a programming tutorial at http://pages.intnet.mu/jhbpage/Program/Qbasic/tutorial/index.htm
, oriented to QBasic, a product very similar to QuickBasic. You can do a Google search for other QuickBasic tutorials.
The key to programming these puzzles is to devise a representation of the system, as either numbers or strings, possibly in arrays, in such a way that extraneous solutions are pruned as soon as possible so the execution time does not take forever. Also, from an execution time point of view QBasic is slower than QuickBasic and QuickBasic is slower than Visual Basic.
Many of the programs I write involve recursion, where either it's not known beforehand how many loops need to be nested, or the number of nests is so large that it's just easier to write one loop and make it recursive.
Some of my programs invoke a PERMUTE subroutine. I usually don't include that code in the solutions, as one of the Algorithms category puzzles is to devise such an algorithm for permutations.
For special purposes, requiring extra precision math, or use of handy prime number functions, I use UBASIC. Those are the ones in my comments that have line numbers on each line of the program.
Other than that, just examine the programs I have submitted in comments.
Posted by Charlie
on 2009-02-01 14:26:52