Skip to content

Instantly share code, notes, and snippets.

@frectonz
Last active January 23, 2023 16:13
Show Gist options
  • Save frectonz/af47ed536730e9ec9e1823de430aba30 to your computer and use it in GitHub Desktop.
Save frectonz/af47ed536730e9ec9e1823de430aba30 to your computer and use it in GitHub Desktop.
From cassido's January 23, 2023 Newsletter
package main
import (
"fmt"
"strconv"
)
func main() {
val1 := missingBits([]int{1, 2, 3, 4, 20, 21, 22, 23})
fmt.Println(val1)
val2 := missingBits([]int{1, 2, 3, 5, 6})
fmt.Println(val2)
val3 := missingBits([]int{1, 3, 20, 27})
fmt.Println(val3)
}
func missingBits(ns []int) []string {
var out []string
for i, x := range ns {
xStr := strconv.Itoa(x)
out = append(out, xStr)
if i+1 == len(ns) {
break
}
next := ns[i+1]
diff := next - x
if diff == 2 {
next_str := strconv.Itoa(x + 1)
out = append(out, next_str)
}
if diff > 2 {
out = append(out, "...")
}
}
return out
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment