diff --git a/realImage_Challenge2016/data/distributors.json b/realImage_Challenge2016/data/distributors.json new file mode 100644 index 000000000..6cd528029 --- /dev/null +++ b/realImage_Challenge2016/data/distributors.json @@ -0,0 +1,12 @@ +[ + { + "name": "DISTRIBUTOR1", + "include": ["INDIA", "UNITEDSTATES"], + "exclude": ["KARNATAKA-INDIA", "CHENNAI-TAMILNADU-INDIA"] + }, + { + "name": "DISTRIBUTOR2", + "include": ["EUROPE"], + "exclude": ["GERMANY", "FRANCE"] + } +] diff --git a/realImage_Challenge2016/distributor/distributor.go b/realImage_Challenge2016/distributor/distributor.go new file mode 100644 index 000000000..c7310affe --- /dev/null +++ b/realImage_Challenge2016/distributor/distributor.go @@ -0,0 +1,22 @@ +package distributor + +import ( + "encoding/json" + "os" +) + +// LoadDistributors reads distributor data from a JSON file +func LoadDistributors(filePath string) ([]Distributor, error) { + file, err := os.ReadFile(filePath) + if err != nil { + return nil, err + } + + var distributors []Distributor + err = json.Unmarshal(file, &distributors) + if err != nil { + return nil, err + } + + return distributors, nil +} diff --git a/realImage_Challenge2016/distributor/models.go b/realImage_Challenge2016/distributor/models.go new file mode 100644 index 000000000..12f6117c0 --- /dev/null +++ b/realImage_Challenge2016/distributor/models.go @@ -0,0 +1,7 @@ +package distributor + +type Distributor struct { + Name string `json:"name"` + Include []string `json:"include"` + Exclude []string `json:"exclude"` +} diff --git a/realImage_Challenge2016/distributor/permissions.go b/realImage_Challenge2016/distributor/permissions.go new file mode 100644 index 000000000..e8b0eb807 --- /dev/null +++ b/realImage_Challenge2016/distributor/permissions.go @@ -0,0 +1,29 @@ +package distributor + +import "strings" + +// normalizeRegion ensures consistent case for comparison +func normalizeRegion(region string) string { + return strings.ToUpper(strings.TrimSpace(region)) +} + +// hasPermission checks if the given distributor can distribute in a region +func HasPermission(distributor Distributor, region string) bool { + normalizedRegion := normalizeRegion(region) + + // Check Exclusions First + for _, excl := range distributor.Exclude { + if strings.HasSuffix(normalizedRegion, normalizeRegion(excl)) { + return false + } + } + + // Check Inclusions + for _, incl := range distributor.Include { + if strings.HasSuffix(normalizedRegion, normalizeRegion(incl)) { + return true + } + } + + return false +} diff --git a/realImage_Challenge2016/go.mod b/realImage_Challenge2016/go.mod new file mode 100644 index 000000000..f6b3e506e --- /dev/null +++ b/realImage_Challenge2016/go.mod @@ -0,0 +1,3 @@ +module realImage_Challenge2016 + +go 1.24.0 diff --git a/realImage_Challenge2016/main.go b/realImage_Challenge2016/main.go new file mode 100644 index 000000000..04c60e0ca --- /dev/null +++ b/realImage_Challenge2016/main.go @@ -0,0 +1,35 @@ +package main + +import ( + "fmt" + "log" + + "realImage_Challenge2016/distributor" +) + +func main() { + distributors, err := distributor.LoadDistributors("data/distributors.json") + if err != nil { + log.Fatalf("Failed to load distributor data: %v", err) + } + + testRegions := []string{ + "CHICAGO-ILLINOIS-UNITEDSTATES", + "CHENNAI-TAMILNADU-INDIA", + "BANGALORE-KARNATAKA-INDIA", + "MUMBAI-MAHARASHTRA-INDIA", + "BERLIN-GERMANY", + "PARIS-FRANCE", + } + + for _, dist := range distributors { + fmt.Printf("\n=== Distributor: %s ===\n", dist.Name) + for _, region := range testRegions { + if distributor.HasPermission(dist, region) { + fmt.Printf("Distribution in %s: YES\n", region) + } else { + fmt.Printf("Distribution in %s: NO\n", region) + } + } + } +}