116k views
0 votes
You have opened a credit card company with great cashback benefits for college students. Your company provides yearly cashback to the customer based on how much they spend in a year. The cashback works as follows: - 1% for the first $500 spent - 1.5% for next $500 (i.e. between $500−$1000 ) - 2% for next $1000 (i.e. between $1000−$2000 ) - 3% for everything above $2000 Example: Let us say a student spent $100 using this particular credit card in a year, then the student will get $1 as cashback. Another student who spent $2500 will get $47.5 Implement the function cashBack that takes year_spent as a parameter and computes the corresponding cashback amount.

User Thevikas
by
7.4k points

2 Answers

4 votes

Final answer:

To calculate yearly cashback, a function called cashBack uses different cashback percentages for specified spending ranges: 1% for the first $500, 1.5% for $500 to $1000, 2% for $1000 to $2000, and 3% for amounts above $2000. The total cashback is the sum of cashback calculated within each spending range.

Step-by-step explanation:

Implementing the Cashback Function

To implement a function cashBack that calculates yearly cashback based on the amount spent, we need to consider the different cashback percentages for specific spending ranges. Here is a step-by-step explanation to create such a function:

  1. Calculate 1% cashback for the first $500 spent.
  2. For the amount between $500 and $1000, calculate 1.5% cashback.
  3. For the amount between $1000 and $2000, calculate 2% cashback.
  4. For any amount above $2000, calculate 3% cashback.

For example, if a student spent $2500 in a year, the cashback would be calculated as:

  • $500 * 1% = $5
  • Next $500 * 1.5% = $7.50
  • Next $1000 * 2% = $20
  • Amount above $2000, which is $500, at 3% = $15

Adding these amounts together gives us the total cashback: $5 + $7.50 + $20 + $15 = $47.5

User PorcupineRending
by
8.1k points
4 votes

Final answer:

To calculate the cashback amount based on spending in a year, you need to implement a function with different cashback rates for different spending ranges.

Step-by-step explanation:

To implement the cashback function, you need to consider different spending ranges and their corresponding cashback rates. Here's how you can do it:

  1. If the year_spent is less than or equal to $500, multiply year_spent by 0.01 to get the cashback amount.
  2. If the year_spent is greater than $500 and less than or equal to $1000, subtract $500 from year_spent, multiply the result by 0.015, and add $5 to get the cashback amount.
  3. If the year_spent is greater than $1000 and less than or equal to $2000, subtract $1000 from year_spent, multiply the result by 0.02, and add $20 to get the cashback amount.
  4. If the year_spent is greater than $2000, subtract $2000 from year_spent, multiply the result by 0.03, and add $40 to get the cashback amount.

For example, if the year_spent is $2500, the cashback amount would be $47.5 ($500 * 0.01 + $500 * 0.015 + $1000 * 0.02 + $500 * 0.03).

User Trey Gramann
by
7.9k points