push all exercices

This commit is contained in:
lbenedar
2026-02-21 14:09:13 +03:00
commit a94d994b8c
72 changed files with 787 additions and 0 deletions

BIN
ch8/1/ch8_1.exe Normal file

Binary file not shown.

27
ch8/1/ch8_1.go Normal file
View File

@@ -0,0 +1,27 @@
package main
import "fmt"
type Number interface {
~int | ~int8 | ~int16 | ~int32 | ~int64 | ~uint | ~uint8 | ~uint16 |
~uint32 | ~uint64 | ~float32 | ~float64
}
func NumDoubler[T Number](val T) T {
return val * 2
}
func main() {
val := 10.3
fmt.Println(val)
val = NumDoubler(val)
fmt.Println(val)
val = NumDoubler(val)
fmt.Println(val)
val = NumDoubler(val)
fmt.Println(val)
val = NumDoubler(val)
fmt.Println(val)
val = NumDoubler(val)
fmt.Println(val)
}

3
ch8/1/go.mod Normal file
View File

@@ -0,0 +1,3 @@
module ch8_1
go 1.25.0

BIN
ch8/2/ch8_2.exe Normal file

Binary file not shown.

38
ch8/2/ch8_2.go Normal file
View File

@@ -0,0 +1,38 @@
package main
import (
"fmt"
"strconv"
)
type Printable interface {
Number | NumberFloat
fmt.Stringer
}
type Number struct {
val int
}
func (num Number) String() string {
return string(strconv.Itoa(num.val))
}
type NumberFloat struct {
val float64
}
func (num NumberFloat) String() string {
return string(strconv.Itoa(int(num.val)))
}
func Print[T Printable](num T) {
fmt.Println(num)
}
func main() {
firstTest := Number{val: 5}
secondTest := NumberFloat{val: 7.2}
Print(firstTest)
Print(secondTest)
}

3
ch8/2/go.mod Normal file
View File

@@ -0,0 +1,3 @@
module ch8_2
go 1.25.0

BIN
ch8/3/ch8_3.exe Normal file

Binary file not shown.

91
ch8/3/ch8_3.go Normal file
View File

@@ -0,0 +1,91 @@
package main
import "fmt"
type Node[T comparable] struct {
val T
next *Node[T]
}
func (elem *Node[T]) Add(newVal T) {
currPos := elem.GoToEnd()
currPos.next = &Node[T]{val: newVal, next: nil}
}
func (elem *Node[T]) GoToEnd() *Node[T] {
currPos := elem
for currPos.next != nil {
currPos = currPos.next
}
return currPos
}
func (elem *Node[T]) GoToPosition(len int) *Node[T] {
currPos := elem
index := 0
for currPos.next != nil && index != len {
currPos = currPos.next
index++
}
return currPos
}
func (elem *Node[T]) Insert(newVal T, index int) {
currPos := elem.GoToPosition(index - 1)
nextNode := currPos.next
currPos.next = &Node[T]{val: newVal, next: nextNode}
}
func (elem Node[T]) Index(cmpVal T) int {
currPos := &elem
index := 0
for currPos != nil && currPos.val != cmpVal {
currPos = currPos.next
index++
}
if currPos == nil {
return -1
}
return index
}
func (elem Node[T]) GoToEndWithOutput() {
currPos := elem
for currPos.next != nil {
fmt.Println(currPos)
currPos = *currPos.next
}
fmt.Println(currPos)
}
func main() {
linkedList := Node[int]{val: 12}
linkedList.Add(4)
linkedList.GoToEndWithOutput()
fmt.Println()
linkedList.Add(7)
linkedList.GoToEndWithOutput()
fmt.Println()
linkedList.Add(22)
linkedList.GoToEndWithOutput()
fmt.Println()
linkedList.Add(65)
linkedList.GoToEndWithOutput()
fmt.Println()
linkedList.Add(2)
linkedList.GoToEndWithOutput()
fmt.Println()
linkedList.Add(3)
linkedList.GoToEndWithOutput()
fmt.Println()
linkedList.Add(1)
linkedList.GoToEndWithOutput()
fmt.Println()
linkedList.Insert(10, 4)
linkedList.GoToEndWithOutput()
fmt.Println()
linkedList.Insert(11, 5)
linkedList.GoToEndWithOutput()
fmt.Println()
fmt.Println(linkedList.Index(11))
}

3
ch8/3/go.mod Normal file
View File

@@ -0,0 +1,3 @@
module ch8_3
go 1.25.0