NB. count number of repetitions
[t0=: ;<@i."0 #/.~ +/\1,2~:/\ 'abccdeeeeefefefaaafff'
0 0 0 1 0 0 1 2 3 4 0 0 0 0 0 0 1 2 0 1 2
NB. identify where number is 1 and next number is 2
(2=1|.t0)*.t0=1
0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 1 0 0 1 0
NB. identify where number is 2 or more and
NB. (next number is 0) or (number is last)
(0=1|.!.0 t0)*.t0>1
0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 1 0 0 1
NB. add last 2 identifiers and box string accordingly, add spaces after each
token in a box, except for the last token
[t1=: (' '}:@,@,.~])&.> 'abccdeeeeefefefaaafff'
<;.2~((2=1|.t0)*.t0=1.)+.(0=1|.!.0 t0)*.t0>1
+-------------+-----+-------------+-+---+-+
|a b c c d e e|e e e|f e f e f a a|a|f f|f|
+-------------+-----+-------------+-+---+-+
NB. make (#t1) $' <span>';'</span> '
[t2=:(#t1) $' <span>';'</span> '
+-------+--------+-------+--------+-------+--------+
| <span>|</span> | <span>|</span> | <span>|</span> |
+-------+--------+-------+--------+-------+--------+
NB. stitch the last 2 boxed arrays together and ravel (beware: line wrap)
+-------------+-------+-----+--------+-------------+-------+-----+--------+-
+-------+
,t1,.t2
+-------------+-------+-----+--------+-------------+-------+-+--------+---+-
------+-+--------+
|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> |
+-------------+-------+-----+--------+-------------+-------+-+--------+---+-
------+-+--------+
NB. if t is odd, delete last box and raze
;}:^:(2|#t1),t1,.t2
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>
T=: 3 : 0
t0=. ;<@i."0 #/.~ +/\1,2~:/\y
t1=. (' '}:@,@,.~])&.> y <;.2~((2=1|.t0)*.t0=1.)+.(0=1|.!.0 t0)*.t0>1
t2=.(#t1) $' <span>';'</span> '
;}:^:(2|#t1),t1,.t2
)
R.E. Boss
> -----Oorspronkelijk bericht-----
> Van: [EMAIL PROTECTED] [mailto:programming-
> [EMAIL PROTECTED] Namens Sherlock, Ric
> Verzonden: vrijdag 11 juli 2008 0:48
> Aan: Programming forum
> Onderwerp: [Jprogramming] Programming brain-teaser
>
> 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
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm