35
loading...
This website collects cookies to deliver better user experience
SONAR_SWEEP(DEPTHS)
INPUT: An unordered array of Int32 items
OUTPUT: The number of times an item is greater than its predecessor
Counter = 0
FOR idx = 1 TO LENGTH(DEPTHS) - 1
IF DEPTHS[idx] > DEPTHS[idx-1]
Counter += 1
ENDIF
ENDFOR
RETURN Counter
int counter = 0;
for(int idx = 0; idx < depths.Length; idx++)
{
if (depths[idx] > depths[idx-1])
counter +=1;
}
return counter;
int counter = depths.Skip(1)
.Select((x, i) => x > depths[i])
.Count(x => x);
Skip(1)
. This will create a new collection. For the sake of clarity we will call this sequence skipped.Select
all elements that are greater than their predecessors. The Select
query uses an evaluating function(x, i) => x > depths[i]
Skip
. Skip
created a new collection, skipped. In our evaluating function, i
is the current index in that skipped collection. Since we skipped one element from the depths
, skipped
and depths
will differ by one element. Thus, x[i]
will actually compare to depths[i-1]
. The evaluating function will keep only the elements that are greater than their previous.Count
the number of elements in our skipped
collection to determine the number of depth increases.int counter = depths.Skip(3)
.Select((x, i) => x > depths[i])
.Count(x => x);