Quick question about data types and maps

agolangf · · 374 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<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[&#34;A&#34;] = 4 dict[&#34;B&#34;] = 6 dict[&#34;E&#34;] = 3 dict[&#34;I&#34;] = 1 //dict[&#34;M&#34;] = &#34;(V)&#34; //dict[&#34;N&#34;] = &#34;(\\)&#34; dict[&#34;O&#34;] = 0 dict[&#34;S&#34;] = 5 dict[&#34;T&#34;] = 7 //dict[&#34;V&#34;] = &#34;\\/&#34; //dict[&#34;W&#34;] = &#34;`//&#34; </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&#39;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&#39;t use each character I&#39;m iterating over to access the key of the map, full code here:</p> <pre><code> package main import ( &#34;fmt&#34; &#34;strings&#34; ) func main() { fmt.Println(translate(&#34;I am elite.&#34;)) } func translate(orig string) (translated string) { dict := make(map[string]string) dict[&#34;A&#34;] = string(4) dict[&#34;B&#34;] = string(6) dict[&#34;E&#34;] = string(3) dict[&#34;I&#34;] = string(1) dict[&#34;M&#34;] = &#34;(V)&#34; dict[&#34;N&#34;] = &#34;(\\)&#34; dict[&#34;O&#34;] = string(0) dict[&#34;S&#34;] = string(5) dict[&#34;T&#34;] = string(7) dict[&#34;V&#34;] = &#34;\\/&#34; dict[&#34;W&#34;] = &#34;`//&#34; 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 &#34;4&#34;, 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&#39;t think to try that :/</p></pre>

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

374 次点击  
加入收藏 微博
0 回复
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传