Friday, June 3, 2016

Atomic Matrix 1: Letters vs. Numbers?

I am building up to solve a problem out of Dr. Cerro's 244 textbook.
First thing that I'd like to do is construct an "Atomic matrix" using molecules given by the user, something sort of like this:

                     Water    Carbon dioxide   Carbon monoxide    ...etc.
Carbon             0                   1                            1

Hydrogen         2                   0                            0

Oxygen            1                   2                            1
.
.
.


So, my goal is for the program to read the input molecular formulas (i.e. H2O for water, or CO2 for carbon dioxide), and construct the matrix with the appropriate values in the appropriate locations, combining the all the input information together.

I wrote this short program a week or so ago. It takes your name, and searches for a specified character, and returns the number of times that character appears in your name:

#Using a for-loop to run iterations through the characters of a scripts
name=raw_input("What is your name? ")
print " "
let=raw_input("What letter would you like to search for? ")
highlet=let.upper()
lowlet=let.lower()
#print highlet
#print lowlet
namelen=len(name)
#print namelen
totlets=0
for num in range (0,namelen):
    check=name[num]
    #print check
    if check==highlet or check==lowlet:
        totlets=totlets+1
    #print "totlets equals %s" %totlets
print " "
print "There are %s %s\'s in your name." %(totlets,highlet)

My plan is to modify the code so that instead of a user designated character, the code will automatically sum the number of times EACH charter appears in the string, using a similar method shown above.

The issue is that some atoms within a molecule occur more then the number of characters (There are two hydrogens in water, but only one H in  "H2O") So, my program will need to also take into account the numeric character following each letter (if there is one).

All that being said, is there a way to ask the code to distinguish between letters and numbers in a string?
I considered doing something involving an if-loop after counting the characters, which would ask if the character following a given letter within the for-loop was a number using something like:

for i in range (0,len(name):
       .
       .
       .
       if name[i+1]="1" or name[i+1]="2" or name[i+1]="3" or name[i+1]="3" or name[i+1]="4"....
            totlets=totlets + name[i+1] - 1  #Since the letter before the subscript would be counted twice.

I feel like there should be an easier way of doing it, like "if the character following name[i] is a number, add that number to the total number of name[i]'s in our count so far."

I'd appreciate any advice.

Thanks,

Elijah

6 comments:

  1. Elijah - yea, I see the problem ... from what I have seen (so far) python does not easily allow you to separate "character" from "integer" - so '5' is a character, and 5 is integer - I may have a round about solution - will post when have it working - the approach is to convert the characters to "ascii" using the "ord" command - and check against a dictionary - there may be other ways also

    ReplyDelete
  2. Elijah - yea, I see the problem ... from what I have seen (so far) python does not easily allow you to separate "character" from "integer" - so '5' is a character, and 5 is integer - I may have a round about solution - will post when have it working - the approach is to convert the characters to "ascii" using the "ord" command - and check against a dictionary - there may be other ways also

    ReplyDelete
  3. Elijah - take a look at this solution - it will read a string - check and see if any particular character in that string is a number (lost track of where this came from ...!)

    def is_number(s):
    try:
    float(s)
    return True
    except ValueError:
    pass

    try:
    import unicodedata
    unicodedata.numeric(s)
    return True
    except (TypeError, ValueError):
    pass

    return False

    print (is_number('foo'))

    inputstring = 'My Chemical Name is C2H5OH'

    for i in range(len(inputstring)):
    mychar = inputstring[i:i+1]
    if (is_number(mychar)):
    print mychar, ' is a number '

    ReplyDelete
    Replies
    1. Oops - the tabs/spaces are not right - fix them to run and see the results

      Delete
  4. Here's what I ended up going with, seems to work pretty well.

    def numbercheck(x):
    if x=='0' or x=='1' or x=='2' or x=='3' or x=='4' or x=='5' or x=='6' or x=='7'or x=='8'or x=='9':
    return True
    else: return False

    ReplyDelete