Skip to content

Commit 5cafd65

Browse files
author
Rajeev Kumar Singh
committed
Code org
1 parent 1ac2e91 commit 5cafd65

File tree

52 files changed

+1001
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

52 files changed

+1001
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package main
2+
3+
import "fmt"
4+
5+
func avg(x float64, y float64) float64 {
6+
return (x + y) / 2
7+
}
8+
9+
func main() {
10+
x := 5.75
11+
y := 6.25
12+
13+
result := avg(x, y)
14+
15+
fmt.Printf("Average of %.2f and %.2f = %.2f\n", x, y, result)
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"math"
6+
)
7+
8+
func getStockPriceChange(prevPrice, currentPrice float64) (float64, float64) {
9+
change := currentPrice - prevPrice
10+
percentChange := (change / prevPrice) * 100
11+
return change, percentChange
12+
}
13+
14+
func main() {
15+
prevStockPrice := 0.0
16+
currentStockPrice := 100000.0
17+
18+
change, percentChange := getStockPriceChange(prevStockPrice, currentStockPrice)
19+
20+
if change < 0 {
21+
fmt.Printf("The Stock Price decreased by $%.2f which is %.2f%% of the prev price\n", math.Abs(change), math.Abs(percentChange))
22+
} else {
23+
fmt.Printf("The Stock Price increased by $%.2f which is %.2f%% of the prev price\n", change, percentChange)
24+
}
25+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package main
2+
3+
import (
4+
"errors"
5+
"fmt"
6+
"math"
7+
)
8+
9+
func getStockPriceChangeWithError(prevPrice, currentPrice float64) (float64, float64, error) {
10+
if prevPrice == 0 {
11+
err := errors.New("Previous price cannot be zero")
12+
return 0, 0, err
13+
}
14+
change := currentPrice - prevPrice
15+
percentChange := (change / prevPrice) * 100
16+
return change, percentChange, nil
17+
}
18+
19+
func main() {
20+
prevStockPrice := 0.0
21+
currentStockPrice := 100000.0
22+
23+
change, percentChange, err := getStockPriceChangeWithError(prevStockPrice, currentStockPrice)
24+
25+
if err != nil {
26+
fmt.Println("Sorry! There was an error: ", err)
27+
} else {
28+
if change < 0 {
29+
fmt.Printf("The Stock Price decreased by $%.2f which is %.2f%% of the prev price\n", math.Abs(change), math.Abs(percentChange))
30+
} else {
31+
fmt.Printf("The Stock Price increased by $%.2f which is %.2f%% of the prev price\n", change, percentChange)
32+
}
33+
}
34+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"math"
6+
)
7+
8+
func getNamedStockPriceChange(prevPrice, currentPrice float64) (change, percentChange float64) {
9+
change = currentPrice - prevPrice
10+
percentChange = (change / prevPrice) * 100
11+
return change, percentChange
12+
}
13+
14+
func main() {
15+
prevStockPrice := 100000.0
16+
currentStockPrice := 90000.0
17+
18+
change, percentChange := getNamedStockPriceChange(prevStockPrice, currentStockPrice)
19+
20+
if change < 0 {
21+
fmt.Printf("The Stock Price decreased by $%.2f which is %.2f%% of the prev price\n", math.Abs(change), math.Abs(percentChange))
22+
} else {
23+
fmt.Printf("The Stock Price increased by $%.2f which is %.2f%% of the prev price\n", change, percentChange)
24+
}
25+
}
+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"math"
6+
)
7+
8+
func getStockPriceChange(prevPrice, currentPrice float64) (float64, float64) {
9+
change := currentPrice - prevPrice
10+
percentChange := (change / prevPrice) * 100
11+
return change, percentChange
12+
}
13+
14+
func main() {
15+
prevStockPrice := 80000.0
16+
currentStockPrice := 120000.0
17+
18+
change, _ := getStockPriceChange(prevStockPrice, currentStockPrice)
19+
20+
if change < 0 {
21+
fmt.Printf("The Stock Price decreased by $%.2f\n", math.Abs(change))
22+
} else {
23+
fmt.Printf("The Stock Price increased by $%.2f\n", change)
24+
}
25+
}
+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package main
2+
3+
import "fmt"
4+
5+
func main() {
6+
var x [5]int // An array of 5 integers
7+
fmt.Println(x)
8+
9+
var y [8]string // An array of 8 strings
10+
fmt.Println(y)
11+
12+
var z [3]complex128 // An array of 3 complex numbers
13+
fmt.Println(z)
14+
15+
// By default, all the array elements are assigned the zero value of the array type.
16+
// For example, if we declare an integer array, all the elements will be initialized with zero.
17+
// If we declare a string array, all the elements will be initialized with an empty string, and so on.
18+
}

07-arrays/02-array-indexing/main.go

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package main
2+
3+
import "fmt"
4+
5+
func main() {
6+
var x [5]int // An array of 5 integers
7+
8+
x[0] = 100
9+
x[1] = 101
10+
x[3] = 103
11+
x[4] = 105
12+
13+
fmt.Printf("x[0] = %d, x[1] = %d, x[2] = %d\n", x[0], x[1], x[2])
14+
fmt.Println("x = ", x)
15+
}
+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package main
2+
3+
import "fmt"
4+
5+
func main() {
6+
// Declaring and initializing an array at the same time
7+
var a = [5]int{2, 4, 6, 8, 10}
8+
fmt.Println(a)
9+
10+
// Short hand declaration for declaring and initializing an array
11+
b := [5]int{2, 4, 6, 8, 10}
12+
fmt.Println(b)
13+
14+
// You don't need to initialize all the elements of the array.
15+
// The un-initialized elements will be assigned the zero value of the corresponding array type
16+
c := [5]int{2}
17+
fmt.Println(c)
18+
19+
// Letting Go compiler infer the length of the array
20+
d := [...]int{3, 5, 7, 9, 11, 13, 17}
21+
fmt.Println(d)
22+
}
+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package main
2+
3+
import "fmt"
4+
5+
func main() {
6+
a1 := [5]string{"English", "Japanese", "Spanish", "French", "Hindi"}
7+
a2 := a1 // A copy of the array `a1` is assigned to `a2`
8+
9+
a2[1] = "German"
10+
11+
fmt.Println("a1 = ", a1) // The array `a1` remains unchanged
12+
fmt.Println("a2 = ", a2)
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package main
2+
3+
import "fmt"
4+
5+
func main() {
6+
// Iterating over and Array and printing its elements
7+
names := [3]string{"Mark Zuckerberg", "Bill Gates", "Larrt Page"}
8+
9+
for i := 0; i < len(names); i++ {
10+
fmt.Println(names[i])
11+
}
12+
13+
// Finding the Sum of an Array
14+
a := [4]float64{3.5, 7.2, 4.8, 9.5}
15+
sum := float64(0)
16+
17+
for i := 0; i < len(a); i++ {
18+
sum = sum + a[i]
19+
}
20+
21+
fmt.Printf("Sum of all the elements in array %v = %f\n", a, sum)
22+
}
+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package main
2+
3+
import "fmt"
4+
5+
func main() {
6+
// Iterating over an array using range form of for loop
7+
daysOfWeek := [7]string{"Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"}
8+
9+
for index, value := range daysOfWeek {
10+
fmt.Printf("Day %d of week = %s\n", index, value)
11+
}
12+
13+
// Finding the sum of an array
14+
a := [4]float64{3.5, 7.2, 4.8, 9.5}
15+
sum := float64(0)
16+
17+
for _, value := range a {
18+
sum = sum + value
19+
}
20+
21+
fmt.Printf("Sum of all the elements in array %v = %f\n", a, sum)
22+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package main
2+
3+
import "fmt"
4+
5+
func main() {
6+
a := [2][2]int{
7+
{3, 5},
8+
{7, 9}, // This comma is necessary
9+
}
10+
fmt.Println(a)
11+
12+
// Just like 1D arrays, you don't need to initialize all the elements in a multi-dimensional array.
13+
// Un-initialized array elements will be assigned the zero value of the array type
14+
b := [3][4]float64{
15+
{1, 3},
16+
{4.5, -3, 7.4, 2},
17+
{6, 2, 11},
18+
}
19+
fmt.Println(b)
20+
}

08-slices/01-create-slice/main.go

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package main
2+
3+
import "fmt"
4+
5+
func main() {
6+
// Creating a slice using a slice literal
7+
var s = []int{3, 5, 7, 9, 11, 13, 17} // Creates an array, and returns a slice reference to the array
8+
9+
// Short hand declaration
10+
t := []int{2, 4, 8, 16, 32, 64}
11+
12+
fmt.Println("s = ", s)
13+
fmt.Println("t = ", t)
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package main
2+
3+
import "fmt"
4+
5+
func main() {
6+
var a = [5]string{"Alpha", "Beta", "Gamma", "Delta", "Epsilon"}
7+
8+
// Creating a slice from the array
9+
var s []string = a[1:4]
10+
11+
fmt.Println("Array a = ", a)
12+
fmt.Println("Slice s = ", s)
13+
14+
/*
15+
low and high parameters are optional in a[low:high]
16+
The default value for low is 0, and high is the length of the slice.
17+
*/
18+
slice1 := a[1:4]
19+
slice2 := a[:3]
20+
slice3 := a[2:]
21+
slice4 := a[:]
22+
23+
fmt.Println("slice1 = ", slice1)
24+
fmt.Println("slice2 = ", slice2)
25+
fmt.Println("slice3 = ", slice3)
26+
fmt.Println("slice4 = ", slice4)
27+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package main
2+
3+
import "fmt"
4+
5+
func main() {
6+
cities := []string{"New York", "London", "Chicago", "Beijing", "Delhi", "Mumbai", "Bangalore", "Hyderabad", "Hong Kong"}
7+
8+
asianCities := cities[3:]
9+
indianCities := asianCities[1:5]
10+
11+
fmt.Println("cities = ", cities)
12+
fmt.Println("asianCities = ", asianCities)
13+
fmt.Println("indianCities = ", indianCities)
14+
}

08-slices/04-slice-modify/main.go

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package main
2+
3+
import "fmt"
4+
5+
func main() {
6+
a := [7]string{"Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"}
7+
8+
slice1 := a[1:]
9+
slice2 := a[3:]
10+
11+
fmt.Println("------- Before Modifications -------")
12+
fmt.Println("a = ", a)
13+
fmt.Println("slice1 = ", slice1)
14+
fmt.Println("slice2 = ", slice2)
15+
16+
slice1[0] = "TUE"
17+
slice1[1] = "WED"
18+
slice1[2] = "THU"
19+
20+
slice2[1] = "FRIDAY"
21+
22+
fmt.Println("\n-------- After Modifications --------")
23+
fmt.Println("a = ", a)
24+
fmt.Println("slice1 = ", slice1)
25+
fmt.Println("slice2 = ", slice2)
26+
}
+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package main
2+
3+
import "fmt"
4+
5+
func main() {
6+
/*
7+
The length of the slice is the number of elements in the slice.
8+
The capacity is the number of elements in the underlying array starting from the first element in the slice.
9+
*/
10+
a := [6]int{10, 20, 30, 40, 50, 60}
11+
s := a[1:4]
12+
13+
fmt.Printf("s = %v, len = %d, cap = %d\n", s, len(s), cap(s))
14+
}
+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package main
2+
3+
import "fmt"
4+
5+
func main() {
6+
s := []int{10, 20, 30, 40, 50, 60, 70, 80, 90, 100}
7+
fmt.Println("Original Slice")
8+
fmt.Printf("s = %v, len = %d, cap = %d\n", s, len(s), cap(s))
9+
10+
s = s[1:5]
11+
fmt.Println("\nAfter slicing from index 1 to 5")
12+
fmt.Printf("s = %v, len = %d, cap = %d\n", s, len(s), cap(s))
13+
14+
s = s[:8]
15+
fmt.Println("\nAfter extending the length")
16+
fmt.Printf("s = %v, len = %d, cap = %d\n", s, len(s), cap(s))
17+
18+
s = s[2:]
19+
fmt.Println("\nAfter dropping the first two elements")
20+
fmt.Printf("s = %v, len = %d, cap = %d\n", s, len(s), cap(s))
21+
}

0 commit comments

Comments
 (0)