Question

In: Computer Science

Python Coding Question: Find the minimum number of coins that make a given value.

Python Coding Question: Find the minimum number of coins that make a given value.

Solutions

Expert Solution

Python Code:

import sys  
  
# l is size of coins array (number of different coins) 
def minimumCoins(coins, l, value): 
      
    # table[i] will be storing the minimum number of coins required for i value.  
    # So table[value] will have result 
    table = [0 for i in range(value + 1)] 
  
    # Base case (If given value is 0) 
    table[0] = 0
  
    # Initialize all table values as Infinite 
    for i in range(1, value + 1): 
        table[i] = sys.maxsize 
  
    # Compute minimum coins required for all values from 1 to value 
    for i in range(1, value + 1): 
          
        # Go through all coins smaller than i 
        for j in range(l): 
            if (coins[j] <= i): 
                sub_result = table[i - coins[j]] 
                if (sub_result != sys.maxsize and sub_result + 1 < table[i]): 
                    table[i] = sub_result + 1
    return table[value] 
  
# We will take the coin denominations to be 1,5,10
# The denominations can be chosen as the user desire 
coins = [1,2,5,10]
print("The coin denominations to be considered are: ", coins)
l = len(coins) 
value = 44
print("The value to be made: ", value)
print("Minimum coins required is: ",minimumCoins(coins, l, value)) 

Output:

Test case 1:

The coin denominations to be considered are: [1, 2, 5, 10]
The value to be made: 44
Minimum coins required is: 6

Test case 2:

The coin denominations to be considered are: [1, 5, 10]
The value to be made: 12
Minimum coins required is: 3


Related Solutions

This is a question about coding in python. I think that the question is asking me...
This is a question about coding in python. I think that the question is asking me to code the following: Implement the scalar product of 2 vectors in 2-space, using two tuple parameters (the function scalarProduct2). Write a docstring; then write some test function calls (can I pick any data for this? - the question gave these examples data examples: (1., 1.) dot (2,3) = (1.,1.) dot (2,0) = (1., 1.) dot (0,2) = (1., 1.,) dot (4,5) = );...
Solve using coding in R script: 1) Given a standard normal distribution, find the value of...
Solve using coding in R script: 1) Given a standard normal distribution, find the value of k such that P(Z < k) = 0.0197. 2) Given a normal distribution with mu = E(X) = 32 and sigma^2 = V(X) = 30, find the normal curve area to the left of x = 31. Report your code as well as your final answer (4 decimals). 3) A company pays its employees an average wage of $17.90 an hour with a standard...
Coding in python question def generate_project_data_files(expected_grade_file_path, std_dev, num_projects, folder_path): """ For given student expected grades, generate...
Coding in python question def generate_project_data_files(expected_grade_file_path, std_dev, num_projects, folder_path): """ For given student expected grades, generate the grades as described in generate_assignment_data given std_dev. For this method, you will generate multiple files for each project. For example, if num_projects = 4, then you should generate four files according to the following naming convention: "P_0.csv" ... "P_3.csv" . The files should be written in the folder defined by folder_path. For example, given num_projects = 1 and folder_path="data", you should create one...
1- Three coins are tossed once. (a) Find the number of macrostates. Explain. (b) Find the...
1- Three coins are tossed once. (a) Find the number of macrostates. Explain. (b) Find the number of microstates. Explain. (c) What is the probability of getting at least one head? Explain. (d) What is the probability of getting one tail? Explain. (e) What is the probability of getting the same face? Explain
Python Coding Question (Data Science) Hello I am having difficulty writing a code in python to...
Python Coding Question (Data Science) Hello I am having difficulty writing a code in python to do a specific task. I have two text files, Positive.txt and Practice_forhw1.txt. I want to write a script that will check if any words in Practice_forhw1.txt match any of the words in Positive.txt then the word would get replaced with a "+1" in the Practice_forhw1.txt and then print out the Practice_forhw1.txt.  (i.e if the word "happy" is in both Positive.txt and Practice_forhw1.txt then I want...
<Python coding question string practice> Can anyone please answer these python string questions?? 1. Write a...
<Python coding question string practice> Can anyone please answer these python string questions?? 1. Write a function called username that takes as parameters a person's first and last names and prints out his or her username formatted as the last name followed by an underscore and the first initial. For example, username("Martin", "freeman") should print the string "freeman_m". 2. Write a function called letters that takes as a parameter a string and prints out the characters of the string, one...
python coding Suppose a list of positive numbers is given like the following list (remember this...
python coding Suppose a list of positive numbers is given like the following list (remember this is only an example and the list could be any list of positive numbers) exampleList: 15 19 10 11 8 7 3 3 1 We would like to know the “prime visibility” of each index of the list. The “prime visibility” of a given index shows how many numbers in the list with indexes lower than the given index are prime. For instance, in...
Toss 5 coins 25 times and note on each throw the number of heads. Make a...
Toss 5 coins 25 times and note on each throw the number of heads. Make a probability distribution of the number of heads. Find mean and variance of that distribution and compare it with the mean and variance of theoretical probability distribution using binomial probability distribution.
Suppose that we want to make change for n cents using the least number of coins....
Suppose that we want to make change for n cents using the least number of coins. The coins are of denominations c 0 , c 1 , . . . , c k for some integers c > 1, and k ≥ 1. (a) Design a greedy algorithm to solve this problem. (b) Prove that your algorithm finds an optimal solution by showing the greedy-choice property and optimal substructure for it. Clearly state each property and then prove them.
Medical coding question: please post a 2 paragraph summary with a minimum of 5 sentences in...
Medical coding question: please post a 2 paragraph summary with a minimum of 5 sentences in each paragraph related to the general format of the ICD-10-CM manual.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT