September 29, 2017

In many data-processing shops it is standard practice to write programs that generate reports that summarize data in hierarchical groups, say a report of population by city nested within county nested within state nested with a grand total. The input data is a set of records, already sorted in the required order. The output is a printed report, often written on sprocket-fed green-bar paper (where I work, we had a high-speed line printer as late as about ten years ago). The trick, of course, is that you never know that a break has occurred until you read the record after the break, so you have to be careful to keep track of where you are in the hierarchy, and to properly initialize and complete each level of the hierarchy; the first and last records in the data set are always potential spots for error.

Your task is to write a program that produces a three-level control-break report; use any data set that interests you. When you are finished, you are welcome to read a suggested solution, or to post your own solution or discuss the exercise in the comments below.

September 26, 2017

We have a simple task today, converting a file from one format to another. Such tasks are often called data laundry, in the sense of washing or cleaning the data as it moves from one format to another. Here’s the input:

Your task is to write a program that converts the input shown above to the desired output. When you are finished, you are welcome to read or run a suggested solution, or to post your own solution or discuss the exercise in the comments below.

September 22, 2017

Eric Lippert, at his blog Fabulous Adventures in Coding, discusses the problem of comma quibbling, which turns a list of words like (“ABC” “DEF” “G” “H”) into the string “ABC, DEF, G and H” with commas between each pair of words except the last pair, which gets the word “and” (without a comma). Here are the rules:

If the input is empty, so is the output.

If the input has a single word, so does the output.

If the input has two words, the output is the two words separated by “and”.

If the input has more than two words, the output is all the words, separated by commas, but with the last comma replaced by “and”.

A word is a maximal sequence of characters not containing a comma or a space.

Your task is to write a function that adds commas to a list of words, using the rules described above. When you are finished, you are welcome to read or run a suggested solution, or to post your own solution or discuss the exercise in the comments below.

such that each element is equal to the one above plus the one to the left.

Your task is to write a program that calculates a Catalan triangle. When you are finished, you are welcome to read or run a suggested solution, or to post your own solution or discuss the exercise in the comments below.

September 15, 2017

In a recent exercise I described my new tablet computer, and talked about installing Guile Scheme because I could not get Chez Scheme to compile. I have finally been able to compile my preferred Scheme interpreter, Chez Scheme, on my Android ARM tablet. This note describes how I did it.

Chez Scheme is written partly in C and partly in Scheme; as a consequence, it requires a working Scheme compiler to be compiled. For popular platforms, such as Linux, Chez Scheme is distributed with the Scheme portion of the compiler already compiled, but for the Android ARM platform, we have to compile the Scheme portion of the compiler ourselves. The procedure is to compile Chez Scheme on some available platform (we will use Linux), cross-compile the Scheme portion of the compiler for the Android ARM platform, compile the C portion of the compiler on Android ARM, and then complete the build on Android ARM. It’s easier than it sounds. First, if you don’t already have Chez Scheme running on your Linux platform, perform the following steps to obtain and compile Chez Scheme (similar instructions apply on other platforms, go to the Chez Scheme Project Page for assistance):

At this point you should have a working Chez Scheme system on the Linux computer. You might want to stop and play with it to make sure it works. Assuming that you compiled on an Intel system, the machine type was a6le, so perform the following steps to cross-compile to machine type arm32le for the Android ARM:

Now the cross-compilation is complete and you are ready to work on the Android ARM system. Still on the desktop, pack up the complete Chez Scheme system:

cd /usr/local/src
sudo tar -czvf ChezScheme-9.4.tar.gz ChezScheme-9.4

We look next at the Android ARM tablet. We will be running under GnuRoot, so that must first be installed and configured. On the tablet, go to the Google Play Store and install program GnuRoot Debian; it should take only a few minutes. The environment installed by GnuRoot is minimal, so perform the following steps to install some useful software on your system:

Depending on your aspirations, you might want to install some other packages, or omit some of those shown above. Next, copy the .gz file to directory /usr/local/src on an Android tablet running GnuRoot; I did it by performing the following commands on the tablet, which was connected to my local network, but they are unlikely to work unmodified on your machine:

Once you have copied the .gz file to the tablet, perform the following steps there. It is odd to install and then uninstall X-windows, but Chez Scheme requires X-windows to compile, and doesn’t require it to run, so this sequence is correct (that was the trick that took me so long to figure out, delaying the compilation by several weeks):

And that’s it. To test your installation, type scheme at the command-line prompt; you should be rewarded with the Chez Scheme welcome text followed by a Scheme prompt:

Chez Scheme Version 9.4
Copyright 1984-2016 Cisco Systems, Inc.
>

Your task is to get your preferred programming environment working on your mobile device, and let us know how it works. If anyone installs Chez Scheme, I would appreciate your feedback on the instructions given above, particularly if you find any errors.

September 12, 2017

How many unique binary search trees can be made from a series of numbers 1, 2, 3, 4, …, n, for any given n?

Your task is to compute the number of unique binary search trees. When you are finished, you are welcome to read or run a suggested solution, or to post your own solution or discuss the exercise in the comments below.

September 8, 2017

Today’s exercise is an Amazon interview question from Career Cup (this is the exact question asked):

There is a given linked list where each node can consist of any number of characters :- For example
a–>bcd–>ef–>g–>f–>ed–>c–>ba.
Now please write a function where the linked list will return true if it is a palindrome .
Like in above example the linked list should return true

Your task is to write a program to determine if a list of strings forms a palindrome. When you are finished, you are welcome to read or run a suggested solution, or to post your own solution or discuss the exercise in the comments below.

September 5, 2017

We have three problems today that involve twiddling bits. In all three cases you are not permitted to perform any branching operation (so no if-else statement) nor are you permitted to perform a loop; you must write a single statement that performs the requested task:

1) Determine the absolute value of an integer.

2) Determine if an integer is a power of 2.

3) Determine the minimum and maximum of two integers.

The restrictions on branching and loops are useful for modern CPUs that have instruction caches that you want to stay inside for speed.

Your task is to write the three bit hacks described above. When you are finished, you are welcome to read or run a suggested solution, or to post your own solution or discuss the exercise in the comments below.

I recently needed to interrupt a prime generator, then restart it; I needed all the primes up to a limit, but of course I didn’t know I had reached the limit until the generator produced the first prime past the limit. I could have saved the too-large prime, then used it before restarting the generator, but that’s inconvenient; it needs a separate variable for the saved prime, and a test to determine if a saved prime is available each time through the restarted generator. A better solution is to push back the value onto the generator:

def pushback(val,gen):
yield val
while True:
yield next(gen)

Your task is to add push-back to the generator of your favorite language. When you are finished, you are welcome to read or run a suggested solution, or to post your own solution or discuss the exercise in the comments below.