Re: splitting to sublist - HELLPPPP
- Posted by petelomax May 31, 2018
- 1150 views
begin said...
i can up with the following, but don't how to really fix it.
You were pretty close, I think, just a single > that needed to be >=. Try this:
function restructure(object orgseq) object ors={}, merge={} integer i=1, tstart=1, len=length(orgseq) while i <= len do tstart = i if orgseq[i][1] = 1002 then if i < len and orgseq[i+1][1] = 1002 then while (i <= len) and (orgseq[i][1] = 1002) do i += 1 end while end if if tstart <= i then if i > len then ors = append(ors, {1002, tstart-1, len}) else ors = append(ors, {1002, tstart-1, i}) end if end if elsif i <= len and orgseq[i+1][1] != 1002 then while (i < len) and orgseq[i+1][1] != 1002 do i += 1 end while if tstart <= i then -- if i > len then if i >= len then ors = append(ors, {1000, tstart, len}) else ors = append(ors, {1000, tstart, i-1}) end if end if end if i += 1 end while return ors end function
begin said...
i am sorry i can nor really explain that or i am doing it very badly.
I can now imagine why you might be struggling. Here is my attempt to explain the problem, as I understand it:
Given a sequence of 'and'/'or' elements, which must start with 'and', collect any consecutive runs of 'or' along with their previous 'and', and any consecutive runs of 'and' excluding those lumped in with an 'or' run, as separate groups. eg { and,and, and,or,or, and } -- (spaces added are mine alone) => {{and,and},{and,or,or},{and}}
And here is my attempt at that:
function get_next(sequence s, integer done, integer and_or) for i=done+1 to length(s) do if s[i][1]=and_or then return i end if end for return 0 end function function restructure(sequence s) sequence res = {} integer done = 0, next if s[1][1]!=1000 then ?9/0 -- sanity check (must start with 'and') end if while done<length(s) do next = get_next(s,done,1002) if next=0 then exit end if if next>done+2 then -- add a run of {and,and,and} -- (not including the one just before -- the or/1002 that we just found) res = append(res,s[done+1..next-2]) done = next-2 end if next = get_next(s,next,1000) if next=0 then exit end if -- add a run of {and,or,or,or} -- (up to but not including the -- and/1000 that we just found) res = append(res,s[done+1..next-1]) done = next-1 end while -- add last run res = append(res,s[done+1..len]) return res end function
HTH, Pete