diff --git a/eBook/exercises/chapter_5/bitwise_complement.go b/eBook/exercises/chapter_5/bitwise_complement.go index d113263..0c9af34 100755 --- a/eBook/exercises/chapter_5/bitwise_complement.go +++ b/eBook/exercises/chapter_5/bitwise_complement.go @@ -1,22 +1,23 @@ -package main - -import "fmt" - -func main() { - for i:=0; i <= 10; i ++ { - fmt.Printf("the complement of %b is: %b\n", i, ^i) - } -} -/* Output: -the complement of 0 is: -1 -the complement of 1 is: -10 -the complement of 10 is: -11 -the complement of 11 is: -100 -the complement of 100 is: -101 -the complement of 101 is: -110 -the complement of 110 is: -111 -the complement of 111 is: -1000 -the complement of 1000 is: -1001 -the complement of 1001 is: -1010 -the complement of 1010 is: -1011 -*/ \ No newline at end of file +package main + +import "fmt" + +func main() { + for i := 0; i <= 10; i++ { + fmt.Printf("the complement of %b is: %b\n", i, ^i) + } +} + +/* Output: +the complement of 0 is: -1 +the complement of 1 is: -10 +the complement of 10 is: -11 +the complement of 11 is: -100 +the complement of 100 is: -101 +the complement of 101 is: -110 +the complement of 110 is: -111 +the complement of 111 is: -1000 +the complement of 1000 is: -1001 +the complement of 1001 is: -1010 +the complement of 1010 is: -1011 +*/ diff --git a/eBook/exercises/chapter_5/fizzbuzz.go b/eBook/exercises/chapter_5/fizzbuzz.go index ffa16dc..c41f957 100755 --- a/eBook/exercises/chapter_5/fizzbuzz.go +++ b/eBook/exercises/chapter_5/fizzbuzz.go @@ -1,24 +1,24 @@ -package main - -import "fmt" - -const ( - FIZZ=3 - BUZZ=5 - FIZZBUZZ=15 -) - -func main() { - for i:=0; i <= 100; i++ { - switch { - case i%FIZZBUZZ==0: - fmt.Println("FizzBuzz") - case i%FIZZ==0: - fmt.Println("Fizz") - case i%BUZZ==0: - fmt.Println("Buzz") - default: - fmt.Println(i) - } - } -} \ No newline at end of file +package main + +import "fmt" + +const ( + FIZZ = 3 + BUZZ = 5 + FIZZBUZZ = 15 +) + +func main() { + for i := 0; i <= 100; i++ { + switch { + case i%FIZZBUZZ == 0: + fmt.Println("FizzBuzz") + case i%FIZZ == 0: + fmt.Println("Fizz") + case i%BUZZ == 0: + fmt.Println("Buzz") + default: + fmt.Println(i) + } + } +} diff --git a/eBook/exercises/chapter_5/for_character.go b/eBook/exercises/chapter_5/for_character.go index 2653f38..2fd5923 100755 --- a/eBook/exercises/chapter_5/for_character.go +++ b/eBook/exercises/chapter_5/for_character.go @@ -1,17 +1,17 @@ -package main - -func main() { - // 1 - use 2 nested for loops - for i:=1; i <= 25; i++ { - for j:=1; j <=i; j++ { - print("G") - } - println() - } - // 2 - use only one for loop and string concatenation - str := "G" - for i:=1; i <= 25; i++ { - println(str) - str += "G" - } -} \ No newline at end of file +package main + +func main() { + // 1 - use 2 nested for loops + for i := 1; i <= 25; i++ { + for j := 1; j <= i; j++ { + print("G") + } + println() + } + // 2 - use only one for loop and string concatenation + str := "G" + for i := 1; i <= 25; i++ { + println(str) + str += "G" + } +} diff --git a/eBook/exercises/chapter_5/for_loop.go b/eBook/exercises/chapter_5/for_loop.go index a484f65..ae38663 100755 --- a/eBook/exercises/chapter_5/for_loop.go +++ b/eBook/exercises/chapter_5/for_loop.go @@ -1,16 +1,18 @@ -package main - -import "fmt" - -func main() { - // 1: - for i:=0; i < 15; i++ { - fmt.Printf("The counter is at %d\n", i) - } - // 2: - i := 0 -START: - fmt.Printf("The counter is at %d\n", i) - i++ - if i < 15 { goto START } -} \ No newline at end of file +package main + +import "fmt" + +func main() { + // 1: + for i := 0; i < 15; i++ { + fmt.Printf("The counter is at %d\n", i) + } + // 2: + i := 0 +START: + fmt.Printf("The counter is at %d\n", i) + i++ + if i < 15 { + goto START + } +} diff --git a/eBook/exercises/chapter_5/i_undefined.go b/eBook/exercises/chapter_5/i_undefined.go index d0c8840..2f79e4a 100755 --- a/eBook/exercises/chapter_5/i_undefined.go +++ b/eBook/exercises/chapter_5/i_undefined.go @@ -1,14 +1,14 @@ -// i_undefined.go -package main - -import ( - "fmt" -) - -func main() { - var i int - for i=0; i<10; i++ { - fmt.Printf("%v\n", i) - } - fmt.Printf("%v\n", i) //<-- compile error: undefined i -} +// i_undefined.go +package main + +import ( + "fmt" +) + +func main() { + var i int + for i = 0; i < 10; i++ { + fmt.Printf("%v\n", i) + } + fmt.Printf("%v\n", i) //<-- compile error: undefined i +} diff --git a/eBook/exercises/chapter_5/multiple_for.go b/eBook/exercises/chapter_5/multiple_for.go index b9b0649..2dcb9b5 100755 --- a/eBook/exercises/chapter_5/multiple_for.go +++ b/eBook/exercises/chapter_5/multiple_for.go @@ -1,17 +1,18 @@ -// multiple_for.go -package main - -import "fmt" - -func main() { - //multiple initialization; a consolidated bool expression with && and ||; multiple ‘incrementation’ - for i, j, s := 0, 5, "a"; i < 3 && j < 100 && s != "aaaaa"; i, j, s = i+1, - j+1, s + "a" { - fmt.Println("Value of i, j, s:", i, j, s) - } -} -/* Output: -Value of i, j, s: 0 5 a -Value of i, j, s: 1 6 aa -Value of i, j, s: 2 7 aaa -*/ \ No newline at end of file +// multiple_for.go +package main + +import "fmt" + +func main() { + //multiple initialization; a consolidated bool expression with && and ||; multiple ‘incrementation’ + for i, j, s := 0, 5, "a"; i < 3 && j < 100 && s != "aaaaa"; i, j, s = i+1, + j+1, s+"a" { + fmt.Println("Value of i, j, s:", i, j, s) + } +} + +/* Output: +Value of i, j, s: 0 5 a +Value of i, j, s: 1 6 aa +Value of i, j, s: 2 7 aaa +*/ diff --git a/eBook/exercises/chapter_5/rectangle_stars.go b/eBook/exercises/chapter_5/rectangle_stars.go index a5eb7cb..1cc2efc 100755 --- a/eBook/exercises/chapter_5/rectangle_stars.go +++ b/eBook/exercises/chapter_5/rectangle_stars.go @@ -1,26 +1,27 @@ -// rectangle_stars.go -package main - -import "fmt" - -func main() { - w, h := 20, 10 - for y := 0; y < h; y++ { - for x := 0; x < w; x++ { - fmt.Print("*") - } - fmt.Println() - } -} -/* Output: -******************** -******************** -******************** -******************** -******************** -******************** -******************** -******************** -******************** -******************** -*/ +// rectangle_stars.go +package main + +import "fmt" + +func main() { + w, h := 20, 10 + for y := 0; y < h; y++ { + for x := 0; x < w; x++ { + fmt.Print("*") + } + fmt.Println() + } +} + +/* Output: +******************** +******************** +******************** +******************** +******************** +******************** +******************** +******************** +******************** +******************** + */ diff --git a/eBook/exercises/chapter_5/season.go b/eBook/exercises/chapter_5/season.go index ba200d5..178edc4 100755 --- a/eBook/exercises/chapter_5/season.go +++ b/eBook/exercises/chapter_5/season.go @@ -1,17 +1,21 @@ -package main - -import "fmt" - -func main() { - fmt.Printf(Season(3)) -} - -func Season(month int) string { - switch month { - case 12,1,2: return "Winter" - case 3,4,5: return "Spring" - case 6,7,8: return "Summer" - case 9,10,11: return "Autumn" - } - return "Season unknown" -} \ No newline at end of file +package main + +import "fmt" + +func main() { + fmt.Printf(Season(3)) +} + +func Season(month int) string { + switch month { + case 12, 1, 2: + return "Winter" + case 3, 4, 5: + return "Spring" + case 6, 7, 8: + return "Summer" + case 9, 10, 11: + return "Autumn" + } + return "Season unknown" +}