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: - 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: - (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
4 1659 bvdet 2,851
Recognized Expert Moderator Specialist
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. - 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: - >>> s
-
'86 - (7 + x*10) / (y+ 232) = (12*x-4) / (2*y-61)*32 + (2) / (x) - 12*6'
Thank you very much, you helped me out quite well. Now I get it more or less. This is kinda difficult to decode: - 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: - 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
bvdet 2,851
Recognized Expert Moderator Specialist
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. - 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 \((.[^)]+)\))') bvdet 2,851
Recognized Expert Moderator Specialist
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 Sign in to post your reply or Sign up for a free account.
Similar topics |
by: pablo |
last post by:
Dear Newsgroupers,
The 'main' page contains a call to a function in an included file. This
function puts a html-form on the screen. Before the form gets posted (to the
'main' page) some prior errorchecking is done. The form fields are then sent
and an object is created (all in the same function).
What I need ( imo ) is an array with objects...
|
by: Börni |
last post by:
Hi,
Is there a way to provide a default for a function parameter.
i tried
function func (type, message, obj = "message")
but it doesn't seem to work.
It seems a bit ugly to make an if/else statement right at the begining
of a function, just to imitate this behavior.
|
by: Rolf Wester |
last post by:
Hi,
I want to pass a C-function as a function parameter but I don't know
how to that correctly. In the example below how would I have to declare
the function argument in the my_sort function definition?
Thank you in advance for any help.
Regards
Rolf
|
by: Daniel Rudy |
last post by:
Hello,
Consider the following code:
/* resolve_hostname
this resolves the hostname into an ip address.
*/
static void resolve_hostname(char result, const char
hostname, const char server)
{
|
by: Matthew Louden |
last post by:
When I pass an array as a function parameter, it yields the following
compile error. Any ideas?? However, if I create a variable that holds an
array, and pass that variable to the function parameter, then it's working
fine. Any ideas?? Thanks!
public static int getFreq(string s)
{...
}
int res = getFreq({"eee", "ewsww"});
| |
by: Glenn Lerner |
last post by:
If I pass a reference type (such as DataSet) to a function, I'm assuming
only a reference is passed (not a copy). So there is no need to declare
function parameter as ref for those types?
Example: private void myFunction(ref DataSet data)
If I declare it as ref anyway then does it mean it will pass a reference
to another reference similar...
|
by: Nelson Smith |
last post by:
Is it possible to pass array as function parameter. I am using .Net
Framework 1.1.
If so how?
Thanks,
Nelson
|
by: Joe |
last post by:
Hi,
I like to know what do you specify in the function parameter (in the
function implementation) if you want the string that you pass in with the
function call to be changed while its in the function and then you get it
back changed as well?
Is it ByRef or just notthing
function prc0(ByRef editablestring as string )
|
by: hzmonte |
last post by:
Correct me if I am wrong, declaring formal parameters of functions as
const, if they should not be/is not changed, has 2 benefits;
1. It tells the program that calls this function that the parameter
will not be changed - so don't worry.
2. It tells the implementor and the maintainer of this function that
the parameter should not be changed...
|
by: othellomy |
last post by:
select left('Hello World /Ok',charindex('/','Hello World /Ok')-1)
Hello World
That works fine.
However I got an error message:
select left('Hello World Ok',charindex('/','Hello World Ok')-1)
Instead of:
'Hello World Ok'
I get:
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
| |
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it. ...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in...
| |
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...
| |