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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T09:30:44+00:00 2026-05-29T09:30:44+00:00

I am having problems creating a table in python. Basically I want to build

  • 0

I am having problems creating a table in python. Basically I want to build a table that for every number tells me if I can use it to break down another(its the table algo from the accepted answer in Can brute force algorithms scale?). Here’s the pseudo code:

for i = 1 to k
    for z = 0 to sum:
        for c = 1 to z / x_i:
            if T[z - c * x_i][i - 1] is true:
                set T[z][i] to true

Here’s the python implementation I have:

from collections import defaultdict


data = [1, 2, 4]
target_sum = 10

# T[x, i] is True if 'x' can be solved
# by a linear combination of data[:i+1]
T = defaultdict(bool)           # all values are False by default
T[0, 0] = True                # base case


for i, x in enumerate(data):    # i is index, x is data[i]
    for s in range(target_sum + 1): #set the range of one higher than sum to include sum itself
        for c in range(s / x + 1):  
            if T[s - c * x, i]:
                T[s, i+1] = True

#query area   
target_result = 1
for node in T:
    if node[0]==target_result:
        print node, ':', T[node]

So what I expect is if target_result is set to 8, it shows how each item in list data can be used to break that number down. For 8, 1,2,4 for all work so I expect them all to be true, but this program is making everything true. For example, 1 should only be able to be broken down by 1(and not 2 or 4) but when I run it as 1, I get:

(1, 2) : True
(1, 0) : False
(1, 3) : True
(1, 1) : True

can anyone help me understand what’s wrong with the code? or perhaps I am not understanding the algorithm that was posted in answer I am referring to.

(Note: I could be completely wrong, but I learned that defaultdict creates entries even if its not there, and if the entry exists the algo turns it to true, maybe thats the problem I’m not sure, but it was the line of thought I tried to go but it didn’t work for me because it seems to break the overall implemention)
Thanks!

  • 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-29T09:30:44+00:00Added an answer on May 29, 2026 at 9:30 am

    The code works if you print the solution using RecursivelyListAllThatWork():

    coeff = [0]*len(data)
    def RecursivelyListAllThatWork(k, sum): # Using last k variables, make sum
        # /* Base case: If we've assigned all the variables correctly, list this
        # * solution.
        # */
        if k == 0:
            # print what we have so far
            print(' + '.join("%2s*%s" % t for t in zip(coeff, data)))
            return
        x_k = data[k-1]
        # /* Recursive step: Try all coefficients, but only if they work. */
        for c in range(sum // x_k + 1):
           if T[sum - c * x_k, k - 1]:
               # mark the coefficient of x_k to be c
               coeff[k-1] = c
               RecursivelyListAllThatWork(k - 1, sum - c * x_k)
               # unmark the coefficient of x_k
               coeff[k-1] = 0
    
    RecursivelyListAllThatWork(len(data), target_sum)
    

    Output

    10*1 +  0*2 +  0*4
     8*1 +  1*2 +  0*4
     6*1 +  2*2 +  0*4
     4*1 +  3*2 +  0*4
     2*1 +  4*2 +  0*4
     0*1 +  5*2 +  0*4
     6*1 +  0*2 +  1*4
     4*1 +  1*2 +  1*4
     2*1 +  2*2 +  1*4
     0*1 +  3*2 +  1*4
     2*1 +  0*2 +  2*4
     0*1 +  1*2 +  2*4
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm having problems with creating mySQL table within Java program. I constantly get Can't
We're having a significant number of problems creating a bulk upload function for our
I am having some problems creating a loop that could show the cheapest price.
I am having some problems creating custom types for my data table, using jquery.datatables.js.
I have been having problems with creating a download list of files for a
I'm creating a website for my church and I'm having problems making it display
I've been having problems creating a foreign key in Java Db through Netbeans. I'm
I am having trouble creating a table with MySQL. I have narrowed the problem
I'm having some problems with creating pagination with a HABTM relationship. First, the tables
I am having a problem in creating two identity columns in a single table.

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.