From e6d601d3a1ab98b5ec12517ca4899b32edb2d1aa Mon Sep 17 00:00:00 2001 From: Bo-Yi Wu Date: Sat, 11 Feb 2017 12:26:05 +0800 Subject: [PATCH] fix: coding style and file format for chapter 7. --- eBook/exercises/chapter_7/array_value.go | 44 +++++----- eBook/exercises/chapter_7/bubblesort.go | 54 ++++++------ eBook/exercises/chapter_7/fibonacci_array.go | 36 ++++---- .../chapter_7/fibonacci_funcarray.go | 44 +++++----- eBook/exercises/chapter_7/for_array.go | 25 +++--- eBook/exercises/chapter_7/insert_slice.go | 46 +++++----- eBook/exercises/chapter_7/magnify_slice.go | 50 +++++------ eBook/exercises/chapter_7/map_function.go | 64 +++++++------- eBook/exercises/chapter_7/min_max.go | 80 +++++++++--------- eBook/exercises/chapter_7/remove_slice.go | 38 ++++----- eBook/exercises/chapter_7/split_string.go | 32 +++---- eBook/exercises/chapter_7/string_reverse.go | 83 ++++++++++--------- eBook/exercises/chapter_7/string_split2.go | 31 +++---- eBook/exercises/chapter_7/sum_array.go | 77 +++++++++-------- eBook/exercises/chapter_7/uniq.go | 46 +++++----- 15 files changed, 375 insertions(+), 375 deletions(-) diff --git a/eBook/exercises/chapter_7/array_value.go b/eBook/exercises/chapter_7/array_value.go index b008c95..d7cb179 100755 --- a/eBook/exercises/chapter_7/array_value.go +++ b/eBook/exercises/chapter_7/array_value.go @@ -1,22 +1,22 @@ -package main - -import "fmt" - -func main() { - var arr1 [5]int - - for i:=0; i < len(arr1); i++ { - arr1[i] = i * 2 - } - - arr2 := arr1 - arr2[2] = 100 - - for i:=0; i < len(arr1); i++ { - fmt.Printf("Array arr1 at index %d is %d\n", i, arr1[i]) - } - fmt.Println() - for i:=0; i < len(arr2); i++ { - fmt.Printf("Array arr2 at index %d is %d\n", i, arr2[i]) - } -} \ No newline at end of file +package main + +import "fmt" + +func main() { + var arr1 [5]int + + for i := 0; i < len(arr1); i++ { + arr1[i] = i * 2 + } + + arr2 := arr1 + arr2[2] = 100 + + for i := 0; i < len(arr1); i++ { + fmt.Printf("Array arr1 at index %d is %d\n", i, arr1[i]) + } + fmt.Println() + for i := 0; i < len(arr2); i++ { + fmt.Printf("Array arr2 at index %d is %d\n", i, arr2[i]) + } +} diff --git a/eBook/exercises/chapter_7/bubblesort.go b/eBook/exercises/chapter_7/bubblesort.go index 9a0b795..7a59026 100755 --- a/eBook/exercises/chapter_7/bubblesort.go +++ b/eBook/exercises/chapter_7/bubblesort.go @@ -1,27 +1,27 @@ -// Q14_Bubblesort.go -package main - -import ( - "fmt" -) - -func main() { - sla := []int{2, 6, 4, -10, 8, 89, 12, 68, -45, 37} - fmt.Println("before sort: ",sla) - // sla is passed via call by value, but since sla is a reference type - // the underlying slice is array is changed (sorted) - bubbleSort(sla) - fmt.Println("after sort: ",sla) -} - -func bubbleSort(sl []int) { - // passes through the slice: - for pass:=1; pass < len(sl); pass++ { - // one pass: - for i:=0; i < len(sl) - pass; i++ { // the bigger value 'bubbles up' to the last position - if sl[i] > sl[i+1] { - sl[i], sl[i+1] = sl[i+1], sl[i] - } - } - } -} \ No newline at end of file +// Q14_Bubblesort.go +package main + +import ( + "fmt" +) + +func main() { + sla := []int{2, 6, 4, -10, 8, 89, 12, 68, -45, 37} + fmt.Println("before sort: ", sla) + // sla is passed via call by value, but since sla is a reference type + // the underlying slice is array is changed (sorted) + bubbleSort(sla) + fmt.Println("after sort: ", sla) +} + +func bubbleSort(sl []int) { + // passes through the slice: + for pass := 1; pass < len(sl); pass++ { + // one pass: + for i := 0; i < len(sl)-pass; i++ { // the bigger value 'bubbles up' to the last position + if sl[i] > sl[i+1] { + sl[i], sl[i+1] = sl[i+1], sl[i] + } + } + } +} diff --git a/eBook/exercises/chapter_7/fibonacci_array.go b/eBook/exercises/chapter_7/fibonacci_array.go index 50c065b..1a01520 100755 --- a/eBook/exercises/chapter_7/fibonacci_array.go +++ b/eBook/exercises/chapter_7/fibonacci_array.go @@ -1,18 +1,18 @@ -package main - -import "fmt" - -var fibs [50]int64 - -func main() { - fibs[0] = 1 - fibs[1] = 1 - - for i:= 2; i < 50; i++ { - fibs[i] = fibs[i-1] + fibs[i-2] - } - - for i:=0; i < 50; i++ { - fmt.Printf("The %d-th Fibonacci number is: %d\n", i, fibs[i]) - } -} \ No newline at end of file +package main + +import "fmt" + +var fibs [50]int64 + +func main() { + fibs[0] = 1 + fibs[1] = 1 + + for i := 2; i < 50; i++ { + fibs[i] = fibs[i-1] + fibs[i-2] + } + + for i := 0; i < 50; i++ { + fmt.Printf("The %d-th Fibonacci number is: %d\n", i, fibs[i]) + } +} diff --git a/eBook/exercises/chapter_7/fibonacci_funcarray.go b/eBook/exercises/chapter_7/fibonacci_funcarray.go index b5cd653..9c80787 100755 --- a/eBook/exercises/chapter_7/fibonacci_funcarray.go +++ b/eBook/exercises/chapter_7/fibonacci_funcarray.go @@ -1,22 +1,22 @@ -package main - -import "fmt" - -var term = 15 - -func main() { - result := fibarray(term) - for ix, fib := range result { - fmt.Printf("The %d-th Fibonacci number is: %d\n", ix, fib) - } -} - -func fibarray(term int) []int { - farr := make([]int, term) - farr[0], farr[1] = 1, 1 - - for i:= 2; i < term; i++ { - farr[i] = farr[i-1] + farr[i-2] - } - return farr -} \ No newline at end of file +package main + +import "fmt" + +var term = 15 + +func main() { + result := fibarray(term) + for ix, fib := range result { + fmt.Printf("The %d-th Fibonacci number is: %d\n", ix, fib) + } +} + +func fibarray(term int) []int { + farr := make([]int, term) + farr[0], farr[1] = 1, 1 + + for i := 2; i < term; i++ { + farr[i] = farr[i-1] + farr[i-2] + } + return farr +} diff --git a/eBook/exercises/chapter_7/for_array.go b/eBook/exercises/chapter_7/for_array.go index b93c3af..00980e7 100755 --- a/eBook/exercises/chapter_7/for_array.go +++ b/eBook/exercises/chapter_7/for_array.go @@ -1,13 +1,12 @@ -package main - -import "fmt" - -func main() { - var arr [15]int - for i:=0; i < 15; i++ { - arr[i] = i - } - fmt.Println(arr) // [0 1 2 3 4 5 6 7 8 9 10 11 12 13 14] - - -} \ No newline at end of file +package main + +import "fmt" + +func main() { + var arr [15]int + for i := 0; i < 15; i++ { + arr[i] = i + } + fmt.Println(arr) // [0 1 2 3 4 5 6 7 8 9 10 11 12 13 14] + +} diff --git a/eBook/exercises/chapter_7/insert_slice.go b/eBook/exercises/chapter_7/insert_slice.go index 5d35d31..f14815c 100755 --- a/eBook/exercises/chapter_7/insert_slice.go +++ b/eBook/exercises/chapter_7/insert_slice.go @@ -1,23 +1,23 @@ -// insert_slice.go -package main - -import ( - "fmt" -) - -func main() { - s := []string{"M", "N", "O", "P", "Q", "R"} - in := []string{"A", "B", "C"} - res := InsertStringSlice(s, in, 0) // at the front - fmt.Println(res) // [A B C M N O P Q R] - res = InsertStringSlice(s, in, 3) // [M N O A B C P Q R] - fmt.Println(res) -} - -func InsertStringSlice(slice, insertion []string, index int) []string { - result := make([]string, len(slice) + len(insertion)) - at := copy(result, slice[:index]) - at += copy(result[at:], insertion) - copy(result[at:], slice[index:]) - return result -} \ No newline at end of file +// insert_slice.go +package main + +import ( + "fmt" +) + +func main() { + s := []string{"M", "N", "O", "P", "Q", "R"} + in := []string{"A", "B", "C"} + res := InsertStringSlice(s, in, 0) // at the front + fmt.Println(res) // [A B C M N O P Q R] + res = InsertStringSlice(s, in, 3) // [M N O A B C P Q R] + fmt.Println(res) +} + +func InsertStringSlice(slice, insertion []string, index int) []string { + result := make([]string, len(slice)+len(insertion)) + at := copy(result, slice[:index]) + at += copy(result[at:], insertion) + copy(result[at:], slice[index:]) + return result +} diff --git a/eBook/exercises/chapter_7/magnify_slice.go b/eBook/exercises/chapter_7/magnify_slice.go index 4b6e13c..8377a8e 100755 --- a/eBook/exercises/chapter_7/magnify_slice.go +++ b/eBook/exercises/chapter_7/magnify_slice.go @@ -1,25 +1,25 @@ -package main - -import "fmt" - -var s []int - -func main() { - s = []int{1, 2, 3} - fmt.Println("The length of s before enlarging is:", len(s)) - fmt.Println(s) - s = enlarge(s, 5) - fmt.Println("The length of s after enlarging is:", len(s)) - fmt.Println(s) -} - -func enlarge(s []int, factor int) []int { - ns := make([]int, len(s) * factor) - // fmt.Println("The length of ns is:", len(ns)) - copy(ns, s) - //fmt.Println(ns) - s = ns - //fmt.Println(s) - //fmt.Println("The length of s after enlarging is:", len(s)) - return s -} \ No newline at end of file +package main + +import "fmt" + +var s []int + +func main() { + s = []int{1, 2, 3} + fmt.Println("The length of s before enlarging is:", len(s)) + fmt.Println(s) + s = enlarge(s, 5) + fmt.Println("The length of s after enlarging is:", len(s)) + fmt.Println(s) +} + +func enlarge(s []int, factor int) []int { + ns := make([]int, len(s)*factor) + // fmt.Println("The length of ns is:", len(ns)) + copy(ns, s) + //fmt.Println(ns) + s = ns + //fmt.Println(s) + //fmt.Println("The length of s after enlarging is:", len(s)) + return s +} diff --git a/eBook/exercises/chapter_7/map_function.go b/eBook/exercises/chapter_7/map_function.go index 30b3ab9..d21d29c 100755 --- a/eBook/exercises/chapter_7/map_function.go +++ b/eBook/exercises/chapter_7/map_function.go @@ -1,32 +1,32 @@ -package main - -import "fmt" - -func main() { - list := []int{0, 1, 2, 3, 4, 5, 6, 7} - mf := func(i int) int { - return i * 10 - } - /* - result := mapFunc(mf, list) - for _, v := range result { - fmt.Println(v) - } - */ - println() - // shorter: - fmt.Printf("%v", mapFunc(mf, list) ) -} - -func mapFunc(mf func(int) int, list []int) ([]int) { - result := make([]int, len(list)) - for ix, v := range list { - result[ix] = mf(v) - } - /* - for ix := 0; ix max { - max = v - } - } - return -} - -func minSlice(sl []int) (min int) { - // min = int(^uint(0) >> 1) - min = math.MaxInt32 - for _, v := range sl { - if v < min { - min = v - } - } - return -} - -/* Output: -The maximum is 643 -The minimum is 2 -*/ +// Q13_1_max.go +package main + +import ( + "fmt" + "math" +) + +func main() { + sl1 := []int{78, 34, 643, 12, 90, 492, 13, 2} + max := maxSlice(sl1) + fmt.Printf("The maximum is %d\n", max) + min := minSlice(sl1) + fmt.Printf("The minimum is %d\n", min) +} + +func maxSlice(sl []int) (max int) { + for _, v := range sl { + if v > max { + max = v + } + } + return +} + +func minSlice(sl []int) (min int) { + // min = int(^uint(0) >> 1) + min = math.MaxInt32 + for _, v := range sl { + if v < min { + min = v + } + } + return +} + +/* Output: +The maximum is 643 +The minimum is 2 +*/ diff --git a/eBook/exercises/chapter_7/remove_slice.go b/eBook/exercises/chapter_7/remove_slice.go index d5c93d1..7e3724d 100755 --- a/eBook/exercises/chapter_7/remove_slice.go +++ b/eBook/exercises/chapter_7/remove_slice.go @@ -1,19 +1,19 @@ -// remove_slice.go -package main - -import ( - "fmt" -) - -func main() { - s := []string{"M", "N", "O", "P", "Q", "R"} - res := RemoveStringSlice(s, 2, 4) - fmt.Println(res) // [M N Q R] -} - -func RemoveStringSlice(slice []string, start, end int) []string { - result := make([]string, len(slice) - (end - start)) - at := copy(result, slice[:start]) - copy(result[at:], slice[end:]) - return result -} \ No newline at end of file +// remove_slice.go +package main + +import ( + "fmt" +) + +func main() { + s := []string{"M", "N", "O", "P", "Q", "R"} + res := RemoveStringSlice(s, 2, 4) + fmt.Println(res) // [M N Q R] +} + +func RemoveStringSlice(slice []string, start, end int) []string { + result := make([]string, len(slice)-(end-start)) + at := copy(result, slice[:start]) + copy(result[at:], slice[end:]) + return result +} diff --git a/eBook/exercises/chapter_7/split_string.go b/eBook/exercises/chapter_7/split_string.go index 24634fb..c4a8c04 100755 --- a/eBook/exercises/chapter_7/split_string.go +++ b/eBook/exercises/chapter_7/split_string.go @@ -1,16 +1,16 @@ -package main - -import "fmt" - -func main() { - str := "Google" - for i:=0; i <= len(str); i++ { - a, b := Split(str, i) - fmt.Printf("The string %s split at position %d is: %s / %s\n", str, i, a, b) - } - -} - -func Split(s string, pos int) (string, string) { - return s[0:pos], s[pos:] -} \ No newline at end of file +package main + +import "fmt" + +func main() { + str := "Google" + for i := 0; i <= len(str); i++ { + a, b := Split(str, i) + fmt.Printf("The string %s split at position %d is: %s / %s\n", str, i, a, b) + } + +} + +func Split(s string, pos int) (string, string) { + return s[0:pos], s[pos:] +} diff --git a/eBook/exercises/chapter_7/string_reverse.go b/eBook/exercises/chapter_7/string_reverse.go index 053fc2d..96d6344 100755 --- a/eBook/exercises/chapter_7/string_reverse.go +++ b/eBook/exercises/chapter_7/string_reverse.go @@ -1,41 +1,42 @@ -package main - -import "fmt" - -func reverse(s string) string { - runes := []rune(s) - n, h := len(runes), len(runes)/2 - for i:= 0; i < h; i ++ { - runes[i], runes[n-1-i] = runes[n-1-i], runes[i] - } - return string(runes) -} - -func main() { -// reverse a string: - str := "Google" - sl := []byte(str) - var rev [100]byte - j := 0 - for i:=len(sl)-1; i >= 0; i-- { - rev[j] = sl[i] - j++ - } - str_rev := string(rev[:]) - fmt.Printf("The reversed string is -%s-\n", str_rev) -// variant: "in place" using swapping - str2 := "Google" - sl2 := []byte(str2) - for i, j := 0, len(sl2) - 1; i < j; i, j = i+1, j-1 { - sl2[i], sl2[j] = sl2[j], sl2[i] - } - fmt.Printf("The reversed string is -%s-\n", string(sl2)) -// variant: using [] int for runes (necessary for Unicode-strings!): - s := "My Test String!" - fmt.Println(s, " --> ", reverse(s)) -} -/* Output: -The reversed string is -elgooG- -The reversed string is -elgooG- -My Test String! --> !gnirtS tseT yM -*/ \ No newline at end of file +package main + +import "fmt" + +func reverse(s string) string { + runes := []rune(s) + n, h := len(runes), len(runes)/2 + for i := 0; i < h; i++ { + runes[i], runes[n-1-i] = runes[n-1-i], runes[i] + } + return string(runes) +} + +func main() { + // reverse a string: + str := "Google" + sl := []byte(str) + var rev [100]byte + j := 0 + for i := len(sl) - 1; i >= 0; i-- { + rev[j] = sl[i] + j++ + } + str_rev := string(rev[:]) + fmt.Printf("The reversed string is -%s-\n", str_rev) + // variant: "in place" using swapping + str2 := "Google" + sl2 := []byte(str2) + for i, j := 0, len(sl2)-1; i < j; i, j = i+1, j-1 { + sl2[i], sl2[j] = sl2[j], sl2[i] + } + fmt.Printf("The reversed string is -%s-\n", string(sl2)) + // variant: using [] int for runes (necessary for Unicode-strings!): + s := "My Test String!" + fmt.Println(s, " --> ", reverse(s)) +} + +/* Output: +The reversed string is -elgooG- +The reversed string is -elgooG- +My Test String! --> !gnirtS tseT yM +*/ diff --git a/eBook/exercises/chapter_7/string_split2.go b/eBook/exercises/chapter_7/string_split2.go index 77e812e..e8d0c22 100755 --- a/eBook/exercises/chapter_7/string_split2.go +++ b/eBook/exercises/chapter_7/string_split2.go @@ -1,15 +1,16 @@ -package main - -import "fmt" - -func main() { - str := "Google" - str2 := Split2(str) - fmt.Printf("The string %s transformed is: %s\n", str, str2) -} - -func Split2(s string) string { - mid := len(s) / 2 - return s[mid:] + s[:mid] -} -// Output: The string Google transformed is: gleGoo \ No newline at end of file +package main + +import "fmt" + +func main() { + str := "Google" + str2 := Split2(str) + fmt.Printf("The string %s transformed is: %s\n", str, str2) +} + +func Split2(s string) string { + mid := len(s) / 2 + return s[mid:] + s[:mid] +} + +// Output: The string Google transformed is: gleGoo diff --git a/eBook/exercises/chapter_7/sum_array.go b/eBook/exercises/chapter_7/sum_array.go index d1cb723..e7dfa40 100755 --- a/eBook/exercises/chapter_7/sum_array.go +++ b/eBook/exercises/chapter_7/sum_array.go @@ -1,39 +1,38 @@ -// leaving out the length 4 in [4] uses slice instead of an array -// which is generally better for performance -package main - -import "fmt" - -func main() { - // var a = [4]float32 {1.0,2.0,3.0,4.0} - var a = []float32 {1.0,2.0,3.0,4.0} - fmt.Printf("The sum of the array is: %f\n", Sum(a)) - var b = []int {1, 2, 3, 4, 5} - sum, average := SumAndAverage(b) - fmt.Printf("The sum of the array is: %d, and the average is: %f", sum, average) -} - -/* -func Sum(a [4]float32) (sum float32) { - for _, item := range a { - sum += item - } - return -} -*/ - -func Sum(a []float32) (sum float32) { - for _, item := range a { - sum += item - } - return -} - -func SumAndAverage (a []int) (int, float32) { - sum := 0 - for _, item := range a { - sum += item - } - return sum, float32(sum/len(a)) -} - +// leaving out the length 4 in [4] uses slice instead of an array +// which is generally better for performance +package main + +import "fmt" + +func main() { + // var a = [4]float32 {1.0,2.0,3.0,4.0} + var a = []float32{1.0, 2.0, 3.0, 4.0} + fmt.Printf("The sum of the array is: %f\n", Sum(a)) + var b = []int{1, 2, 3, 4, 5} + sum, average := SumAndAverage(b) + fmt.Printf("The sum of the array is: %d, and the average is: %f", sum, average) +} + +/* +func Sum(a [4]float32) (sum float32) { + for _, item := range a { + sum += item + } + return +} +*/ + +func Sum(a []float32) (sum float32) { + for _, item := range a { + sum += item + } + return +} + +func SumAndAverage(a []int) (int, float32) { + sum := 0 + for _, item := range a { + sum += item + } + return sum, float32(sum / len(a)) +} diff --git a/eBook/exercises/chapter_7/uniq.go b/eBook/exercises/chapter_7/uniq.go index b4c2aed..5f0b590 100755 --- a/eBook/exercises/chapter_7/uniq.go +++ b/eBook/exercises/chapter_7/uniq.go @@ -1,23 +1,23 @@ -// Q29_uniq.go -package main - -import ( - "fmt" -) - -var arr []byte = []byte{'a','b','a','a','a','c','d','e','f','g'} - -func main() { - arru := make([]byte,len(arr)) // this will contain the unique items - ixu := 0 // index in arru - tmp := byte(0) - for _, val := range arr { - if val!=tmp { - arru[ixu] = val - fmt.Printf("%c ", arru[ixu]) - ixu++ - } - tmp = val - } - // fmt.Println(arru) -} +// Q29_uniq.go +package main + +import ( + "fmt" +) + +var arr []byte = []byte{'a', 'b', 'a', 'a', 'a', 'c', 'd', 'e', 'f', 'g'} + +func main() { + arru := make([]byte, len(arr)) // this will contain the unique items + ixu := 0 // index in arru + tmp := byte(0) + for _, val := range arr { + if val != tmp { + arru[ixu] = val + fmt.Printf("%c ", arru[ixu]) + ixu++ + } + tmp = val + } + // fmt.Println(arru) +}