0
I have a list consist of different integers, and i will give a number, then I need to add the integers in that list
I have a list consist of different integers, and i will give a number, then I need to add the integers in that list, and i wanna know how many elements are used in our list to get our specified number, how can I do it ?. Hellp me out guys
18 RĂŠponses
+ 6
Don't DM me the task instruction, post it đHERE.
+ 3
# not sure but I think you wanna do something like this
https://code.sololearn.com/cFjQ4JcmFfOH/?ref=app
This is just a basic version, you need to find all the possible sum of numbers inside list, and compare it with your number
+ 3
Some clues to get you started:
⢠Since your goal is to minimize the coin counts, Iâd think about how I want to arrange my coin value list; should it be ascending or descending?
⢠Floor division (x // y) and modulo/remainder (x % y) can be helpful in finding out how many of x fit in y and how many are left over.
⢠What does it mean when the remainder becomes zero?
⢠How many times does the loop need to run?
+ 2
That's up to you to decide... I'm not really clear on what you're trying to do. Can you maybe rephrase?
+ 2
[new info made this answer irrelevant and possibly confusing; see the more recent one]
+ 2
It would be helpful if you gave examples for input, output and given list.
Which integers do we "add"? Integers that already are in the list? The input integer? Do we add them to each list item? Or do we append them to the list?
How do we get the "specified number"? Sum of list items? Product if list items? List length?
+ 2
Here is the problem statement guys, I have function Mathchallenge(num) take the input, which will be an integer from 1 to 250, and return an integer output that will specify the least number number of conis, that when added, equal the input integer, Coins are based on a system as fallows: there are coins representing the integers 1,5,7,9, and 11. So for example: if num is 16, then the output should be 2 because you can achieve the number 16 with the coins 7 and 9. If num is 25, then the output should be 3 because you can achieve 25 with either 11,9 and coins or with 9, 9, and 7 coins
Example 1 :-
Input: 6
Output 2
Example 2:-
Input: 16
Output: 2
+ 2
Jeremy Miller
I also thought the same, but there is one problem with this:
https://code.sololearn.com/cxx0oGrFgF0v/?ref=app
for 26, output should be 4 but mine shows 6.
am i missing something in this code?
+ 2
Sandeep The issue with your script is that you are assuming the largest number must always be the optimal choice regardless of the other % results.
You should evaluate the list in that order (because generally the larger coins will lead to fewer in the final count), but you should check the other values as well.
That is, in the second iteration, your script ignores the higher optimality of five:
⢠15 % 5 => 0
⢠15 // 5 => 3 (+ 1) => 4
in favor of eleven:
⢠15 % 11 => 4
⢠15 // 11 => 1 (+ 1) => 2
Because the â1â coin is the only one having a value equal to or less than four (15 % 11) youâre stuck with four coins in the last step:
⢠4 % 1 => 0
⢠4 // 1 => 4 (+ 2) => 6
Youâre right to check the list in descending order because this will, generally, find the smallest coin set faster, but your algorithm should optimize the âremainderâ value, not the âcoinâ value itself, since it is the remainder value that leads to more lower-value coins being added.
+ 2
There are several solutions here.
https://www.google.com/amp/s/www.geeksforgeeks.org/coin-change-dp-7/amp/
https://www.google.com/amp/s/www.geeksforgeeks.org/find-minimum-number-of-coins-that-make-a-change/amp/
Honestly, people should take googling for solutions as one of the essential modern programming skills. It's as important as auto-complete.đ
+ 2
Thanks for the help Bob_Li
I was using loops, nested loop to find the solution but it's always recursion in the end xD
How did you find the solution? maybe you can tell me some advance google search techniques ;)
+ 2
Sandeep
Itâs a famous problem; the wikipedia page discusses the brute-force recursion approach you tried and the mathematic particulars of further optimization.
https://en.m.wikipedia.org/wiki/Change-making_problem
+ 2
Sandeep
yes, most problems posted here are homeworks which are often variations of math puzzles with certain names.
The trick is to use the right keyword when googling.
Doing them without references often lead to brute force methods. But knowing that smarter people have tackled them before, that is like... cheating in an examđ
Googling is a secret programming language.
+ 2
Thanks Bob_Li and Jeremy Miller , I will be looking more into these kind of problems :]
Also Bob_Li
I will be looking for some ways to cheat because a wise man once said, "you have to be smart to cheat" XD
+ 1
S GOUSE BASHA This challenge sounds incredibly familiar. Are you taking the Harvard CS50 course by chance? And even if not, what is your code? We cannot do the work for you.
+ 1
S GOUSE BASHA
maybe using a for loop and performing floor division with number can help.
+ 1
#input the integers, or if there's more, you can expand
a = int(input())
b = int(input())
c = int(input())
yourlist = [a, b, c]
x = 0
print("sum =", a+b+c)
for i in yourlist:
x += 1
print ("variable quantity =", x)
- 1
Ucvuckhc 6is of is is a to 66h 5hh5hth6by 5hh5hth6b h44h4 is 4g and g of yv vy and we,,,,,,,,,,,,,use it,s