﻿ [Code Challenge] "Plan Your Dream Wedding" | InterSystems Developer
Discussion
· May 13

# [Code Challenge] "Plan Your Dream Wedding"

Hello,

I would like to propose you a challenge.

It has been created by a web user called "uttumuttu" and it is being explained here: https://www.codewars.com/kata/66314d6b7cb7030393dddf8a

Here is the proposed challenge copied and pasted:

### DESCRIPTION:

This kata was created as a contender for the EPIC Challenge 2024.

## Background

It's your dream to have a magical wedding day, and you're willing to spend every last penny on it — while simultaneously planning for a stable retirement income.

Your monthly surplus income (i.e., income after all taxes and mandatory monthly costs) is `C` dollars (e.g., `C = \$2,500`), and will remain so until your retirement. At the end of each month, you will invest the surplus income in your savings account with a certain rate of monthly return `r > 0` given in percentage (e.g., `r = 0.5` percent per month), growing with compound interest. Your savings account is initially empty.

You are planning to retire in `T` years (e.g., `T = 35`), and have calculated that upon retirement, you need minimum `S` dollars of savings to finance a stable retirement (e.g., `S = \$3,000,000`).

You are also planning to have a wedding in `W` years (e.g., `W = 5`), with `1 <= W <= T`. What is the largest amount of savings you can spend on the wedding while ensuring that you will also have enough money for your retirement?

Notice that the savings spent on the wedding no longer produce investment returns at rate `r` per month. Also, you cannot finance the wedding on debt — you can only use the savings up to the wedding date!

## Example 1

Let's say the function `max_wedding_cost(C, r, S, T, W)` gets the above inputs `C = 2_500` (dollars per month), `r = 0.5` (percent per month), `S = 3_000_000` (dollars), `T = 35` (years) and `W = 5` (years).

If you spend zero dollars on your wedding, you will have approximately `3_561_775.75` dollars in your savings account upon your retirement, or `561_775.75` dollars over the required `S = 3_000_000` dollars.

It turns out that the maximum amount you can spend on your wedding day is approximately `93_278.33` dollars. Your answer should be within `0.01%` of the reference answer. See notes for further details on accuracy.

## Example 2

Suppose in the above example `S = 2_000_000`, i.e., you need a million dollars less for your retirement.

In this case it turns out that you can blow all your savings up to wedding day — a total of approximately `174_425.08` dollars — on your wedding. Even though you have to re-start saving from scratch, you can still reach your two-million-dollar retirement goal.

## Notes

Your magical savings account operates on double-precision floating point arithmetic, so you don't have to deal with rounding numbers to cents. Just return the final result as a floating point number without any rounding; it only needs to be within 0.01% of the reference solution.

It is guaranteed that with zero wedding costs, you will reach your retirement goal `S`. Parameters will have the following ranges: `100 <= C <= 3_000``0.1 <= r <= 1.0``100_000 <= S <= 3_000_000``10 <= T <= 40`, and `1 <= W <= T`.

I have solved it by myself in Java, and I was curious about how could you solve it using ObjectScript.

The initial code provided by the challenge in Java is as follows:

``````class Kata
{
public static double maxWeddingCost(int C, double r, int S, int T, int W)
{
return 0;
}
}``````

The approach I used was this, I renamed the variables to make it clearer:

``````public class Kata {
public static double calculateTotalSavingsAtTime(double initialPrincipal, double monthlyInterestRate, double monthlyDeposit, int timeMonths) {
return monthlyDeposit * ((Math.pow(1 + monthlyInterestRate, timeMonths) - 1) / monthlyInterestRate) + initialPrincipal * Math.pow(1 + monthlyInterestRate, timeMonths);
}

public static double calculateRequiredPrincipal(double targetSavings, double monthlyInterestRate, double monthlyDeposit, int timeMonths) {
return (targetSavings - monthlyDeposit * ((Math.pow(1 + monthlyInterestRate, timeMonths) - 1) / monthlyInterestRate)) / Math.pow(1 + monthlyInterestRate, timeMonths);
}

public static double maxWeddingCost(int monthlySurplusIncome, double monthlyInterestRate, int retirementSavingsGoal, int yearsToRetirement, int yearsToWedding) {
double savingsAtWedding = calculateTotalSavingsAtTime(0, monthlyInterestRate / 100, monthlySurplusIncome, 12 * yearsToWedding);
double remainingSavingsNeeded = calculateRequiredPrincipal(retirementSavingsGoal, monthlyInterestRate / 100, monthlySurplusIncome, 12 * (yearsToRetirement - yearsToWedding));
double maxWeddingCost = savingsAtWedding - Math.max(0, remainingSavingsNeeded);
return maxWeddingCost;
}
}``````

How would you like to solve it, using ObjectScript?

Discussion (6)1

Being happy married for 55 years I find the subject of such a calculation just disgusting.

I just would refuse to marry a partner calculating impact of the wedding costs on his retirement.
I'd rather prefer one that attempts to raise his income by working more and harder
and not thinks of forecasting retirement before the wedding.

Hello everyone,

I appreciate your responses and perspectives on the challenge I posted. It's important to clarify that my intention was simply to share a programming challenge out of curiosity, and the context or the scenario of the programming challenge was created by another user. It's not a reflection of personal beliefs or values, but rather an abstract context in which this programming challenge was formulated.

Robert, I completely understand your viewpoint, and I respect your perspective based on your experiences. Marriage is indeed a deeply personal matter, and financial considerations may vary greatly from person to person.

Ba, thank you for sharing your experience. It's wonderful that your parents were able to support you during your wedding, and it's a testament to the importance of family support during such significant life events.

Luc, your perspective adds an interesting angle to the discussion. Indeed, the decision to allocate resources, whether for a wedding or other purposes, is deeply intertwined with individual priorities and values.

Once again, thank you all for engaging in the conversation, and I hope we can continue to share insights and perspectives in a respectful manner.

To be sincere and to be honest I did only posted it because I was curious about how could you solve it using ObjectScript. ✅✅✅🟩🟩🟩✅✅✅

Sorry if the context is not the best one.

At my wedding all cost were covered by my parents.
And I was very grateful for that.

strange habit to spend money for loosing freedom
😈

The calculateTotalSavingsAtTime method can be shortened because initialPrincipal is always 0:

```    public static double calculateTotalSavingsAtTime(double initialPrincipal, double r, double monthlyDeposit, int timeMonths) {
return monthlyDeposit * ((Math.pow(1 + r, timeMonths) - 1) / r) + initialPrincipal * Math.pow(1 + r, timeMonths);
}```
How would you like to solve it, using ObjectScript?

For example, like this: