1
\$\begingroup\$

A company sells iPods online. There are 100 stocks maintained at Argentina and Brazil. A single iPod costs $100 in Brazil and $50 in Argentina. The cost of exporting stocks from one country to the other will cost $400 per 10 blocks. The transportation is always done in the multiples of 10. Calculate the minimum cost for purchasing the given no of units. Assume that for each transaction, the available stock in each country is maintained to be 100.

Input and output format:

  • The country from which the order is being placed: No of units
  • Minimum costs:No of stock left in Brazil: No of stock left at Argentina

Sample input and output:

Brazil:5

500:95:100

Brazil:50

4500:100:50
class Country:
     shp=40
     def country_details(self,numberofitems,sellingprice):
            self.sp=sellingprice
            self.no=numberofitems
     def display(self):
            print self.sp
            print self.no
            print self.shp  

Brazil=Country()
Brazil.country_details(100,100)
Brazil.display()

Argentina=Country()
Argentina.country_details(100,50)
Argentina.display()

print "Enter the country from which you order from"
x=raw_input()
if x=="Brazil":
    c=1
else:
    c=2

print "Enter the amount of products needed"
y=raw_input()
y=int(y)

def profit(y):
    if c==1:
        if y%10==0:
            P=orderfromArgentinafb(y);
        else:
            P=orderfromBrazil(y);
    if c==2:
        P=orderfromArgentina(y);

    return P

def calculate(y):
    if (y<101):
        Pr=profit(y)
        return Pr
    else:
        Pr=profit(y-(y%100))+orderfromBrazil(y-100)
        return Pr

def orderfromBrazil(y):
    Cost=y*Brazil.sp
    if c==2:
        Cost=Cost+y*Brazil.shp
    Brazil.no=Brazil.no-y
    return Cost

def orderfromArgentina(y):
    Cost=y*Argentina.sp
    Argentina.no=Argentina.no-y
    return Cost

def orderfromArgentinafb(y):
    Cost=y*Argentina.sp+(y*Argentina.shp)
    Argentina.no=Argentina.no-y
    if Argentina.no <0:
        Argentina.no=0
    return Cost

Pr=calculate(y)
print Pr

print  "%d:%d:%d" %(Pr,Brazil.no,Argentina.no)
\$\endgroup\$
1

1 Answer 1

1
\$\begingroup\$

You can write

if raw_input("Enter the country from which you order from") == "Brazil":
    c = 1
else:
    c = 2

instead of

print "Enter the country from which you order from"
x=raw_input()
if x=="Brazil":
    c=1
else:
    c=2

as you are not using x anywhere. I less variable and raw_input has optional string so why not use that?

In function profit instead of using

if y%10==0:
    P=orderfromArgentinafb(y);
else:
    P=orderfromBrazil(y);

you can use

if y % 10:
    return orderfromBrazil(y)
else:
    return orderfromArgentinafb(y)
  • You don't need the variable P when you are just returning it.
  • Why are there semicolons at the end? This isn't C
  • Notice the changing the order of return. It gives the same result but with one less comparison.

Same thing can be done in function calculate.

Use if...elif... in case you are just doing one of the things. Using as it is leads to more comparisons than are needed.

In both the functions orderfromArgentina and orderfromArgentinafb a simple reordering of statements can lead to removal of additional variable Cost.

You need to read a style guide. Check for official tutorials of Python. They contain brief style guide. That is also an optimization. It doesn't effect memory or time complexity but it leads to better maintenance

\$\endgroup\$
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.