Skip to content

Instantly share code, notes, and snippets.

@ara-ta3
Last active August 29, 2015 14:07
Show Gist options
  • Save ara-ta3/3c7d403f039bf1244709 to your computer and use it in GitHub Desktop.
Save ara-ta3/3c7d403f039bf1244709 to your computer and use it in GitHub Desktop.
(define (selectOptimalCoupons amount myCouponList)
(consume amount (sort myCouponList >) (list))
)
(define (consume amount coupons usedCoupons)
(if (null? coupons)
usedCoupons
(if (>= amount (car coupons))
(consume (- amount (car coupons)) (cdr coupons) (append usedCoupons (list (car coupons))))
(consume amount (cdr coupons) usedCoupons))
)
)
(print (selectOptimalCoupons 100 '()))
(print (selectOptimalCoupons 100 '(50 50 100)))
(print (selectOptimalCoupons 470 '(50 50 50 100 100 100 100 100 500)))
(print (selectOptimalCoupons 1230 '(50 50 50 50 50 50 100 100 100 100 100 100 500 500)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment