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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T15:56:46+00:00 2026-05-23T15:56:46+00:00

I need to add two simple polynomials (represented as strings). The following example would

  • 0

I need to add two simple polynomials (represented as strings). The following example would clarify my requirement.

input1 = '5a+6b'
input2 = '2a+3b+9c'

The required sum should be as follows:

 '7a+9b+9c'

Currently I have created a function (of 20 lines) to perform this task for me, but I think this can be improved.

EDIT: Adding my code

def add_domain_strings():
    input_list = ['5a+6b', '2a+3b+9c']
    vars_dict = {}
    for input in input_list:
        temp_list = input.split('+')
        for i in temp_list:
            split_index = None
            for j in i:
                if not j.isdigit():
                    split_index = i.index(j)
                    break
            if i[split_index:] in vars_dict:
                vars_dict[i[split_index:]] += int(i[:split_index])
            else:
                vars_dict[i[split_index:]] = int(i[:split_index])
    sum_string = ''
    for k,v in vars_dict.iteritems():
        if sum_string:
            sum_string += '+%s%s' % (v,k)
        else:
            sum_string += '%s%s' % (v,k)
    return sum_string
  • 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-23T15:56:47+00:00Added an answer on May 23, 2026 at 3:56 pm

    You could either use sympy:

    >>> import sympy
    >>> from sympy import *
    >>> a = Symbol('a')
    >>> b = Symbol('b')
    >>> c = Symbol('c')
    >>> eval('5*a+6*b')
    5*a + 6*b
    >>> eval('5*a+6*b') + eval('2*a+3*b+9*c')
    7*a + 9*b + 9*c
    

    Or write a simple parser:

    >>> input1 = '5a+6b'
    >>> input2 = '2a+3b+9c'
    >>> s = "+".join((input1, input2))
    >>> s
    '5a+6b+2a+3b+9c'
    >>> d = {}
    >>> for i in s.split("+"):
    ...     d[i[-1]] = d.get(i[-1], 0) + int(i[:-1])
    ... 
    >>> d
    {'a': 7, 'c': 9, 'b': 9}
    >>> "+".join("".join((str(j),i)) for i, j in d.items())
    '7a+9c+9b'
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

A simple question: I need to add some logging to my program. If two
Simple task: I need to convert two characters to two numbers, add them together
I need to add two lines of html to every page on a large
I have a SQL query (MS Access) and I need to add two columns,
I have two raw sound streams that I need to add together. For the
I need to develop a simple cache (no concurrency or refresh required) to hold
I've got two columns and i need to add line between them. This is
I need to create a simple c# application to add some quarterly figures. I
I need add a new user group for mediawiki. The new group has more
need to add a 2nd css stylesheet to a page. do i add a

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.