Recent Posts
Algorithm
A sequencing problem
by firebus
08/31/2011 - 21:05
I had to implement a function the other day to abbreviate a set of days of the week, taking every sequence of adjacent days and replacing them with a hyphenated range. Comma-separating non-adjacent items.
So, {Mon, Tue, Wed, Fri} would become "Mon - Wed, Fri"
I came up with a really ugly, mishmashy recursive solution for it.
Bigokro suggested a non-recursive, linear-time algorithm which is not terrible.
But I still feel like there's something more elegant out there.
Does anyone know if this kind of algorithm has a name and if there's a canonical solution?
Powered by Drupal - Design by Artinet - Amazon Affiliate
Recent comments
4 years 38 weeks ago
4 years 44 weeks ago
4 years 50 weeks ago
4 years 50 weeks ago
5 years 20 weeks ago
5 years 23 weeks ago
5 years 26 weeks ago
5 years 26 weeks ago
5 years 26 weeks ago
5 years 27 weeks ago