-----Oorspronkelijk bericht-----
Van: [EMAIL PROTECTED] [mailto:programming-
[EMAIL PROTECTED] Namens Arie Groeneveld
Verzonden: zaterdag 12 juli 2008 9:42
Aan: Programming forum
Onderwerp: Re: [Jprogramming] Programming brain-teaser
bt =: 3 : 0
grp=.<@,@(,.&' ')@([{. y }.~])/"1(,.(0,+/[EMAIL
PROTECTED]:))([:#/.~+/\@(~:{.,}:)) y
m2i=.I.;(4<#)&.> grp
m2=.(3&{.;('<span>','</span>',~[:}:4&}.))&.> m2i { grp
;<S: 0 m2 m2i } grp
)
bt 'abccdeeeeefefefaaafff'
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>
bt 'abccdeeeeefefefaaafffa'
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>a
minor flaw: second one has a trailing space
Sherlock, Ric schreef:
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