Well we can't have people worried about lack of forum traffic so...

I came across the following problem yesterday:

http://www.dustindiaz.com/programming-brain-teaser/
Starting Array:
var arr = ['a', 'b', 'c', 'c', 'd','e', 'e',
'e', 'e', 'e', 'f', 'e', 'f', 'e',
'f', 'a', 'a', 'a', 'f', 'f', 'f'];

Desired Result:
a b c c d e e <span>e e e</span> f e f e f a a <span>a</span> f f <span>f</span>

In English:
Group together all duplicate items that occur anytime beyond twice by wrapping 
them with a tag, naturally "bookending" them.

Spoiler Alert.
==============
My solution below.





















Finding the mask of the atoms that need enclosing was Ok. But I find my 
solution for enclosing them long winded and overly complex. Is there a better 
way?

t=:  'abccdeeeeefefefaaafff'
t1=: 'cccdeeeeefefefaaaaffgf'

firstones=: > (0: , }:)
lastones=:  > (0: ,~ }.)
msk=: [: (]-firstones) (0,}:=}.)
group=:([: (1 , }.) [: ((_1|.lastones) +. firstones) msk) <;.1 ]
space=: }.@,@(' '&,.) &.>
bookend=: ;@,@(] ,. ('<span>';'</span>') $~ #)

   bookend space group t
a b c c d e e<span>e e e</span>f e f e f a a<span>a</span>f f<span>f</span>

----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Reply via email to