Skip to content

Instantly share code, notes, and snippets.

@selmanon
Created March 31, 2019 12:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save selmanon/2c3dd87f9732947e947a482171cf7b00 to your computer and use it in GitHub Desktop.
Save selmanon/2c3dd87f9732947e947a482171cf7b00 to your computer and use it in GitHub Desktop.
/*
* Any array may be viewed as a number of "runs" of equal numbers.
* For example, the following array has two runs:
* 1, 1, 1, 2, 2
* Three 1's in a row form the first run, and two 2's form the second.
* This array has two runs of length one:
* 3, 4
* And this one has five runs:
* 1, 0, 1, 1, 1, 2, 0, 0, 0, 0, 0, 0, 0
* Your task is to implement the runs() function so that it returns the number
* of runs in the given array.
*/
package runs
fun runs(a: IntArray): Int {
if(a.size == 0)
return 0
var runs = 1
var differentRun = a[0]
for(runIndice in a.indices)
if(differentRun != a[runIndice]){
runs++;
differentRun = a[runIndice]
}
return runs
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment