Skip to content

Instantly share code, notes, and snippets.

@so-jelly
Created May 21, 2020 22:48
Show Gist options
  • Save so-jelly/00d287b1080489eaaa2a614ac0f1cb4a to your computer and use it in GitHub Desktop.
Save so-jelly/00d287b1080489eaaa2a614ac0f1cb4a to your computer and use it in GitHub Desktop.
go lang go lan
func advanceLetter(s string, n int) string {
var x []rune
// add a known rune to the slice in case we need to advance it
x = append(x, rune(1337))
// Create a slice of runes
for _, r := range s {
x = append(x, r)
}
// Need to reverse index
l := len(x) - 1
// indicate a carried character place
var carry bool
for i := 0; i < l+1; i++ {
// get our next rune working backwards
r := x[l-i]
// only use the original increment on the first pass
if i > 0 {
n = 0
}
// increment a carried place by one
if carry {
n = 1
// did we reach our dummy?
if r == rune(1337) {
// set it to one less than A
r = 64
}
} else {
if r == rune(1337) {
// we can stop here
break
}
}
carry = false
// increment
r = r + rune(n)
// loop from Z(90) to A(65)
if r > 90 {
// carry our place
carry = true
// 64 is the "zero value"
r = r - 90 + 64
}
// save our character
x[l-i] = r
}
// make a new string to return
var a string
// work forwards again converting our runes to characters and appending
for _, r := range x {
if r != rune(1337) {
a = a + fmt.Sprintf("%c", r)
}
}
return a
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment