Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8082859
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T17:15:39+00:00 2026-06-05T17:15:39+00:00

This problem is same as asked in here . Given a list of coins,

  • 0

This problem is same as asked in here.

Given a list of coins, their values (c1, c2, c3, … cj, …), and the total sum i. Find the minimum number of coins the sum of which is i (we can use as many coins of one type as we want), or report that it’s not possible to select coins in such a way that they sum up to S.

I”m just introduced to dynamic programming yesterday and I tried to make a code for it.

# Optimal substructure: C[i] = 1 + min_j(C[i-cj])
cdict = {}
def C(i, coins):

    if i <= 0:
        return 0

    if i in cdict:
        return cdict[i]
    else:
        answer = 1 + min([C(i - cj, coins) for cj in coins])
        cdict[i] = answer
        return answer

Here, C[i] is the optimal solution for amount of money ‘i’. And available coins are {c1, c2, … , cj, …}
for the program, I’ve increased the recursion limit to avoid maximum recursion depth exceeded error. But, this program gives the right answer only someones and when a solution is not possible, it doesn’t indicate that.

What is wrong with my code and how to correct it?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T17:15:40+00:00Added an answer on June 5, 2026 at 5:15 pm

    This is a great algorithms question, but to be honest I don’t think your implementation is correct or it could be that I don’t understand the input/output of your function, for that I apologize.

    heres a modified version of your implementation.

    def C(i, coins, cdict = None):
        if cdict == None:
            cdict = {}
        if i <= 0:
            cdict[i] = 0
            return cdict[i]
        elif i in cdict:
            return cdict[i]
        elif i in coins:
            cdict[i] = 1
            return cdict[i]
        else:
            min = 0
            for cj in coins:
                result = C(i - cj, coins)
                if result != 0:
                    if min == 0 or (result + 1) < min:
                        min = 1 + result
            cdict[i] = min
            return cdict[i]
    

    This is my attempt at solving a similar problem, but this time returning a list of coins. I initially started with a recursive algorithm, which accepts a sum and a list of coins, which may return either a list with the minimun number of coins or None if no such configuration could be found.

    def get_min_coin_configuration(sum = None, coins = None):
    if sum in coins: # if sum in coins, nothing to do but return.
        return [sum]
    elif max(coins) > sum: # if the largest coin is greater then the sum, there's nothing we can do.
        return None
    else: # check for each coin, keep track of the minimun configuration, then return it.
        min_length = None
        min_configuration = None
        for coin in coins:
            results = get_min_coin_configuration(sum = sum - coin, coins = coins)
            if results != None:
                if min_length == None or (1 + len(results)) < len(min_configuration):
                    min_configuration = [coin] + results
                    min_length = len(min_configuration)
        return min_configuration
    

    ok now lets see if we can improve it, by using dynamic programming (I just call it caching).

    def get_min_coin_configuration(sum = None, coins = None, cache = None):
    if cache == None: # this is quite crucial if its in the definition its presistent ...
        cache = {}
    if sum in cache:
        return cache[sum]
    elif sum in coins: # if sum in coins, nothing to do but return.
        cache[sum] = [sum]
        return cache[sum]
    elif max(coins) > sum: # if the largest coin is greater then the sum, there's nothing we can do.
        cache[sum] = None
        return cache[sum]
    else: # check for each coin, keep track of the minimun configuration, then return it.
        min_length = None
        min_configuration = None
        for coin in coins:
            results = get_min_coin_configuration(sum = sum - coin, coins = coins, cache = cache)
            if results != None:
                if min_length == None or (1 + len(results)) < len(min_configuration):
                    min_configuration = [coin] + results
                    min_length = len(min_configuration)
        cache[sum] = min_configuration
        return cache[sum]
    

    now lets run some tests.

    assert all([ get_min_coin_configuration(**test[0]) == test[1] for test in
    [({'sum':25,  'coins':[1, 5, 10]}, [5, 10, 10]),
     ({'sum':153, 'coins':[1, 5, 10, 50]}, [1, 1, 1, 50, 50, 50]),
     ({'sum':100, 'coins':[1, 5, 10, 25]}, [25, 25, 25, 25]),
     ({'sum':123, 'coins':[5, 10, 25]}, None),
     ({'sum':100, 'coins':[1,5,25,100]}, [100])] ])
    

    granted this tests aren’t robust enough, you can also do this.

    import random
    random_sum = random.randint(10**3, 10**4)
    result = get_min_coin_configuration(sum = random_sum, coins = random.sample(range(10**3), 200))
    assert sum(result) == random_sum
    

    its possible that the no such combination of coins equals our random_sum but I believe its rather unlikely …

    Im sure there are better implementation out there, I tried to emphasize readability more than performance.
    good luck.

    Updated
    the previous code had a minor bug its suppose to check for min coin not the max, re-wrote the algorithm with pep8 compliance and returns [] when no combination could be found instead of None.

    def get_min_coin_configuration(total_sum, coins, cache=None):  # shadowing python built-ins is frowned upon.
        # assert(all(c > 0 for c in coins)) Assuming all coins are > 0
        if cache is None:  # initialize cache.
            cache = {}
        if total_sum in cache:  # check cache, for previously discovered solution.
            return cache[total_sum]
        elif total_sum in coins:  # check if total_sum is one of the coins.
            cache[total_sum] = [total_sum]
            return [total_sum]
        elif min(coins) > total_sum:  # check feasibility, if min(coins) > total_sum
            cache[total_sum] = []     # no combination of coins will yield solution as per our assumption (all +).
            return []
        else:
            min_configuration = []  # default solution if none found.
            for coin in coins:  # iterate over all coins, check which one will yield the smallest combination.
                results = get_min_coin_configuration(total_sum - coin, coins, cache=cache)  # recursively search.
                if results and (not min_configuration or (1 + len(results)) < len(min_configuration)):  # check if better.
                    min_configuration = [coin] + results
            cache[total_sum] = min_configuration  # save this solution, for future calculations.
        return cache[total_sum]
    
    
    
    assert all([ get_min_coin_configuration(**test[0]) == test[1] for test in
                 [({'total_sum':25,  'coins':[1, 5, 10]}, [5, 10, 10]),
                  ({'total_sum':153, 'coins':[1, 5, 10, 50]}, [1, 1, 1, 50, 50, 50]),
                  ({'total_sum':100, 'coins':[1, 5, 10, 25]}, [25, 25, 25, 25]),
                  ({'total_sum':123, 'coins':[5, 10, 25]}, []),
                  ({'total_sum':100, 'coins':[1,5,25,100]}, [100])] ])
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have exactly the same problem asked here. Problem using Managed C++ (.Net 2.0)
I'm running in to the same problem this individual has . Specifically, I'm trying
Seems like just yesterday I had this same problem with Play! v1. After trying
I am having the same problem as this post but the answer does not
This is pretty much the same problem i have, except with very different code:
I have basically the same problem outlined in this question, however I am using
I 've got the same problem like in this question: SSL Error on Port
I've run into the same problem as found in this question . However, I
I have faced the same problem many times. The Same Problem was With This
I've got another problem in the same code... I'm getting this error: initialization method

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.