[xquery-talk] longest sequence of monotonically increasing integers

Peter Coppens Peter.Coppens at datadirect.com
Fri Mar 3 03:25:18 PST 2006


I am sure someone can come up with a more elegant solution, but I have
the impression this one does the trick

declare function local:f
  ($running as xs:integer,$max as xs:integer,$seq as xs:integer*) 
    as xs:integer {
  if($seq[1] + 1 eq $seq[2]) then
    local:f($running + 1, $max, subsequence($seq,2))
  else if($seq[1] eq $seq[2]) then
    local:f($running, $max, subsequence($seq,2))
  else if(exists(subsequence($seq,2))) then
    local:f(1, max(($max,$running)),subsequence($seq,2))
  else
    max(($max,$running))
};

local:f(1 ,1, ( 1945, 1951, 1952, 1952, 1953, 1961, 1962, 1998 ))


Hope it helps,

Peter

> -----Original Message-----
> From: talk-bounces at xquery.com [mailto:talk-bounces at xquery.com] On
Behalf
> Of Howard Katz
> Sent: Friday, March 03, 2006 3:29 AM
> To: talk at xquery.com
> Subject: [xquery-talk] longest sequence of monotonically increasing
> integers
> 
> Anybody have a nice method of finding the length of the longest
> monotonically increasing subsequence in a sequence of integers? For
> example
> given
> 
>      ( 1945, 1951, 1952, 1952, 1953, 1961, 1962, 1998 )
> 
> I need to be able to derive "3" for the run ( 1951, 1952, 1953 ). The
> double
> entry for "1952" can be ignored.
> 
> TIA,
> Howard
> 
> _______________________________________________
> talk at xquery.com
> http://xquery.com/mailman/listinfo/talk



More information about the talk mailing list