0

Let us say we have a list and target of:
list: [1,2,3,4,5] and target: 20 and we want to find total combinations to reach this, with multiplication, which is: [1,4,5], [4,5], [2,5,2], [1,2,2,5] I did this code, but I can't seem to know how to remove the ones so I have them too, I mean that I receive: [1,4,5], [1,2,2,5].
But without [1], I can't seem to get it, I tried to "cheat" somehow to get it, but I can't since my code doesn't fit for it...

def Coin(target, lst, temp=[], comb=[]):
    if target == 1:    
        comb.append(temp)
        return comb
    if len(lst) == 0:
        return []
    if target >= lst[0]:
        if lst[0] > 1:
            take=Coin(target/lst[0],lst,temp+[lst[0]],comb)
            dont_take=Coin(target,lst[1:],temp,comb)
        else:
            take_1 = Coin(target, lst[1:], temp + [1], comb)
            return take_1
        return take
    return comb

print(Coin(20, [1,2,3,4,5], [], []))
[[1, 2, 2, 5], [1, 4, 5]]

How to add the parts without 1? I don't need a solution, since as said, not homework, but practice for exam. Just a clue will be enough, I want to find it myself, but I need a clue for it.

E_net4
  • 27,810
  • 13
  • 101
  • 139
  • So 1 should be never included? – Riccardo Bucco Aug 31 '22 at 13:22
  • Please format your code properly, you're calling coin_change, but your function here is called Coin – funnydman Aug 31 '22 at 13:24
  • No no, it should be included. but I need to receive the sublist with 1 and without 1. I think, no? since all combination to reach 20 with the numbers, are with 1 and without 1 – LearningToCode Aug 31 '22 at 13:25
  • Oh sorry, didnt add the original function, it was an helper, one minute., – LearningToCode Aug 31 '22 at 13:25
  • Edited again, didnt notice, stackoverflow code isnt working well, it syntax make it go steps back or forward, not where it is supposed to be, fixed it. – LearningToCode Aug 31 '22 at 13:28
  • Do you want to get all possible combinations? [1,4,5], [4,5], [2,5,2], [1,2,2,5] .. etc? – funnydman Aug 31 '22 at 13:30
  • Yea, there are only those 4, but yea – LearningToCode Aug 31 '22 at 13:34
  • because I need to get all combinations, although 1 is nothing, its still a combination. – LearningToCode Aug 31 '22 at 13:34
  • 1
    By what logic do you not want to get `[4, 5]`, `[1, 4, 5]`, `[1, 1, 4, 5]`, `[1, 1, 1, 4, 5]`, and so on forever? – btilly Aug 31 '22 at 18:23
  • @btilly Hi thanks for the answer. Hmm, what do you mean by what logic? just the reasoning, a number should appear once, there is no signficance for order. – LearningToCode Aug 31 '22 at 18:28
  • 2
    @LearningToCode If you want to find combinations where `2` appears more than once, then you should allow `1` to appear more than once. And vice versa if you only allow `1` to appear once, you should only allow `2` to appear once as well. (Combinations traditionally only allow the element to appear as many times as it does in the input.) – btilly Aug 31 '22 at 20:39
  • @btilly Hi, what do you mean by 2 appear more than once? I am finding it hard to find the logic. What is the 1 to appear? – LearningToCode Aug 31 '22 at 20:51
  • "a number should appear once" So why did you include `[1, 2, 2, 5]` in the list of answers? – n. m. could be an AI Sep 01 '22 at 11:12
  • @n.1.8e9-where's-my-sharem. What is the division of 20? How do you reach 20 with numbers 1,2,5? tell me so.. By appear once, I mean You will not have: 1,1,2,2,5 >> 1,1,1,2,2,5 >>> 1,1,1,1,2,2,5 >>> and such.. Only 1,2,2,5 and 2,2,5... – LearningToCode Sep 01 '22 at 13:31
  • "How do you reach 20 with numbers 1,2,5?" If a number may not appear more than once, then you don't reach 20. If a number may appear more than once, then 1,1,2,2,5 is OK. "By appear once, I mean" It doesn't work this way. Come up with a rule. – n. m. could be an AI Sep 01 '22 at 15:55

3 Answers3

0

Maybe you should combine

if lst[0] > 1: 
else:

together, that means for 1 we should also decide whether take it or not.

rdc
  • 33
  • 5
  • I cant see how it will help :\ It will be wrong to do so also, since I have to separate cases. if it has 1 without the else, I will be in repetition of recursion and max recursion depth ( infinity ) – LearningToCode Aug 31 '22 at 15:49
  • Should the combination means that we should use each element at most once? – rdc Aug 31 '22 at 23:13
  • Once depending on target... If I have target 20, and list [1,2,3,4,5] I will get [1,2,2,5], [2,2,5], [1,4,5] [4,5] But not [1,1,2,2,5], [1,1,1,2,2,5] – LearningToCode Sep 01 '22 at 00:48
0

This is much easier to do with a generator using yield than a function using return.

The difference is that you can only return once, while you can yield any number of times (including 0 if there are no solutions).

If you wish to return a list, you still can, like this:

def coin (target, lst):
    return list(_coin(target, lst, 0)

def _coin (target, lst, i):
    ...

If that doesn't matter, the generator saves memory by not having to generate the whole list at once. And you simply:

def coin (target, lst, i=0):
    ... # Use yield as often as you want, all will be returned.

Second, you are running into the most common gotcha in Python. You are using mutable objects as defaults. If you're going to continue with your current approach you need to:

def coin(target, lst, temp=None, comb=None):
    if temp is None:
        temp = []
    if comb is None:
        comb = []
    ...

And third, you should get in the habit of following standard style conventions. In many ways, what the convention is doesn't matter that much. But that everyone is on the same page, does. Therefore you should try to follow the most common Python convention. In which the function should be named coin instead of Coin.

btilly
  • 43,296
  • 3
  • 59
  • 88
  • While this is might be helpful it does not answer the question – funnydman Aug 31 '22 at 17:00
  • @funnydman The problem explicitly says, *I don't need a solution, since as said, not homework, but practice for exam.* I therefore explicitly did not try to provide that, and left to the OP the "ah hah" of how to make the generator work, and how much it simplifies the code. – btilly Aug 31 '22 at 17:55
  • Well, thanks, but I dont have to learn generator... We did not learn it, I do not have time to study generator, my test is more important. Thanks though. About the coin or Coin, yea I know, I dont do big words usually, but I had two functions of coin of rec that I tried to do, I wanted a short name and didnt want to delete the first, so just did Coin instead of coin. Anyway, its not the problem of the code. – LearningToCode Aug 31 '22 at 18:09
  • Oh and also, we are usually told by our professor not to put val on arguments, which means I cant do new=[] at argument, thats why I have a recursion helper ( the function here is an helper, which has a main function that calls to her ) – LearningToCode Aug 31 '22 at 18:12
  • at my code yea I put there, but its meaningless, its just that before, as said, I had a main function, so to print it without the main, I just put [] []. nevertheless, about the generator, again, as said, we have not learned and as such not allowed to use it. – LearningToCode Aug 31 '22 at 18:13
-1

Edit: The rules for the question is:

  1. Positive integers only ( 0 not allowed ).

  2. Number can appear once only ( at input ).

  3. can not repeat numbers on list. EG: [1,2,3,4], n=12 >> [1,12], [12,1], [3,4], [2,6], [1,3,4], [1,2,6].
    NOT: [1,2,2,3], [2,2,3]

  4. That is all I guess.

    def coin_change_MULTI(num, lst):
        if num == 1 and 1 not in lst:
            return []
        return Coin_MULTI(num, sorted(lst), [], [])
    def Coin_MULTI(target, lst, temp=[], comb=[]):
        if target == 1:
            if big_than_target(1, lst):
                return [[1]]
            comb.append(temp)
            return comb
        if len(lst) == 0: return []
        if target >= lst[0]:
            if lst[0] > 1:
                take=Coin_MULTI(target/lst[0],lst[1:],temp+[lst[0]],comb)
                dont_take=Coin_MULTI(target,lst[1:],temp,comb)
                return comb
            else:
                take_1 = Coin_MULTI(target, lst[1:], temp + [1], comb)
                dont_take_1 = Coin_MULTI(target, lst[1:], temp, comb)
                return comb
            return take + dont_take
        return comb
    
    
    
    print(coin_change_MULTI(12, [2,4,6,12,7,3, 1]))
    print(coin_change_MULTI(1, [2,4,6,12,7,3,1]))
    print(coin_change_MULTI(1, [2,4,6,12,7,3]))
    print(coin_change_MULTI(100, [2,4,6,12,7,3,1]))
    print(coin_change_MULTI(576, [2,4,6,12,7,3,1]))
    print(coin_change_MULTI(12096, [2,4,6,12,7,3,1]))
    print(coin_change_MULTI(0, [2,4,6,12,7,3,1]))
    print((coin_change_MULTI(24, [2,4,6,12,7,3,1])))
    
    
    [[1, 2, 6], [1, 3, 4], [1, 12], [2, 6], [3, 4], [12]]
    [[1]]
    []
    []
    [[1, 2, 4, 6, 12], [2, 4, 6, 12]]
    [[1, 2, 3, 4, 6, 7, 12], [2, 3, 4, 6, 7, 12]]
    []
    [[1, 2, 3, 4], [1, 2, 12], [1, 4, 6], [2, 3, 4], [2, 12], [4, 6]]
    

Process finished with exit code 0

  • What about such input: [5, 4, 3, 2, 1], it returns an empty list – funnydman Sep 01 '22 at 10:29
  • How exactly? I got this: [[1, 2, 2, 5], [1, 4, 5], [2, 2, 5], [4, 5]] – LearningToCode Sep 01 '22 at 13:32
  • Oh never mind, I just noticed... Weird, I will check it out. Thanks for notifying me! ( last message I did [1,2,3,4,5] instead of [5,4,3,2,1] – LearningToCode Sep 01 '22 at 13:33
  • @funnydman well, I think I can cheat and sort the list and then it will work hehe, but I still want to know why it wont work that way, I am debugging but not finding the problem.. I mean, I do see problem, but I dont know how to fix it :\ – LearningToCode Sep 01 '22 at 13:40
  • Try (6, [2,3]). Or (1,[1,2]). Or (2,[0,1,2]). – n. m. could be an AI Sep 01 '22 at 21:07
  • Where exactly is the problem? with the first from left I receive: [[2, 3]]. Regarding 1, that I know, For that case I didnt go with it, since I am putting the code on my formula for the test ( I actually wrote a note regarding 1, if it has 1, just write something that will return 1, easy peazy as said ). Regarding the last: Yea, that I probably should have mentioned. I will make it clearer at the answer now and edit. Sorry. – LearningToCode Sep 01 '22 at 21:58
  • @n.1.8e9-where's-my-sharem. added the rules at answer, sorry. – LearningToCode Sep 01 '22 at 22:00
  • You get [[2, 3]], I get [[2, 3], [2, 3], [2, 3]]. – n. m. could be an AI Sep 02 '22 at 07:51
  • "[1,1,2] is the same as [1,2]" That's an example, not a rule. When are two lists considered the same? Their product is the same, but so is the product of `[2]`. You may say that "1 is a special case, it cannot appear more than once in a list, all other numbers can occur any number of times". Or you may say something else. But you need to say something that can be interpreted unambiguously. (I poke holes in your logic not because I find it entertaining, but because your logic better not have any weak spots at the actual exam). – n. m. could be an AI Sep 02 '22 at 08:02
  • @n.1.8e9-where's-my-sharem. About the first message: then something you input is wrong, I receive on target =6 and list [2,3] the [[2,3]] – LearningToCode Sep 02 '22 at 08:37
  • @n.1.8e9-where's-my-sharem. Regarding the second. its a rule, but I dont know how to say in english.. My home laungage is hebrew, my english is not good as you see... what you wrote is exactly as you said at the "". that is why I am not an exam writer :) ( especially in english ) – LearningToCode Sep 02 '22 at 08:39
  • By giving an example, I wrote a rule, but now I used google translate: Given a list of integers (positive and unique integers) (each number appears once) called "lst" and an integer n, you must implement a recursive function that searches for and returns the number of subsets whose terms can be multiplied exactly to the number n, terms can be repeated in 2 or more subsets Lists, the order is not important... the example from quesiton: 6  The output will be returned n=12 and lst = [2,4,6,12,7,3,1]...12 whose multiplication[ 1,2,6[ ,]1,3,4[ ,[12] ,[12,1] ,[4,3] ,[2,6] are 6 sublists – LearningToCode Sep 02 '22 at 08:44
  • Regarding the actual exam question, I don't see `[1,2,2,3]` in the list of answers. I wonder why. – n. m. could be an AI Sep 02 '22 at 09:05
  • @n.1.8e9-where's-my-sharem. Oh you are right, I didnt notice it. probably they forgot it, anyway, they said themselves, terms can be repeated in 2 or more subsets lists. Anyway, the question is a bit problematic, now I see.. even their example is wrong hehe, no Idea what to say.. I guess this question goes to the "trash" since I dont know how exactly to rule it. – LearningToCode Sep 02 '22 at 10:07
  • "probably they forgot it" I rather doubt it. In my opinion you misunderstand the assignment. You are welcome to figure it out on the exam day, or just believe me. – n. m. could be an AI Sep 02 '22 at 11:53
  • @n.1.8e9-where's-my-sharem. it is all good, I will just move on, I dont want to stick to one question. Since I dont understand it completely, I will just dump this question. Thanks anyway :) – LearningToCode Sep 02 '22 at 12:36