英文:
counting up with characters based on index
问题
我的当前代码:
var basicChars = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789")
func GetFromIndex(index int) string {
length := len(basicChars)
size := 0 // size是最终字符串中字符的数量
for {
if float64(index) > math.Pow(float64(length), float64(size))-2 {
size++
} else {
break
}
}
str := make([]rune, size)
for i := 0; i < size; i++ {
str[i] = basicChars[index%length]
}
return string(str)
}
我试图使用字母而不是数字进行计数。
我知道可以使用for循环,但没有很好的方法来保存状态或无限增加。
英文:
My current code:
var basicChars = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789")
func GetFromIndex(index int) string {
length := len(basicChars)
size := 0 // size is the amount of characters in the final string
for {
if float64(index) > math.Pow(float64(length), float64(size))-2 {
size++
} else {
break
}
}
str := make([]rune, size)
for i := 0; i < size; i++ {
str[i] = basicChars[index%length]
}
return string(str)
}
I am trying to count up with letters instead of numbers.
I know I can use for loops but there is no good way of saving state or going up indefinitely
答案1
得分: 1
根据你的描述,GetFromIndex(122)应该返回a8,而不是bb。
要么我没有理解正确的情景,要么我们缺少信息。
-编辑-
我不确定这是否是最好的方法:
- 第一个字符每次移动
- 第二个字符-> 3次
- 第三个字符-> 9次...
我减去了无用的循环:
- 第二个字符->每3 * len(array)次
- 第三个字符->每9 * len(array)次
并获取它们在数组中的位置
func TestA(t *testing.T) {
for i:=0; i<=30; i++ {
fmt.Printf("%s i:{%d} \n",GetFromIndexBis(i),i)
}
}
func GetFromIndex(index int) string {
var basicChars = []rune("abc")
len := len(basicChars)
pow := 1
sumPow := 0
res :=""
// 第一个字符是简单的模数
res += string(basicChars[index%len])
for {
pow = pow * len
// 索引是否足够大?
if index < pow+sumPow{
break
}
// 移除没有推动轮子的第一个循环
start := index - sumPow
// 需要进行完整转动的循环数
fullCycles := pow * len
if start>=fullCycles {
nbrOfUselessCycles := start / fullCycles
start = start - fullCycles * nbrOfUselessCycles
}
index := (start / pow) -1
// 这是最后一个
if (index == -1) {
res += string(basicChars[len-1])
}else {
res += string(basicChars[index])
}
sumPow += pow
}
return res
}
英文:
based on your description GetFromIndex(122) should return a8, not bb
either I didn't get the correct scenario or we are missing informations
-eddit-
I'm not sure it's the best way to do it
- the first character will moove every time
- the 2nd -> 3 times
- the 3rd -> 9 times ...
I substract the useless cycles
- the 2nd -> each 3 * len(array)
- the 3rd -> each 9 * len(array)
and get their position in the array
func TestA(t *testing.T) {
for i:=0; i<=30; i++ {
fmt.Printf("%s i:{%d} \n",GetFromIndexBis(i),i)
}
}
func GetFromIndex(index int) string {
var basicChars = []rune("abc")
len := len(basicChars)
pow := 1
sumPow := 0
res :=""
// the first is a simple modulo
res += string(basicChars[index%len])
for {
pow = pow * len
// is the index big enought ?
if index < pow+sumPow{
break
}
// remove the first cycles where nothing pushed the wheels
start := index - sumPow
// number of cycle we need to make a full turn
fullCycles := pow * len
if start>=fullCycles {
nbrOfUselessCycles := start / fullCycles
start = start - fullCycles * nbrOfUselessCycles
}
index := (start / pow) -1
// it's the last one
if (index == -1) {
res += string(basicChars[len-1])
}else {
res += string(basicChars[index])
}
sumPow += pow
}
return res
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论