Question

In: Computer Science

(a) You have a crisis of conscience and you have decided to donate the funds to...


(a)
You have a crisis of conscience and you have decided to donate the funds to charity. You want
to divide D dollars (in integer values) to different charities. You want to give at most L dollars to
one single charity. You are concerned that the FBI will come knocking at your door at any
moment, so you want to donate the larger amounts as soon as possible (and you want to
donate the lesser amounts later on). In other words, your first donation is larger than or equal
to your second donation, and so on. In pseudo-code, write a dynamic programing algorithm to
compute the number of ways you can divide the funds. Can you write a solution in O(DL)?
Explain your answer.

(b) Write a python procedure for (a) that takes an input D and L and returns the number of
ways of dividing the funds. Output your solution when D = 1000 and L = 15.

Solutions

Expert Solution

a.) We can donate at most 'L' dollars at once.

We can donate 1 to min(L, D) dollars at a time, where 'D' is the remaining amount of money that we have.

If we have donated 'K' dollars in current donation we must have to donated less that or equal to "K" dollars in next donation.

Keeping these points in mind, lets design algo.

Algorithm:-

  • Lets call function name "TotalNumberOfWays(D, last)" that will return number of ways with "D" dollars, "last" dollars donated in previous donation.
  • We can pick 1 to min(last, D) amount to donate in current donation, Initial value of will be "L". Lets say we have donated "K" amount of money in current donation than "TotalNumberOfWays(D, K)" will be call for next donation.
  • We will store value computed in call "TotalNumberOfWays(D, last)" so that we do not need to compute this value again (Dynamic programming). Lets call it "DP[][]" array and DP[D][last] will store number of ways for function call "TotalNumberOfWays(D, last)".

Psuedocode:-

TotalNumberOfWays(D, last):

if D == 0:

return 1

else if DP[D][last] is already computed:

return DP[D][last]

ways = 0

for K = last to 1:

ways += TotalNumberOfWays(D, K)

DP[D][last] = ways

return DP[D][ways]

b.) Python3 code:-

DP = [[None for i in range(16)]for j in range(1001)]
def TotalNumberOfWays(D, last):
   if D == 0:
       return 1
   elif DP[D][last] is not None:
       return DP[D][last]
   ways = 0
   for k in range(min(D, last), 0, -1):
       ways += TotalNumberOfWays(D-k, k)
   DP[D][last] = ways
   return DP[D][last]

ways = TotalNumberOfWays(1000, 15)
print("Total number of ways of donations : ", ways)

Screenshot of output:-


Related Solutions

After a recent sale of your business, you decided to donate $1 million to your local...
After a recent sale of your business, you decided to donate $1 million to your local university to fund a scholarship fund. Assume the university can earn 6% interest rate on its investments. You want the scholarship to be paid out annually, and the first scholarship to be paid out in one year. You also want that the annual scholarship amount increases with inflation thereafter. The inflation is expected at 2%. What will be the amount of the first year...
How do you now define ‘conscience’? How do you understand conscience to play out for you...
How do you now define ‘conscience’? How do you understand conscience to play out for you in making a moral decision? Also, most students indicate to me that they were never taught how to evaluate moral decisions, whether in the past or in the future. Given that, how does the schema of intention/means/circumstances help you in evaluating or coming to a moral decision?
In addition to cash contributions to charity, Dean decided to donate shares of stock and a...
In addition to cash contributions to charity, Dean decided to donate shares of stock and a portrait painted during the earlier part of the last century. Dean purchased the stock and the portrait many years ago as investments. Dean reported the following recipients: [The following information applies to the questions displayed below.] In addition to cash contributions to charity, Dean decided to donate shares of stock and a portrait painted during the earlier part of the last century. Dean purchased...
16. In addition to cash contributions to charity, Dean decided to donate shares of stock and...
16. In addition to cash contributions to charity, Dean decided to donate shares of stock and a portrait painted during the earlier part of the last century. Dean purchased the stock and portrait many years ago as investments. Dean reported the following recipients: Charity Property Cost FMV State University Cash $ 16,000 $ 16,000 Red Cross Cash 15,000 15,000 State History Museum Painting 5,100 86,000 City Medical Center Dell stock 38,000 27,000 b. Assume that Dean’s AGI this year is...
During the credit crisis, Barclays decided not to have its “head above the parapet.” Who within...
During the credit crisis, Barclays decided not to have its “head above the parapet.” Who within the firm should have responsibility for making such a decision?
Explain the role of the hedge funds in 2008 Global Crisis.
Explain the role of the hedge funds in 2008 Global Crisis.
Jason has decided to donate his kidney to his brother Matthew so that he can live....
Jason has decided to donate his kidney to his brother Matthew so that he can live. How would the psychoanalytic, social learning, and cognitive theorists explain his actions?
Why was there a run on money market funds at the beginning of the current crisis?...
Why was there a run on money market funds at the beginning of the current crisis? Be sure to describe what the money market is, who buys and sells money market funds, and why this may have been the asset that experienced runs (versus other financial assets).
At the height of the housing crisis in the US, in 2009-2010 the Government decided for...
At the height of the housing crisis in the US, in 2009-2010 the Government decided for a temporary extension of unemployment benefits to 99 weeks, from the standard six months.    (In parallel way, during this Corona time, millions of unemployed seek the same kind of benefits. ) a) What do you think of this decision? What are the positive and negative impacts of this decision to the society and the overall economy? b) How would J.M. Keynes react to...
At the height of the housing crisis in the US, in 2009-2010 the Government decided for...
At the height of the housing crisis in the US, in 2009-2010 the Government decided for a temporary extension of unemployment benefits to 99 weeks, from the standard six months.    (In parallel way, during this Corona time, millions of unemployed seek the same kind of benefits. ) What do you think of this decision? What are the positive and negative impacts of this decision to the society and the overall economy? How would J.M. Keynes react to this extension?...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT