Skip to main content
Commonmark migration
Source Link

###Top-level solution based on recursion

Top-level solution based on recursion

###Java solution

Java solution

###Remark

Remark

###Top-level solution based on recursion

###Java solution

###Remark

Top-level solution based on recursion

Java solution

Remark

replaced http://mathematica.stackexchange.com/ with https://mathematica.stackexchange.com/
Source Link

Addressing your speed request in your edit, here is a Java solution (be sure to load the Java reloader first, along the steps described e.g. herehere:

Addressing your speed request in your edit, here is a Java solution (be sure to load the Java reloader first, along the steps described e.g. here:

Addressing your speed request in your edit, here is a Java solution (be sure to load the Java reloader first, along the steps described e.g. here:

Modified top-level code to handle arbitrary length lists
Source Link
Leonid Shifrin
  • 114.9k
  • 16
  • 333
  • 424
Clear[toLinkedList];
toLinkedList[lst_] := Fold[ll[#2, #1] &, ll[], Reverse@lst]

ClearAll[fillGaps];
fillGaps[dates_] := 
    Block[{$IterationLimit = Infinity}, 
       fillGaps[ll[], toLinkedList[dates]];toLinkedList[dates]]];

fillGaps[accum_, ll[val : {d_, _}, tail : ll[{dn_, _}, _ll]]] :=
   With[{nxt = DatePlus[d, 1]},
     fillGaps[
       If[nxt === dn, ll[accum, val], accum],
       If[nxt === dn, tail, ll[val, ll[{nxt, 0}, tail]]]
     ]];

fillGaps[accum_, ll[val_, ll[]]] := 
   Append[List @@ Flatten[accum, Infinity, ll], val];
Clear[toLinkedList];
toLinkedList[lst_] := Fold[ll[#2, #1] &, ll[], Reverse@lst]

ClearAll[fillGaps];
fillGaps[dates_] := fillGaps[ll[], toLinkedList[dates]];

fillGaps[accum_, ll[val : {d_, _}, tail : ll[{dn_, _}, _ll]]] :=
   With[{nxt = DatePlus[d, 1]},
     fillGaps[
       If[nxt === dn, ll[accum, val], accum],
       If[nxt === dn, tail, ll[val, ll[{nxt, 0}, tail]]]
     ]];

fillGaps[accum_, ll[val_, ll[]]] := 
   Append[List @@ Flatten[accum, Infinity, ll], val];
Clear[toLinkedList];
toLinkedList[lst_] := Fold[ll[#2, #1] &, ll[], Reverse@lst]

ClearAll[fillGaps];
fillGaps[dates_] := 
    Block[{$IterationLimit = Infinity}, 
       fillGaps[ll[], toLinkedList[dates]]];

fillGaps[accum_, ll[val : {d_, _}, tail : ll[{dn_, _}, _ll]]] :=
   With[{nxt = DatePlus[d, 1]},
     fillGaps[
       If[nxt === dn, ll[accum, val], accum],
       If[nxt === dn, tail, ll[val, ll[{nxt, 0}, tail]]]
     ]];

fillGaps[accum_, ll[val_, ll[]]] := 
   Append[List @@ Flatten[accum, Infinity, ll], val];
Added a faster Java solution
Source Link
Leonid Shifrin
  • 114.9k
  • 16
  • 333
  • 424
Loading
added 1 characters in body
Source Link
Leonid Shifrin
  • 114.9k
  • 16
  • 333
  • 424
Loading
added 1 characters in body
Source Link
Leonid Shifrin
  • 114.9k
  • 16
  • 333
  • 424
Loading
Source Link
Leonid Shifrin
  • 114.9k
  • 16
  • 333
  • 424
Loading