Guest User

Untitled

a guest
Jan 17th, 2018
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.54 KB | None | 0 0
  1. const romanNumeralDict map[int]string = {
  2. 1000: "M",
  3. 900 : "CM",
  4. 500 : "D",
  5. 400 : "CD",
  6. 100 : "C",
  7. 90 : "XC",
  8. 50 : "L",
  9. 40 : "XL",
  10. 10 : "X",
  11. 9 : "IX",
  12. 5 : "V",
  13. 4 : "IV",
  14. 1 : "I",
  15. }
  16.  
  17. # command-line-arguments
  18. ./Roman_Numerals.go:9: syntax error: unexpected {
  19.  
  20. var romanNumeralDict = map[int]string{
  21. 1000: "M",
  22. 900 : "CM",
  23. 500 : "D",
  24. 400 : "CD",
  25. 100 : "C",
  26. 90 : "XC",
  27. 50 : "L",
  28. 40 : "XL",
  29. 10 : "X",
  30. 9 : "IX",
  31. 5 : "V",
  32. 4 : "IV",
  33. 1 : "I",
  34. }
  35.  
  36. romanNumeralDict := map[int]string{
  37. ...
  38.  
  39. const myString = "hello"
  40. const pi = 3.14 // untyped constant
  41. const life int = 42 // typed constant (can use only with ints)
  42.  
  43. const (
  44. First = 1
  45. Second = 2
  46. Third = 4
  47. )
  48.  
  49. package main
  50.  
  51. import (
  52. "fmt"
  53. )
  54.  
  55. // http://stackoverflow.com/a/27457144/10278
  56.  
  57. func romanNumeralDict() func(int) string {
  58. // innerMap is captured in the closure returned below
  59. innerMap := map[int]string{
  60. 1000: "M",
  61. 900: "CM",
  62. 500: "D",
  63. 400: "CD",
  64. 100: "C",
  65. 90: "XC",
  66. 50: "L",
  67. 40: "XL",
  68. 10: "X",
  69. 9: "IX",
  70. 5: "V",
  71. 4: "IV",
  72. 1: "I",
  73. }
  74.  
  75. return func(key int) string {
  76. return innerMap[key]
  77. }
  78. }
  79.  
  80. func main() {
  81. fmt.Println(romanNumeralDict()(10))
  82. fmt.Println(romanNumeralDict()(100))
  83.  
  84. dict := romanNumeralDict()
  85. fmt.Println(dict(400))
  86. }
  87.  
  88. var romanNumeralDict = struct {
  89. m map[int]string
  90. }{m: map[int]string {
  91. 1000: "M",
  92. 900: "CM",
  93. //YOUR VALUES HERE
  94. }}
  95.  
  96. func main() {
  97. d := 1000
  98. fmt.Printf("Value of Key (%d): %s", d, romanNumeralDict.m[1000])
  99. }
  100.  
  101. // romanNumeralDict returns map[int]string dictionary, since the return
  102. // value is always the same it gives the pseudo-constant output, which
  103. // can be referred to in the same map-alike fashion.
  104. var romanNumeralDict = func() map[int]string { return map[int]string {
  105. 1000: "M",
  106. 900: "CM",
  107. 500: "D",
  108. 400: "CD",
  109. 100: "C",
  110. 90: "XC",
  111. 50: "L",
  112. 40: "XL",
  113. 10: "X",
  114. 9: "IX",
  115. 5: "V",
  116. 4: "IV",
  117. 1: "I",
  118. }
  119. }
  120.  
  121. func printRoman(key int) {
  122. fmt.Println(romanNumeralDict()[key])
  123. }
  124.  
  125. func printKeyN(key, n int) {
  126. fmt.Println(strings.Repeat(romanNumeralDict()[key], n))
  127. }
  128.  
  129. func main() {
  130. printRoman(1000)
  131. printRoman(10)
  132. }
Add Comment
Please, Sign In to add comment