I'm in the middle of learning PHP, and the following list-related problem has come up. The language doesn't really matter, so I'll give you this in pseudo-code. Pseudo-code answers are fine, of course.
Say, there's a list of two different, repeating elements - two single characters, for instance. So my list looks roughly like this:
myList = [C, C, D, C, D, D, D, C, C, D, C, D, C, C, ...]
However, that's not the form I want. Instead, the list should look like this:
myList* = [CC, D, C, DDD, CC, D, C, D, CC, ...]
myList* = shorten(myList)
What's the most elegant way of turning the single-character list into one that contains continuous strings of subsequent characters as its elements? My solution strikes me as rather crappy, given that it involves multiply nested if-statements, various state variables and other nastiness.
Pseudo-code away! Many thanks in advance for any implementation of
shorten()
you throw at me.