Python counting significant numbers

I marked this question as javascript, because although I currently wrote it in Python, if it would be easier to implement in Javascript, I could easily implement it in Javascript.

My task is to make a significant figure calculator for the chemistry department. This means that the student enters his data into the fields, the web application will perform predefined operations on his fields and track significant numbers and see if their answer has the correct number of significant numbers.

When I broke the problem and realized that this was a good workflow, I realized that I needed a way for Python (backend, since this is a web application made in Django) or Javascript (because you can always not check it at the front end ) to determine the number of significant digits. I did a little research and came across this question which tells me that I need to work with python strings instead of float. My current python code seems ALMOST complete, but there is another serious issue I am facing.

import re def find_sigfigs(x): # change the 'E' to lower case if the student typed it in as uppercase x = x.lower() if ('e' in x): myStr = x.split('e') # this function assumes that the number on the left of the 'e' is # the number of sigfigs. That would be true for user input but not # true when python converts a float to scientific notation return len( (re.search('[0-9]+', myStr[0])).group() ) else: # put it in e format and return the result of that ### problem: python makes me hard code the number of sigfigs as '2' ### without the 2 there it always defaults to 6 return find_sigfigs('%.*e' %(2,float(x))) >>> find_sigfigs('1.3e-4') >>> 2 >>> find_sigfigs('1234') >>> 3 >>> find_sigfigs('123456') >>> 3 >>> find_sigfigs('1.2345e3') >>> 5 

then without 2

 return find_sigfigs('%.e' %(float(x))) #Because it changes it to 1.234000e3 >>> find_sigfigs('1234') >>> 7 #Because it changes it to 1.234560e5 >>> find_sigfigs('123456') >>> 7 

So simple, my problem is that I need a simple way to count sigfigs if it is not explicitly declared by the student (he is when he is in scientific notation). Is there any simple way that I can reset every zero to "e" until it reaches the first digit other than zero. I think I need to start from the back of the split line and delete zeros until I get a great number?

EDIT: So, after a bit more trivial, I hope this is a suitable solution to the problem. I tested it several times, but not too rigorously (it probably works, but who knows! I'm not too good at siggs ...)

 def find_sigfigs(x): '''Returns the number of significant digits in a number. This takes into account strings formatted in 1.23e+3 format and even strings such as 123.450''' # change all the 'E' to 'e' x = x.lower() if ('e' in x): # return the length of the numbers before the 'e' myStr = x.split('e') return len( myStr[0] ) - 1 # to compenstate for the decimal point else: # put it in e format and return the result of that ### NOTE: because of the 8 below, it may do crazy things when it parses 9 sigfigs n = ('%.*e' %(8, float(x))).split('e') # remove and count the number of removed user added zeroes. (these are sig figs) if '.' in x: s = x.replace('.', '') #number of zeroes to add back in l = len(s) - len(s.rstrip('0')) #strip off the python added zeroes and add back in the ones the user added n[0] = n[0].rstrip('0') + ''.join(['0' for num in xrange(l)]) else: #the user had no trailing zeroes so just strip them all n[0] = n[0].rstrip('0') #pass it back to the beginning to be parsed return find_sigfigs('e'.join(n)) 
+4
source share
1 answer

I think the regexes are a bit overloaded here, but your method should work, and I'm sure this is not a performance issue.

I think that you are on the right track with what you described at the end. I would use split('e') and then rstrip('0') , which will remove the terminating zeros. Then you can put the string back if you want to save the recursive call.

+2
source

Source: https://habr.com/ru/post/1381372/


All Articles