Fast Iterative List Subsequences in Java
Today on Ray Myers Blog, (cadr life), he posted about returning lists of n-sized subsequences of a larger list. He used Java and tried to implement the recursive solution that he would normally write in Lisp or Haskell. He also issued a challenge to write this function without recursion. Well, my implementation is certainly not clean, but it is iterative, and is significantly faster for large lists (up to 10x). You can download my Java source file (with a main setup to time my version vs. his recursive version) here.
Problem: Spam-Egg List
So on Stoto’s Weblog Tuesday there was a little programming challenge called the Spam-Egg List Problem.
Here is the problem:
Write a function called dumpList that takes as its parameters a string and a reference to an arbitrarily complex nested list and prints the value of each list element on a separate line. The value on each line should be preceded by the string and numbers indicating the depth and index of the element in the list. Assume that the list contains only strings and other nested lists.
For example, given the following nested list:
list = [ 'a string', [ 'a', 'b', 'c' ], ’spam’, [ 'eggs' ] ] and the string ‘Foo’, the output of dumpList(’Foo’, list) would look like:
Foo.0: a string
Foo.1.0: a
Foo.1.1: b
Foo.1.2: c
Foo.2: spam
Foo.3.0: eggs