Skip to content

Commit

Permalink
fix: coding style and file format for chapter 9.
Browse files Browse the repository at this point in the history
  • Loading branch information
appleboy committed Feb 11, 2017
1 parent d9041c7 commit cee64d1
Show file tree
Hide file tree
Showing 8 changed files with 207 additions and 205 deletions.
57 changes: 29 additions & 28 deletions eBook/exercises/chapter_9/dlinked_list.go
Original file line number Diff line number Diff line change
@@ -1,28 +1,29 @@
// Q20_linked_list.go
package main

import (
"fmt"
"container/list"
)

func main() {
lst := list.New()
lst.PushBack(100)
lst.PushBack(101)
lst.PushBack(102)
// fmt.Println("Here is the double linked list:\n", lst)
for e := lst.Front(); e != nil; e = e.Next() {
// fmt.Println(e)
fmt.Println(e.Value)
}
}
/* Example output:
&{0x12542bc0 <nil> 0x12547590 1}
&{0x12542ba0 0x12542be0 0x12547590 2}
&{<nil> 0x12542bc0 0x12547590 4}
100
101
102
*/
// Q20_linked_list.go
package main

import (
"container/list"
"fmt"
)

func main() {
lst := list.New()
lst.PushBack(100)
lst.PushBack(101)
lst.PushBack(102)
// fmt.Println("Here is the double linked list:\n", lst)
for e := lst.Front(); e != nil; e = e.Next() {
// fmt.Println(e)
fmt.Println(e.Value)
}
}

/* Example output:
&{0x12542bc0 <nil> 0x12547590 1}
&{0x12542ba0 0x12542be0 0x12547590 2}
&{<nil> 0x12542bc0 0x12547590 4}
100
101
102
*/
12 changes: 6 additions & 6 deletions eBook/exercises/chapter_9/even/even.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// even.go
package even

func Even(i int) bool { // exported function
return i%2 == 0
}
// even.go
package even

func Even(i int) bool { // exported function
return i%2 == 0
}
68 changes: 35 additions & 33 deletions eBook/exercises/chapter_9/fibo/fibonacci.go
Original file line number Diff line number Diff line change
@@ -1,33 +1,35 @@
package fibo

/*
func Fibonacci(n int) (res int) {
if n <= 1 {
res = 1
} else {
res = Fibonacci(n-1) + Fibonacci(n-2)
}
return
}
*/
// accepts a general operation op:
func Fibonacci(op string, n int) (res int) {
if n <= 1 {
switch op {
case "+":
res = 1
case "*":
res = 2
default: res = 0
}
} else {
switch op {
case "+":
res = Fibonacci(op, n-1) + Fibonacci(op, n-2)
case "*":
res = Fibonacci(op, n-1) * Fibonacci(op, n-2)
default: res = 0
}
}
return
}
package fibo

/*
func Fibonacci(n int) (res int) {
if n <= 1 {
res = 1
} else {
res = Fibonacci(n-1) + Fibonacci(n-2)
}
return
}
*/
// accepts a general operation op:
func Fibonacci(op string, n int) (res int) {
if n <= 1 {
switch op {
case "+":
res = 1
case "*":
res = 2
default:
res = 0
}
} else {
switch op {
case "+":
res = Fibonacci(op, n-1) + Fibonacci(op, n-2)
case "*":
res = Fibonacci(op, n-1) * Fibonacci(op, n-2)
default:
res = 0
}
}
return
}
53 changes: 26 additions & 27 deletions eBook/exercises/chapter_9/greetings/greetings.go
Original file line number Diff line number Diff line change
@@ -1,27 +1,26 @@
package greetings

import "time"

func GoodDay(name string) string {
return "Good Day " + name
}

func GoodNight(name string) string{
return "Good Night " + name
}

func IsAM() bool {
localTime := time.Now()
return localTime.Hour() <= 12
}

func IsAfternoon() bool {
localTime := time.Now()
return localTime.Hour() <= 18
}

func IsEvening() bool {
localTime := time.Now()
return localTime.Hour() <= 22
}

package greetings

import "time"

func GoodDay(name string) string {
return "Good Day " + name
}

func GoodNight(name string) string {
return "Good Night " + name
}

func IsAM() bool {
localTime := time.Now()
return localTime.Hour() <= 12
}

func IsAfternoon() bool {
localTime := time.Now()
return localTime.Hour() <= 18
}

func IsEvening() bool {
localTime := time.Now()
return localTime.Hour() <= 22
}
123 changes: 61 additions & 62 deletions eBook/exercises/chapter_9/main_fibo.go
Original file line number Diff line number Diff line change
@@ -1,62 +1,61 @@
package main

import (
"fmt"
"./fibo/fibo"
)

var nextFibo int
var op string

func main() {
/*
result := 0
for i:=0; i <= 10; i++ {
result = fibo.Fibonacci(i)
fmt.Printf("fibonacci(%d) is: %d\n", i, result)
}
*/
op = "+"
calls()
fmt.Println("Change of operation from + to *")
nextFibo = 0
op = "*"
calls()
}

func calls() {
next()
fmt.Println("...")
next()
fmt.Println("...")
next()
fmt.Println("...")
next()
}

func next() {
result := 0
nextFibo++
result = fibo.Fibonacci(op, nextFibo)
fmt.Printf("fibonacci(%d) is: %d\n", nextFibo, result)
}
/* *****************************************************************
Output is:
fibonacci(1) is: 1
...
fibonacci(2) is: 2
...
fibonacci(3) is: 3
...
fibonacci(4) is: 5
Change of operation from + to *
fibonacci(1) is: 2
...
fibonacci(2) is: 4
...
fibonacci(3) is: 8
...
fibonacci(4) is: 32
********************************************************************/


package main

import (
"./fibo/fibo"
"fmt"
)

var nextFibo int
var op string

func main() {
/*
result := 0
for i:=0; i <= 10; i++ {
result = fibo.Fibonacci(i)
fmt.Printf("fibonacci(%d) is: %d\n", i, result)
}
*/
op = "+"
calls()
fmt.Println("Change of operation from + to *")
nextFibo = 0
op = "*"
calls()
}

func calls() {
next()
fmt.Println("...")
next()
fmt.Println("...")
next()
fmt.Println("...")
next()
}

func next() {
result := 0
nextFibo++
result = fibo.Fibonacci(op, nextFibo)
fmt.Printf("fibonacci(%d) is: %d\n", nextFibo, result)
}

/* *****************************************************************
Output is:
fibonacci(1) is: 1
...
fibonacci(2) is: 2
...
fibonacci(3) is: 3
...
fibonacci(4) is: 5
Change of operation from + to *
fibonacci(1) is: 2
...
fibonacci(2) is: 4
...
fibonacci(3) is: 8
...
fibonacci(4) is: 32
********************************************************************/
44 changes: 22 additions & 22 deletions eBook/exercises/chapter_9/main_greetings.go
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
package main

import (
"fmt"
"./greetings/greetings"
)

func main() {
name := "James"
fmt.Println(greetings.GoodDay(name))
fmt.Println(greetings.GoodNight(name))
if greetings.IsAM() {
fmt.Println("Good morning", name)
} else if greetings.IsAfternoon(){
fmt.Println("Good afternoon", name)
} else if greetings.IsEvening(){
fmt.Println("Good evening", name)
} else {
fmt.Println("Good night", name)
}
}
package main

import (
"./greetings/greetings"
"fmt"
)

func main() {
name := "James"
fmt.Println(greetings.GoodDay(name))
fmt.Println(greetings.GoodNight(name))

if greetings.IsAM() {
fmt.Println("Good morning", name)
} else if greetings.IsAfternoon() {
fmt.Println("Good afternoon", name)
} else if greetings.IsEvening() {
fmt.Println("Good evening", name)
} else {
fmt.Println("Good night", name)
}
}
26 changes: 13 additions & 13 deletions eBook/exercises/chapter_9/main_oddeven.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
// test_oddeven.go
package main

import (
"fmt"
"./even/even"
)

func main() {
for i:=0; i<=100; i++ {
fmt.Printf("Is the integer %d even? %v\n", i, even.Even(i))
}
}
// test_oddeven.go
package main

import (
"./even/even"
"fmt"
)

func main() {
for i := 0; i <= 100; i++ {
fmt.Printf("Is the integer %d even? %v\n", i, even.Even(i))
}
}
Loading

0 comments on commit cee64d1

Please sign in to comment.