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 6616891
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T20:38:16+00:00 2026-05-25T20:38:16+00:00

I’m a bit confused on how to approach this problem. I know what I

  • 0

I’m a bit confused on how to approach this problem. I know what I want to do but can’t wrap my head on how to logically solve this problem.
say I have a list:

numlist = [10,4]

and I have the following values in another list:

datalist = [10,5,4,2,1]

how do I break down the numbers in numlist using only numbers from datalist?

An example of an answer would be:

10, 4
10, 2,2
10, 2,1,1
10, 1,1,1,1
5,5, 4
5,5, 2,2

…and so on.

I understand how to do this vaguely. make a for loop, for each entry in the list and compare if it can be divided by the datalist, and if so print the result. I think I need recursions which is where I’m having trouble understanding.

here’s my code so far (I have some print statements for troubleshooting):

def variableRecursion(self, solutionList):
    #solution list contrains ['red', 2, 'green', 1] which means 2 reds(value 4) and 1 green(value 2)

    #adding fake lookup list for now, in real code, I can use real data because I am reversing the order
    list = [('red', 4), ('green', 2), ('blue', 1) ]
    for x1, x2 in zip(solutionList[::2], solutionList[1::2]):
        for x in list:
            for y1, y2 in zip(x[::2], x[1::2]):
                #print x1, x2
                keyName = x1
                keyShares = x2
                keyValue = lookup.get_value(x1)
                if ((keyValue%y2) == 0) and (keyValue != y2):
                    tempList = []
                    #print 'You can break ', keyName, keyValue, ' with ', y1, y2, ' exactly ', keyValue/x2, ' times.'
                    #newKeyShares = keyShares - 1
                    for a1, a2 in zip(solutionList[::2], solutionList[1::2]):
                        #print a1, a2
                        print 'You can break ', keyName, keyValue, ' with ', y1, y2, ' exactly ', keyValue/y2, ' times.'
                        newKeyShares = keyShares - 1
                        print 'there is a match', a1, a2, ' but we will change the shares to ', newKeyShares
                        print a1
                        if (a1 == keyName):
                            print 'a'
                            tempList.append([(keyName), (newKeyShares)])
                        elif (a1 == y1):
                            print 'b'
                            tempList.append([(y1), (a2+keyValue/y2)])
                        else:
                            print 'c'
                            try:
                                tempList.append([(y1), (a2+keyValue/y2)])
                            except e:    
                                tempList.append([(a1), (a2)])
                    print tempList
                    appendList.appendList(tempList)
                    tempList = []
                    #exit()
                    #print solutionList
  • 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-05-25T20:38:17+00:00Added an answer on May 25, 2026 at 8:38 pm

    This problem is very similar to Problem 31 of Project Euler: “How many different ways can £2 be made using any number of coins?”. Only in your example, you are asking to enumerate all the ways you can add up numbers to get 10 and 4.

    The best way to approach the problem is to first try breaking up only a single number. Let’s look at the possible breakups for five, using numbers [5,4,2,1]:

    [5]
    [4,1]
    [2,2,1]
    [2,1,1,1]
    [1,1,1,1,1]
    

    The following python code will give you a list of these combinations:

    def possibleSplits(value,validIncrements):
        ret = []
        for increment in validIncrements:
            if increment > value:
                continue
            if increment == value:
                ret.append([increment])
                continue
            if increment < value:
                remainder = value - increment
                toAdd = possibleSplits(remainder, validIncrements)
                for a in toAdd:
                    ret.append([increment] + a)
        return ret
    

    This code assumes that different orderings of otherwise identical answers should be treated as distinct. For example, both [4,1] and [1,4] will appear as solutions when you split 5. If you prefer, you can constrain it to only have answers that are numerically ordered (so [1,4] appears but not [4,1])

    def orderedPossibleSplits(value, validIncrements):
        ret = []
        splits = possibleSplits(value, validIncrements)
        for value in splits:
            value.sort()
            if value not in ret:
                ret.append(value)
        return ret
    

    Now you can use this to find the possible splits for 10, and the possible splits for 4, and combine them:

    increments = [10, 5, 4, 2, 1]
    tenSplits = orderedPossibleSplits(10, increments)
    fourSplits = orderedPossibleSplits(4, increments)
    results = []
    for tenSplit in tenSplits:
        for fourSplit in fourSplits:
            results.append(tenSplit + fourSplit)
    

    edit: As noted in the comments, calling possibleSplits with a value of 100 is very slow – upwards of ten minutes and counting. The reason this occurs is because possibleSplits(100) will recursively call possibleSplits(99), possibleSplits(98), and possibleSplits(96), each of which call three possibleSplits of their own, and so on. We can approximate the processing time of possibleSplits(N) with datalist[1,2,4] and large N as

    processingTime(N) = C + processingTime(N-1) + processingTime(N-2) + processingTime(N-4)

    For some constant time C.

    So the relative time for possibleSplits is

    N     1 | 2 | 3 | 4 | 5 ... 20    ... 98                         | 99                         | 100 
    Time  1 | 1 | 1 | 4 | 7 ... 69748 ... 30633138046209681029984497 | 56343125079040471808818753 | 103631163705253975385349220
    

    Supposing possibleSplits(5) takes 7 ns, possibleSplits(100) takes about 3 * 10^9 years. This is probably an unsuitably long time for most practical programs. However, we can reduce this time by taking advantage of memoization. If we save the result of previously calculated calls, we can get linear time complexity, reducing possibleSplits(100) to 100 ns.

    The comments also noted that the expected value of orderedPossibleSplits(100) has about 700 elements. possibleSplits(100) will therefore have a much much larger number of elements, so it’s impractical to use it even with memoization. Instead, we’ll discard it and rewrite orderedPossibleSplits to use memoization, and to not depend on possibleSplits.

    #sorts each element of seq and returns it
    def orderedInnerLists(seq):
        return map(sorted, seq)
    
    #returns a copy of seq with duplicates removed
    def removeDuplicates(seq):
        ret = []
        for value in seq:
            if value not in ret:
                ret.append(value)
        return ret
    
    memoizedResults = {}
    def orderedPossibleSplits(value,validIncrements):
        memoizeKey = (value, tuple(validIncrements))
        if memoizeKey in memoizedResults:
            return memoizedResults[memoizeKey]
        ret = []
        for increment in validIncrements:
            if increment > value:
                continue
            if increment == value:
                ret.append([increment])
                continue
            if increment < value:
                remainder = value - increment
                toAdd = orderedPossibleSplits(remainder, validIncrements)
                for a in toAdd:
                    ret.append([increment] + a)
        memoizeValue = removeDuplicates(orderedInnerLists(ret))
        memoizedResults[memoizeKey] = memoizeValue
        return memoizeValue
    

    On my machine, orderedPossibleSplits(100, [1,2,4]) takes about ten seconds – much improved from our original three billion year run time.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Does anyone know how can I replace this 2 symbol below from the string
I want to count how many characters a certain string has in PHP, but
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I want to construct a data frame in an Rcpp function, but when I
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a jquery bug and I've been looking for hours now, I can't
this is what i have right now Drawing an RSS feed into the php,

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.