String function parameter replacing

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • TheReshi
    New Member
    • May 2014
    • 2

    String function parameter replacing

    Hi guys!

    I would like to ask your help. I have started learning python, and there are a task that I can not figure out how to complete. So here it is.

    We have a input.txt file containing the next 4 rows:

    Code:
    f(x, 3*y) * 54 = 64 / (7 * x) + f(2*x, y-6)
    
    x + f(21*y, x - 32/y) + 4 = f(21 ,y)
    
    86 - f(7 + x*10, y+ 232) = f(12*x-4, 2*y-61)*32 + f(2, x)
    
    65 - 3* y = f(2*y/33 , x + 5)
    The task is to change the "f" function and it's 2 parameters into dividing. There can be different amount of spaces between the two parameters. For example f(2, 5) is the same as f(2 , 5) and should be (2 / 5) with exactly one space before and after the divide mark after the running of the code. Also, if one of the parameters are a multiplificatio n or a divide, the parameter must go into bracket. For example: f(3, 5*7) should become (3 / (5*7)). And there could be any number of function in one row. So the output should look like this:

    Code:
    (x / (3*y)) * 54 = 64 / (7 * x) + ((2*x) / (y-6))
    
    x + ((21*y) / (x - 32/y)) + 4 = (21 / y)
    
    86 - ((7 + x*10) / (y+ 232)) = ((12*x-4) / (2*y-61))*32 + (2 / x)
    
    65 - 3* y = ((2*y/33) / (x + 5))
    I would be very happy if anyone could help me.

    Thank you in advance,
    David
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    Here's a simplified version using a re solution showing unconditional formatting of the replacement string. I'll leave it up to you to finalize the formatting.
    Code:
    import re
    
    s = "86 - f(7 + x*10, y+ 232) = f(12*x-4, 2*y-61)*32 + f(2, x) - 12*6"
    patt = re.compile(r'(f\((.[^)]+)\))')
    
    start = 0
    end = len(s)
    while True:
        m = patt.search(s, start, end)
        if m:
            s1 = m.group(1)
            s2 = m.group(2)
            a,b = [ss.strip() for ss in s2.split(",")]
            s = s.replace(s1, "(%s) / (%s)" % (a,b), 1)
            start = m.end()+1
        else:
            break
    Resulting string:
    Code:
    >>> s
    '86 - (7 + x*10) / (y+ 232) = (12*x-4) / (2*y-61)*32 + (2) / (x) - 12*6'

    Comment

    • TheReshi
      New Member
      • May 2014
      • 2

      #3
      Thank you very much, you helped me out quite well. Now I get it more or less. This is kinda difficult to decode:
      Code:
      patt = re.compile(r'(f\((.[^)]+)\))')
      .

      However, I tried to give it some conditions, like if the left or right part of the f() call contains "+, -, *, /" marks, then replace it with a bracketed one. It only works for the first two call function but it leaves out the third one. I don't know why, I tried to print every useful information, and it looks like the search just can't find the 3rd one no matter where it starts. Could you help me out at this one as well? I'm sorry for being this problematic, but I really would like to understand how it works. Thank you in advance. Here's the code:

      Code:
      import re
       
      s = "86 - f(7 + x*10, y+ 232) = f(12*x-4, 2*y-61)*32 + f(2, x) - 12*6"
      patt = re.compile(r'(f\((.[^)]+)\))')
       
      start = 0
      end = len(s)
      while True:
          m = patt.search(s, start, end)
          if m:
              s1 = m.group(1)
              s2 = m.group(2)
              a,b = [ss.strip() for ss in s2.split(",")]
      	if "+" in b or "-" in b or "*" in b or "/" in b:
      		b = b.replace(b, "(%s)" % b)
      	else:
      		print "Nothing.";
      
      	if "+" in a or "-" in a or "*" in a or "/" in a:
      		a = a.replace(a, "(%s)" % a)
      	else:
      		print "Nothing.";
      
              s = s.replace(s1, "(%s / %s)" % (a,b), 1)
              start = m.end()+1
          else:
              break
      
      print s

      Comment

      • bvdet
        Recognized Expert Specialist
        • Oct 2006
        • 2851

        #4
        Actually, I did not need to use start and end variables. That was causing the missed third match. The search method will find the first occurrence of the match string. After redefining the string using the previous match, the next match will be found. I modified a few other things so it would work.
        Code:
        import re
         
        s = "86 - f(7 + x*10, y+ 232) = f(12*x-4, 2*y-61)*32 + f(2, x) - 12*6"
        patt = re.compile(r"(f\((.[^)]+)\))")
         
        while True:
            m = patt.search(s)
            if m:
                s1 = m.group(1)
                s2 = m.group(2)
                a,b = [ss.strip() for ss in s2.split(",")]
                if "+" in b or "-" in b or "*" in b or "/" in b:
                    b = "(%s)" % b     
                if "+" in a or "-" in a or "*" in a or "/" in a:
                    a = "(%s)" % a 
                s = s.replace(s1, "(%s / %s)" % (a,b), 1)
            else:
                break
        print s
        Hopefully inline code will display properly:
        patt = re.compile(r'(f \((.[^)]+)\))')
        Last edited by bvdet; May 13 '14, 01:58 PM. Reason: Note the re pattern is not displayed properly due to a formatting error in the code block.

        Comment

        • bvdet
          Recognized Expert Specialist
          • Oct 2006
          • 2851

          #5
          Here's an attempt to explain the re pattern
          r'(f\(([^)]+)\))'
          Open parenthesis - start group 1
          f - Match "f" character
          backslash+( - Match "(" character
          Open parenthesis - start group 2
          [^)] - Match characters that are not ")"
          + - Greedily matches one or more of the previous expression
          Close parenthesis - end group 2
          backslash+) - Match ")" character
          Close parenthesis - end group 1

          Comment

          Working...