In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article introduces the relevant knowledge of "python string four expression coding problem-solving analysis". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
First, cycle through the innermost parentheses, solve the expression, and then replace the corresponding expression in the string with the result. Loop until all parentheses of the entire expression are solved.
When solving the expression without parentheses, from left to right, first cycle out all divisions, then cycle out all multiplication, and then do addition and subtraction.
Unparenthesis
First of all, let's look at the case of the topic:
'10-3 * (20-10 + (- 10 Universe 5) * 27 Universe 3-(- 100) / (10-3 inch 5)) + (- 2. 5 cycles / 12)'
First, extract all the characters in the inner parentheses and write the regular expression as follows:
The purpose of'\ ([^ ()] +)\)'(and\) is to match the left and right parentheses. Note that the left and right parentheses indicate the meaning of grouping in the regular expression, so add\ escape to the original parenthesis. The ^ in the # [^ ()] character group indicates no, and ^ () means to match the content without parentheses # +? Indicates that the matching content is one or more, and the minimum matching pattern # ([^ ()] +?) Indicates that the content without parentheses is extracted as a group.
Write code to unparenthesize:
Def cal (s): s = s.replace ('','') # remove all spaces in the string Prevent matching errors while'('in s or')'in s:ret = re.findall ('\ (([^ ()] +?)\), s) for i in ret:s = s.replace (f'({I})', count (I)) # count function is used to calculate the addition, subtraction, multiplication and division expression s = s.replace ('- -') without parentheses '+') # Note 1: else:s = count (s) return s to solve multiplication and division when dealing with the situation where the minus sign may occur after the parenthesis.
Here, to calculate the addition, subtraction, multiplication and division expression without parentheses, first do division, then extract the numbers on the left and right sides of the division expression, and write the regular expression as follows:
([-]?\ d +\.\ d + | [-]?\ d +) / ([-]?\ d +\.\ d + | [-]?\ d +) # [-]? Indicates that there may be a negative sign # [-]?\ d + indicates that there may be a matching floating point number # [-]?\ d + indicates a matching integer # ([-]?\ d +\.\ d + | [-]?\ d +\.\ d + | [-]?\ d +) / ([-]?\ d +) / ([-]?\ d +.\ d +) indicates extraction of positive and negative floating point numbers or positive and negative integers on both sides
Circular operation division process:
Def count (s): while'/'in s:result = re.findall ('([-]?\ d +\.\ d + | [-]?\ d +) / ([-]?\ d +\.\ d + |, s) for i in result:s = s.replace (f'{I [0]} / {I [1]}', f'{float (I [0]) / float (I [1])}')
Circular multiplication process (there is only one operator difference between the regularity of multiplication and the regularity of division):
While'*'in s:result = re.findall ('([-]?\ d +\.\ d + | [-]?\ d +)\ * ([-]?\ d +\.\ d + | [-]?\ d +)', s) for i in result:if float (I [0]) < 0 and float (I [1]) < 0: # Note 2: negative multiply negative numbers and get positive s = s.replace (f'{I [0]} * {I [1]}') S.replace + {float (I [0]) * float (I [1])}') else:s = s.replace (f'{I [0]} * {I [1]}', f'{float (I [0]) * float (I [1])}')
Solving the feeling of addition and subtraction is the easiest step, as long as you match the positive and negative floating point numbers or positive and negative integers according to the above rules, and then add up all the values.
Result = re.findall ('([-]?\ d+\.\ d+ | [-]?\ d+)', s) x = 0for I in result:x + = float (I) s = str (x) return s complete code import redef count (s): while'/'in s:result = re.findall ('([-]?\ d+\.\ d+ | [-]?\ d+) / ([-]?\ d+\.\ d+ | [-]?\ d+)')' S) for i in result:s = s.replace (f'{I [0]} / {I [1]}', f'{float (I [0]) / float (I [1])}') while'*'in s:result = re.findall ('([-]?\ d +\.\ d + | [-]?\ d +)\ * ([-]?\ d +\.\ d + | [-]?\ d +)')' S) for i in result:if float (I [0]) < 0 and float (I [1]) < 0 s.replace s = s.replace (f'{I [0]} * {I [1]}', fudge + {float (I [0]) * float (I [1])}') else:s = s.replace (f'{I [0]} * {I [1]}') F'{float (I [0]) * float (I [1])}') result = re.findall ('([-]?\ d +\.\ d + | [-]?\ d +)', s) x = 0for I in result:x + = float (I) s = str (x) return sdef cal (s): s = s.replace (',') while'('in s or')'in s:ret = re.findall ('\ (([^ ()] +?)\)')' S) for i in ret:s = s.replace (f'({I})', count (I)) s = s.replace ('- -' '+') else:s = count (s) return sprint (cal ('10-3 * (20-10 + (10-10 + (10-10) / (10-3) print (cal ('10-3 * (20-10 + (- 10-10)) * 27)) print (cal ('10-3 * () (50-30 + (- 10amp 5) *) (9-2x 5max 3 + 7 / 3m 99max 4x 2020 + 10 * 789max 15))-(- 4x 3) / (16-3pm 2) points for attention in out:58.088.0703066.4000000001
Note 1: the lightening sign may occur after dealing with the parentheses.
Note 2: a negative number multiplied by a negative number gets a positive number.
The above two precautions were not taken into account when coding. Later, the running program found that the results were wrong, and it took a long time to debug to find that the above two precautions had been ignored. Finally, the calculation results were correct after adding 2 lines of code.
The basic four operations are already very familiar, but there will still be omissions in the coding process. This often happens when writing projects. A similar situation often occurs when programmers communicate with the demand side, and the demand side often forgets to mention some common sense details, while the programmer is not proficient in the business on the demand side, and the programmer fails to give full consideration to the details not mentioned on the demand side, which will lead to omissions and bug.
Therefore, it is very important to strengthen communication, and it is very important for programmers to consider the needs comprehensively and carefully.
This is the end of the content of "python string four expression coding problem solving Analysis". Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.