Hi Sunanda,

here is my tea break result:

find-longest-run: function [
    [catch]
    data-array [block!]
] [
    longest-run-size
    longest-run-start
    size
    position
    number
] [
    if empty? data-array [throw make error! "a non-empty array expected"]
    longest-run-start: position: back tail data-array
    longest-run-size: size: 1
    while [not head? position] [
        if not number? first position [throw make error! "an integer array expected"]
        number: first position
        position: back position
        either (first position) = (number - 1) [
            size: size + 1
            if size >= longest-run-size [
                longest-run-size: size
                longest-run-start: position
            ]
        ] [size: 1]
    ]
    reduce [longest-run-size longest-run-start]
]

-L

----- Original Message ----- >

Here's a little problem I needed an answer to yesterday.
> 
> As I needed the answer yesterday, I hacked together some code.  But there has 
> got to be a more elegant way, surely.  Maybe using parse on a block?
> 
> The problem is this......You have a block containing integers, like this:
> 
> sparse-array: [ 12 13 14 15 16 17
>                 7 8
>                 20 21 22 23 24 25 26
>                 19
>                 59 58 57 56 55 54 53 52
>                 20 21 22 23
>                 101 102 103 104 105 106 107
>              ]
> 
> They are not in ascending order, and there are some numbers duplicated. But 
> there are runs of consecutive, ascending integers: 12--17, 7--8 etc.  We want 
> to find the location in the block of the first longest, such sequence. 
> 
> The answer for this dataset is 20--26.   101--107 is as long, but isn't the 
> first. 59--52 is longer, but it is descending.
> 
> My solution is below. Someone can do better, surely!
> 
> Thanks,
> Sunanda
> 



-- 
To unsubscribe from this list, just send an email to
[EMAIL PROTECTED] with unsubscribe as the subject.

Reply via email to