1
0
Fork 0

Compare commits

...

2 Commits

Author SHA1 Message Date
ChaosLL1337 78cbe47176 Unterricht stunde 28.10 2026-10-28 10:07:02 +00:00
ChaosLL1337 bf881b9671 Beginn aufgabe Design Pattern 2026-10-22 09:37:18 +00:00
9 changed files with 218 additions and 8 deletions

View File

@ -1,4 +1,4 @@
module gitty.informatik.th-mannheim.de/steger/pr3-ws202627/go/03-modules
module gitty.informatik.th-mannheim.de/3010261/pr3-ws202627/go/03-modules
go 1.25.0

View File

@ -3,7 +3,7 @@ package myMath_test
import (
"testing"
"gitty.informatik.hs-mannheim.de/steger/pr3-code/go/03-modules/myMath"
"gitty.informatik.hs-mannheim.de/3010261/pr3-code/go/03-modules/myMath"
)
func TestAdd(t *testing.T) {

View File

@ -0,0 +1,3 @@
module gitty.informatik.th-mannheim.de/3010261/pr3-ws202627/go/04-design-pattern
go 1.27.3

View File

@ -0,0 +1,29 @@
package main
import (
"fmt"
"gitty.informatik.th-mannheim.de/3010261/pr3-ws202627/go/04-design-pattern/patterns"
)
func main() {
noodle, err := patterns.NewNoodleMaker().
Name("Spaghetti").
Type("Lang").
Length(25).
Girth(2).
Build()
if err != nil {
fmt.Println("Fehler:", err)
return
}
fmt.Printf("%+v\n", noodle)
standard, _ := patterns.NewNoodleMaker().Build()
fmt.Printf("%+v\n", standard)
_, err = patterns.NewNoodleMaker().Length(0).Build()
if err != nil {
fmt.Println("Erwarteter Fehler:", err)
}
}

View File

@ -0,0 +1,48 @@
package patterns
import (
"errors"
)
type Noodle struct {
Type string
Name string
Length int
Girth int
}
type NoodleMaker struct {
noodle Noodle
}
func NewNoodleMaker() *NoodleMaker {
return &NoodleMaker{
noodle: Noodle{"Röhre", "Fusilli", 10, 5},
}
}
func (b *NoodleMaker) Length(l int) *NoodleMaker {
b.noodle.Length = l
return b
}
func (c *NoodleMaker) Girth(g int) *NoodleMaker {
c.noodle.Girth = g
return c
}
func (d *NoodleMaker) Name(n string) *NoodleMaker {
d.noodle.Name = n
return d
}
func (e *NoodleMaker) Type(t string) *NoodleMaker {
e.noodle.Type = t
return e
}
func (m *NoodleMaker) Build() (Noodle, error) {
if m.noodle.Length <= 0 {
return Noodle{}, errors.New("Länge muss größer als 0 sein")
}
if m.noodle.Girth <= 0 {
return Noodle{}, errors.New("Dicke muss größer als 0 sein")
}
return m.noodle, nil
}

View File

@ -0,0 +1,48 @@
package patterns
import (
"testing"
)
func TestNoodleMaker(t *testing.T) {
tests := []struct {
name string
build func() (Noodle, error)
want Noodle
wantErr bool
}{
{
name: "Defaults",
build: func() (Noodle, error) { return newNoodleMaker().Build() },
want: Noodle{Type: "Röhre", Name: "Fusilli", Length: 10, Girth: 5},
},
{
name: "New Noodle",
build: func() (Noodle, error) {
return newNoodleMaker().Name("Spaghetti").Type("Lang").Length(25).Girth(2).Build()
},
want: Noodle{Type: "Lang", Name: "Spaghetti", Length: 25, Girth: 2},
},
{
name: "Length 0",
build: func() (Noodle, error) { return newNoodleMaker().Length(0).Build() },
wantErr: true,
},
{
name: "Girth -1",
build: func() (Noodle, error) { return newNoodleMaker().Girth(-1).Build() },
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := tt.build()
if (err != nil) != tt.wantErr {
t.Fatalf("error = %v, wantErr %v", err, tt.wantErr)
}
if !tt.wantErr && got != tt.want {
t.Errorf("got %+v, want %+v", got, tt.want)
}
})
}
}

View File

@ -1,17 +1,27 @@
package main
import (
"fmt"
"time"
)
func print(ci <-chan int) {
//TODO: implement
for value := range ci {
time.Sleep(100 * time.Millisecond)
fmt.Println(value)
}
}
func main() {
c := make(chan int)
done := make(chan bool)
go func() { print(c); done <- true; close(done) }()
c <- 1
c <- 2
c <- 3
close(c)
print(c)
<-done
}

View File

@ -4,6 +4,7 @@ import (
"fmt"
"math/rand"
"sort"
"sync"
"time"
)
@ -22,6 +23,54 @@ func mergeSortSequential(data []float64) {
copy(data, merge(left, right))
}
func mergeSortParallel(data []float64) {
if len(data) <= 1000 {
mergeSortSequential(data)
return
}
mid := len(data) / 2
left := data[:mid]
right := data[mid:]
var wg sync.WaitGroup
wg.Add(2)
go func() { mergeSortParallel(left); wg.Done() }()
go func() { mergeSortParallel(right); wg.Done() }()
wg.Wait()
copy(data, merge(left, right))
}
func mergeSortChannelsInternal(data []float64, sorted chan<- float64) {
if len(data) < 1000 {
sortedData := make([]float64, len(data))
copy(sortedData, data)
mergeSortSequential(sortedData)
for _, := 0; i < count; i++ {
}
close(sorted)
}
}
func mergeSortChannels(data []float64) {
sorted := make(chan float64)
go mergeSortChannelsInternal(data, sorted)
sortedData := make([]float64, len(data))
i := 0
for x := range sorted {
sortedData[i] = x
i++
}
copy(data, sortedData)
}
func merge(left, right []float64) []float64 {
result := make([]float64, 0, len(left)+len(right))
i, j := 0, 0
@ -58,7 +107,8 @@ func main() {
//2. SORTING
start := time.Now()
mergeSortSequential(data)
//mergeSortSequential(data)
mergeSortParallel(data)
elapsed := time.Since(start)
fmt.Printf("MergeSort took %s\n", elapsed)

View File

@ -1,6 +1,7 @@
package main
import (
"fmt"
"math/rand"
"time"
)
@ -8,12 +9,33 @@ import (
func main() {
ch1 := make(chan string)
ch2 := make(chan string)
go func() {
time.Sleep(time.Duration(rand.Intn(5)+1) * time.Second)
ch1 <- "Message from channel 1"
}()
msg1 := <-ch1
println("Received:", msg1)
go func() {
time.Sleep(time.Duration(rand.Intn(5)+1) * time.Second)
ch2 <- "Message from channel 2"
}()
count := 10
var channels []chan string
for i := range count {
ch := make(chan string)
channels = append(channels, ch)
go func() {
time.Sleep(time.Duration(rand.Intn(5)+1) * time.Second)
ch <- fmt.Sprintln("Message from Channel %d", i)
}()
}
fanIn := make(chan string, count)
for i := range count {
go func() { fanIn <- <-channels[i] }()
}
println("Recieved: ", <-fanIn)
}