基于索引逐个计数的字符

huangapple go评论116阅读模式
英文:

counting up with characters based on index

问题

我的当前代码:

  1. var basicChars = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789")
  2. func GetFromIndex(index int) string {
  3. length := len(basicChars)
  4. size := 0 // size是最终字符串中字符的数量
  5. for {
  6. if float64(index) > math.Pow(float64(length), float64(size))-2 {
  7. size++
  8. } else {
  9. break
  10. }
  11. }
  12. str := make([]rune, size)
  13. for i := 0; i < size; i++ {
  14. str[i] = basicChars[index%length]
  15. }
  16. return string(str)
  17. }

我试图使用字母而不是数字进行计数。

我知道可以使用for循环,但没有很好的方法来保存状态或无限增加。

英文:

My current code:

  1. var basicChars = []rune(&quot;abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789&quot;)
  2. func GetFromIndex(index int) string {
  3. length := len(basicChars)
  4. size := 0 // size is the amount of characters in the final string
  5. for {
  6. if float64(index) &gt; math.Pow(float64(length), float64(size))-2 {
  7. size++
  8. } else {
  9. break
  10. }
  11. }
  12. str := make([]rune, size)
  13. for i := 0; i &lt; size; i++ {
  14. str[i] = basicChars[index%length]
  15. }
  16. return string(str)
  17. }

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)次

并获取它们在数组中的位置

  1. func TestA(t *testing.T) {
  2. for i:=0; i<=30; i++ {
  3. fmt.Printf("%s i:{%d} \n",GetFromIndexBis(i),i)
  4. }
  5. }
  6. func GetFromIndex(index int) string {
  7. var basicChars = []rune("abc")
  8. len := len(basicChars)
  9. pow := 1
  10. sumPow := 0
  11. res :=""
  12. // 第一个字符是简单的模数
  13. res += string(basicChars[index%len])
  14. for {
  15. pow = pow * len
  16. // 索引是否足够大?
  17. if index < pow+sumPow{
  18. break
  19. }
  20. // 移除没有推动轮子的第一个循环
  21. start := index - sumPow
  22. // 需要进行完整转动的循环数
  23. fullCycles := pow * len
  24. if start>=fullCycles {
  25. nbrOfUselessCycles := start / fullCycles
  26. start = start - fullCycles * nbrOfUselessCycles
  27. }
  28. index := (start / pow) -1
  29. // 这是最后一个
  30. if (index == -1) {
  31. res += string(basicChars[len-1])
  32. }else {
  33. res += string(basicChars[index])
  34. }
  35. sumPow += pow
  36. }
  37. return res
  38. }
英文:

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

  1. func TestA(t *testing.T) {
  2. for i:=0; i&lt;=30; i++ {
  3. fmt.Printf(&quot;%s i:{%d} \n&quot;,GetFromIndexBis(i),i)
  4. }
  5. }
  6. func GetFromIndex(index int) string {
  7. var basicChars = []rune(&quot;abc&quot;)
  8. len := len(basicChars)
  9. pow := 1
  10. sumPow := 0
  11. res :=&quot;&quot;
  12. // the first is a simple modulo
  13. res += string(basicChars[index%len])
  14. for {
  15. pow = pow * len
  16. // is the index big enought ?
  17. if index &lt; pow+sumPow{
  18. break
  19. }
  20. // remove the first cycles where nothing pushed the wheels
  21. start := index - sumPow
  22. // number of cycle we need to make a full turn
  23. fullCycles := pow * len
  24. if start&gt;=fullCycles {
  25. nbrOfUselessCycles := start / fullCycles
  26. start = start - fullCycles * nbrOfUselessCycles
  27. }
  28. index := (start / pow) -1
  29. // it&#39;s the last one
  30. if (index == -1) {
  31. res += string(basicChars[len-1])
  32. }else {
  33. res += string(basicChars[index])
  34. }
  35. sumPow += pow
  36. }
  37. return res
  38. }

huangapple
  • 本文由 发表于 2022年10月5日 16:08:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/73957502.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定