Update 07.3.md and 07.6.md (#273)

* Update 07.3.md

* Update 07.3.md

* Update 07.6.md
This commit is contained in:
王耀
2016-08-23 11:58:44 +08:00
committed by 无闻
parent 23935f6f12
commit d40ca54c10
3 changed files with 13 additions and 12 deletions

View File

@@ -67,7 +67,7 @@ for ix := range seasons {
```go
for row := range screen {
for column := range screen[0] {
for column := range screen[row] {
screen[row][column] = 1
}
}
@@ -89,7 +89,7 @@ b) 如果 a) 无法正常工作,写一个 for 循环让值可以 double。
**练习 7.7** sum_array.go
a) 写一个 Sum 函数,传入参数为一个 4 位 float 数组成的数组 arrF返回该数组的所有数字和。
a) 写一个 Sum 函数,传入参数为一个 32 位 float 数组成的数组 arrF返回该数组的所有数字和。
如果把数组修改为切片的话代码要做怎样的修改?如果用切片形式方法实现不同长度数组的的和呢?

View File

@@ -23,9 +23,9 @@ func main() {
0:ÿ 2:界
我们知道Unicode 字符会占用 2 个字节,有些甚至需要 3 个或者 4 个字节来进行表示。如果发现错误的 UTF8 字符,则该字符会被设置为 U+FFFD 并且索引向前移动一个字节。和字符串转换一样,您同样可以使用 `c := []int(s)` 语法,这样切片中的每个 int 都会包含对应的 Unicode 代码,因为字符串中的每次字符都会对应一个整数。类似的,您也可以将字符串转换为元素类型为 rune 的切片:`r := []rune(s)`
我们知道Unicode 字符会占用 2 个字节,有些甚至需要 3 个或者 4 个字节来进行表示。如果发现错误的 UTF8 字符,则该字符会被设置为 U+FFFD 并且索引向前移动一个字节。和字符串转换一样,您同样可以使用 `c := []int32(s)` 语法,这样切片中的每个 int 都会包含对应的 Unicode 代码,因为字符串中的每次字符都会对应一个整数。类似的,您也可以将字符串转换为元素类型为 rune 的切片:`r := []rune(s)`
可以通过代码 `len([]int(s))` 来获得字符串中字符的数量,但使用 `utf8.RuneCountInString(s)` 效率会更高一点。(参考[count_characters.go](exercises/chapter_4/count_characters.go))
可以通过代码 `len([]int32(s))` 来获得字符串中字符的数量,但使用 `utf8.RuneCountInString(s)` 效率会更高一点。(参考[count_characters.go](exercises/chapter_4/count_characters.go))
您还可以将一个字符串追加到某一个字符数组的尾部:
@@ -178,7 +178,7 @@ func FindDigits(filename string) []byte {
如果您使用两个切片来实现反转,请再尝试使用一个切片(提示:使用交换法)。
如果您想要反转 Unicode 编码的字符串,请使用 `[]int` 类型的切片。
如果您想要反转 Unicode 编码的字符串,请使用 `[]int32` 类型的切片。
**练习 7.15**

View File

@@ -7,7 +7,7 @@ import (
)
func main() {
sl1 := []int{78,34,643,12,90,492, 13, 2}
sl1 := []int{78, 34, 643, 12, 90, 492, 13, 2}
max := maxSlice(sl1)
fmt.Printf("The maximum is %d\n", max)
min := minSlice(sl1)
@@ -20,20 +20,21 @@ func maxSlice(sl []int) (max int) {
max = v
}
}
return
return
}
func minSlice(sl [] int) (min int) {
// min = int(^uint(0) >> 1)
min = math.MaxInt32
func minSlice(sl []int) (min int) {
// min = int(^uint(0) >> 1)
min = math.MaxInt32
for _, v := range sl {
if v < min {
min = v
}
}
return
return
}
/* Output:
The maximum is 643
The minimum is 2
*/
*/