<p>I just learned about maps in golang and decided to try them out with this <a href="/r/dailyprogrammer" rel="nofollow">/r/dailyprogrammer</a> challenge: </p>
<p><a href="https://www.reddit.com/r/dailyprogrammer/comments/67dxts/20170424_challenge_312_easy_l33tspeak_translator/" rel="nofollow">https://www.reddit.com/r/dailyprogrammer/comments/67dxts/20170424_challenge_312_easy_l33tspeak_translator/</a></p>
<p>TLDR, they have a mapping of english to 1337 speak.</p>
<p>I tried to use a map to represent the mapping of characters but have run into an issue.</p>
<p>The mapping looks like this: </p>
<pre><code> dict := make(map[string]rune)
dict["A"] = 4
dict["B"] = 6
dict["E"] = 3
dict["I"] = 1
//dict["M"] = "(V)"
//dict["N"] = "(\\)"
dict["O"] = 0
dict["S"] = 5
dict["T"] = 7
//dict["V"] = "\\/"
//dict["W"] = "`//"
</code></pre>
<p>My issue is with the non-digit 1337 speak characters. If I omit the M, N, V, and W this works great. I can't represent those 1337 speak values as runes since they are multiple characters, and casting the digits as strings does not just turn them into their string representation.</p>
<p>Does anyone have an idea how to deal with this? Or is it a case of a single map not being the right structure for this.</p>
<hr/>**评论:**<br/><br/>niosop: <pre><p>Why not map[string]string ?</p></pre>Idontlikecold: <pre><p>So I am iterating over the english string with <code>range</code>, which means each character is a rune. So then I can't use each character I'm iterating over to access the key of the map, full code here:</p>
<pre><code> package main
import (
"fmt"
"strings"
)
func main() {
fmt.Println(translate("I am elite."))
}
func translate(orig string) (translated string) {
dict := make(map[string]string)
dict["A"] = string(4)
dict["B"] = string(6)
dict["E"] = string(3)
dict["I"] = string(1)
dict["M"] = "(V)"
dict["N"] = "(\\)"
dict["O"] = string(0)
dict["S"] = string(5)
dict["T"] = string(7)
dict["V"] = "\\/"
dict["W"] = "`//"
for _, a := range orig {
if _, v := dict[strings.ToUpper(string(a))]; v {
translated += dict[strings.ToUpper(string(a))]
} else {
translated += string(a)
}
}
return translated
}
</code></pre>
<p>output:</p>
<pre><code> (V) l.
</code></pre></pre>niosop: <pre><p>Just use "4", not string(4)</p>
<p><a href="https://play.golang.org/p/OtcBRsYFI4" rel="nofollow">https://play.golang.org/p/OtcBRsYFI4</a></p></pre>Idontlikecold: <pre><p>wow. Thank you no idea why I didn't think to try that :/</p></pre>
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889
0 回复
- 请尽量让自己的回复能够对别人有帮助
- 支持 Markdown 格式, **粗体**、~~删除线~~、
`单行代码`
- 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
- 图片支持拖拽、截图粘贴等方式上传