如何创建一个重复的字母字符串?

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

How to create a repeating alphabetic string?

问题

如何创建一系列字母字符串,使得第一个字符串是"a",第26个字符串是"z",第27个字符串是"aa",以此类推:

a
b
c
d
...
...
...
x
y
z
aa
ab
ac
ad
...
...
...
ax
ay
az
aaa
aab
aac
...
...
...
aax
aay    
aaz
aaaa
aaab
...
...

我有以下的代码示例,但是第27个字符串是"za"(应该是"aa")。我最终想要能够创建一个具有该模式的无限长度字符串。

package main

import (
	"fmt"
)

func main() {
	for i := 0; i < 100; i++ {
		fmt.Println(getAlphabeticString(i))
	}
}

func getAlphabeticString(n int) string {
	alphabet := "abcdefghijklmnopqrstuvwxyz"
	result := ""

	for n >= 0 {
		result = string(alphabet[n%26]) + result
		n = (n / 26) - 1
	}

	return result
}

你可以尝试运行这段代码,它会输出前100个字符串。

英文:

How do I create a bunch of alphabetic strings so that the first is "a", the 26th is "z", 27th is "aa"....e.g.:

a
b
c
d
...
...
...
x
y
z
aa
ab
ac
ad
...
...
...
ax
ay
az
aaa
aab
aac
...
...
...
aax
aay    
aaz
aaaa
aaab
...
...

I have the following playground code, but number 27 is "za" (s/b "aa"). I want to ultimately have the ability to create an infinite-length string w/ that pattern.

答案1

得分: 3

下面是一个重写的函数,应该可以解决问题。你会注意到它利用了字母A-Z具有连续的Unicode编码编号的事实,这意味着不需要存储整个字母表。

func NextAlias(last string) string {
    if last == "" {
        return "a"
    } else if last[len(last)-1] == 'z' {
        return last[:len(last)-1] + "aa"
    } else {
        return last[:len(last)-1] + string(last[len(last)-1] + 1)
    }
}

链接:https://play.golang.org/p/Y2ViJKs_T4

英文:

Below is a rewrite of your function that should do the trick. You'll note that is uses the fact that the letters A-Z have a sequential unicode numbering, meaning the alphabet does not have to be stored.

func NextAlias(last string) string {
	if last == &quot;&quot; {
		return &quot;a&quot;
	} else if last[len(last)-1] == &#39;z&#39; {
		return last[:len(last)-1] + &quot;aa&quot;
	} else {
		return last[:len(last)-1] + string(last[len(last)-1] + 1)
	}
}

https://play.golang.org/p/Y2ViJKs_T4

答案2

得分: 0

不使用您的代码,我重新编写了一个更简单的算法来实现这个功能;https://play.golang.org/p/Iv6X8-SXY3

package main

import "fmt"

func main() {
    letters := []string{"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"}

    prefix := "a"
    for i := 0; i < 5; i++ {
        for i, _ := range letters {
            letters = append(letters, prefix+letters[i%26])
        }
        prefix += "a"
    }   

    for j, _ := range letters {
        fmt.Println(letters[j])
    }
}

您还可以扩展此代码,以便使用变体前缀,例如 ba、bc、bd、bba 等。要实现这一点,您需要更改外部循环,利用 letters 中的现有项,而不是静态计算仅由 a 组成的前缀。要增加字符串的长度,只需在外部循环中增加 i 的值即可。您还可以将此代码封装在一个函数中,并将其作为参数传递给该函数。最后,如果性能是一个问题,我建议在创建切片时使用较大的初始容量,因为这里大量使用了 append,这将导致大量的重新分配内存。

英文:

Rather than making use of your code I just re-wrote a little simpler algorithm to do this; https://play.golang.org/p/Iv6X8-SXY3

package main

import &quot;fmt&quot;

func main() {
	letters := []string{&quot;a&quot;, &quot;b&quot;, &quot;c&quot;, &quot;d&quot;, &quot;e&quot;, &quot;f&quot;, &quot;g&quot;, &quot;h&quot;, &quot;i&quot;, &quot;j&quot;, &quot;k&quot;, &quot;l&quot;, &quot;m&quot;, &quot;n&quot;, &quot;o&quot;, &quot;p&quot;, &quot;q&quot;, &quot;r&quot;, &quot;s&quot;, &quot;t&quot;, &quot;u&quot;, &quot;v&quot;, &quot;w&quot;, &quot;x&quot;, &quot;y&quot;, &quot;z&quot;}
	
	prefix := &quot;a&quot;
	for i := 0; i &lt; 5; i++ {
		for i, _ := range letters {
		    letters = append(letters, prefix+letters[i%26])
		}
        prefix += &quot;a&quot;
	}	

	for j, _ := range letters {
	     fmt.Println(letters[j])
    }
}

You could also expand this so there is a variant prefix to get things like ba, bc, bd, bba, ect. To do that you'd want to change the outer loop to make use of the existing items in letters rather than statically calculating a prefix made only of a's. To increase the length of the strings just make i larger on the outer loop. You could also wrap this in a function and make that an argument to it. Lastly, if performance is a concern I recommend making your slice with a large initial capacity because this is making liberal use of append which will cause a lot or realloc's.

huangapple
  • 本文由 发表于 2015年8月28日 05:20:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/32259624.html
匿名

发表评论

匿名网友

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

确定