iT邦幫忙

2025 iThome 鐵人賽

DAY 7
0
Software Development

clojure 30 days系列 第 7

clojure 30 days - day 5

  • 分享至 

  • xImage
  •  

Problem Description

After a hard quarter in the office you decide to get some rest on a vacation.
So you will book a flight for you and your girlfriend and try to leave all
the mess behind you.

You will need a rental car in order for you to get around in your vacation.
The manager of the car rental makes you some good offers.

Every day you rent the car costs $40. If you rent the car for 7 or more days,
you get $50 off your total. Alternatively, if you rent the car for 3 or more
days, you get $20 off your total.

Write a code that gives out the total amount for different days(d).

Note

  • keywords: let / cond

Implementation


(defn rental-car-cost [d]
 ;; Step 1: Calculate base cost using let binding
 (let [base (* d 40)]
   ;; Step 2: Apply discounts based on rental duration using cond
   (cond
     ;; Step 3a: Check for 7+ days discount ($50 off)
     (>= d 7) (- base 50)
     ;; Step 3b: Check for 3+ days discount ($20 off)
     (>= d 3) (- base 20)
     ;; Step 3c: Default case - no discount
     :else base)))

;test
(defn tester [act exp]
 (= (rental-car-cost act) exp))

(comment
 (tester 1 40)
 (tester 3 100)
 (tester 8 270))


上一篇
clojure 30 days - day 4
系列文
clojure 30 days7
圖片
  熱門推薦
圖片
{{ item.channelVendor }} | {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言