From 4031f76c6f4f6a2038968ae5e34d9fef0a2df897 Mon Sep 17 00:00:00 2001 From: Ajey Kulkarni Date: Tue, 16 Oct 2018 13:03:14 -0500 Subject: [PATCH 1/8] variables related stuff --- .../02_var_zero-value-abk/test-abk.go | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 03_variables/02_var_zero-value-abk/test-abk.go diff --git a/03_variables/02_var_zero-value-abk/test-abk.go b/03_variables/02_var_zero-value-abk/test-abk.go new file mode 100644 index 00000000..53ca0eff --- /dev/null +++ b/03_variables/02_var_zero-value-abk/test-abk.go @@ -0,0 +1,21 @@ +package main + +import "fmt" + + +// var keyword +var y = 9.99 + +func main() { + // Short delcation operator + x := 44.5 + + fmt.Println(x) + + foo() +} + +func foo() { + fmt.Println("in foo",y) +} + From 4e0e4c0a5af3d6c4ad8be74dbd5dbf60a80b732f Mon Sep 17 00:00:00 2001 From: Ajey Kulkarni Date: Tue, 16 Oct 2018 14:30:25 -0500 Subject: [PATCH 2/8] Ninja level 1 solutions --- .../04_exercise_solutions/ninja-level1.go | 43 +++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 03_variables/04_exercise_solutions/ninja-level1.go diff --git a/03_variables/04_exercise_solutions/ninja-level1.go b/03_variables/04_exercise_solutions/ninja-level1.go new file mode 100644 index 00000000..a8a546ca --- /dev/null +++ b/03_variables/04_exercise_solutions/ninja-level1.go @@ -0,0 +1,43 @@ +package main + +import "fmt" + +var x int = 42 +var y string = "James Bond" +var z bool = true + +type abk_int int +type hotdog int + +var x_myint abk_int + +func main() { + +// exercise3() +// exercise4() + exercise5() + +} + +func exercise3() { + + s := fmt.Sprintf("%d %s %t",x,y,z) + fmt.Println(s) +} + +func exercise4() { + fmt.Printf("%d %T\n",x_myint,x_myint) + x_myint = abk_int(100) + + fmt.Printf("%d %T\n",x_myint,x_myint) +} + +func exercise5() { + fmt.Printf("%d %T\n",x_myint,x_myint) + x_myint = abk_int(100) + fmt.Printf("%d %T\n",x_myint,x_myint) + + x = int(x_myint) + fmt.Printf("%d %T\n",x, x) + +} From 240007b8fd0b5c43fb8555a4eaaf34b8f57bd5cf Mon Sep 17 00:00:00 2001 From: Ajey Kulkarni Date: Thu, 18 Oct 2018 11:05:15 -0500 Subject: [PATCH 3/8] ninja-level5 solutions --- 20_struct/10_exercises/ninja-level5.go | 111 +++++++++++++++++++++++++ 1 file changed, 111 insertions(+) create mode 100644 20_struct/10_exercises/ninja-level5.go diff --git a/20_struct/10_exercises/ninja-level5.go b/20_struct/10_exercises/ninja-level5.go new file mode 100644 index 00000000..16baabad --- /dev/null +++ b/20_struct/10_exercises/ninja-level5.go @@ -0,0 +1,111 @@ +package main + +import ( +"fmt" +) + +type person struct { + fname string + lname string + ficf []string +} + +func main() { + + fmt.Println("\nSolution 2\n") + solution2() + + fmt.Println("\nSolution 3\n") + solution3() + + fmt.Println("\nSolution 4\n") + solution4() +} + +func solution2() { + + //Creation of maps using structs + p1 := person{ + fname: "aj", + lname: "kul", + ficf: []string{"kasata", "vanilla"}, + } + p2 := person{ + fname: "Ro", + lname: "kul1", + ficf: []string{"rainbow", "mint", "chocolate"}, + } + + /* + fmt.Println(p1) + for _, icecream := range p1.ficf { + fmt.Println(icecream) + } + for _, icecream := range p2.ficf { + fmt.Println(icecream) + } + */ + + m := map[string]person{ + p1.lname: p1, + p2.lname: p2, + } + + fmt.Println(m["kul"]) +} + + +type vehicle struct { + doors int + color string +} + +type truck struct { + vehicle + fourWheel bool +} + +type sedan struct { + vehicle + luxury bool +} + +func solution3() { + + // Usage of embedded structs + t1 := truck{ + vehicle: vehicle{ + doors: 2, + color: "red"}, + fourWheel: true, + } + + s1 := sedan{ + vehicle: vehicle{ + doors: 4, + color: "black", + }, + luxury: false, + } + + fmt.Println(t1) + fmt.Println(s1) + fmt.Printf("%d %s %t", t1.doors, t1.color, t1.fourWheel) + + +} + + + +func solution4() { + + // Usage of anonymous struct + t1 := struct { + doors int + color string + }{ + doors: 4, + color: "red", + } + fmt.Println(t1) +} \ No newline at end of file From 1219c0fae05789b0b4725cb22ed736c0a8c964f7 Mon Sep 17 00:00:00 2001 From: Ajey Kulkarni Date: Fri, 19 Oct 2018 10:10:07 -0500 Subject: [PATCH 4/8] ninja-level6 solutions --- 21_interfaces/06_exercises/ninja-level6.go | 59 ++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 21_interfaces/06_exercises/ninja-level6.go diff --git a/21_interfaces/06_exercises/ninja-level6.go b/21_interfaces/06_exercises/ninja-level6.go new file mode 100644 index 00000000..6dd5ffa0 --- /dev/null +++ b/21_interfaces/06_exercises/ninja-level6.go @@ -0,0 +1,59 @@ +package main + +import "fmt" + +func main() { + solution3() + solution4() +} + +func solution3() { + + y := []int{10, 200, 30} + z := []int{10, 200, 300} + + + defer fmt.Println(foo(y...)) + fmt.Println(bar(z)) +} + +func foo(x ...int) int { + + sum := 0 + for _, num := range x { + sum += num + } + return sum +} + +func bar(x []int) int { + sum := 0 + for _, num := range x { + sum += num + } + return sum +} + + +//Solution 4 + +type person struct { + first string + last string + age int +} + +func (p person) speak() { + fmt.Printf("\n name: %s %s age: %d", p.first, p.last, p.age) + +} + +func solution4() { + + p1 := person{ + "abk", + "ku", + 41, + } + p1.speak() +} From bd3900a66ae9ed91d01622e04db7a81fb8ad0fbd Mon Sep 17 00:00:00 2001 From: Ajey Kulkarni Date: Fri, 19 Oct 2018 10:42:04 -0500 Subject: [PATCH 5/8] ninja-level6 solution5 --- 21_interfaces/06_exercises/ninja-level6.go | 40 +++++++++++++++++++++- 1 file changed, 39 insertions(+), 1 deletion(-) diff --git a/21_interfaces/06_exercises/ninja-level6.go b/21_interfaces/06_exercises/ninja-level6.go index 6dd5ffa0..5d29517b 100644 --- a/21_interfaces/06_exercises/ninja-level6.go +++ b/21_interfaces/06_exercises/ninja-level6.go @@ -1,10 +1,14 @@ package main -import "fmt" +import ( + "fmt" + "math" +) func main() { solution3() solution4() + solution5() } func solution3() { @@ -57,3 +61,37 @@ func solution4() { } p1.speak() } + + +type Square struct { + length float64 + width float64 +} + +type Circle struct { + radius float64 +} + +type Shape interface { + area() float64 +} + +func info(s Shape) { + fmt.Println(s.area()) +} + +func (s Square) area() float64 { + return (s.length * s.width) +} + +func (c Circle) area() float64 { + return (math.Pi * c.radius * c.radius) +} + +func solution5() { + + s1 := Square{10, 10} + c1 := Circle{9} + info(s1) + info(c1) +} \ No newline at end of file From e17183b2a6235189d6ad0b6fb4dc037b1c4d67a5 Mon Sep 17 00:00:00 2001 From: Ajey Kulkarni Date: Fri, 19 Oct 2018 10:47:45 -0500 Subject: [PATCH 6/8] ninja-level6 solution6 --- 21_interfaces/06_exercises/ninja-level6.go | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/21_interfaces/06_exercises/ninja-level6.go b/21_interfaces/06_exercises/ninja-level6.go index 5d29517b..ed4814e8 100644 --- a/21_interfaces/06_exercises/ninja-level6.go +++ b/21_interfaces/06_exercises/ninja-level6.go @@ -9,6 +9,7 @@ func main() { solution3() solution4() solution5() + solution6() } func solution3() { @@ -94,4 +95,12 @@ func solution5() { c1 := Circle{9} info(s1) info(c1) -} \ No newline at end of file +} + + +func solution6() { + + func(x int) { + fmt.Println("I am anonymous function", x) + }(10) +} From aa0cc0e13ce7b8207d60608814943e6c16472f6d Mon Sep 17 00:00:00 2001 From: Ajey Kulkarni Date: Fri, 19 Oct 2018 16:45:34 -0500 Subject: [PATCH 7/8] ninja-level6 solution7 --- 21_interfaces/06_exercises/ninja-level6.go | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/21_interfaces/06_exercises/ninja-level6.go b/21_interfaces/06_exercises/ninja-level6.go index ed4814e8..bc6b6517 100644 --- a/21_interfaces/06_exercises/ninja-level6.go +++ b/21_interfaces/06_exercises/ninja-level6.go @@ -10,6 +10,7 @@ func main() { solution4() solution5() solution6() + solution7() } func solution3() { @@ -104,3 +105,10 @@ func solution6() { fmt.Println("I am anonymous function", x) }(10) } + +func solution7() { + f1 := func() { + fmt.Println("Function Assignment ") + } + f1() +} \ No newline at end of file From bf6782adf579a6a160c59a3a70fd8be813561b9f Mon Sep 17 00:00:00 2001 From: Ajey Kulkarni Date: Fri, 19 Oct 2018 16:53:46 -0500 Subject: [PATCH 8/8] ninja-level6 solution8 --- 21_interfaces/06_exercises/ninja-level6.go | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/21_interfaces/06_exercises/ninja-level6.go b/21_interfaces/06_exercises/ninja-level6.go index bc6b6517..9888baca 100644 --- a/21_interfaces/06_exercises/ninja-level6.go +++ b/21_interfaces/06_exercises/ninja-level6.go @@ -11,6 +11,7 @@ func main() { solution5() solution6() solution7() + fmt.Println(solution8()()) } func solution3() { @@ -111,4 +112,11 @@ func solution7() { fmt.Println("Function Assignment ") } f1() +} + +func solution8() func() int { + + return func() int { + return 42 + } } \ No newline at end of file