英文:
return values from "map" & print them
问题
我只会为你提供翻译服务,不会执行代码或返回代码的输出。以下是你提供的代码的翻译:
我只是想打印map函数的输出...
输入: "hello world"
期望输出: map['h': 1, 'e': 1, 'l': 3, 'o': 2, 'r': 1, 'w': 1, 'd': 1, ' ': 1]
我的代码:
package main
import "fmt"
/*
https://www.programmingexpert.io/programming-with-go/maps/practice-questions/1
*/
func main() {
CharacterFrequency("hello world")
}
func CharacterFrequency(sentence string) map[rune]int {
characters := map[rune]int{}
for _, char := range sentence {
value, ok := characters[char]
if !ok {
characters[char] = 1
} else {
characters[char] = value + 1
}
}
fmt.Printf("%c", characters) // 这里我没有得到正确的值
return characters
}
//fmt.Println(characters)
//fmt.Printf("%c", characters)
fmt.Printf("%c", map[rune]int) // 这里我没有得到正确的值
期望输出:
map['h': 1, 'e': 1, 'l': 3, 'o': 2, 'r': 1, 'w': 1, 'd': 1, ' ': 1]
fmt.Println(characters) 给我以下输出
map[32:1 100:1 101:1 104:1 108:3 111:2 114:1 119:1]
fmt.Printf("%c", characters) 给我以下输出
map[ :☺ d:☺ e:☺ h:☺ l:♥ o:☻ r:☺ w:☺]
fmt.Printf("%c", map[rune]int) 报错
".\10_go_map_dict_q1.go:29:19: map[rune]int (类型) 不是一个表达式"
英文:
I am just trying to print the output of map function...
Input: "hello world"
Output expected: map['h': 1, 'e': 1, 'l': 3, 'o': 2, 'r': 1, 'w': 1, 'd': 1, ' ': 1]
My Code:
package main
import "fmt"
/*
https://www.programmingexpert.io/programming-with-go/maps/practice-questions/1
*/
func main() {
CharacterFrequency("hello world")
}
func CharacterFrequency(sentence string) map[rune]int {
characters := map[rune]int{}
// characters := make(map[rune]int)
for _, char := range sentence {
value, ok := characters[char]
if !ok {
characters[char] = 1
} else {
characters[char] = value + 1
}
}
//fmt.Println(characters)
//fmt.Printf("%c", characters)
fmt.Printf("%c", map[rune]int) // so here is where I am not getting proper values
return characters
}
//fmt.Println(characters)
//fmt.Printf("%c", characters)
fmt.Printf("%c", map[rune]int) // so here is where I am not getting proper values
expecting:
map['h': 1, 'e': 1, 'l': 3, 'o': 2, 'r': 1, 'w': 1, 'd': 1, ' ': 1]
fmt.Println(characters) gave me below
map[32:1 100:1 101:1 104:1 108:3 111:2 114:1 119:1]
fmt.Printf("%c", characters) gave me below
map[ :☺ d:☺ e:☺ h:☺ l:♥ o:☻ r:☺ w:☺]
fmt.Printf("%c", map[rune]int) blows up with
".\10_go_map_dict_q1.go:29:19: map[rune]int (type) is not an expression"
答案1
得分: 1
package main
import "fmt"
func main() {
CharacterFrequency("hello world")
}
func CharacterFrequency(sentence string) map[string]int {
characters := map[string]int{}
for _, char := range sentence {
characters[string(char)]++
}
fmt.Printf("%v", characters)
// 输出: map[ :1 d:1 e:1 h:1 l:3 o:2 r:1 w:1]
return characters
}
package main
import "fmt"
func main() {
CharacterFrequency("hello world")
}
func CharacterFrequency(sentence string) map[string]int {
characters := map[string]int{}
for _, char := range sentence {
characters[string(char)]++
}
fmt.Printf("%v", characters)
// 输出: map[ :1 d:1 e:1 h:1 l:3 o:2 r:1 w:1]
return characters
}
英文:
package main
import "fmt"
func main() {
CharacterFrequency("hello world")
}
func CharacterFrequency(sentence string) map[string]int {
characters := map[string]int{}
for _, char := range sentence {
characters[string(char)]++
}
fmt.Printf("%v", characters)
// Output: map[ :1 d:1 e:1 h:1 l:3 o:2 r:1 w:1]
return characters
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论