3

I am creating a simple Python calculator that uses the order of operations to do simple math, and adds in a square root and integer division feature. The concept is that the user could enclose the value they want to be square rooted within a sqr() function (for example, sqr(25) is equal to 5).

The problem is, I am having trouble pulling this value out of the equation string. Here is a pseudo-code of what it should do:

b = 'sqr(25)+5*3' # equation

# My pseudo-code:
# 1. Identify use of sqr()
# 2. Pull 'sqr(25)' out to be solved
# 3. Pull value (25) out and solve it with math.sqrt()
# 4. Replace 'sqr(25)' with the solved value (5)
# 
# b should now be equal to:
#   '5+5*3'

I am having trouble with pulling out the value from sqr() and putting it back in the correct spot. I tried looking online but this seems to be a bit more of an obscure question.

halfer
  • 19,824
  • 17
  • 99
  • 186
miike3459
  • 1,431
  • 2
  • 16
  • 32
  • Please read and follow the posting guidelines in the help documentation, as suggested when you created this account. [On topic](http://stackoverflow.com/help/on-topic) and [how to ask](http://stackoverflow.com/help/how-to-ask) apply here. StackOverflow is not a design, coding, research, or tutorial service. – Prune Jul 13 '18 at 23:58
  • ["Can Someone Help Me?" is not a valid SO question](https://meta.stackoverflow.com/questions/284236/why-is-can-someone-help-me-not-an-actual-question). This usually suggests that what you need is time with a local tutor or walk through a tutorial, rather than Stack Overflow. – Prune Jul 13 '18 at 23:58
  • You should take a look at [ask] and edit your question accordingly, specifically, you should show the code you wrote, describe what it did, what you expected it to do and how they differed. As to the calculator thing, I'm not sure what you searched but it's a very common homeworky-type problem with a couple of standard general approaches you should look into. They're both simpler and more general than what you're attempting. – pvg Jul 13 '18 at 23:58
  • 1
    Try with regular expressions: `re.findall(r'sqr\((\d+)\)', b)`, will get you the number you need to calculate the square root of. – user3483203 Jul 13 '18 at 23:58
  • Check what a regex can do for you. It can be done with carefully locating `sqr(` and the value and `)` but a regex is straightforward and will hand you its result in easy-to-process chunks. – Jongware Jul 13 '18 at 23:59

2 Answers2

2

You can use re.sub from the re module to solve your problem here. First, let's define a simple helper function that takes in a string of a number and calculates the square root, then returns a string:

def str_2_sqrt(s):
    return str(math.sqrt(int(s)))

Now for the hard part, we are going to use re.sub, with a custom lambda function that calls str_2_sqrt and places the result back in the string:

>>> re.sub(r'sqr\((\d+)\)', lambda x: str_2_sqrt(x.group(1)), b)
5.0+5*3
user3483203
  • 50,081
  • 9
  • 65
  • 94
0
b = 'sqr(25)+5*3' # equation
import math
def f(s):
   s1=s.replace('sqr','')
   s2=s1.replace(s1.split(')')[0][1:],str(int(math.sqrt(int(s1.split(')')[0][1:])))))
   return s2.replace(')','').replace('(','')
print(f(b))
U13-Forward
  • 69,221
  • 14
  • 89
  • 114