-2

I am calculating total cost of shipment for different countries. As input I have an initial fee and origin country and destination country. As output I would like to get the total cost. Of course depending on the origin and destination country the formula to calculate the total cost varies.

To solve this solution I thought I should use a matrix which contains the formulas needed to apply to calculate the total cost. My problem is I really dont know how to make matrix which contains formulas. Could you point me in the right direction, please?

Just to make it more understandable I made a sketch of the matrix I was thinking of below.

Input function:

totalCostCalc(init_cost, origin_country, destination_country)

Calculation matrix: enter image description here

The different colors of matrix elements show the different formulas or functions applied.

Could you please help me by showing how to start, or something similar as I am really stuck.

Ignacio Vergara Kausel
  • 5,521
  • 4
  • 31
  • 41
hunsnowboarder
  • 170
  • 2
  • 18

1 Answers1

1

I don't know if you will prefer any other solution, but making such a matrix of formulae isn't a good thing, I would say.

I think you should create a dictionary of contries as key and their value being the continents.

So, whenever the totalCostCalc function is called, you need to just check three conditions.

def totalCostCalc(init_cost, origin_country, origin_country_fee, destination_country, destination_country_fee)
    if (origin_country == destination_country):
        return init_cost
    elif (d[origin_country] == d[destination_country]):
        return init_cost*origin_country_fee*destination_country_fee
    else:
        return init_cost*origin_country_fee*destination_country_fee*continent_fee

where d is the dictionary defined as d = {'Germany': 'Europe', 'Hungary': 'Europe', 'China': 'Asia'}.

Ignacio Vergara Kausel
  • 5,521
  • 4
  • 31
  • 41
Miraj50
  • 4,257
  • 1
  • 21
  • 34