Skip to content

Instantly share code, notes, and snippets.

@rawnly
Created January 23, 2021 16:15
Show Gist options
  • Save rawnly/f7edf9f50e9c29c23917a458c13c37d6 to your computer and use it in GitHub Desktop.
Save rawnly/f7edf9f50e9c29c23917a458c13c37d6 to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"strconv"
"strings"
)
func main() {
str := "845674"
seq := strings.Split(str, "")
var lastIndex = 0
var toBePrinted = []string{}
for i, s := range seq {
curr, _ := strconv.Atoi(s)
if i == 0 {
toBePrinted = append(toBePrinted, s)
} else if len(seq) - 1 != i {
prev, _ := strconv.Atoi(seq[i-1])
next, _ := strconv.Atoi(seq[i+1])
if curr > prev && curr < next || curr > next && curr > prev {
toBePrinted[lastIndex] = toBePrinted[lastIndex] + s
} else if curr > next || curr < prev {
toBePrinted = append(toBePrinted, s)
lastIndex++
}
} else {
prev, _ := strconv.Atoi(seq[i-1])
if curr > prev {
toBePrinted[lastIndex] = toBePrinted[lastIndex] + s
} else {
toBePrinted = append(toBePrinted, s)
lastIndex++
}
}
}
fmt.Println(toBePrinted)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment