Compare commits
7
Commits
787dd02bb3
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
3744760c4e | ||
|
|
a6cba26de2 | ||
|
|
6ccc22ec9f | ||
|
|
95b45fb1b6 | ||
|
|
0e6a3b24f3 | ||
|
|
9b6e6badaa | ||
|
|
d570ed6810 |
@@ -1,6 +1,9 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import "fmt"
|
import (
|
||||||
|
"fmt"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
func exercise1() int {
|
func exercise1() int {
|
||||||
fmt.Println("=== 练习1:无参数单返回值函数 ===")
|
fmt.Println("=== 练习1:无参数单返回值函数 ===")
|
||||||
@@ -9,14 +12,11 @@ func exercise1() int {
|
|||||||
// 定义一个函数cube,接收一个int参数,返回其立方
|
// 定义一个函数cube,接收一个int参数,返回其立方
|
||||||
// 调用这两个函数并返回它们的和
|
// 调用这两个函数并返回它们的和
|
||||||
|
|
||||||
____ func ____
|
square := func(n int) int { return n * n }
|
||||||
square := func(n int) ____ { return n * n }
|
|
||||||
____
|
|
||||||
}
|
|
||||||
|
|
||||||
cube := func(n int) ____ { ____ }
|
cube := func(n int) int { return n * n * n }
|
||||||
|
|
||||||
return ____(3) + ____(2)
|
return square(3) + cube(2)
|
||||||
}
|
}
|
||||||
|
|
||||||
func exercise2(a, b int) {
|
func exercise2(a, b int) {
|
||||||
@@ -25,19 +25,23 @@ func exercise2(a, b int) {
|
|||||||
// 定义一个函数,接收两个int参数a和b
|
// 定义一个函数,接收两个int参数a和b
|
||||||
// 打印a + b, a - b, a * b
|
// 打印a + b, a - b, a * b
|
||||||
// 格式: "加: 5, 减: 1, 乘: 6"
|
// 格式: "加: 5, 减: 1, 乘: 6"
|
||||||
fmt.Printf("加: %d, 减: %d, 乘: %d\n", ____, ____, ____)
|
sum := a + b
|
||||||
|
difference := a - b
|
||||||
|
product := a * b
|
||||||
|
|
||||||
|
fmt.Printf("加: %d, 减: %d, 乘: %d\n", sum, difference, product)
|
||||||
}
|
}
|
||||||
|
|
||||||
func exercise3(nums ...int) int {
|
func exercise3(nums ...int) int {
|
||||||
fmt.Println("\n=== 练习3:可变参数 ===")
|
fmt.Println("\n=== 练习3:可变参数 ===")
|
||||||
|
|
||||||
// 定义一个可变参数函数,计算所有数字的和
|
// 定义一个可变参数函数,计算所有数字的和
|
||||||
sum := ____
|
sum := 0
|
||||||
for _, num := range ____ {
|
for _, num := range nums {
|
||||||
____
|
sum += num
|
||||||
}
|
}
|
||||||
|
|
||||||
return ____
|
return sum
|
||||||
}
|
}
|
||||||
|
|
||||||
func exercise4(name, city string) (string, string) {
|
func exercise4(name, city string) (string, string) {
|
||||||
@@ -47,13 +51,13 @@ func exercise4(name, city string) (string, string) {
|
|||||||
// nameWithGreeting: 格式为 "Hello, {name}"
|
// nameWithGreeting: 格式为 "Hello, {name}"
|
||||||
// info: 格式为 "{name} lives in {city}"
|
// info: 格式为 "{name} lives in {city}"
|
||||||
|
|
||||||
var ____ string
|
var nameWithGreeting string
|
||||||
var ____
|
var info string
|
||||||
|
|
||||||
nameWithGreeting = fmt.Sprintf("Hello, %s", name)
|
nameWithGreeting = fmt.Sprintf("Hello, %s", name)
|
||||||
info = fmt.Sprintf("%s lives in %s", name, city)
|
info = fmt.Sprintf("%s lives in %s", name, city)
|
||||||
|
|
||||||
return ____, ____
|
return nameWithGreeting, info
|
||||||
}
|
}
|
||||||
|
|
||||||
func exercise5(name string) string {
|
func exercise5(name string) string {
|
||||||
@@ -63,13 +67,13 @@ func exercise5(name string) string {
|
|||||||
// 返回大写的name
|
// 返回大写的name
|
||||||
// 提示:使用strings.ToUpper
|
// 提示:使用strings.ToUpper
|
||||||
|
|
||||||
____ := ____
|
upperName := strings.ToUpper(name)
|
||||||
|
|
||||||
return ____
|
return upperName
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
fmt.Println("=== 基础函数 练习 ===\n")
|
fmt.Println("=== 基础函数 练习 ===")
|
||||||
|
|
||||||
result1 := exercise1()
|
result1 := exercise1()
|
||||||
fmt.Printf("练习1结果: %d\n", result1)
|
fmt.Printf("练习1结果: %d\n", result1)
|
||||||
|
|||||||
@@ -1,42 +1,40 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"time"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func exercise1(x, y int) (int, int) {
|
func exercise1(x, y int) (int, int) {
|
||||||
// 返回x+y和x-y
|
// 返回x+y和x-y
|
||||||
return ____, ____
|
return x + y, x - y
|
||||||
}
|
}
|
||||||
|
|
||||||
func exercise2(n int) (int, error) {
|
func exercise2(n int) (int, error) {
|
||||||
// 如果n<0,返回0和错误
|
// 如果n<0,返回0和错误
|
||||||
// 否则返回n和nil
|
// 否则返回n和nil
|
||||||
if ____ {
|
if n < 0 {
|
||||||
return ____, ____
|
return n, fmt.Errorf("division by zero")
|
||||||
}
|
}
|
||||||
return ____, ____
|
return n, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func exercise3(s string) (bool, int) {
|
func exercise3(s string) (bool, int) {
|
||||||
// 返回字符串是否包含"hello"和字符串长度
|
// 返回字符串是否包含"hello"和字符串长度
|
||||||
contains := true
|
contains := true
|
||||||
length := ____
|
length := len(s)
|
||||||
return ____, ____
|
return contains, length
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
fmt.Println("=== 多返回值 练习 ===\n")
|
fmt.Println("=== 多返回值 练习 ===")
|
||||||
|
|
||||||
sum, diff := exercise1(10, 3)
|
sum, diff := exercise1(10, 3)
|
||||||
fmt.Printf("练习1: 加=%d, 减=%d\n", ____, ____)
|
fmt.Printf("练习1: 加=%d, 减=%d\n", sum, diff)
|
||||||
|
|
||||||
result, err := exercise2(-5)
|
result, err := exercise2(-5)
|
||||||
if ____ {
|
if err != nil {
|
||||||
fmt.Printf("练习2: 错误 = %v\n", ____)
|
fmt.Printf("练习2: 错误 = %v\n", result)
|
||||||
} else {
|
} else {
|
||||||
fmt.Printf("练习2: 结果 = %d\n", ____)
|
fmt.Printf("练习2: 结果 = %d\n", result)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,11 +9,11 @@ func exercise1() func(int) int {
|
|||||||
// 该闭包接收一个int参数并返回乘以multiplier的结果
|
// 该闭包接收一个int参数并返回乘以multiplier的结果
|
||||||
|
|
||||||
multiplier := 3
|
multiplier := 3
|
||||||
____ := func(num int) ____ {
|
multifunc := func(num int) int {
|
||||||
return ____
|
return multiplier * num
|
||||||
}
|
}
|
||||||
|
|
||||||
return ____
|
return multifunc
|
||||||
}
|
}
|
||||||
|
|
||||||
func exercise2() func() int {
|
func exercise2() func() int {
|
||||||
@@ -23,14 +23,14 @@ func exercise2() func() int {
|
|||||||
// 每次调用返回的函数时,计数器加1
|
// 每次调用返回的函数时,计数器加1
|
||||||
// 提示:使用匿名函数捕获外部变量
|
// 提示:使用匿名函数捕获外部变量
|
||||||
|
|
||||||
count := ____
|
count := 0
|
||||||
|
|
||||||
____ := func() ____ {
|
countfunc := func() int {
|
||||||
____
|
count++
|
||||||
return ____
|
return count
|
||||||
}
|
}
|
||||||
|
|
||||||
return ____
|
return countfunc
|
||||||
}
|
}
|
||||||
|
|
||||||
func exercise3() func() int {
|
func exercise3() func() int {
|
||||||
@@ -40,15 +40,15 @@ func exercise3() func() int {
|
|||||||
// 每次调用返回下一个斐波那契数
|
// 每次调用返回下一个斐波那契数
|
||||||
// 数列: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34...
|
// 数列: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34...
|
||||||
|
|
||||||
a, b := ____, ____
|
a, b := 0, 1
|
||||||
|
|
||||||
____ := func() ____ {
|
fib := func() int {
|
||||||
result := ____
|
result := a
|
||||||
a, b = ____
|
a, b = b, a+b
|
||||||
return ____
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
return ____
|
return fib
|
||||||
}
|
}
|
||||||
|
|
||||||
func exercise4() func(int) bool {
|
func exercise4() func(int) bool {
|
||||||
@@ -59,13 +59,13 @@ func exercise4() func(int) bool {
|
|||||||
// 检查函数判断输入数字是否在[min, max]范围内
|
// 检查函数判断输入数字是否在[min, max]范围内
|
||||||
|
|
||||||
min := 10
|
min := 10
|
||||||
max := ____
|
max := 20
|
||||||
|
|
||||||
____ := func(num int) ____ {
|
check := func(num int) bool {
|
||||||
return num >= ____ && num <= ____
|
return num >= min && num <= max
|
||||||
}
|
}
|
||||||
|
|
||||||
return ____
|
return check
|
||||||
}
|
}
|
||||||
|
|
||||||
func exercise5(start int) func(int) int {
|
func exercise5(start int) func(int) int {
|
||||||
@@ -75,18 +75,18 @@ func exercise5(start int) func(int) int {
|
|||||||
// 从start开始,每次调用累加参数值
|
// 从start开始,每次调用累加参数值
|
||||||
// 提示:闭包应该能访问外部作用域的变量
|
// 提示:闭包应该能访问外部作用域的变量
|
||||||
|
|
||||||
total := ____
|
total := start
|
||||||
|
|
||||||
____ := func(n int) ____ {
|
adder := func(n int) int {
|
||||||
____
|
total += n
|
||||||
return ____
|
return total
|
||||||
}
|
}
|
||||||
|
|
||||||
return ____
|
return adder
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
fmt.Println("=== 闭包 练习 ===\n")
|
fmt.Println("=== 闭包 练习 ===")
|
||||||
|
|
||||||
add3 := exercise1()
|
add3 := exercise1()
|
||||||
fmt.Printf("练习1结果: add3(4) = %d\n", add3(4))
|
fmt.Printf("练习1结果: add3(4) = %d\n", add3(4))
|
||||||
|
|||||||
@@ -8,9 +8,9 @@ func exercise1(apply func(int) int, value int) int {
|
|||||||
// 接收一个函数apply和一个值value
|
// 接收一个函数apply和一个值value
|
||||||
// 调用apply函数处理value并返回结果
|
// 调用apply函数处理value并返回结果
|
||||||
|
|
||||||
result := ____
|
result := apply(value)
|
||||||
|
|
||||||
return ____
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
func exercise2() func(int) int {
|
func exercise2() func(int) int {
|
||||||
@@ -19,7 +19,9 @@ func exercise2() func(int) int {
|
|||||||
// 返回一个函数,该函数将输入值乘以2
|
// 返回一个函数,该函数将输入值乘以2
|
||||||
// 提示:返回一个匿名函数
|
// 提示:返回一个匿名函数
|
||||||
|
|
||||||
return ____
|
return func(x int) int {
|
||||||
|
return x * 2
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func exercise3(vals []int, filter func(int) bool) []int {
|
func exercise3(vals []int, filter func(int) bool) []int {
|
||||||
@@ -28,15 +30,15 @@ func exercise3(vals []int, filter func(int) bool) []int {
|
|||||||
// 接收一个int切片和一个过滤函数
|
// 接收一个int切片和一个过滤函数
|
||||||
// 返回过滤后的切片(只保留满足条件的元素)
|
// 返回过滤后的切片(只保留满足条件的元素)
|
||||||
|
|
||||||
var result ____
|
var result []int
|
||||||
|
|
||||||
for _, val := range ____ {
|
for _, val := range vals {
|
||||||
if ____(val) {
|
if filter(val) {
|
||||||
result = append(result, ____)
|
result = append(result, val)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return ____
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
func exercise4(vals []int, mapper func(int) int) []int {
|
func exercise4(vals []int, mapper func(int) int) []int {
|
||||||
@@ -45,13 +47,13 @@ func exercise4(vals []int, mapper func(int) int) []int {
|
|||||||
// 接收一个int切片和一个映射函数
|
// 接收一个int切片和一个映射函数
|
||||||
// 返回映射后的切片(每个元素都经过mapper函数处理)
|
// 返回映射后的切片(每个元素都经过mapper函数处理)
|
||||||
|
|
||||||
result := make([]int, ____)
|
result := make([]int, len(vals))
|
||||||
|
|
||||||
for i, val := range ____ {
|
for i, val := range vals {
|
||||||
result[i] = ____(val)
|
result[i] = mapper(val)
|
||||||
}
|
}
|
||||||
|
|
||||||
return ____
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
func exercise5(vals []int, reducer func(int, int) int, initial int) int {
|
func exercise5(vals []int, reducer func(int, int) int, initial int) int {
|
||||||
@@ -61,13 +63,13 @@ func exercise5(vals []int, reducer func(int, int) int, initial int) int {
|
|||||||
// 返回归约结果
|
// 返回归约结果
|
||||||
// 例如:对切片求和、求积等
|
// 例如:对切片求和、求积等
|
||||||
|
|
||||||
result := ____
|
result := initial
|
||||||
|
|
||||||
for _, val := range ____ {
|
for _, val := range vals {
|
||||||
result = ____(result, val)
|
result = reducer(result, val)
|
||||||
}
|
}
|
||||||
|
|
||||||
return ____
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
func exercise6() int {
|
func exercise6() int {
|
||||||
@@ -79,18 +81,18 @@ func exercise6() int {
|
|||||||
// 返回 double(square(3)) 的结果
|
// 返回 double(square(3)) 的结果
|
||||||
// 即:先平方(9),再双倍(18)
|
// 即:先平方(9),再双倍(18)
|
||||||
|
|
||||||
square := func(n int) int { return ____ }
|
square := func(n int) int { return n * n }
|
||||||
double := func(n int) int { return ____ }
|
double := func(n int) int { return n * 2 }
|
||||||
|
|
||||||
compose := func(f, g func(int) int, x int) int {
|
compose := func(f, g func(int) int, x int) int {
|
||||||
return ____(____(x))
|
return g(f(x))
|
||||||
}
|
}
|
||||||
|
|
||||||
return compose(____, ____, 3)
|
return compose(square, double, 3)
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
fmt.Println("=== 高阶函数 练习 ===\n")
|
fmt.Println("=== 高阶函数 练习 ===")
|
||||||
|
|
||||||
平方 := func(n int) int { return n * n }
|
平方 := func(n int) int { return n * n }
|
||||||
result1 := exercise1(平方, 4)
|
result1 := exercise1(平方, 4)
|
||||||
|
|||||||
@@ -7,13 +7,13 @@ func exercise1() [5]int {
|
|||||||
|
|
||||||
// 声明一个长度为5的int数组
|
// 声明一个长度为5的int数组
|
||||||
// 初始化所有元素为10
|
// 初始化所有元素为10
|
||||||
var arr ____
|
var arr [5]int
|
||||||
|
|
||||||
for i := 0; i < ____(arr); i++ {
|
for i := 0; i < len(arr); i++ {
|
||||||
arr[i] = ____
|
arr[i] = 10
|
||||||
}
|
}
|
||||||
|
|
||||||
return ____
|
return arr
|
||||||
}
|
}
|
||||||
|
|
||||||
func exercise2() string {
|
func exercise2() string {
|
||||||
@@ -22,7 +22,7 @@ func exercise2() string {
|
|||||||
// 修改数组元素并返回修改后的字符串表示
|
// 修改数组元素并返回修改后的字符串表示
|
||||||
fruits := [4]string{"苹果", "香蕉", "橙子", "葡萄"}
|
fruits := [4]string{"苹果", "香蕉", "橙子", "葡萄"}
|
||||||
|
|
||||||
fruits[____] = "芒果"
|
fruits[0] = "芒果"
|
||||||
|
|
||||||
fmt.Printf("修改后的水果: %s\n", fruits)
|
fmt.Printf("修改后的水果: %s\n", fruits)
|
||||||
|
|
||||||
@@ -35,13 +35,13 @@ func exercise3() int {
|
|||||||
// 计算数组所有元素的和
|
// 计算数组所有元素的和
|
||||||
numbers := [5]int{10, 20, 30, 40, 50}
|
numbers := [5]int{10, 20, 30, 40, 50}
|
||||||
|
|
||||||
sum := ____
|
sum := 0
|
||||||
|
|
||||||
for i := 0; i < ____(numbers); i++ {
|
for i := 0; i < len(numbers); i++ {
|
||||||
sum += ____
|
sum += numbers[i]
|
||||||
}
|
}
|
||||||
|
|
||||||
return ____
|
return sum
|
||||||
}
|
}
|
||||||
|
|
||||||
func exercise4() [3]int {
|
func exercise4() [3]int {
|
||||||
@@ -51,11 +51,11 @@ func exercise4() [3]int {
|
|||||||
// 修改原数组,观察拷贝的数组是否变化
|
// 修改原数组,观察拷贝的数组是否变化
|
||||||
arr1 := [3]int{1, 2, 3}
|
arr1 := [3]int{1, 2, 3}
|
||||||
|
|
||||||
arr2 := ____
|
arr2 := arr1
|
||||||
|
|
||||||
arr1[0] = 99
|
arr1[0] = 99
|
||||||
|
|
||||||
return ____
|
return arr2
|
||||||
}
|
}
|
||||||
|
|
||||||
func exercise5() int {
|
func exercise5() int {
|
||||||
@@ -64,19 +64,19 @@ func exercise5() int {
|
|||||||
// 计算二维数组的和
|
// 计算二维数组的和
|
||||||
matrix := [3][2]int{{1, 2}, {3, 4}, {5, 6}}
|
matrix := [3][2]int{{1, 2}, {3, 4}, {5, 6}}
|
||||||
|
|
||||||
sum := ____
|
sum := 0
|
||||||
|
|
||||||
for i := 0; i < ____(matrix); i++ {
|
for i := 0; i < len(matrix); i++ {
|
||||||
for j := 0; j < ____(matrix[i]); j++ {
|
for j := 0; j < len(matrix[i]); j++ {
|
||||||
sum += ____
|
sum += matrix[i][j]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return ____
|
return sum
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
fmt.Println("=== 数组 练习 ===\n")
|
fmt.Println("=== 数组 练习 ===")
|
||||||
|
|
||||||
result1 := exercise1()
|
result1 := exercise1()
|
||||||
fmt.Printf("练习1结果: %v\n", result1)
|
fmt.Printf("练习1结果: %v\n", result1)
|
||||||
|
|||||||
@@ -6,10 +6,10 @@ func exercise1() []int {
|
|||||||
fmt.Println("=== 练习1:切片声明与初始化 ===")
|
fmt.Println("=== 练习1:切片声明与初始化 ===")
|
||||||
|
|
||||||
// 创建一个长度为3的int切片,初始值为[1, 2, 3]
|
// 创建一个长度为3的int切片,初始值为[1, 2, 3]
|
||||||
s := make([]int, ____)
|
s := make([]int, 3)
|
||||||
s[0], s[1], s[2] = ____, ____, ____
|
s[0], s[1], s[2] = 1, 2, 3
|
||||||
|
|
||||||
return ____
|
return s
|
||||||
}
|
}
|
||||||
|
|
||||||
func exercise2() int {
|
func exercise2() int {
|
||||||
@@ -17,10 +17,10 @@ func exercise2() int {
|
|||||||
|
|
||||||
// 向切片中添加元素并返回新长度
|
// 向切片中添加元素并返回新长度
|
||||||
fruits := []string{"苹果", "香蕉"}
|
fruits := []string{"苹果", "香蕉"}
|
||||||
fruits = append(fruits, ____)
|
fruits = append(fruits, "梨子")
|
||||||
fruits = append(fruits, ____, ____)
|
fruits = append(fruits, "蓝莓", "草莓")
|
||||||
|
|
||||||
return ____(fruits)
|
return len(fruits)
|
||||||
}
|
}
|
||||||
|
|
||||||
func exercise3() []int {
|
func exercise3() []int {
|
||||||
@@ -29,14 +29,14 @@ func exercise3() []int {
|
|||||||
// 从切片中提取子切片
|
// 从切片中提取子切片
|
||||||
numbers := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
|
numbers := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
|
||||||
|
|
||||||
// 提取索引2到6(不包含6)
|
// 提取索引2到5(不包含5)
|
||||||
sub1 := numbers[____:____]
|
sub1 := numbers[2:5]
|
||||||
|
|
||||||
// 提取前5个元素
|
// 提取前5个元素
|
||||||
sub2 := numbers[:____]
|
sub2 := numbers[:5]
|
||||||
|
|
||||||
// 提取从第5个开始到末尾
|
// 提取从第5个开始到末尾
|
||||||
sub3 := numbers[____:]
|
sub3 := numbers[4:]
|
||||||
|
|
||||||
fmt.Printf("子切片1: %v\n", sub1)
|
fmt.Printf("子切片1: %v\n", sub1)
|
||||||
fmt.Printf("子切片2: %v\n", sub2)
|
fmt.Printf("子切片2: %v\n", sub2)
|
||||||
@@ -50,13 +50,13 @@ func exercise4() int {
|
|||||||
|
|
||||||
// 修改原始切片,观察使用copy复制的切片是否受影响
|
// 修改原始切片,观察使用copy复制的切片是否受影响
|
||||||
s1 := []int{1, 2, 3}
|
s1 := []int{1, 2, 3}
|
||||||
s2 := make([]int, ____(s1))
|
s2 := make([]int, len(s1))
|
||||||
|
|
||||||
copy(____, s1)
|
copy(s2, s1)
|
||||||
|
|
||||||
s1[0] = 99
|
s1[0] = 99
|
||||||
|
|
||||||
return ____
|
return s2[0]
|
||||||
}
|
}
|
||||||
|
|
||||||
func exercise5() []int {
|
func exercise5() []int {
|
||||||
@@ -65,19 +65,19 @@ func exercise5() []int {
|
|||||||
// 创建一个新切片,包含原切片中所有偶数
|
// 创建一个新切片,包含原切片中所有偶数
|
||||||
numbers := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
|
numbers := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
|
||||||
|
|
||||||
var evens ____
|
var evens []int
|
||||||
|
|
||||||
for _, num := range ____ {
|
for _, num := range numbers {
|
||||||
if num%2 == ____ {
|
if num%2 == 0 {
|
||||||
evens = append(____, num)
|
evens = append(evens, num)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return ____
|
return evens
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
fmt.Println("=== 切片 练习 ===\n")
|
fmt.Println("=== 切片 练习 ===")
|
||||||
|
|
||||||
result1 := exercise1()
|
result1 := exercise1()
|
||||||
fmt.Printf("练习1结果: %v\n", result1)
|
fmt.Printf("练习1结果: %v\n", result1)
|
||||||
|
|||||||
@@ -7,13 +7,13 @@ func exercise1() map[string]int {
|
|||||||
|
|
||||||
// 创建一个map,键为string类型,值为int类型
|
// 创建一个map,键为string类型,值为int类型
|
||||||
// 添加"张三": 90, "李四": 85, "王五": 95
|
// 添加"张三": 90, "李四": 85, "王五": 95
|
||||||
scores := ____
|
scores := make(map[string]int)
|
||||||
|
|
||||||
scores["张三"] = ____
|
scores["张三"] = 90
|
||||||
scores["李四"] = ____
|
scores["李四"] = 85
|
||||||
scores["王五"] = ____
|
scores["王五"] = 95
|
||||||
|
|
||||||
return ____
|
return scores
|
||||||
}
|
}
|
||||||
|
|
||||||
func exercise2() string {
|
func exercise2() string {
|
||||||
@@ -25,7 +25,7 @@ func exercise2() string {
|
|||||||
"李四": 85,
|
"李四": 85,
|
||||||
}
|
}
|
||||||
|
|
||||||
score, exists := ____
|
score, exists := scores["李四"]
|
||||||
|
|
||||||
if exists {
|
if exists {
|
||||||
fmt.Printf("李四的分数是: %d\n", score)
|
fmt.Printf("李四的分数是: %d\n", score)
|
||||||
@@ -45,13 +45,13 @@ func exercise3() int {
|
|||||||
"赵六": 88,
|
"赵六": 88,
|
||||||
}
|
}
|
||||||
|
|
||||||
sum := ____
|
sum := 0
|
||||||
|
|
||||||
for ____, score := range ____ {
|
for _, score := range scores {
|
||||||
sum += ____
|
sum += score
|
||||||
}
|
}
|
||||||
|
|
||||||
return ____
|
return sum
|
||||||
}
|
}
|
||||||
|
|
||||||
func exercise4() map[string]int {
|
func exercise4() map[string]int {
|
||||||
@@ -64,9 +64,9 @@ func exercise4() map[string]int {
|
|||||||
"王五": 95,
|
"王五": 95,
|
||||||
}
|
}
|
||||||
|
|
||||||
____(scores, "李四")
|
delete(scores, "李四")
|
||||||
|
|
||||||
return ____
|
return scores
|
||||||
}
|
}
|
||||||
|
|
||||||
func exercise5() []string {
|
func exercise5() []string {
|
||||||
@@ -81,19 +81,19 @@ func exercise5() []string {
|
|||||||
"钱七": 92,
|
"钱七": 92,
|
||||||
}
|
}
|
||||||
|
|
||||||
var topStudents ____
|
var topStudents []string
|
||||||
|
|
||||||
for name, score := range ____ {
|
for name, score := range scores {
|
||||||
if score >= ____ {
|
if score >= 90 {
|
||||||
topStudents = append(____, ____)
|
topStudents = append(topStudents, name)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return ____
|
return topStudents
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
fmt.Println("=== map 练习 ===\n")
|
fmt.Println("=== map 练习 ===")
|
||||||
|
|
||||||
result1 := exercise1()
|
result1 := exercise1()
|
||||||
fmt.Printf("练习1结果: %v\n", result1)
|
fmt.Printf("练习1结果: %v\n", result1)
|
||||||
|
|||||||
Reference in New Issue
Block a user