Skip to content

Instantly share code, notes, and snippets.

@BeKnowDo
Created June 12, 2017 18:39
Show Gist options
  • Save BeKnowDo/f88b18ed4777908960428480af688a65 to your computer and use it in GitHub Desktop.
Save BeKnowDo/f88b18ed4777908960428480af688a65 to your computer and use it in GitHub Desktop.
Sequence Classifier
sequenceClassifier = (arr) => {
let classifier = null,
strictlyIncreased = 1,
strictlyDecreased = 1,
constant = 1,
hasDecreased = false,
hasIncreased = false
arr.map(function(item, index) {
let currentValue = arr[index],
previousValue = arr[index - 1],
nextValue = arr[index + 1]
// have any of the values decreased
if (nextValue < currentValue) {
hasDecreased = true
}
// have any of the values increaed
if (nextValue > currentValue) {
hasIncreased = true
}
// check if every index is equal to one another, if so add to the tally
if (currentValue === nextValue) {
constant++
}
// have a feeling this would break eventually
if (currentValue > nextValue && constant !== arr.length) {
classifier = 0
}
// I don't like this. I don't like this at all...
// check if next values are consistently increasing
if (previousValue !== undefined && currentValue > previousValue) {
strictlyIncreased++
// if both are equal classify as strictly increasing
if (strictlyIncreased === arr.length) {
classifier = 1
}
}
// check if values are not decreasing
if (hasIncreased === true && hasDecreased === false && index === arr.length - 1 && constant !== arr.length && strictlyIncreased !== arr.length) {
classifier = 2
}
// check if values are consistently decreasing
if (currentValue > nextValue) {
strictlyDecreased++
// if both are equal classify as strictly increasing
if (strictlyDecreased === arr.length) {
classifier = 3
}
}
// check if values ever increased and that series hasn't strictly increased
if (hasIncreased === false && strictlyIncreased !== arr.length && strictlyDecreased !== arr.length && constant !== arr.length && hasDecreased === true) {
classifier = 4
}
// if constant tally equal to the array's length return classifier
if (constant === arr.length) {
classifier = 5
}
})
const determinClassifier = (identifier) => {
switch (identifier) {
case 0:
return 'unordered'
case 1:
return 'strictly increasing'
case 2:
return 'not decreasing'
case 3:
return 'strictly decreasing'
case 4:
return 'not increasing'
case 5:
return 'constant'
default:
return false
}
return classifier
}
return classifier
}
console.clear()
sequenceClassifier([14,9,8,5,3,1])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment