how to find maximum profit while selling some garbage item - algorithm

I have one problem statement for which i need write an algo. can somebody help me?
Problem is :
i have different length of iron rod let say {26, 103, 59}, i want to sell the same length of iron rod so that i can earn maximum profit. Also i have to cutting charge lets say 10 Rs/unit.
Case 1, if cutting charge is Rs 100 and sell length of 51 feet with cost 100 per unit.
then 103/ 51 = 2 length ((51 * 100 * 2) - ((1 * 100) + 200 ) = 9900
59 / 51 = 1 length ((51 * 100 * 1) - ((8 * 100) + 1* 100) = 4200
26/52 = 0 length((0 * 100)) - (26 * 100 ) = -2600
now total profit is = 11500
But if cutting charge is vary then this calculation like failed, can some one tell how can develop algo to find the maximum profit.

Related

Calculate shipping costs on the basis of product weight

my math is not so good, but can you guys help me with this
problem statement
Suppose I have 4 books with weights and prices.
Book1, 0.5KG
Book2, 0.8KG
Book3, 1KG
Book4, 0.3KG
I have a base price (shipping cost) based on weight, which is 30 Rs Per 0.5KG.
Now when I select "book 1", the shipping cost will be 30 Rs, but how can I get the shipping cost for book 2,book 3 and book 4?
it's not related to any programming or algorithm , anyways
if
30Rs -> 0.5KG
x -> 0.8Kg
then simply for Book2
x = (30Rs*0.8KG)/0.5KG = 48Rs
similarly for book3 and book4:
book3 = (30Rs*1KG)/0.5KG = 60Rs
book4 = (30Rs*0.3KG)/0.5KG = 18Rs
another way to solve it is if every 30RS corresponds to 0.5KG then by dividing each side by 5 then 6RS corresponds to 0.1KG.
Book2 is 0.8KG which is 8 times the value 0.1KG then it must cost 8 times the value 6RS so 8 * 6 = 48RS similarly for **Book3 and Book4 where
Book3 = 10 * 6 = 60RS
Book4 = 3 * 6 = 18RS
If the pricing is in brackets, that is the cost for 0 - 0.5kg is 30RS, and 0.5 - 1kg is 60RS, then you have to do as follows:
First find how many brackets you have:
weight / bracketSize
For your books, this will be:
Book1: 0.5/0.5 // 1
Book2: 0.8/0.5 // 1.6
Book3: 1/0.5 // 2
Book4, 0.3/0.5 // 0.6
Then, you need to round that value up to the nearest whole number. How you do this will depend on what language you're using, but it's often called Ceiling or ceil:
Book1: Ceiling(1) // 1
Book2: Ceiling(1.6) // 2
Book3: Ceiling(2) // 2
Book4, Ceiling(0.6) // 1
Then multiply by price to get your answer.
Book1: 1 * 30 // 30
Book2: 2 * 30 // 60
Book3: 2 * 30 // 60
Book4, 1 * 30 // 30
In one line:
result = Ceiling(weight / bracketSize) * pricePerBracket

Dynamic Programming Problem - Maximize Profit by Selling Stocks

I am looking at this code challenge:
You are selling stocks over n days and you have x number of stocks.
x = y1 + y2 + ... + yn denoting the number of stocks you sell per day over n days.
There is a list of default prices for each day p[1..n] and a list of selling deduction f[0..x].
Selling deduction depends on how many stocks you sell at a time hence 0 to x.
Actual price per day depends on how many stocks you sell.
For instance, p[1] is 500 and f[120] is 60. If you have 200 stocks and you sell 120 on day 1.
Profit = 120 * (500 - 60) = 50400
The deduction carries on into subsequent days, so if you sell the remaining stocks on day 2 with p[2] = 300 and f[80] = 40.
Profit = 80 * (300 - 40 - 60) = 16000
I'm trying to maximize total profit by deciding how many stocks to sell on each day.
I'm not very good at DP problems and have only practiced the traditional problems. I'm getting kind of stuck on this one. I think the approach might use a 3D array with x, n, and p being number of stocks, number of days, and price on that day after the deductions from previous days and the day of if applicable.
I'm trying to figure out the recurrence first for P(x,i,p) denoting maximum profit by selling x shares by day i with p being the price of stocks on day i, but I'm getting stuck as well trying to figure out patterns by working with small examples.
How can I approach this?
One simple recursive that could solve this problem in O(x * n) is like bellow (Where x is the total amount of stocks and n is the total number of days) :
p = [100, 200, 90, 190]
f = [1, 3, 4, 5, 6, 3, 5]
def max_profit(stocks, day):
if day == 1:
return stocks * (p[day - 1] - f[stocks - 1]), [stocks]
max = 0
history = []
for i in range(0, stocks + 1):
prev_profit, prev_history = max_profit(stocks - i, day - 1)
profit = prev_profit + i * (p[day - 1] * f[stocks - 1])
if profit > max:
max = profit
history = prev_history
history.append(i)
return max, history
print(max_profit(7, 4))
In this algorithm I've considered all the ways that is possible to sell stock, I mean 0 share , 1 share, 2 share , .... x shares, and then called max_profit for previouse day recursively.

How many distinct integers can be formed by rearranging the digits of an integer. [Permutation]

Number should not start with zero.
Example:
Given 123, ans = 3! = 6
[123, 132, 213, 231, 312, 321]
Given 1122, ans = 4!/2! = 6
[1122, 1212, 1221, 2211, 2121, 2112]
Given 100, ans = 1
[010, 001 is NOT allowed]
I was asked to write a java code to solve this problem.
I failed to form a generic algorithm for handling zero. Please help me with a solution and some reading material.
Write a function that finds number of combinations. Suppose its called
Integer com(List numbers); To implement this read this: https://en.wikipedia.org/wiki/Combination
Now to solve the problem, First find Total numbers can be found by a com(listOfNumbers). Then find number of combinations that has 0 in the beginning and then subtract it.
For example:
1122
Total combinations possible com(1122) = 4! / (2! X 2!)
Total numbers starting with 0 = 0
So ans = 4! / (2! X 2!)
001122
Total combinations possible com(001122) = 6! / (2! X 2! X 2!)
Total numbers starting with 0
= fix one 0 in the beginning and find all possible combination for the rest of the numbers
= com(01122) = 5! / (2! X 2!)
So ans = com(001122) - com(01122)
001
Total combinations possible com(001) = 3! / (2!) = 3
Total numbers starting with 0
= fix one 0 in the beginning and find all possible combination for the rest of the numbers
= com(01) = 2! = 2
So ans = com(001) - com(01) = 1
Group your digits like this:
0: m[0] times
1: m[1] times
2: m[2] times
...
9: m[9] times
overall number of digits: N
Then number of permutations is N!, and number of permutations without repeats is
PwR = N! / (m[0]! * m[1]!.. * m[9]!)
Note that there are ZwR permutations with leading zero (consider the same set without one zero (if m[0] = 0, ignore this part):
ZwR = (N-1)! / ((m[0] - 1)! * m[1]!.. * m[9]!)
So number of valid results is
P = PwR - ZwR =
N! / (m[0]! * m[1]!.. * m[9]!) -
(N-1)! / ((m[0] - 1)! * m[1]!.. * m[9]!) =
//separate common multiplier
(N-1)! / ((m[0] - 1)! * m[1]!.. * m[9]!) * (N/m[0] - 1)
For your 001 example:
N=3, m[0]=2, m[1] = 1
P = (3-1)! / (1!*1!) * (3/2 - 1) = 2 * 1/2 = 1

Progressive non-linear algorithm for increasing discount

A system has to support 100 users and the price for support is 3
A system has to support 10 000 users and the price for support is 1
I have to devise an algorithm to give me the price in between so it will gradually rise with the number of users.
I tried to multiply the number of users by 0.0002 to get the discount value and I got
300 users * 0.0002 = 0.06 discount, so price for support = 2.94
total income = 300 * 2.94 = 882
5000 users * 0.0002 = 1 discount, so price for support = 2
total income = 5000 * 2 = 10 000
8000 users * 0.0002 = 1.6 discount, so price for support = 1.4
total income = 8000 * 1.4 = 11 200
10 000 users * 0.0002 = 2 discount, so price for support = 1
total income = 8000 * 1.4 = 10 000
So you see after a given point I am actually having more users but receiving less payment.
I am not a mathematician and I now this is not really a programming question, but I don't know where else to ask this. I will appreciate if someone can help me with any information. Thanks!
price = n * (5 - log10(n)) will work for 100 < n < 10000.
Just make sure you're using base-10 log and not natural (base-e) log. If your language doesn't have base-10 log normally, you can calculate it like this:
function log10(x) { return log(x)/log(10); }.
For 100 users, that's 100 * (5 - log10(100)), which gives 100 * (5 - 2), which is 300.
For 1000 users, that's 1000 * (5 - log10(1000)), which gives 1000 * (5 - 3), which is 2000.
For 10000 users, that's 10000 * (5 - log10(10000)), which gives 10000 * (5 - 4), which is 10000.
Let's pick some more random figures.
2500 users: 2500 * (5 - log10(2500)) gives us 2500 * (5 - 3.39794), which is 4005.
6500 users: 6500 * (5 - log10(6500)) gives us 6500 * (5 - 3.81291), which is 7716.
8000 users: 8000 * (5 - log10(8000)) gives us 8000 * (5 - 3.90309), which is 8775.
Should work out about right for what you're modelling.
Scaling the price per user linearly didn't work as you showed, but you can try scaling the total income linearly instead.
total income for 100 users = 300
total income for 10000 users = 10000
total income for n users = (n-100) / (10000-100) * (10000-300) + 300
You know that the total income for n users is the price for support per user times the number of users, that means, now you have to find the function f(n) such that f(n) * n = (n-100) / (10000-100) * (10000-300) + 300.
And if you have to show that as the total income always increase, the price for support always decrease, just show that f'(n) ≤ 0 when 100 ≤ n ≤ 10000.

Calculating bid price from total cost

I have to following problem to solve. I have to calculate the bidding price from a total price. For example, a client wants to pay 2000$ as a total price, but from this price, there are added costs:
-usage price which is 10% from the bidding price with a minimum of 10$ and a maximum of 50$
-seller price: 2% from bidding price
-added price: 5$ for a bid between 1 and 500
10$ for a bid between 501 and 1000
15$ for a bid between 1001 and 3000
20$ for a bid over 3000$
-storing cost: 100$
from all this, I have to calculate the bidding price for a total of 2000$, for example. I kind of have no clue of how this can be done. Can anyone give me some hints or pieces of answer of what the algorithm should be?
EDIT: ok I got how to calculate the algebra, now where i'm stuck is how to write the algorithm in code or pseudo-code. Anyone got a hint?
You can express all those costs as a function of the bid price. Generate a giant equation that is the sum of all those functions and solve for a particular final value, e.g.:
usage cost(bid) = PIN(bid*0.10, 10, 50)
seller cost(bid) = bid*.02
added cost(bid) = PIN(ceiling(bid/500)*5, 5, 10) + PIN(ceiling((bid - 1000)/2000)*5, 0, 10)
storing cost(bid) = 100
So the final cost is something like:
final cost(bid) = PIN(bid*.1, 10, 50) + pin(ceiling(bid/500)*5, 5, 20) + PIN(ceiling((bid - 1000)/2000)*10, 0, 20) + bid*.02 + 100 + bid
Solve for a particular value and you're done.
For example, if you want the total cost to be $2000:
2000 = PIN(bid*.1, 10, 50) + pin(ceiling(bid/500)*5, 5, 10) + PIN(ceiling((bid - 1000)/2000)*5, 0, 10) + bid*.02 + 100 + bid.
Bid must be at least > 1500 and < 2000, which works out nicely since we can make those PIN sections constant:
2000 = 50 + 10 + 5 + 100 + bid*1.02
1835 = bid*1.02
bid = 1799.0196078431372549019607843137
The PIN expressions are the hardest to factor out, so you might have to guess a few times until you get something that narrows down the range of bids you want to calculate.

Resources