Input
2x+x
Return:
3x

Input:
+2x-1y+3x+2
Return:
5x-y+2

Recommended Answers

All 7 Replies

Do you have a question?
If this is a school task try to code something yourself.

What you have now is pseudocode,that maybe has some info to this task that we dont know.

Do you have a question?
If this is a school task try to code something yourself.

What you have now is pseudocode,that maybe has some info to this task that we dont know.

This is not school task!

This is an interesting problem with simply.

I'd like to know how this can be solved.

Why do you think is school task? Relatively arrogant attitude, is not it?

To go from string
"+2x-1y+3x+2"
to string
"5x-y+2"
would have to be done by a specialized string parser following rules you set.

Quite an interesting problem.

You do not understand!

This is the input "+2x-1y+3x+2", I am using the function that returns a response to me.

Hoping for more community, please delete this post/topic!

I do not write this code:

#include <stdio.h>
#include <assert.h>
#include <cmath>

int sign=1;
int val=-1;
char var='0';
int algus = 1;

int kord[200];

int liige(FILE *fo,char c) {
      if (kord[(int)c] != 0) {
	    if ((!algus) && (kord[c] > 0)) {
		  fprintf(fo,"+");
	    }
	    if (kord[c]!=0) 
		  algus=0;
	    
	    if (kord[c] < 0) 
		  fprintf(fo,"-");
	    if ((kord[c] != 1) || (c == '0'))
		  fprintf(fo,"%d", kord[c]);
	    if (c!='0') 
		  fprintf(fo,"%c",c);
      }
      if ((c == '0') && algus && (kord[c] == 0))
	    fprintf(fo,"0");
     
}

main() {
      FILE *fi,*fo;
      char buf[1000];
      int c;
      char *s;
      
      assert(fi = fopen("yatest.x1.sis","r"));
      assert(fo = fopen("ya.val","w"));
      s = fgets(buf, 1000, fi);
      
      while (1) {
	    c = *s;
	    
	    if (((c==0) || (c=='-') || (c=='+')) &&  // jqudsime stringi lopp voi margini
		  ((val != -1) || (var != '0'))) {     // ja oleme leidnud arvu vqi muutuja
		  if (val == -1)                  // vaikimisi kordaja 1
			val = 1;
		  
		  kord[var] += val * sign;        
	    
	    val = -1;               // arvu pole, vabaliige
	    var = '0';
	    }
	    
	    if (c==0) 
		  break;
	    
	    if (c=='-') 
		  sign=-1;
	    
	    if (c=='+') 
		  sign=1;
	    
	    if ((c>='0') && (c<='9')) {
		  if (val == -1) 
			val=0;
		  val = val*10 + (c - '0');
	    }
	    if ((c>='a') && (c<='z')) {
		  var = c;
	    }
	    
	    s++;			
      }
     // 	for (c='a';c<='z';c++) {
	//    		printf("%d ",kord[c]);
	 //   	}
	   // 	printf("%d\n",kord['0']);
	    
	    for (c='a';c<='z';c++) {
		  liige(fo,c);
	    }
	    liige(fo,'0');
	    fprintf(fo,"\n");
	    fclose(fo);
	    fclose(fi);
}

But something similar and would like a simpler solution in Python.


Delete this topic, it is after all in your mind, so pointless.

I think nobody tries to be nasty. However I little generalized and gave up using find in my old multisearch. I think this parsing could be useful start.

inp=['2x+x','2x-1y+3x+2']

def multis(s,text,start=0):
    x=''
    for ch in text[start:]:
        if ch in s:
            if x: yield (x,ch)
            else: yield ch
            x=''
        else:
            x+=ch
    else:
        if x: yield x

for formula in inp:
    print formula
    print list(multis('+-xy',formula)) ## replace with 'for <var> in multis...' and your analysis

Output:

2x+x
[('2', 'x'), '+', 'x']
2x-1y+3x+2
[('2', 'x'), '-', ('1', 'y'), '+', ('3', 'x'), '+', '2']

If you only need code to do the thing, you can cheat with symPy http://code.google.com/p/sympy/:

Features

Currently, SymPy core has around 13000 lines of code (including extensive comments and docstrings) and its capabilities include:

* basic arithmetics *,/,+,-,**
* basic simplification (like a*b*b + 2*b*a*b -> 3*a*b^2)
* expansion (like (a+b)^2 -> a^2 + 2*a*b + b^2)
* functions (exp, ln, ...)
* complex numbers (like exp(I*x).expand(complex=True) -> cos(x)+I*sin(x))
* differentiation
* taylor (laurent) series
* substitution (like x -> ln(x), or sin -> cos)
* arbitrary precision integers, rationals and floats
* noncommutative symbols
* pattern matching

Then there are SymPy modules (73000 lines including documentation) for these tasks:

* more functions (sin, cos, tan, atan, asin, acos, factorial, zeta, legendre)
* limits (like limit(x*log(x), x, 0) -> 0)
* integration using extended Risch-Norman heuristic
* polynomials (division, gcd, square free decomposition, groebner bases, factorization)
* solvers (algebraic, difference and differential equations, and systems of equations)
* symbolic matrices (determinants, LU decomposition...)
* Pauli and Dirac algebra
* geometry module
* plotting (2D and 3D)

Thanks tonyjv.

To understand other users. This is very good - respect.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.