Files
wehub-resource-sync dde272c4b8
i18n - Build Validation / Validate i18n Builds (24) (push) Has been cancelled
CI - Node.js / Lint (24) (push) Has been cancelled
CI - Node.js / Build (24) (push) Has been cancelled
CI - Node.js / Test (24) (push) Has been cancelled
CI - Node.js / Test - Upcoming Changes (24) (push) Has been cancelled
CI - Node.js / Test - i18n (italian, 24) (push) Has been cancelled
CI - Node.js / Test - i18n (portuguese, 24) (push) Has been cancelled
CD - Docker - GHCR Images / Build and Push Images (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 11:55:53 +08:00

3.4 KiB

id, title, challengeType, dashedName
id title challengeType dashedName
6a15cadf5f240d05a2649557 Challenge 313: Rental Cost 29 challenge-313

--description--

Given a rental timestamp, a return timestamp, and a rental tier, return the total cost of the rental including any late fees.

  • Given timestamps are UTC ISO strings, for example: "2026-06-18T18:30:00Z".
  • The rental tier is the number of days before the rental is due back: 1, 3, or 7.
  • Rentals are due back by 12:00 PM UTC or earlier on the last day of the rental period. For example, a 1-day rental checked out at any time on March 15 is due back by 12:00 PM UTC on March 16.
  • Each day past the due date and time incurs a late fee.

Pricing is as follows:

Tier Base cost Late fee per day
1 day $4.99 $3.99
3 days $3.99 $2.99
7 days $2.99 $0.99

Return the total cost rounded to two decimal places in the format "$D.CC".

--hints--

get_rental_cost("2026-06-18T18:30:00Z", "2026-06-19T10:30:00Z", 1) should return "$4.99".

({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(get_rental_cost("2026-06-18T18:30:00Z", "2026-06-19T10:30:00Z", 1), "$4.99")`)
}})

get_rental_cost("2026-06-18T14:30:00Z", "2026-06-20T12:30:00Z", 1) should return "$12.97".

({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(get_rental_cost("2026-06-18T14:30:00Z", "2026-06-20T12:30:00Z", 1), "$12.97")`)
}})

get_rental_cost("2026-06-18T10:15:00Z", "2026-06-18T19:45:00Z", 3) should return "$3.99".

({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(get_rental_cost("2026-06-18T10:15:00Z", "2026-06-18T19:45:00Z", 3), "$3.99")`)
}})

get_rental_cost("2026-06-18T15:20:00Z", "2026-06-23T08:10:00Z", 3) should return "$9.97".

({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(get_rental_cost("2026-06-18T15:20:00Z", "2026-06-23T08:10:00Z", 3), "$9.97")`)
}})

get_rental_cost("2026-06-18T12:00:00Z", "2026-06-25T12:00:00Z", 7) should return "$2.99".

({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(get_rental_cost("2026-06-18T12:00:00Z", "2026-06-25T12:00:00Z", 7), "$2.99")`)
}})

get_rental_cost("2026-06-18T08:00:00Z", "2027-06-18T14:00:00Z", 7) should return "$358.40".

({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(get_rental_cost("2026-06-18T08:00:00Z", "2027-06-18T14:00:00Z", 7), "$358.40")`)
}})

--seed--

--seed-contents--

def get_rental_cost(rented, returned, tier):

    return rented

--solutions--

from datetime import datetime, timedelta
import math

def get_rental_cost(rented, returned, tier):
    pricing = {
        1: {"base": 4.99, "late": 3.99},
        3: {"base": 3.99, "late": 2.99},
        7: {"base": 2.99, "late": 0.99},
    }
    rented_dt = datetime.fromisoformat(rented.replace("Z", "+00:00"))
    returned_dt = datetime.fromisoformat(returned.replace("Z", "+00:00"))
    due = rented_dt + timedelta(days=tier)
    due = due.replace(hour=12, minute=0, second=0, microsecond=0)
    base = pricing[tier]["base"]
    late = pricing[tier]["late"]
    if returned_dt <= due:
        return f"${base:.2f}"
    days_late = math.ceil((returned_dt - due).total_seconds() / 86400)
    return f"${base + days_late * late:.2f}"