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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T16:43:35+00:00 2026-05-29T16:43:35+00:00

I’m trying to learn a bit about dynamic programming and had a peice of

  • 0

I’m trying to learn a bit about dynamic programming and had a peice of code that creates a table of data, but I’m having a hard time figuring out what certain parts do.

Here’s the code:

from collections import defaultdict


data = [1, 2, 4]
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]
    #print i, x
    for s in range(sum + 1): #set the range of one higher than sum to include sum itself
        #print s
        for c in range(s / x + 1):  
            if T[s - c * x, i]:
                T[s, i + 1] = True

I’m slowly starting to figure out how this code works but the second last line if T[s - c * x, i]: is a bit hard because I cannot replicate it fully in a terminal. When I type something simlair(using the values), it creates a matrix like [1,2] but how can that be true or false so it proceeds to the statement below?

If it helps, its part of some code that helps break down numbers. So I’m trying to figure out all the ways to break down 1,2,4 into 10. In this question, Can brute force algorithms scale?, I was suggested to create a dynamic table to reference.

Here’s the end result(which I’m having trouble interpreting the results):

{(7, 3): True, (1, 3): True, (9, 1): True, (3, 0): False, (8, 0): False, (2, 1): True, (6, 2): True, (5, 1): True, (0, 3): True, (10, 3): True, (7, 2): True, (4, 0): False, (1, 2): True, (9, 0): False, (3, 3): True, (8, 1): True, (6, 3): True, (5, 0): False, (2, 2): True, (10, 0): False, (4, 1): True, (1, 1): True, (3, 2): True, (0, 0): True, (8, 2): True, (7, 1): True, (9, 3): True, (6, 0): False, (2, 3): True, (10, 1): True, (4, 2): True, (1, 0): False, (5, 3): True, (0, 1): True, (8, 3): True, (7, 0): False, (9, 2): True, (6, 1): True, (3, 1): True, (2, 0): False, (4, 3): True, (5, 2): True, (0, 2): True, (10, 2): True}

Any clarification or suggestions of what to read up on to help understand would be great!

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-29T16:43:35+00:00Added an answer on May 29, 2026 at 4:43 pm

    Not sure I fully undestand the question.

    If the tuple (x, y) exists in dictionary T, then T[x, y] will just return True or False according to what has been saved to it. If (x, y) does not already exist, because of T = defaultdict(bool), a non-existing key will cause bool() to be called, which returns False. You can replace the bool with some custom function to make this clearer

    >>> from collections import defaultdict
    >>> def on_missing():
    ...     print("missing key!")
    ...     return 'empty!'
    ... 
    >>> T = defaultdict(on_missing)
    >>> T[0, 0] = True     # Store 'True' for key (0, 0) in T
    >>> T[0, 0]            # Retrieve the stored value
    True
    >>> T[1, 2]            # (1, 2) does not exist, so call on_missing()
    missing key!
    'empty!'       
    >>> T[1, 2] = True     # Now store a value for (1, 2)
    >>> T[1, 2]            # We can see 'True' is stored.
    True
    

    Since you are only assigning True, the condition if T[s - c * x, i]: is basically just checking whether (s-c*x, i) has been assigned manually or not. Normally we’ll use a set() for that.

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

Sidebar

Related Questions

I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I want to construct a data frame in an Rcpp function, but when I
I'm trying to create an if statement in PHP that prevents a single post
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
I've got a string that has curly quotes in it. I'd like to replace

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.