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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T13:12:50+00:00 2026-05-24T13:12:50+00:00

I’ve profiled my application, and it spends 90% of its time in plus_minus_variations .

  • 0

I’ve profiled my application, and it spends 90% of its time in plus_minus_variations.

The function finds ways to make various numbers given a list of numbers using addition and subtraction.

For example:
Input

1, 2

Output

1+2=3
1-2=-1
-1+2=1
-1-2=-3

This is my current code. I think it could be improved a lot in terms of speed.

def plus_minus_variations(nums):
    result = dict()
    for i, ops in zip(xrange(2 ** len(nums)), \
            itertools.product([-1, 1], repeat=len(nums))):
        total = sum(map(operator.mul, ops, nums))
        result[total] = ops
    return result

I’m mainly looking for a different algorithm to approach this with. My current one seems pretty inefficient. However, if you have optimization suggestions about the code itself, I’d be happy to hear those too.

Additional:

  • It’s okay if the result is missing some of the answers (or has some extraneous answers) if it finishes a lot faster.
  • If there are multiple ways to get a number, any of them are fine.
  • For the list sizes I’m using, 99.9% of the ways produce duplicate numbers.
  • It’s okay if the result doesn’t have the way that the numbers were produced, if, again, it finishes a lot faster.
  • 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-24T13:12:50+00:00Added an answer on May 24, 2026 at 1:12 pm

    This seems to be significantly faster for large random lists, I guess you could further micro-optimize it, but I prefer readability.

    I chunk the list into smaller pieces and create variations for it. Since you get a lot less than 2 ** len(chunk) variatons it’s going to be faster. Chunk length is 6, you can play with it to see what’s the optimal chunk length.

    def pmv(nums):
        chunklen=6
        res = dict()
        res[0] = ()
        for i in xrange(0, len(nums), chunklen):
            part = plus_minus_variations(nums[i:i+chunklen])
            resnew = dict()
            for (i,j) in itertools.product(res, part):
                resnew[i + j] = tuple(list(res[i]) + list(part[j]))
            res = resnew
        return res
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I need to clean up various Word 'smart' characters in user input, including but
I need a function that will clean a strings' special characters. I do NOT
I want to construct a data frame in an Rcpp function, but when I
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
I would like to count the length of a string with PHP. The string

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.