solve lab1a in go
This commit is contained in:
commit
93359bf0e1
42
lab1/IF-1-1_PuzonasR_L1_dat_1.json
Normal file
42
lab1/IF-1-1_PuzonasR_L1_dat_1.json
Normal file
@ -0,0 +1,42 @@
|
||||
[
|
||||
{"name":"Fruit juice 76","sugar":193.65132,"criteria":145},
|
||||
{"name":"Ice cream 16","sugar":186.08167,"criteria":132},
|
||||
{"name":"Smoothie 27","sugar":196.04042,"criteria":157},
|
||||
{"name":"Apple 88","sugar":142.26443,"criteria":82},
|
||||
{"name":"Ice cream 73","sugar":164.25926,"criteria":109},
|
||||
{"name":"Ice cream 71","sugar":148.9654,"criteria":136},
|
||||
{"name":"Tea 94","sugar":146.7983,"criteria":114},
|
||||
{"name":"Candy 14","sugar":189.80835,"criteria":145},
|
||||
{"name":"Fruit juice 48","sugar":149.91238,"criteria":105},
|
||||
{"name":"Smoothie 25","sugar":146.42593,"criteria":97},
|
||||
{"name":"Apple 23","sugar":124.13099,"criteria":90},
|
||||
{"name":"Ice cream 48","sugar":132.76225,"criteria":97},
|
||||
{"name":"Candy 12","sugar":166.74808,"criteria":136},
|
||||
{"name":"Fruit juice 0","sugar":159.86632,"criteria":138},
|
||||
{"name":"Cake 50","sugar":172.98724,"criteria":121},
|
||||
{"name":"Cake 22","sugar":102.71184,"criteria":58},
|
||||
{"name":"Smoothie 51","sugar":169.24905,"criteria":146},
|
||||
{"name":"Apple 13","sugar":197.4303,"criteria":176},
|
||||
{"name":"Cake 47","sugar":132.49425,"criteria":96},
|
||||
{"name":"Smoothie 75","sugar":117.34118,"criteria":70},
|
||||
{"name":"Apple 65","sugar":105.77017,"criteria":78},
|
||||
{"name":"Fruit juice 7","sugar":136.12982,"criteria":123},
|
||||
{"name":"Fruit juice 8","sugar":107.79159,"criteria":74},
|
||||
{"name":"Apple 20","sugar":100.0637,"criteria":62},
|
||||
{"name":"Chocolate 26","sugar":159.88702,"criteria":135},
|
||||
{"name":"Fruit juice 91","sugar":195.36041,"criteria":174},
|
||||
{"name":"Cake 12","sugar":170.92297,"criteria":145},
|
||||
{"name":"Cake 48","sugar":104.69248,"criteria":45},
|
||||
{"name":"Smoothie 95","sugar":107.425224,"criteria":57},
|
||||
{"name":"Ice cream 43","sugar":154.7334,"criteria":112},
|
||||
{"name":"Smoothie 42","sugar":124.851166,"criteria":96},
|
||||
{"name":"Ice cream 61","sugar":191.13419,"criteria":173},
|
||||
{"name":"Ice cream 52","sugar":123.938934,"criteria":75},
|
||||
{"name":"Ice cream 4","sugar":113.318596,"criteria":54},
|
||||
{"name":"Fruit juice 26","sugar":151.61147,"criteria":137},
|
||||
{"name":"Candy 61","sugar":139.66057,"criteria":90},
|
||||
{"name":"Smoothie 23","sugar":176.65881,"criteria":142},
|
||||
{"name":"Cake 11","sugar":198.92917,"criteria":185},
|
||||
{"name":"Apple 83","sugar":159.48036,"criteria":114},
|
||||
{"name":"Fruit juice 29","sugar":112.71098,"criteria":65}
|
||||
]
|
42
lab1/IF-1-1_PuzonasR_L1_dat_2.json
Normal file
42
lab1/IF-1-1_PuzonasR_L1_dat_2.json
Normal file
@ -0,0 +1,42 @@
|
||||
[
|
||||
{"name":"Tea 35","sugar":102.67245,"criteria":153},
|
||||
{"name":"Candy 45","sugar":160.1899,"criteria":119},
|
||||
{"name":"Fruit juice 14","sugar":104.17689,"criteria":78},
|
||||
{"name":"Candy 87","sugar":132.45172,"criteria":84},
|
||||
{"name":"Fruit juice 12","sugar":116.65934,"criteria":142},
|
||||
{"name":"Cake 37","sugar":101.45675,"criteria":161},
|
||||
{"name":"Cake 78","sugar":174.44292,"criteria":121},
|
||||
{"name":"Smoothie 20","sugar":146.48544,"criteria":175},
|
||||
{"name":"Tea 45","sugar":154.54736,"criteria":121},
|
||||
{"name":"Tea 9","sugar":161.4415,"criteria":191},
|
||||
{"name":"Cake 32","sugar":124.795944,"criteria":182},
|
||||
{"name":"Chocolate 12","sugar":180.10109,"criteria":147},
|
||||
{"name":"Apple 6","sugar":101.775,"criteria":143},
|
||||
{"name":"Chocolate 29","sugar":144.85179,"criteria":169},
|
||||
{"name":"Fruit juice 81","sugar":156.3534,"criteria":208},
|
||||
{"name":"Cake 51","sugar":114.41123,"criteria":148},
|
||||
{"name":"Tea 16","sugar":167.4808,"criteria":196},
|
||||
{"name":"Apple 92","sugar":166.1134,"criteria":119},
|
||||
{"name":"Candy 15","sugar":184.1573,"criteria":210},
|
||||
{"name":"Ice cream 79","sugar":109.4283,"criteria":138},
|
||||
{"name":"Apple 33","sugar":137.14066,"criteria":114},
|
||||
{"name":"Chocolate 86","sugar":190.01999,"criteria":248},
|
||||
{"name":"Fruit juice 2","sugar":198.52423,"criteria":164},
|
||||
{"name":"Tea 82","sugar":159.13023,"criteria":118},
|
||||
{"name":"Smoothie 31","sugar":158.44916,"criteria":200},
|
||||
{"name":"Tea 89","sugar":172.12093,"criteria":146},
|
||||
{"name":"Apple 22","sugar":118.49223,"criteria":154},
|
||||
{"name":"Fruit juice 81","sugar":146.55879,"criteria":94},
|
||||
{"name":"Tea 62","sugar":114.96604,"criteria":150},
|
||||
{"name":"Ice cream 10","sugar":117.40653,"criteria":93},
|
||||
{"name":"Fruit juice 62","sugar":169.27748,"criteria":188},
|
||||
{"name":"Cake 1","sugar":198.42546,"criteria":180},
|
||||
{"name":"Chocolate 39","sugar":172.85019,"criteria":217},
|
||||
{"name":"Apple 21","sugar":142.42413,"criteria":105},
|
||||
{"name":"Ice cream 73","sugar":114.56453,"criteria":152},
|
||||
{"name":"Tea 34","sugar":116.19339,"criteria":105},
|
||||
{"name":"Fruit juice 57","sugar":189.37839,"criteria":154},
|
||||
{"name":"Chocolate 63","sugar":107.178024,"criteria":72},
|
||||
{"name":"Cake 19","sugar":158.35861,"criteria":206},
|
||||
{"name":"Ice cream 29","sugar":154.20683,"criteria":99}
|
||||
]
|
42
lab1/IF-1-1_PuzonasR_L1_dat_3.json
Normal file
42
lab1/IF-1-1_PuzonasR_L1_dat_3.json
Normal file
@ -0,0 +1,42 @@
|
||||
[
|
||||
{"name":"Tea 32","sugar":156.2579,"criteria":189},
|
||||
{"name":"Smoothie 75","sugar":138.6357,"criteria":162},
|
||||
{"name":"Fruit juice 34","sugar":195.00311,"criteria":252},
|
||||
{"name":"Fruit juice 37","sugar":100.18925,"criteria":133},
|
||||
{"name":"Fruit juice 53","sugar":154.46834,"criteria":180},
|
||||
{"name":"Tea 23","sugar":153.91217,"criteria":175},
|
||||
{"name":"Tea 71","sugar":192.72974,"criteria":203},
|
||||
{"name":"Tea 98","sugar":138.71013,"criteria":162},
|
||||
{"name":"Candy 8","sugar":149.59752,"criteria":160},
|
||||
{"name":"Candy 14","sugar":183.19849,"criteria":242},
|
||||
{"name":"Chocolate 16","sugar":130.96481,"criteria":165},
|
||||
{"name":"Smoothie 18","sugar":166.2304,"criteria":193},
|
||||
{"name":"Smoothie 73","sugar":132.69725,"criteria":151},
|
||||
{"name":"Apple 73","sugar":160.8539,"criteria":207},
|
||||
{"name":"Fruit juice 20","sugar":122.614525,"criteria":177},
|
||||
{"name":"Cake 12","sugar":182.76157,"criteria":236},
|
||||
{"name":"Chocolate 0","sugar":146.81622,"criteria":206},
|
||||
{"name":"Apple 99","sugar":182.31323,"criteria":211},
|
||||
{"name":"Candy 49","sugar":136.16162,"criteria":193},
|
||||
{"name":"Chocolate 56","sugar":128.53404,"criteria":145},
|
||||
{"name":"Fruit juice 44","sugar":177.52731,"criteria":217},
|
||||
{"name":"Ice cream 78","sugar":112.97374,"criteria":146},
|
||||
{"name":"Cake 42","sugar":107.52386,"criteria":123},
|
||||
{"name":"Cake 99","sugar":137.5299,"criteria":185},
|
||||
{"name":"Apple 48","sugar":194.78995,"criteria":231},
|
||||
{"name":"Fruit juice 61","sugar":109.11185,"criteria":138},
|
||||
{"name":"Chocolate 41","sugar":136.47095,"criteria":185},
|
||||
{"name":"Tea 95","sugar":133.09335,"criteria":179},
|
||||
{"name":"Fruit juice 59","sugar":168.67265,"criteria":208},
|
||||
{"name":"Candy 21","sugar":125.954384,"criteria":139},
|
||||
{"name":"Fruit juice 33","sugar":146.21214,"criteria":183},
|
||||
{"name":"Candy 48","sugar":196.9039,"criteria":249},
|
||||
{"name":"Tea 8","sugar":171.0748,"criteria":193},
|
||||
{"name":"Smoothie 74","sugar":111.57121,"criteria":124},
|
||||
{"name":"Apple 91","sugar":169.62463,"criteria":201},
|
||||
{"name":"Cake 12","sugar":115.3386,"criteria":173},
|
||||
{"name":"Fruit juice 62","sugar":174.87453,"criteria":226},
|
||||
{"name":"Fruit juice 60","sugar":152.54874,"criteria":175},
|
||||
{"name":"Apple 56","sugar":165.61108,"criteria":213},
|
||||
{"name":"Candy 3","sugar":136.42447,"criteria":175}
|
||||
]
|
81
lab1/gen-data.go
Normal file
81
lab1/gen-data.go
Normal file
@ -0,0 +1,81 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"log"
|
||||
"math/rand"
|
||||
"os"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
type DataEntry struct {
|
||||
Name string `json:"name"`
|
||||
Sugar float32 `json:"sugar"`
|
||||
Criteria int `json:"criteria"`
|
||||
}
|
||||
|
||||
var PossibleNames = []string{
|
||||
"Cake",
|
||||
"Candy",
|
||||
"Apple",
|
||||
"Fruit juice",
|
||||
"Chocolate",
|
||||
"Tea",
|
||||
"Smoothie",
|
||||
"Ice cream",
|
||||
};
|
||||
|
||||
func generateEntry(percent float64) DataEntry {
|
||||
entry := DataEntry{}
|
||||
nameIndex := rand.Uint32() % uint32(len(PossibleNames))
|
||||
entry.Name = fmt.Sprintf("%s %d", PossibleNames[nameIndex], rand.Uint32() % 100)
|
||||
entry.Sugar = rand.Float32() * 100 + 100
|
||||
if rand.Float64() < percent {
|
||||
entry.Criteria = int(entry.Sugar - rand.Float32() * 50 - 10)
|
||||
} else {
|
||||
entry.Criteria = int(entry.Sugar + rand.Float32() * 50 + 10)
|
||||
}
|
||||
return entry
|
||||
}
|
||||
|
||||
func main() {
|
||||
if len(os.Args) != 4 {
|
||||
fmt.Println("Usage:", os.Args[0], "<data-file> <count> <filter-percent>")
|
||||
os.Exit(-1)
|
||||
}
|
||||
|
||||
ouputFilename := os.Args[1]
|
||||
count, err := strconv.Atoi(os.Args[2])
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
filterPercent, err := strconv.ParseFloat(os.Args[3], 64)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
outputFile, err := os.OpenFile(ouputFilename, os.O_TRUNC | os.O_CREATE | os.O_WRONLY, 0644)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
defer outputFile.Close()
|
||||
|
||||
outputFile.WriteString("[\n")
|
||||
for i := 0; i < count; i++ {
|
||||
entry := generateEntry(filterPercent)
|
||||
entry_bytes, err := json.Marshal(entry)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
continue
|
||||
}
|
||||
outputFile.WriteString("\t");
|
||||
outputFile.Write(entry_bytes)
|
||||
if (i < count-1) {
|
||||
outputFile.WriteString(",");
|
||||
}
|
||||
outputFile.WriteString("\n");
|
||||
}
|
||||
outputFile.WriteString("]")
|
||||
}
|
5
lab1/gen-data.sh
Executable file
5
lab1/gen-data.sh
Executable file
@ -0,0 +1,5 @@
|
||||
#!/bin/sh
|
||||
COUNT=40
|
||||
go run gen-data.go IF-1-1_PuzonasR_L1_dat_1.json $COUNT 1.0
|
||||
go run gen-data.go IF-1-1_PuzonasR_L1_dat_2.json $COUNT 0.5
|
||||
go run gen-data.go IF-1-1_PuzonasR_L1_dat_3.json $COUNT 0.0
|
5
lab1/go/README.md
Normal file
5
lab1/go/README.md
Normal file
@ -0,0 +1,5 @@
|
||||
# Lab1
|
||||
|
||||
```shell
|
||||
go run main.go ../IF-1-1_PuzonasR_L1_dat_1.json IF-1-1_PuzonasR_L1_rez.txt
|
||||
```
|
104
lab1/go/main.go
Normal file
104
lab1/go/main.go
Normal file
@ -0,0 +1,104 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
const inputQueueSize = 10
|
||||
const inputMonitorCount = 3
|
||||
|
||||
const outputQueueSize = 10
|
||||
const outputMonitorCount = 3
|
||||
|
||||
type DataEntry struct {
|
||||
Name string `json:"name"`
|
||||
Sugar float32 `json:"sugar"`
|
||||
Criteria float32 `json:"criteria"`
|
||||
}
|
||||
|
||||
func filterEntry(entry DataEntry) bool {
|
||||
time.Sleep(time.Millisecond * 100 + time.Millisecond * time.Duration(entry.Criteria))
|
||||
return entry.Sugar > entry.Criteria
|
||||
}
|
||||
|
||||
func outputEntry(file *os.File, entry DataEntry) {
|
||||
time.Sleep(time.Millisecond * 200)
|
||||
fmt.Println("Output:", entry)
|
||||
file.WriteString(entry.Name)
|
||||
file.WriteString("\n")
|
||||
}
|
||||
|
||||
func filterEntries(inputGroup, outputGroup *sync.WaitGroup, input <-chan DataEntry, output chan<- DataEntry) {
|
||||
for entry := range input {
|
||||
fmt.Println("Started to filter:", entry)
|
||||
isFiltered := filterEntry(entry)
|
||||
fmt.Println("Finished to filter:", entry)
|
||||
if (isFiltered) {
|
||||
outputGroup.Add(1)
|
||||
output <- entry
|
||||
}
|
||||
inputGroup.Done()
|
||||
}
|
||||
}
|
||||
|
||||
func ouputEntries(file *os.File, group *sync.WaitGroup, channel <-chan DataEntry) {
|
||||
for entry := range channel {
|
||||
outputEntry(file, entry)
|
||||
group.Done()
|
||||
}
|
||||
}
|
||||
|
||||
func main() {
|
||||
if len(os.Args) != 3 {
|
||||
fmt.Println("Usage:", os.Args[0], "<data-file> <output-file>")
|
||||
os.Exit(-1)
|
||||
}
|
||||
|
||||
dataFilename := os.Args[1]
|
||||
outputFilename := os.Args[2]
|
||||
fileData, err := os.ReadFile(dataFilename)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
entries := []DataEntry{}
|
||||
err = json.Unmarshal(fileData, &entries)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
outputFile, err := os.OpenFile(outputFilename, os.O_TRUNC | os.O_CREATE | os.O_WRONLY, 0644)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
defer outputFile.Close()
|
||||
|
||||
inputChannel := make(chan DataEntry, inputQueueSize)
|
||||
outputChannel := make(chan DataEntry, outputQueueSize)
|
||||
|
||||
var inputsGroups = sync.WaitGroup{}
|
||||
var ouputsGroups = sync.WaitGroup{}
|
||||
inputsGroups.Add(len(entries))
|
||||
|
||||
for i := 0; i < inputMonitorCount; i++ {
|
||||
go filterEntries(&inputsGroups, &ouputsGroups, inputChannel, outputChannel)
|
||||
}
|
||||
for i := 0; i < outputMonitorCount; i++ {
|
||||
go ouputEntries(outputFile, &ouputsGroups, outputChannel)
|
||||
}
|
||||
|
||||
for _, entry := range entries {
|
||||
inputChannel <- entry
|
||||
}
|
||||
close(inputChannel)
|
||||
inputsGroups.Wait()
|
||||
ouputsGroups.Wait()
|
||||
close(outputChannel)
|
||||
|
||||
fmt.Println("Finished")
|
||||
}
|
Loading…
Reference in New Issue
Block a user