Compare commits

...

10 Commits

Author SHA1 Message Date
289fc4b56c new start 2025-10-03 07:45:59 +00:00
3c2299b90f new commit 2025-06-11 11:04:27 +00:00
eba1e3ec49 exercices of section 13 2025-02-05 12:17:08 +01:00
ec3676ddd4 last 2 lectures of section 12
last 2 lectures of section 12
2025-02-05 11:37:23 +01:00
248388f5de Section12 - 94 2023-11-29 12:42:04 +01:00
1c925bc32a Section11 Complete 2023-11-23 13:01:04 +01:00
a2dc92f941 Section11-5 2023-11-17 13:28:33 +01:00
12c600ef09 Section11-2 2023-11-15 15:18:17 +01:00
110aab4934 Section10-Finished 2023-11-15 14:18:47 +01:00
f0c2e76a4f Section10-82 2023-11-09 14:14:28 +01:00
23 changed files with 728 additions and 0 deletions

22
Section10/81.go Normal file
View File

@@ -0,0 +1,22 @@
package main
import "fmt"
func main() {
numbers := []int{2, 3}
numbers = append(numbers, 10)
fmt.Println(numbers)
numbers = append(numbers, 20, 30, 40)
fmt.Println(numbers)
n := []int{100, 200}
numbers = append(numbers, n...)
fmt.Println(numbers)
src := []int{10, 20, 30}
dst := make([]int, len(src))
nn := copy(dst, src)
fmt.Println(src, dst, nn)
}

22
Section10/82.go Normal file
View File

@@ -0,0 +1,22 @@
package main
import "fmt"
func main() {
a := [5]int{1, 2, 3, 4, 5}
b := a[1:3]
fmt.Printf("%v, %T\n", b, b)
s1 := []int{1, 2, 3, 4, 5, 6}
s2 := s1[1:3]
fmt.Println(s2)
fmt.Println(s1[2:])
fmt.Println(s1[:3])
fmt.Println(s1[:])
s1 = append(s1[:4], 100)
fmt.Println(s1)
s1 = append(s1[:4], 200)
fmt.Println(s1)
}

54
Section10/84.go Normal file
View File

@@ -0,0 +1,54 @@
package main
import (
"fmt"
"unsafe"
)
func main() {
// s1 := []int{10, 20, 30, 40, 50}
// s3, s4 := s1[0:2], s1[1:3]
// s3[1] = 600
// fmt.Println(s1)
// fmt.Println(s4)
// arr1 := [4]int{10, 20, 30, 40}
// slice1, slice2 := arr1[0:2], arr1[1:3]
// arr1[1] = 2
// fmt.Println(slice1)
// fmt.Println(slice2)
// fmt.Println(arr1)
cars := []string{"Ford", "Honda", "Audi", "Range Rover"}
newCars := []string{}
newCars = append(newCars, cars[0:2]...)
cars[0] = "Nissan"
fmt.Println(cars, newCars)
s1 := []int{10, 20, 30, 40, 50}
newSlice := s1[0:3]
fmt.Println(len(newSlice), cap(newSlice))
newSlice = s1[2:5]
fmt.Println(len(newSlice), cap(newSlice))
fmt.Printf("%p\n", &s1)
fmt.Printf("%p %p \n", &s1, &newSlice)
newSlice[0] = 1000
fmt.Println("s1: ", s1)
a := [5]int{1, 2, 3, 4, 5}
s := []int{1, 2, 3, 4, 5}
fmt.Printf("array's size in bytes: %d \n", unsafe.Sizeof(a))
fmt.Printf("slice's size in bytes: %d \n", unsafe.Sizeof(s))
}

32
Section10/87.go Normal file
View File

@@ -0,0 +1,32 @@
package main
import "fmt"
func main() {
var nums []int
fmt.Printf("%#v\n", nums)
fmt.Printf("Lenght: %d, Capacity: %d \n", len(nums), cap(nums))
nums = append(nums, 1, 2)
fmt.Printf("Lenght: %d, Capacity: %d \n", len(nums), cap(nums))
nums = append(nums, 3)
fmt.Printf("Lenght: %d, Capacity: %d \n", len(nums), cap(nums))
nums = append(nums, 4)
fmt.Printf("Lenght: %d, Capacity: %d \n", len(nums), cap(nums))
nums = append(nums, 5)
fmt.Printf("Lenght: %d, Capacity: %d \n", len(nums), cap(nums))
nums = append(nums[0:4], 200, 300, 400, 500, 600)
fmt.Printf("Lenght: %d, Capacity: %d \n", len(nums), cap(nums))
fmt.Println(nums)
letters := []string{"A", "B", "C", "D", "E", "F"}
letters = append(letters[:1], "X", "Y")
fmt.Println(letters, len(letters), cap(letters))
fmt.Println(letters[3:6])
}

21
Section11/89-1.go Normal file
View File

@@ -0,0 +1,21 @@
/*
Coding Exercise #1
Using a composite literal declare and initialize
a slice of type string with 3 elements.
Iterate over the slice and print each element in
the slice and its index.
*/
package main
import "fmt"
func main() {
countries := []string{"Romania", "Brazil", "Germany"}
for i, v := range countries {
fmt.Printf("Index: %d, Element: %q\n", i, v)
}
}

45
Section11/89-2.go Normal file
View File

@@ -0,0 +1,45 @@
/*
Coding Exercise #2
There are some errors in the following Go program. Try to identify the errors,
change the code and run the program without errors.
package main
import "fmt"
func main() {
mySlice := []float64{1.2, 5.6}
mySlice[2] = 6
a := 10
mySlice[0] = a
mySlice[3] = 10.10
mySlice = append(mySlice, a)
fmt.Println(mySlice)
}
*/
package main
import "fmt"
func main() {
mySlice := []float64{1.2, 5.6}
mySlice[1] = 6
a := 10
mySlice[0] = float64(a)
mySlice[1] = 10.10
mySlice = append(mySlice, float64(a))
fmt.Println(mySlice)
}

32
Section11/89-3.go Normal file
View File

@@ -0,0 +1,32 @@
/*
Coding Exercise #3
1. Declare a slice called nums with three float64 numbers.
2. Append the value 10.1 to the slice
3. In one statement append to the slice the values: 4.1, 5.5 and 6.6
4. Print out the slice
5. Declare a slice called n with two float64 values
6. Append n to nums
7. Print out the nums slice
*/
package main
import "fmt"
func main() {
nums := []float64{1.1, 2.1, 3.1}
nums = append(nums, 10.1)
fmt.Println(nums)
nums = append(nums, 4.1, 5.5, 6.6)
fmt.Println(nums)
n := []float64{8.1, 9.3}
nums = append(nums, n...)
fmt.Println(nums)
}

58
Section11/89-4.go Normal file
View File

@@ -0,0 +1,58 @@
/*
Create a Go program that reads some numbers from the command line and then
calculates the sum and the product of all the numbers given at the command line.
The user should give between 2 and 10 numbers.
Notes:
- the program should be run in a terminal (go run main.go) not in Go Playground
- example:
go run main.go 3 2 5
Expected output: Sum: 10, Product: 30
*/
// run this program in a terminal with arguments
// ex: go run main.go 5 7.1 9.9 10
package main
import (
"fmt"
"log"
"os"
"strconv"
)
func main() {
if len(os.Args) < 2 { //if not run with arguments
log.Fatal("Please run the program with arguments (2-10 numbers)!")
}
//taking the arguments in a new slice
args := os.Args[1:]
// declaring and initializing sum and product of type float64
sum, product := 0., 1.
if len(args) < 2 || len(args) > 10 {
fmt.Println("Please enter between 2 and 10 numbers!")
} else {
for _, v := range args {
num, err := strconv.ParseFloat(v, 64)
if err != nil {
continue //if it can't convert string to float64, continue with the next number
}
sum += num
product *= num
}
}
fmt.Printf("Sum: %v, Product: %v\n", sum, product)
}

24
Section11/89-5.go Normal file
View File

@@ -0,0 +1,24 @@
/*
Consider the following slice declaration:
nums := []int{5, -1, 9, 10, 1100, 6, -1, 6}
Using a slice expression and a for loop iterate over
the slice ignoring the first and the last two elements.
Print those elements and their sum.
*/
package main
import "fmt"
func main() {
nums := []int{5, -1, 9, 10, 1100, 6, -1, 6}
sum := 0
for _, v := range nums[2 : len(nums)-2] {
fmt.Println(v)
sum += v
}
fmt.Println("Sum:", sum)
}

23
Section11/89-6.go Normal file
View File

@@ -0,0 +1,23 @@
/*
Consider the following slice declaration:
friends := []string{"Marry", "John", "Paul", "Diana"}
Using copy() function create a copy of the slice. Prove that the slices
are not connected by modifying one slice and notice that the other slice is not modified.
*/
package main
import "fmt"
func main() {
friends := []string{"Marry", "John", "Paul", "Diana"}
yourFriends := make([]string, len(friends))
copy(yourFriends, friends)
yourFriends[0] = "Dan"
fmt.Println(friends, yourFriends)
}

24
Section11/89-7.go Normal file
View File

@@ -0,0 +1,24 @@
/*
Consider the following slice declaration:
friends := []string{"Marry", "John", "Paul", "Diana"}
Using append() function create a copy of the slice.
Prove that the slices are not connected by modifying
one slice and notice that the other slice is not modified.
*/
package main
import "fmt"
func main() {
friends := []string{"Marry", "John", "Paul", "Diana"}
yourFriends := []string{}
yourFriends = append(yourFriends, friends...)
yourFriends[0] = "Dan"
fmt.Println(friends, yourFriends)
}

23
Section11/89-8.go Normal file
View File

@@ -0,0 +1,23 @@
/*
Consider the following slice declaration:
years := []int{2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010}
Using a slice expression and append() function create a new slice called
newYears that contains the first 3 and the last 3 elements of the slice.
newYears should be []int{2000, 2001, 2002, 2008, 2009, 2010}
*/
package main
import "fmt"
func main() {
years := []int{2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010}
newYears := []int{}
newYears = append(years[:3], years[len(years)-3:]...)
fmt.Printf("%#v\n", newYears)
}

32
Section12/90.go Normal file
View File

@@ -0,0 +1,32 @@
package main
import "fmt"
func main() {
s1 := "Hi there Go!"
fmt.Println(s1)
fmt.Println("He sais: \"Hello!\"")
fmt.Println(`He sais: "Hello again!"`)
s2 := `I like \n Go!` //raw string
fmt.Println(s2)
fmt.Println("Price: 100\nBrand: Nike")
fmt.Println(`
Price: 100
Brand: Nike
`)
fmt.Println(`C:\Users\Sebastian`)
fmt.Println("C:\\Users\\Sebastian")
var s3 = "I love " + "Go " + "Programming"
fmt.Println(s3 + "!")
fmt.Println("Element at index 0:", s3[0])
// s3[5] = 'x'
fmt.Printf("%s\n", s3)
fmt.Printf("%q\n", s3)
}

29
Section12/93.go Normal file
View File

@@ -0,0 +1,29 @@
package main
import (
"fmt"
"strings"
"unicode/utf8"
)
func main() {
var1, var2 := 'a', 'b'
fmt.Printf("Type: %T, Value: %d\n", var1, var2)
str := "țară"
fmt.Println(len(str))
fmt.Println("Byte (non rune) at position 1:", str[1])
for i := 0; i < len(str); i++ {
fmt.Printf("%c", str[i])
}
fmt.Println("\n" + strings.Repeat("#", 20))
for i := 0; i < len(str); {
r, size := utf8.DecodeRuneInString(str[i:])
fmt.Printf("%c", r)
i += size
}
fmt.Println("\n" + strings.Repeat("#", 20))
}

18
Section12/94.go Normal file
View File

@@ -0,0 +1,18 @@
package main
import (
"fmt"
"unicode/utf8"
)
func main() {
s1 := "Golang"
fmt.Println(len(s1))
name := "Codruța"
fmt.Println(len(name))
n := utf8.RuneCountInString(name)
fmt.Println(n)
}

19
Section12/96.go Normal file
View File

@@ -0,0 +1,19 @@
package main
import (
"fmt"
)
func main() {
s1 := "I love Golang!"
fmt.Println(s1[2:5])
s2 := "我爱戈兰"
fmt.Println(s2[0:2])
rs := []rune(s2)
fmt.Printf("%T\n", rs)
fmt.Println(string(rs[0:3]))
}

64
Section12/98.go Normal file
View File

@@ -0,0 +1,64 @@
package main
import (
"fmt"
"strings"
)
func main() {
p := fmt.Println
result := strings.Contains("I love Go Programming!", "love")
p(result)
result = strings.ContainsAny("success", "xys")
p(result)
result = strings.ContainsRune("golang", 'g')
p(result)
n := strings.Count("cheese", "e")
p(n)
n = strings.Count("Five", "")
p(n)
p(strings.ToLower("GO PyTHON jaVA"))
p(strings.ToUpper("GO PyTHON jaVA"))
p("go" == "go")
p("GO" == "go")
p(strings.ToLower("GO") == strings.ToLower("go"))
p(strings.EqualFold("GO", "go"))
myStr := strings.Repeat("ab", 10)
p(myStr)
myStr = strings.Replace("192.168.0.1", ".", ":", -1)
p(myStr)
myStr = strings.ReplaceAll("192.168.0.1", ".", ":")
p(myStr)
s := strings.Split("a,b,c", ",")
fmt.Printf("%T\n", s)
fmt.Printf("%#v\n", s)
s = strings.Split("Go for Go!", "")
fmt.Printf("%#v\n", s)
s = []string{"I", "learn", "Golang"}
myStr = strings.Join(s, "-")
p(myStr)
myStr = "Orange Green \n Blue Yellow"
fields := strings.Fields(myStr)
fmt.Printf("%T\n", fields)
fmt.Printf("%#v\n", fields)
s1 := strings.TrimSpace("\t Goodbye Windows, Welcome Linux!\n ")
fmt.Printf("%q\n", s1)
s2 := strings.Trim("...Hello, Gophers!!!?", ".!?")
fmt.Printf("%q\n", s2)
}

39
Section13/1.go Normal file
View File

@@ -0,0 +1,39 @@
/*
Coding Exercise #1
1. Using the var keyword declare a string called name and initialize it with your name.
2. Using short declaration syntax declare a string called country and assign the country you are living in to the string variable.
3. Print the following string on multiple lines like this:
Your name: `here the value of name variable`
Country: `here the value of country variable`
4. Print out the following strings:
a) He says: "Hello"
b) C:\Users\a.txt
*/
package main
import "fmt"
func main() {
var name string = "Andrei"
country := "Romania"
fmt.Printf("Your name: %s\nCountry: %s\n", name, country)
//equivalent to:
fmt.Printf(`Your name: %s
Country: %s
`, name, country)
fmt.Println("He says: \"Hello\"")
fmt.Println("C:\\Users\\a.txt")
}

29
Section13/2.go Normal file
View File

@@ -0,0 +1,29 @@
/*
1. Declare a rune called r that stores the non-ascii letter ă
2. Print out the type of r
3. Declare 2 strings that contains the values ma and m
4. Concatenate the strings and the rune in a new string (the new string will hold the value mamă ).
BTW: mamă means mother in Romanian.
Note: You should convert rune to string to contatenate it to another string.
*/
package main
import "fmt"
func main() {
r := 'ă' // declaring a rune
fmt.Printf("r type:%T\n", r) // rune is alias to int32
s1, s2 := "ma", "m" // declaring 2 strings
// concatenating strings
s := s1 + s2 + string(r) // converting rune to string (expliction conversion is required)
fmt.Printf("s is %s\n", s) // => s is mamă
}

35
Section13/3.go Normal file
View File

@@ -0,0 +1,35 @@
/*
Consider the following string declaration: s1 := "țară means country in Romanian"
1. Iterate over the string and print out byte by byte
2. Iterate over the string and print out rune by rune and the byte index where the rune starts in the string
Are you stuck? Do you want to see the solution for this exercise?
*/
package main
import "fmt"
func main() {
s1 := "țară means country in Romanian"
// iterating over the string and print out byte by byte
fmt.Printf("Bytes in string: ")
for i := 0; i < len(s1); i++ {
fmt.Printf("%v ", s1[i])
}
fmt.Println()
// iterating over the string and print out rune by rune
// and the byte index where the rune starts in the string
for i, r := range s1 {
fmt.Printf("byte index: %d -> rune: %c\n", i, r)
}
fmt.Println()
}

21
Section13/4.go Normal file
View File

@@ -0,0 +1,21 @@
package main
import (
"fmt"
"unicode/utf8"
)
func main() {
s1 := "Go is cool!"
x := s1[0]
fmt.Println(x)
// ERROR -> cannot assign to s1[0]
// s1[0] = 'x'
// printing the number of runes in the string
// fmt.Println(len(s1))
fmt.Println(utf8.RuneCountInString(s1))
}

31
Section13/5.go Normal file
View File

@@ -0,0 +1,31 @@
/*
Consider the following string declaration:s := "你好 Go!"
1. Convert the string to a byte slice.
2. Print out the byte slice
3. Iterate over the byte slice and print out each index and byte in the byte slice
*/
package main
import "fmt"
func main() {
s := "你好 Go!"
// converting string to byte slice
b := []byte(s)
// printing out the byte slice
fmt.Printf("%#v\n", b)
// iterating over the byte slice and printing out each index and byte in the byte slice
for i, v := range b {
fmt.Printf("%d -> %d\n", i, v)
}
}

31
Section13/6.go Normal file
View File

@@ -0,0 +1,31 @@
/*
Consider the following string declaration:s := "你好 Go!"
1. Convert the string to a rune slice.
2. Print out the rune slice
3. Iterate over the rune slice and print out each index and rune in the rune slice
*/
package main
import "fmt"
func main() {
s := "你好 Go!"
// converting string to rune slice
r := []rune(s)
// printing out the rune slice
fmt.Printf("%#v\n", r)
// iterating over the rune slice and printing out each index and rune in the rune slice
for i, v := range r {
fmt.Printf("%d -> %c\n", i, v)
}
}