-1

Can somebody explain me a code in Racket in which I change 2 Euro and a 1 Euro Coin with 10 Cents and 20 Cents?

I can only change one time a 2 Euro coin and one time a 1 Euro coin. With 20 Cents and 10 Cents. Here is my code:

(define (change sum coins) 
  (if (< sum 200) 
    0 
    (if (= sum 200) 
      1 
      (if (and (> sum 0) 
               (<= (length coins) 0)) 
        0 
        (+ (change (- sum (car coins)) (cdr coins)) 
           (change sum (cdr coins))))))) 

(change 200'(20 10)) 
(change 100'(20 10)) 

So what I have to modify? Thanks for your help!

Will Ness
  • 70,110
  • 9
  • 98
  • 181
  • you can browse through the tag that I added and its info page, for more info. :) or do a search like e.g. [this](https://stackoverflow.com/search?q=%5Bcoin-change%5D+and+%5Bracket%5D) or [this one](https://stackoverflow.com/search?q=%5Bcoin-change%5D+and+%5Bscheme%5D+is%3Aq). – Will Ness Sep 08 '21 at 17:55
  • or [this](https://stackoverflow.com/search?tab=votes&q=%5bcoin-change%5d%20and%20%5bscheme%5d%20is%3aa). :) – Will Ness Sep 08 '21 at 18:25
  • Thank you for the help! I will try to figure out but I am a novice with racket :) – Andra Botean Sep 08 '21 at 21:15
  • I tried to paste the code of Coin Change algorithm with limited coins but do not know how to implement my conditions (2 Euro, 1 Euro, 20 Cents, 10 Cents). Every example or explanation is welcome. Thanks! – Andra Botean Sep 08 '21 at 21:22
  • your problem is probably with unlimited coins. see my 2nd comment's link for that. :) – Will Ness Sep 09 '21 at 12:40
  • So I did my best and tried to put code together but I does not want to work out. It is with limited coins. I can only change one time a 2 Euro coin and one time a 1 Euro coin. With 20 Cents and 10 Cents. Here is my code: (define (change sum coins) (if (< sum 200) 0 (if (= sum 200) 1 (if (and (> sum 0) (<= (length coins) 0)) 0 (+ (change (- sum (car coins)) (cdr coins)) (change sum (cdr coins))))))) (change 200'(20 10)) (change 100'(20 10)) So what I have to modify? Thanks for your help! – Andra Botean Sep 09 '21 at 18:16
  • 1
    Your code belongs in the question, but "If the sum is less than 200, there are no ways to give change; if the sum is exactly 200, there is one way; otherwise, ..." doesn't sound right to me. (You should not be using any hard-coded values at all.) – molbdnilo Sep 10 '21 at 09:07
  • yours is not the limited coins case. that would be if you are only given a limited supply of 10 and 20 cents, like "use no more than 5 20-cent coins and no more than 20 10-cent coins to change the sum of 1 Euro". in your case there's no limitation of the number of small coins. and the fact that 1 Euro or 2 Euros are coins is irrelevant, the point is to get a given sum in value. – Will Ness Sep 10 '21 at 12:42

1 Answers1

0

Since the 200 target is hardcoded, you should start with the current sum of 0, and count up, not down:

(define (change sum coins) 
  (if (> sum 200)             ; invalid solution
    0 
    (if (= sum 200)              ; good solution
      1 
      (if (= (length coins) 0)     ; no more coins to use
        0 
        (+ (change (+ sum (car coins))  ; use first coin,
                   coins)               ; _increasing_ the sum
           (change sum 
                   (cdr coins)))))))      ; don't use first coin any more

Now we have

> (change 0 '(10))
1
> (change 0 '(20 10))
11
> (change 0 '(7))
0
> 

Now you can abstract away the target value of 200, making it a parameter in a new function which will have to make this function its inner definition so it has access to its parameter:

(define (coin-change-ways target coins)
  (define (change sum coins) 
     ....
     ....)
  (change 0 coins))

See this for more explanations.

Will Ness
  • 70,110
  • 9
  • 98
  • 181
  • Thank you very much! Now it makes more sense to me. I have a question. I am quite new to Racket and I would be really glad to learn and understand how to build functions and recursions. Could you give me any suggestions how I could figure this out? Andra – Andra Botean Sep 10 '21 at 16:13
  • see https://stackoverflow.com/tags/htdp/info, https://stackoverflow.com/tags/sicp/info, https://stackoverflow.com/tags/racket/info, https://stackoverflow.com/tags/scheme/info. – Will Ness Sep 10 '21 at 16:15
  • Thank you so much for your help. I bookmarked the resources and I am looking forward to understand the materia. I accepted the answer and hope that it will remain here on the forum. :) – Andra Botean Sep 10 '21 at 16:34
  • you're welcome. :) next time be sure to include your code and tests right from the start. I'm pretty sure you wouldn't have received the downvotes in that case. people here usually want to help but a question without any specifics in it just isn't answerable. and that can be frustrating and give one a feeling of wasted time. :) – Will Ness Sep 10 '21 at 17:43
  • Thank you! I am new here and do not know how it works, but I am more than happy to follow the rules next time. – Andra Botean Sep 10 '21 at 19:17
  • @AndraBotean the most important thing you did do: you stayed involved. happy trails! :) – Will Ness Sep 10 '21 at 19:24