My task was to
'Write a function selectCoins that asks the user to enter an amount of money
(in pence) and then outputs the number of coins of each denomination (from £2 down
to 1p) that should be used to make up that amount exactly (using the least possible
number of coins). For example, if the input is 292, then the function should report:
1 × £2, 0 × £1, 1 × 50p, 2 × 20p, 0 × 10p, 0 × 5p, 1 × 2p, 0 × 1p. (Hint: use integer
division and remainder).'
def selectCoins():
twopound = 200
onepound = 100
fiftyp = 50
twentyp = 20
tenp = 10
fivep = 5
twop = 2
onep = 1
a = 0
b = 0
c = 0
d = 0
e = 0
f = 0
g = 0
h = 0
money = int(input('Enter how much money you have in pence'))
while True:
if money >= twopound:
money = money - twopound
a = a + 1
elif money >= onepound:
money = money - onepound
b = b + 1
elif money >= fiftyp:
money = money - fiftyp
c = c + 1
elif money >= twentyp:
money = money - twentyp
d = d + 1
elif money >= tenp:
money = money - tenp
e = e + 1
elif money >= fivep:
money = money - fivep
f = f + 1
elif money >= twop:
money = money - twop
g = g + 1
elif money >= onep:
money = money - onep
h = h + 1
else:
money = 0
break
print(a,b,c,d,e,f,g,h)
I am new to programming so when I run this code it just inputs
'1 0 0 0 0 0 0 0' when I type 292 instead of what it should output.
Since you're new to coding, you should start writing the procedure you'd follow on paper, and then find out which tools you can use to automate this process.
Important
Read the full answer in order!
Don't fall for the temptation of reading the code right away.
The solutions I provide are hidden, but you can read them hovering your mouse over them or clicking on them (if you're using StackExchange mobile app, touch the "spoiler" link in each block).
The algorithm
What I'd do is:
Assume I have bins with coins, each bin labeled with the coin denomination.
The bins are sorted from the largest to the lowest denomination, and I always pick as much coins as I need from the highest denomination bin before moving to the next bin.
Write in a piece of paper the value for which I need to calculate the number of coins of each denomination I need.
Start with the first bin (the one holding the highest denomination).
Pick as many coins I need from that bin, in such a way that I don't "overshoot" the amount written on the piece of paper (notice that this number can be zero).
This can be done with an integer division; for example, if your value is 700 and the bin has denomination 200, you calculate the integer division 700 ÷ 200 = 3 (plus a remainder of 100)
Calculate the total amount of the coins I've picked.
Strike the value calculated in step 5 and write the remainder as a "new" value.
Since you've already calculated the integer division in step 4, you can calculate the remainder. You can also consider that there's a "Modulo" operator in most programming languages that will give you the remainder of an integer division right away. Using the above example, 700 mod 200 = 100, which reads "700 modulo 200 is 100", or "The remainder of the integer division 700 ÷ 200 is 100".
Move on to the next bin of coins.
Repeat from step 4 until I use all the bins or the value is zero.
Example
Suppose I start with a value of 292 and I have bins with the following denominations (already sorted from highest to lowest denominations):
| 200 | 100 | 50 | 20 | 10 | 5 | 2 | 1 |
+------+------+------+------+------+------+------+------+
| I | II | III | IV | V | VI | VII | VIII |
So, let's see what happens if I apply the algorithm above:
Write the value: 292
Start with the first bin (denomination: 200)
Pick 1 coin from the bin
The total amount picked from the bin is 200
The remainder is 92
Strike the previous value
The new value is 92
Move to the next bin (denomination: 100)
Pick 0 coins from the bin
The total amount picked from the bin is 0
The remainder is 92
Strike the previous value
The new value is 92
Move to the next bin (denomination: 50)
Pick 1 coin from the bin
The total amount picked from the bin is 50
The remainder is 42
Move to the next bin (denomination: 20)
Pick 2 coins from the bin
The total amount picked from the bin is 20
The remainder is 2
Move to the next bin (denomination: 10)
Pick 0 coins from the bin
The total amount picked from the bin is 0
The remainder is 2
Move to the next bin (denomination: 10)
Pick 0 coin from the bin
The total amount picked from the bin is 0
The remainder is 2
Move to the next bin (denomination: 5)
Pick 0 coin from the bin
The total amount picked from the bin is 0
The remainder is 2
Move to the next bin (denomination: 2)
Pick 1 coin from the bin
The total amount picked from the bin is 2
The remainder is 0
Done
Implementing this in Python
Python is an amazingly clear language, and makes this sort of tasks easy. So let's try to translate our algorithm to Python.
The toolbox
Assuming you're using Python 3.x, you'll need to know some operators:
The integer division operator (//): If you divide with just a single slash, you'll get the "real division" (e.g. 3 / 2 == 1.5), but if you use a double slash, you'll get the "integer division (e.g. 3 // 2 = 1)
The modulo operator (%): As explained above, this operator returns the remainder of a division (e.g. 7 % 4 == 3)
Used together, these operators will give you what you need on each step:
292 // 200 == 2
292 % 200 == 92
92 // 100 == 0
92 % 100 == 92
...
One useful characteristic of Python is that you can perform a "multiple assignment": You can assign multiple values to multiple variables in one single step:
# Initialize the value:
value = 292
# Initialize the denomination:
denomination = 200
# Calculate the amount of coins needed for the specified denomination
# and get the remainder (overwriting the value), in one single step:
coins, value = value // denomination, value % denomination
# ^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^
# | The remainder
# The number of coins
# (using integer division)
With this knowledge, we can write the solution:
Correcting your code
Remember: Read all of the above before revealing the solutions below.
def selectCoins():
twopound = 200
onepound = 100
fiftyp = 50
twentyp = 20
tenp = 10
fivep = 5
twop = 2
onep = 1
a = 0
b = 0
c = 0
d = 0
e = 0
f = 0
g = 0
h = 0
money = int(input('Enter how much money you have in pence')) # Example: 292
# Calculate the number of coins needed and the remainder
# The remainder will "overwrite" the value previously held in the "money" variable
a, money = money // twopound, money % twopound # a = 1, money = 92
b, money = money // onepound, money % onepound # b = 0, money = 92
c, money = money // fiftyp, money % fiftyp # c = 1, money = 42
d, money = money // twentyp, money % twentyp # d = 2, money = 2
e, money = money // tenp, money % tenp # e = 0, money = 2
f, money = money // fivep, money % fivep # f = 0, money = 2
g, money = money // twop, money % twop # g = 1, money = 0
e, money = money // onep, money % onep # e = 0, money = 0
print(a,b,c,d,e,f,g,h)
This solution uses both integer division and remainder to perform the calculations.
Let's do it the right way: with a loop
Let's face it: the above code is verbose. There must be a better way... and there is! Use a loop.
Consider the algorithm: you repeat the steps jumping from one bin to the next and getting the number of coins you need and the remainder. This can be written in a loop.
So, let's add a list to our toolbox:
denominations = [200, 100, 50, 20, 10, 5, 2, 1]
And let's store the results of each step in a second list:
coins = [] # We'll use the '.append()' method to add elements to this list
So, starting in the first "bin":
n, money = money // denominations[0] , money % denominations[0]
coins.append(n)
Let's put this in a loop:
def select_coins_v2():
denominations = [200, 100, 50, 20, 10, 5, 2, 1]
coins = []
money = int(input('Enter how much money you have in pence'))
for i in range(len(denominations)):
n, money = money // denominations[i], money % denominations[i]
coins.append(n)
print(coins)
And that's it!
Another improvement: get the denomination only once and use it twice
Notice that the code above still has an issue: you read denominations twice. It would be nice if the denomination value could be read only once.
Of course, there is a way:
def select_coins_v3():
denominations = [200, 100, 50, 20, 10, 5, 2, 1]
coins = []
money = int(input('Enter how much money you have in pence'))
for d in denominations: # 'd' will hold the value of the denomination
n, money = money // d, money % d
coins.append(n)
print(coins)
As a friend of mine says: "Fast, precise and concise; not slow, difuse and confusing"
TL;DR
In Python 3.x, the "integer division" operator is // and the remainder (modulo) operator is %.
You can perform multiple assignement in a single line of code:
a, b = 1, 2
You can store the denominations in a list:
denominations = [200, 100, 50, 20, 10, 5, 2, 1]
You can read from the denominations list and get both the integer division and the remainder in a single step:
n, money = money // denominations[0], money % denominations[0]
You can write a loop that does all of the above:
for d in denominations: n, money = money // d, money % d
Bonus: Use a dictionary
What if I want to print both the denominations and the number of coins of each denomination I used? You can traverse both lists with a loop, but you can also keep it simple by using a dictionary:
def select_coins_v4():
denominations = [200, 100, 50, 20, 10, 5, 2, 1]
coins = []
money = int(input('Enter how much money you have in pence'))
for d in denominations: # 'd' will hold the value of the denomination
n, money = money // d, money % d
coins.append(n)
number_of_coins = dict(zip(denominations, coins))
print(number_of_coins)
Python offers a great deal of flexibility. Feel free to try different ways of getting what you need... and choose the easier one.
Hope this helps.
the cool thing about using real denominations is that the greedy solution will always find the optimal solution ... this stops holding true with weird denominations... but these problems are always easiest if you break them into parts
def get_one_change(amt_due):
# find how many of the largest denomination that you can use is
# ie for 60 = 1x50p is the count and number of largest
# for 4 = 4x1p ; 21 = 2x10p ; etc
return pence,count # ie 50,1 would be 1 50p coin
once you have this you just need to repeatedly call it and adjust your result until you have no change due
def get_change(amount_due):
changes_due = [] # to keep track
while amount_due:
pence,qty_of_coin = get_one_change(amount_due)
changes_due.append({"coin":pence,"qty":qty_of_coin})
amount_due = amount_due - (pence*qty_of_coin)
return changes_due
now you can just call your get_change method with your users input
Related
I'm trying to find out how many times you have to throw the dice to get on file 5 100 times(board is played from 0 to 5). This is how I tried(I know the answer is 690 but I don't know what I'm doing wrong).
from random import *
seed(8)
five = 0
count = 0
add = 0
while five < 100:
count = count + 1
print(randint(1,6))
add = add + randint(1,6)
if add % 5 == 0 :
five = five + 1
else: add = add + randint(1,6)
print(count)
This is the code I think you were trying to write. This does average about 600. Is it possible your "answer" came from Python 2? The random seed algorithm is quite likely different.
from random import *
seed(8)
five = 0
count = 0
add = 0
while five < 100:
count += 1
r = randint(0,5)
if r == 5:
five += 1
else:
add += r
print(count, add)
You're adding a second dice throw every time you don't get on 5, this makes the probability distribution irregular (i.e. advancing by 7 will be more probable (1/6) than any other value, e.g. 1/9 for 5) so your result will not be the same as counting single throws.
BTW there is no fixed result for this, just a higher probability around a given number of throws. However, given that you seeded the random number generator with a constant, every run should give the same result. And it should be the right one if you don't double throw the dice.
Here is an example of the process that arrives at 690:
import random
random.seed(8)
fiveCount = 0
throwCount = 0
position = 0
while fiveCount < 100:
position = (position + random.randint(1,6)) % 6
throwCount += 1
fiveCount += position == 5
print(throwCount) # 690
Other observations:
Updating the position wraps around using modulo 6 (there are 6 positions from 0 to 5 inclusively)
Your check of add%5 == 0 does not reflect this. It should have been add%6 == 5 instead but it is always preferable to model the computation as close as possible to the real world process (so keep the position in the 0...5 range)
so, I had this exercise where I would receive a list of integers and had to find how many sum pairs were multiple to 60
example:
input: list01 = [10,90,50,40,30]
result = 2
explanation: 10 + 50, 90 + 30
example2:
input: list02 = [60,60,60]
result = 3
explanation: list02[0] + list02[1], list02[0] + list02[2], list02[1] + list02[2]
seems pretty easy, so here is my code:
def getPairCount(numbers):
total = 0
cont = 0
for n in numbers:
cont+=1
for n2 in numbers[cont:]:
if (n + n2) % 60 == 0:
total += 1
return total
it's working, however, for a big input with over 100k+ numbers is taking too long to run, and I need to be able to run in under 8 seconds, any tips on how to solve this issue??
being with another lib that i'm unaware or being able to solve this without a nested loop
Here's a simple solution that should be extremely fast (it runs in O(n) time). It makes use of the following observation: We only care about each value mod 60. E.g. 23 and 143 are effectively the same.
So rather than making an O(n**2) nested pass over the list, we instead count how many of each value we have, mod 60, so each value we count is in the range 0 - 59.
Once we have the counts, we can consider the pairs that sum to 0 or 60. The pairs that work are:
0 + 0
1 + 59
2 + 58
...
29 + 31
30 + 30
After this, the order is reversed, but we only
want to count each pair once.
There are two cases where the values are the same:
0 + 0 and 30 + 30. For each of these, the number
of pairs is (count * (count - 1)) // 2. Note that
this works when count is 0 or 1, since in both cases
we're multiplying by zero.
If the two values are different, then the number of
cases is simply the product of their counts.
Here's the code:
def getPairCount(numbers):
# Count how many of each value we have, mod 60
count_list = [0] * 60
for n in numbers:
n2 = n % 60
count_list[n2] += 1
# Now find the total
total = 0
c0 = count_list[0]
c30 = count_list[30]
total += (c0 * (c0 - 1)) // 2
total += (c30 * (c30 - 1)) // 2
for i in range(1, 30):
j = 60 - i
total += count_list[i] * count_list[j]
return total
This runs in O(n) time, due to the initial one-time pass we make over the list of input values. The loop at the end is just iterating from 1 through 29 and isn't nested, so it should run almost instantly.
Below is a translation of Tom Karzes's answer but using numpy. I benchmarked it and it is only faster if the input is already a numpy array, not a list. I still want to write it here because it nicely shows how loops in python can be one-liners in numpy.
def get_pairs_count(numbers, /):
# Count how many of each value we have, modulo 60.
numbers_mod60 = np.mod(numbers, 60)
_, counts = np.unique(numbers_mod60, return_counts=True)
# Now find the total.
total = 0
c0 = counts[0]
c30 = counts[30]
total += (c0 * (c0 - 1)) // 2
total += (c30 * (c30 - 1)) // 2
total += np.dot(counts[1:30:+1], counts[59:30:-1]) # Notice the slicing indices used.
return total
I am currently passing the sample tests and 2 of the other 10 cases so 4 out of 12. However, I don't make it through all of the data. I am getting a Terminated due to timeout error which means that my solution isn't fast enough.
def stockmax(prices):
total = 0
for index, price in enumerate(prices):
if index < len(prices) - 1:
section = max(prices[index+1:])
if prices[index] < section:
total += section - prices[index]
return total
I tried to do everything in only one loop. But how exactly can speed this type of question up. I also tried to cut some lines of the code but it is equally as inefficient.
def stockmax(prices):
total = 0
for index, price in enumerate(prices):
if index < len(prices) - 1 and prices[index] < max(prices[index+1:]):
total += max(prices[index+1:]) - prices[index]
return total
Though it passes the same amount of test cases.
I also tried to use heapq but it passes the same test cases and fails due to time.
def stockmax(prices):
total = 0
for index, price in enumerate(prices):
if index < len(prices) - 1:
section = heapq.nlargest(1,prices[index+1:])[0]
if prices[index] < section:
total += section - prices[index]
return total
https://www.hackerrank.com/challenges/stockmax/topics/dynamic-programming-basics
for details on the problem.
https://hr-testcases-us-east-1.s3.amazonaws.com/330/input09.txt?AWSAccessKeyId=AKIAJ4WZFDFQTZRGO3QA&Expires=1538902058&Signature=3%2FnfZzPO8XKRNyGG0Yu9qJIptgk%3D&response-content-type=text%2Fplain
for a link of some test cases but will expire after a while.
Problem
Your algorithms have become so good at predicting the market that you now know what the share price of Wooden Orange Toothpicks Inc. (WOT) will be for the next number of days.
Each day, you can either buy one share of WOT, sell any number of shares of WOT that you own, or not make any transaction at all. What is the maximum profit you can obtain with an optimum trading strategy?
For example, if you know that prices for the next two days are prices = [1,2], you should buy one share day one, and sell it day two for a profit of 1. If they are instead prices = [2,1], no profit can be made so you don't buy or sell stock those days.
Function Description
Complete the stockmax function in the editor below. It must return an integer that represents the maximum profit achievable.
stockmax has the following parameter(s):
prices: an array of integers that represent predicted daily stock prices
Input Format
The first line contains the number of test cases t.
Each of the next t pairs of lines contain:
The first line contains an integer n, the number of predicted prices for WOT.
The next line contains n space-separated integers prices [i], each a predicted stock price for day i.
Constraints
1 <= t <= 10
1 <= n <= 50000
1 <= prices [i] <= 100000
Output Format
Output lines, each containing the maximum profit which can be obtained for the corresponding test case.
Sample Input
3
3
5 3 2
3
1 2 100
4
1 3 1 2
Sample Output
0
197
3
Explanation
For the first case, you cannot obtain any profit because the share price never rises.
For the second case, you can buy one share on the first two days and sell both of them on the third day.
For the third case, you can buy one share on day 1, sell one on day 2, buy one share on day 3, and sell one share on day 4.
Clearly, for any price we can buy, we would want to sell it at the highest price. Fortunately, we are given that highest price. So, iterating backwards, we know the highest future price seen at any point we visit in our travel "back in time."
Python code:
def stockmax(prices):
n = len(prices)
highest = prices[n - 1]
m = [0] * n
# Travel back in time,
# deciding whether to buy or not
for i in xrange(n - 2, -1, -1):
# The most profit buying stock at this point
# is what we may have made the next day
# (which is stored in m[i + 1])
# and what we could make if we bought today
m[i] = m[i + 1] + max(
# buy
highest - prices[i],
# don't buy
0
)
# Update the highest "future price"
highest = max(highest, prices[i])
return m[0]
If you can use Numpy, then something similar to the below should be rather quick (I believe it's the same idea as the answer from #גלעד ברקן).
import numpy as np
with open('.../input09.txt') as fd:
numtests = int(fd.readline().strip())
counter = 0
numvals = 0
vals = None
steps = None
for line in fd:
if (counter % 2 == 0) :
numvals = int(line.strip())
else:
vals = np.fromstring(line, dtype=int, sep=' ', count=numvals)
assert len(vals) == numvals
cum_max = np.maximum.accumulate(vals[::-1])
np.roll(cum_max, -1)
cum_max[len(cum_max) - 1] = 0
delta = (cum_max - vals)
print('#', counter + 1, 'sum:', np.sum(delta * (delta > 0)))
counter += 1
it runs almost instantly on tests from the input09.txt.
Here is my solution written in ruby.
The solution obtained perfect score.
def solution(a)
gain = 0
i = a.count - 1
min = false
mi = false
while i > 0
s = a.delete_at(i)
unless min
mi = a.index(a.min)
min = a[mi]
end
g = s - min
gain = g if g > gain
i -= 1
min = false if i == mi
end
gain
end
My assignment is to create a program that simulates 2 coins being tossed at the same time. If both heads, then Group A gets a point; if both tails then Group B gets a point. If the coins are different then the Prof gets a point. The program must take 2 inputs: number of games and number of tosses per game. Here are 2 separate sample runs to illustrate:
How many games? 1
How many coin tosses per game? 100
Game 0:
Group A: 25 (25.0%); Group B: 19 (19.0%); Prof: 56 (56.0%)
Wins: Group A=0 (0.0%); Group B=0 (0.0%); Prof=1 (100.0%)
How many games? 5
How many coin tosses per game? 10
Game 0:
Group A: 3 (30.0%); Group B: 1 (10.0%); Prof: 6 (60.0%)
Game 1:
Group A: 6 (60.0%); Group B: 1 (10.0%); Prof: 3 (30.0%)
Game 2:
Group A: 4 (40.0%); Group B: 1 (10.0%); Prof: 5 (50.0%)
Game 3:
Group A: 4 (40.0%); Group B: 1 (10.0%); Prof: 5 (50.0%)
Game 4:
Group A: 5 (50.0%); Group B: 3 (30.0%); Prof: 2 (20.0%)
Wins: Group A=2 (40.0%); Group B=0 (0.0%); Prof=3 (60.0%)
My code (albeit clunky) works for taking the inputs, simulating coin tosses, and calculating and displaying the number of points per group and the percent. My problem however, is in calculating and storing the number of wins across all of the games played. Here is my code as of now:
import random
def coinFlip():
games = input("How many games? ")
tosses = input("How many coin tosses per game? ")
for i in range(games):
gA = 0
gAW = 0
gB = 0
gBW = 0
prof = 0
profW = 0
for j in range(tosses):
flip1 = random.randint(0, 1)
flip2 = random.randint(0, 1)
if (flip1 == 0 and flip2 == 0):
gA += 1
elif (flip1 == 1 and flip2 == 1):
gB += 1
else:
prof += 1
gAper = ((gA * 1.0) / tosses) * 100
gBper = ((gB * 1.0) / tosses) * 100
profper = ((prof * 1.0) / tosses) * 100
if (gA > gB and gA > prof):
gAW += 1
elif (gB > gA and gB > prof):
gBW += 1
elif ( prof > gA and prof > gB):
profW += 1
gAWper = ((gAW * 1.0) / games) * 100
gBWper = ((gBW * 1.0) / games) * 100
profWper = ((profW * 1.0) / games) * 100
print "Game {}:".format(i)
print " Group A: {} ({}%); Group B: {} ({}%); Prof: {} ({}%)".format(gA, gAper, gB, gBper, prof, profper)
print "Wins: Group A = {} ({}%); Group B = {} ({}%); Prof: {} ({}%)".format(gAW, gAWper, gBW, gBWper, profW, profWper)
I'm thinking I should store the wins in a list, but that's where I'm lost.
The critical problem is that you have reset the long-term counts at the start of every game. Thus, nobody gets to record more than one win. This works great for my Monday-night Frisbee games, but is not effective for your assignment.
Return to your psuedo-code and see where the loops and initializations match up. Here's a code version:
def coinFlip():
# Set-up you do only once per program execution
games = input("How many games? ")
tosses = input("How many coin tosses per game? ")
games_won_A = 0
games_won_B = 0
games_won_prof = 0
for i in range(games):
# Set-up things you do once per game
tosses_won_A = 0
tosses_won_B = 0
tosses_won_prof = 0
for j in range(tosses):
# Things you do every toss
flip1 = random.randint(0, 1)
flip2 = random.randint(0, 1)
...
# Summary things you do every game
# ... such as compute percentages
# Summary things you do at the end of the program execution
# ... such as print the overall totals
Does that get you moving?
BTW< note that this becomes a lot shorter if you put the counters into a list. For instance, counting the winner of each flip becomes a single line:
win_count[flip1 + flip2] += 1
win_count can be a list of three elements, recording the wins for A, prof, and B, in that order.
This probably isn't what the OP was looking for, but in general generating random numbers with numpy can accomplish this quickly and simply.
import numpy as np
# get these from input or wherever
games = 5
tosses = 10
n_coins = 2
experiment = np.random.randint(2,size=(games, tosses, n_coins))
flip_results = experiment.sum(axis=2) # 0 means group A wins, 1 Prof, 2 group B
game_results = np.stack((flip_results == 0, flip_results == 1, flip_results == 2))
game_results = game_results.sum(axis=2)
total_results = game_results.sum(axis=1)
print(game_results, total_results)
Could I code differently to slim down the point of this Python source code? The point of the program is too get the users total amount and add it too the shipping cost. The shipping cost is determined by both country (Canada or USA) and price of product:
The shipping of a product that is $125.00 in Canada is $12.00.
input ('Please press "Enter" to begin')
while True:
print('This will calculate shipping cost and your grand total.')
totalAmount = int(float(input('Enter your total amount: ').replace(',', '').replace('$', '')))
Country = str(input('Type "Canada" for Canada and "USA" for USA: '))
usa = "USA"
canada = "Canada"
lessFifty = totalAmount <= 50
fiftyHundred = totalAmount >= 50.01 and totalAmount <= 100
hundredFifty = totalAmount >= 100.01 and totalAmount <= 150
twoHundred = totalAmount
if Country == "USA":
if lessFifty:
print('Your shipping is: $6.00')
print('Your grand total is: $',totalAmount + 6)
elif fiftyHundred:
print('Your shipping is: $8.00')
print('Your grand total is: $',totalAmount + 8)
elif hundredFifty:
print('Your shipping is: $10.00')
print('Your grand total is: $',totalAmount + 10)
elif twoHundred:
print('Your shipping is free!')
print('Your grand total is: $',totalAmount)
if Country == "Canada":
if lessFifty:
print('Your shipping is: $8.00')
print('Your grand total is: $',totalAmount + 8)
elif fiftyHundred:
print('Your shipping is: $10.00')
print('Your grand total is: $',totalAmount + 10)
elif hundredFifty:
print('Your shipping is: $12.00')
print('Your grand total is: $',totalAmount + 12)
elif twoHundred:
print('Your shipping is free!')
print('Your grand total is: $',totalAmount)
endProgram = input ('Do you want to restart the program?')
if endProgram in ('no', 'No', 'NO', 'false', 'False', 'FALSE'):
break
Here is the core to simplify your code. It prints shipping cost for $100.00 in USA
totalAmount = 100
chargeCode = (int(100*(totalAmount+0.001))-1)/5000 #0 -- <=50, 1 -- 50.01-100, etc
if chargeCode > 3: chargeCode = 3
shipping = {}
shipping[("USA", 0)] = 6
shipping[("USA", 1)] = 8
shipping[("USA", 2)] = 10
shipping[("USA", 3)] = 0
shipping[("Canada", 0)] = 8
shipping[("Canada", 1)] = 10
shipping[("Canada", 2)] = 12
shipping[("Canada", 3)] = 0
print shipping[("USA", chargeCode)]
totalAmount+0.001 is used to avoid fun with float point numbers:
int(100*(81.85)) == 8184
returns True on my system because float point 81.85 is a bit less than decimal 81.85.
Here's a basic strategy for the cost calculation:
import math
amount = int(totalAmount)
assert amount >= 0
shipping = {
'USA': [6, 8, 10],
'Canada': [8, 10, 12]
}
try:
surcharge = shipping[country][amount and (math.ceil(amount / 50.0) - 1)]
except IndexError:
surcharge = 0
total = amount + surcharge
The key notion here is that the the shipping cost ranges follow a fairly linear progression: [0-50], (50-100], (100-150], (150, inf)
Note that the first group is a little funny, as it includes the lower bound of 0 where the other groups don't include the lower bound (they are an open interval at the bottom). So going forward we'll consider the first group as the following: 0 or (0-50]
We want to transform the amount the user supplies into an index into the shipping cost lists [6, 8, 10] and [8, 10, 12]. The lists are of length 3, so the indexes are 0, 1 and 2. Notice that if we divide any number in the range (0, 150] by 50.0 (we add the .0 to 50 so we a get a real number back -- 1 / 50 == 0 but 1 / 50.0 == 0.02 -- for the next step) we get a number in the range (0 and 3].
Now realize that math.ceil will round a real number to the nearest whole number that is greater than or equal to itself -- ex. math.ceil(.001) == 1.0, math.ceil(1.5) == 2.0, math.ceil(2) == 2.0. So applying math.ceil to numbers in the range (0, 3] we will supply either 1.0, 2.0 or 3.0. Cast those numbers to int (int(2.0) == 2) and we get the values 1, 2 and 3. Subtract 1 from those values and we get the 0, 1, 2. Voila! Those numbers match the indexes into our shipping array.
You can express this transformation with the Python: int(math.ceil(amount / 50.0) - 1)
We are almost there. We've handled any amount in the range (0, 150]. But what if amount is 0. math.ceil(0) == 0.0 and 0.0 - 1 == -1.0 This will not index properly into our array. So we handle 0 separately by checking first if amount is equal to 0, and if it is, using 0 as our shipping array index instead of the applying our transformation to amount to determine the index. This can be accomplished using Python's short circuit and operator (the web should have plenty of information on this) in the following expression: amount and int(math.ceil(amount / 50.0) - 1)
Note that any value above 150 will reduce to an index greater than 2 and applying that index against the shipping array will result in an IndexError. But any value greater than 150 has free shipping so we can catch the IndexError and apply a surcharge of 0:
try:
surcharge = shipping[country][amount and int(math.ceil(amount / 50.0) - 1)]
except IndexError:
surcharge = 0
And we're done!