From c2ad885319d37957a291e171b6399f8b1925a82f Mon Sep 17 00:00:00 2001 From: priyankalondhe Date: Sat, 8 Oct 2022 14:02:42 +0530 Subject: [PATCH] Challenge 2019: Problem statement 1 solution --- main.go | 123 ++++++++++++++++++++++++++++++++++++++++++++++++++++ output.csv | 4 ++ output1.csv | 4 -- output2.csv | 4 -- 4 files changed, 127 insertions(+), 8 deletions(-) create mode 100644 main.go create mode 100644 output.csv delete mode 100644 output1.csv delete mode 100644 output2.csv diff --git a/main.go b/main.go new file mode 100644 index 0000000..6298ef0 --- /dev/null +++ b/main.go @@ -0,0 +1,123 @@ +package main + +import ( + "encoding/csv" + "fmt" + "io" + "log" + "os" + "strconv" + "strings" +) + +func main() { + // open input.csv partnersFile + inputFile, err := os.Open("input.csv") + if err != nil { + log.Fatal(err) + } + // close inputFile at the end of main + defer inputFile.Close() + + // create output.csv file + outputFile, err := os.Create("output.csv") + if err != nil { + log.Fatal(err) + } + // close output file at the end of main + defer outputFile.Close() + + // create a csv writer for output file + outputFileWriter := csv.NewWriter(outputFile) + // flush writer at the end + defer outputFileWriter.Flush() + + // read csv input.csv using a csv.Reader + inputFileCsvReader := csv.NewReader(inputFile) + + // iterate over input and partners to minimum cost of delivery + for { + inputRecord, err := inputFileCsvReader.Read() + if err == io.EOF { + break + } else if err != nil { + log.Fatal(err) + } + + inputTheaterId := inputRecord[2] + deliverySize, err := strconv.Atoi(inputRecord[1]) + if err != nil { + log.Fatal("Error while converting deliverySize string to int: ", err) + } + + // open partners.csv partnersFile + partnersFile, err := os.Open("partners.csv") + if err != nil { + log.Fatal(err) + } + // read csv partner.csv using a csv.Reader + partnerFileCsvReader := csv.NewReader(partnersFile) + + //skip first record + if _, err := partnerFileCsvReader.Read(); err != nil { + fmt.Println("Error reading first line of partner.csv: ", err) + } + + minDeliveryCost := 0 + isDeliveryPossible := false + deliveryPartner := "" + + var outputRecord []string + + // write deliveryId to output + outputRecord = append(outputRecord, inputRecord[0]) + + for { + partnerRecord, err := partnerFileCsvReader.Read() + if err == io.EOF { + break + } else if err != nil { + log.Fatal(err) + } + + sizeSlab := strings.Split(partnerRecord[1], "-") + sizeSlabMin, err := strconv.Atoi(strings.TrimSpace(sizeSlab[0])) + if err != nil { + log.Fatal(err) + } + sizeSlabMax, err := strconv.Atoi(strings.TrimSpace(sizeSlab[1])) + if err != nil { + log.Fatal(err) + } + + if inputTheaterId == strings.TrimSpace(partnerRecord[0]) && (deliverySize >= sizeSlabMin && deliverySize <= sizeSlabMax) { + isDeliveryPossible = true + rate, err := strconv.Atoi(strings.TrimSpace(partnerRecord[3])) + if err != nil { + log.Fatal(err) + } + deliveryCost := deliverySize * rate + minCost, err := strconv.Atoi(strings.TrimSpace(partnerRecord[2])) + if err != nil { + log.Fatal(err) + } + if deliveryCost < minCost { + deliveryCost = minCost + } + if minDeliveryCost == 0 || deliveryCost < minDeliveryCost { + minDeliveryCost = deliveryCost + deliveryPartner = partnerRecord[4] + } + } + } + if !isDeliveryPossible { + outputRecord = append(outputRecord, fmt.Sprintf("%t", isDeliveryPossible)) + outputRecord = append(outputRecord, "") + outputRecord = append(outputRecord, "") + } else { + outputRecord = append(outputRecord, fmt.Sprintf("%t", isDeliveryPossible), deliveryPartner, fmt.Sprintf("%d", minDeliveryCost)) + } + partnersFile.Close() + outputFileWriter.Write(outputRecord) + } +} diff --git a/output.csv b/output.csv new file mode 100644 index 0000000..a325801 --- /dev/null +++ b/output.csv @@ -0,0 +1,4 @@ +D1,true,P1,2000 +D2,true,P1,3250 +D3,true,P3,15300 +D4,false,, diff --git a/output1.csv b/output1.csv deleted file mode 100644 index 7c7b275..0000000 --- a/output1.csv +++ /dev/null @@ -1,4 +0,0 @@ -D1,true ,P1,2000 -D2,true ,P1,3250 -D3,true ,P3,15300 -D4,false,"","" diff --git a/output2.csv b/output2.csv deleted file mode 100644 index adcd15b..0000000 --- a/output2.csv +++ /dev/null @@ -1,4 +0,0 @@ -D1,true ,P2,3000 -D2,true ,P1,3250 -D3,true ,P3,15300 -D4,false,"",""