Need help with a problem statement ( Map, json, slice)


(Goku Saiyan) #1

I have this problem statement

Problem statement:

You are traveling via rail route SomPointA to Destination. You have to buy all the different types of food available across the stations.

All the food items may / may not be available on each station. Given JSON data,

find the optimal way where you will be able to get all the food items at the minimum price. You can buy different items from different stations.

In case two stations offer the same food at equal prices, you need to buy it from the first station.

Input:

{

"SomePointA": {

	"Burger": 15.0,

	"Fries": 20.0,

	"ColdDrink": 25.0

},

"SomePointB": {

	"Burger": 10.0,

	"Fries": 15.0,

	"ColdDrink": 30.0,

	"HotDog": 15.0

},

"Destination": {

	"Burger": 30.0,

	"Fries": 10.0,

	"ColdDrink": 10.0

}

}


Output:

{

"Burger": {"station":"SomePointB", "price": 10},

"Fries": {"station":"Destination", "price": 10},

"ColdDrink": {"station":"Destination", "price": 10},

"HotDog": {"station": "SomePointB", "price": 15}

}

what i have written so far . I cannot seem to create a data structure which can get me the results ( like i know output is in json but can’t seem to figure it out ) also i have to iterate over every element compare the price and then store it ( theoritically i understand but can’t write )

import (

"encoding/json"

"fmt"

)

func main() {

data := []byte(`{


"SomePointA": {

	"Burger": 15.0,

	"Fries": 20.0,

	"ColdDrink": 25.0

},

"SomePointB": {

	"Burger": 10.0,

	"Fries": 15.0,

	"ColdDrink": 30.0,

	"HotDog": 15.0

},

"Destination": {

	"Burger": 30.0,

	"Fries": 10.0,

	"ColdDrink": 10.0

}


}`)

var result map[string]interface{}


json.Unmarshal(\[\]byte(data), &result)


for s, _ := range result {

res:= result[s].(map[string]interface{})

fmt.Println(res)

}

}