Two Elisp Challenges

I ran across a couple of nice interview questions and an interesting story over at Tanya Khovanova’s Math Blog. The two questions are:

  1. Given a list of integers from 1 to n but with one of the integers missing, write an efficient algorithm for finding the missing integer.
  2. Given a list of integers from 1 to n but with one of the integers missing and another integer repeated, write an efficient algorithm for finding the missing and repeated integers.

In both problems the numbers in the list are in no particular order.

So the challenge is to write the two algorithms in Emacs Lisp. The interviewer expected a O(n log n) algorithm for time for the second problem. Can you do better?

Don’t go over to Khnovanova’s blog until you have solved the problems because there are spoilers in the comments. After you solve the problem, though, be sure to read her story about “hiring the smartest people in the world.”

Posted in Programming | Tagged , | 8 Comments

DOCUMENTATION-TEMPLATE

I’m just starting a Lisp project that, among other things, involves fiddling with dates. Working with dates isn’t particularly hard but it’s also not very rewarding. Oddly, Common Lisp doesn’t have any functions to format and manipulate dates. I certainly didn’t want to write a bunch of uninteresting code so I fired up Quicklisp to see what was available. Happily, I found the Simple-Date-Time library that did all the things I wanted it to.

The only problem was that there are a lot of functions but no documentation other than the doc strings in the functions. I thought about writing a quick script to extract them for use as documentation when I thought that this was surely something that someone else had already done. So I fired up Quicklisp again and found Edi Weitz’s DOCUMENTATION-TEMPLATE library. I had Quicklisp retrieve it for me, loaded it at the REPL, typed

(create-template 'simple-date-time :target #P"~/Desktop/dt.html")

and ended up with nicely formatted html documentation that I added to the documentation stack in my browser.

Sadly, it only works with LispWorks, SBCL, and AllegroCL but if you have one of those systems and need some documentation for a library, this is a quick and easy way to generate it. If you want to see what the finished product looks like, here is the documentation for DOCUMENTATION-TEMPLATE generated with DOCUMENTATION-TEMPLATE.

Posted in Programming | Tagged | Leave a comment

Checking The Easy Cases First

Over the weekend, I was amusing myself with this problem from the always entertaining Programming Praxis. The problem is to partition the set {1, 2, ..., 16} into two equally sized subsets such that

  • The sums of the integers in each subset are equal.
  • The sums of the squares of the integers in each subset are equal.
  • The sums of the cubes of the integers in each subset are equal.

That seems straight forward enough. My strategy was to generate combinations of the 16 integers 8 at a time and for each combination, s1:

  1. Calculate the set difference, s2, of {1, 2, ..., 16} with s1.
  2. Check if the sums in S1 and s2 were equal.
  3. Return the answer (s1 s2) if so,
  4. Get the next combination and return to step 1 if not.

That plan worked remarkably wells and gave me the answer in under .02 seconds.

Then I realized that the goal sums were known in advance. For example each sum of integers would have to be one-half the sum of 1 to 16 or 68. Using that fact meant that the number of summings was cut in half and there was no need to calculate a set difference until s1 had the correct sums. That cut the running time in about half. I felt pretty good about that but then I wondered how many correct sums for the integers, the squares, and the cubes there were.

So I adapted the code to count how many combinations summed to the correct amount for each type of sum. There were lots of integer combinations that were correct, just a few combinations of the squares, and exactly 1 of the cubes. Now the code is:

(dolist (c all-combinations)
  (if (= (reduce (lambda (x y) (+ x (* y y y))) c :initial-value 0) 9248)
      (return (list c
                    (set-difference '(1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16)
                                    c)))))

which produced the correct answer in .002 seconds. The point of this post is that it can pay to experiment a bit before you write a lot of code 1.

Footnotes:

1 Of course, this is a bit silly since you have to write most of the code to discover the fact that finding the correct sum of cubes suffices but the point stands: a little experimentation can save a bunch of time.

Posted in Programming | Tagged | 1 Comment

Troubleshooting Emacs Libraries

While trawling around the WikEmacs site today, I came across this page on troubleshooting that explained some commands that I wasn’t familiar with. The first two are helpful if you are trying to search out library problems. For example, if you want to know where your version of org-mode is coming from you could find out with 【Meta+xlocate libraryReturnorgReturn】 and Emacs will tell you where it is loading the library from

Library is file ~/tools/org-mode/lisp/org.elc

The related command list-load-path-shadows will show you if your load-path variable is causing one version of a library to shadow another. For example, when I run it, it tells me that the version of org-mode that came with Emacs is shadowed by the newer version from my tools directory.

Finally, and perhaps most useful, is the find-library command. Suppose you want to look at the code for a library you are using (including the core libraries), let’s say key-chord.el. Merely type 【Meta+xfind-libraryReturnkey-chordReturn】 and Emacs will open a buffer in a new window with the code loaded in it. Very handy.

Posted in General | Tagged | 1 Comment

TPK In Common Lisp

Yesterday I wrote about the Trabb Pardo Knuth algorithm and gave an implementation in Emacs Lisp. Elisp allows an nice implementation but was a bit frustrating because the Elisp interpreter handles overflows internally and never signals an overflow condition. Therefore, I decided to try it in Common Lisp to see how they differ and also to force an overflow.

You can’t get an integer overflow in Common Lisp because of the bignum support but you can with floating point calculations. Here’s my CL implementation of the TPK algorithm. I tried to make it as much like the Elisp version as possible.

(defun tpk ()
  (let ((s (make-array 11)))
    (dotimes (i 11)
      (princ "Enter number: " *query-io*)
      (setf (svref s i) (read *query-io*)))
    (map nil (lambda (n)
               (print (handler-case (expt 2.0 n)
                        (floating-point-overflow () "OVERFLOW"))))
         (reverse s))))

One nice thing about CL is that reverse works on any simple sequence and thus works on vectors. On the other hand, there isn’t a read-number function so I had to use read-line and parse-integer instead. Also, the CL mapc requires lists so I used map, which accepts vectors, instead.

When I ran tpk, I got gratifying results.

CL-USER> (tpk)
Enter number: 1
Enter number: 10
Enter number: 100
Enter number: 200
Enter number: 300
Enter number: 400
Enter number: 500
Enter number: 600
Enter number: 700
Enter number: 800
Enter number: 900

"OVERFLOW" 
"OVERFLOW" 
"OVERFLOW" 
"OVERFLOW" 
"OVERFLOW" 
"OVERFLOW" 
"OVERFLOW" 
"OVERFLOW" 
1.2676506e30 
1024.0 
2.0 
NIL

Update: Pascal Bourguignon pointed out that the specification said to read in numbers not integers so I’ve fixed the code above to use read rather than (parse-integer (read-line *query-io*)).

Posted in Programming | Tagged , , | 2 Comments

The Trabb Pardo Knuth Algorithm in Elisp

The latest Programming Praxis Exercise is interesting. Back in 1973, Luis Trabb Pardo and Don Knuth published an algorithm that was meant to assess the power of a programming language. The algorithm was

Ask for 11 numbers to be read into a sequence S
Reverse S
For each number in S
  Call some function to do a mathematical operation on the number
  If the result overflows
    Alert the user
  else
    Print the result

That seems simple enough but notice how it involves arrays (lists would be more natural in Lisp but the intent was to use arrays), indexing, I/O, iteration, general functions, mathematical functions, and error detection. Be implementing the algorithm in a language you get a good feel for how the language handles common programming tasks.

Naturally, I had to try it out in Lisp. I used Emacs Lisp as a sort of minimal Lisp but still got a very nice and concise implementation.

(require 'cl)
(let ((s (make-vector 11 0)))
  (dotimes (i 11)
    (aset s i (read-number "Enter a number: ")))
  (coerce (reverse (coerce s 'list)) 'vector)
  (mapc (lambda (n)
          (print (condition-case nil (expt 2 n) ((overflow-error) "OVERFLOW"))))
        s))

If I had used a list instead of an array, I wouldn’t have needed the two calls to coerce but I would have needed two reverses if I filled the list in the most natural way.

The condition-case is like try/except from other languages. It performs the exponentiation and returns the result unless an overflow error occurs in which case it returns OVERFLOW. Whatever is returned is then printed. The only problem I had was getting an overflow. Generating an overflow is hard in most Lisps because of their bignum facilities. Emacs Lisp doesn’t use bignums but it does handle overflows in the interpreter. A sexpr like (expt 10 1000) just returns 0. If you use (expt 10.0 1000) it returns 1.0e+INF. Division by zero does generate an error but its an arith-error not an overflow-error. If anyone knows how to generate an overflow in Elisp, leave a comment.

Posted in Programming | Tagged , | 4 Comments

The WikEmacs Elisp Cookbook

I stopped by WikEmacs today to see how the site was progressing. It looks pretty nice and has obviously seen some hard work by its contributors. Being me, I immediately went to the Emacs Lisp Cookbook to see how it looked. It’s pretty much the same as the EmacsWiki Elisp Cookbook but a little better formatted and with some of the material rearranged. So while there isn’t much new material it is nice to see that it’s migrated to WikEmacs.

I don’t take a stand on whether one site is better than the other; I’m just happy to see more information about Emacs collected together wherever folks feel most comfortable contributing. If you haven’t checked WikEmacs out lately, drop by and see what you think.

Posted in Programming | Leave a comment

And So It Begins

Tor/Forge, part of the Macmillan empire announced that they will start shipping their ebooks without DRM starting in July. This is the beginning of the DRM death spiral predicted by Charlie Stross (although he has tempered that prediction a bit in his analysis of Tor’s announcement).

John Scalzi weighns in with his own thoughts and an announcement that his forthcoming novel, Redshirts, will be DRM-free even though it will be released in June. Like Stross, he views this as a positive move.

I am hopeful that this action will trigger the collapse of DRM in the publishing industry. Once the publishers see that the earth continues to orbit the sun even without DRM, I expect that they will all jump on the bandwagon.

Posted in General | Tagged | Leave a comment

Decimalizing Latitude and Longitude

Xah Lee has reintroduced a challenge from last year. Given a string of latitude/longitude is degrees, minutes, seconds, write a function that returns them as signed decimal numbers. That is,

"37°26′36.42″N 06°15′14.28″W" → (37.44345 -6.253966666666667)

I remember looking at this challenge last year and thinking it wasn’t very interesting but when I started thinking about it this time, I realized that there are a couple of twists that do make it interesting.

Here’s my solution in Elisp:

(require 'cl)

(defun decimalize-lat-lon (lat-lon)
  "Decimalize latitude longitude
\"37°26′36.42″N 06°15′14.28″W\" --> (37.44345 -6.253966666666667)"
  (let (result)
    (dolist (ll (split-string lat-lon " +"))
      (destructuring-bind (deg min sec dir)
          (split-string ll "[°′″]")
        (if (string= "" dir) (error "malformed lat-lon %s" ll))
        (let ((factor (if (member dir '("S" "W")) -1 1)))
          (push (* factor (+ (string-to-number deg)
                             (/ (string-to-number min) 60.0)
                             (/ (string-to-number sec) 3600.0))) result))))
    (reverse result)))

Elisp purists might complain about my using dolist, destructuring-bind, and push from the Common Lisp package but they’re convenient and don’t do anything that can’t be done a bit more verbosely in pure Emacs Lisp. The real workhorse in this function is the Elisp split-string function. It gets used twice: once to split the original string into latitude and longitude and once to break the latitude and longitude into their constituent parts.

The check on whether dir is the empty string really checks for any missing constituents and is just a sanity check. I didn’t bother rounding the numbers to any particular number of decimal places or making the function a command. Either of those are easily done if the user needs them.

Update: Mickey over at Mastering Emacs and Aaron Hawley in the comments give nice solutions using Emacs calc. Mickey’s post shows off some of the great features of calc so be sure to take a look.

Posted in Programming | Tagged , | 2 Comments

A Key Sequence For revert-buffer

The other day I was trawling through Aaron Hawley’s excellent Emacs Reference Sheet and came across the entry for【Ctrl+x Ctrl+v Return】saying “same as previous.” The previous entry was for revert-buffer. My first thought was “How did I not know this?” I use revert-buffer a lot when I’m syncing files between computers with Git so this would really be useful to me. My next thought was, “Gee, I should blog about this.”

I remembered that every time I call revert-buffer it tells me that I can run it with【Super+u】 as well as with a menu. I always ignored these because I don’t have a 【Super】 key and I avoid using the menu interface. Why didn’t it tell me about the much easier【Ctrl+x Ctrl+v】? So I called【Ctrl+h k Ctrl+x Ctrl+v】to see what it said. Of course, it said that【Ctrl+x Ctrl+v】runs find-alternate-file (actually ido-find-alternate-file in my case), something I already knew but forgot in the excitement of discovering something new. I was disappointed that my discovery didn’t work out. I thought that Hawley must have made a typo.

Today, I came across the same entry and this time I noticed that 【Return】 at the end of the 【Ctrl+x Ctrl+v】. Suddenly, it all made sense. The 【Ctrl+x Ctrl+vdoes run find-alternate-file and the 【Return】 selects the current buffer’s file so the result the same as revert-buffer.

This is another one of those small things about Emacs that doesn’t seem like much but does, in fact, make my life easier. It was there all the time if I’d had the wits to see it. Fortunately, I had Hawley to help me see the light.

Posted in General | Tagged | 6 Comments