Home > Mobile >  Writing code for function / smiles in python
Writing code for function / smiles in python

Time:03-19

I wrote the following code, but when I run the test underneath it classifies all the chemicals as "None". I don't understand how to fix this. When I replace "smiles" with "test" and run the test then the output is correct.

def hydrocarbon_group(smiles):
    # your code here:
    if "=" in smiles:
        smiles = "alkene"
        #print(smiles, "is an alkene") 
    else:
        if "1" in smiles:
            smiles = "cycloalkane"
            #print(smiles, "is a cycloalkane")
        else:
            if "#" in smiles:
                smiles = "alkyne"
                #print(smiles, "is an alkyne")
            else:
                if "C" in smiles:
                    smiles = "alkane"
                    #print(smiles, "is a alkane")
                else:
                    smiles = "None"
                    #print(smiles, "is none")
        
#test = "C"
#hydrocarbon_group(test)


# test your function by checking whether the output below is correct
print('methane is', hydrocarbon_group("C"))
print('propene is', hydrocarbon_group("C=CC"))
print('ethyne is', hydrocarbon_group('C#C'))
print('cyclobutane is', hydrocarbon_group('C#C'))
print('methyl isocyanate is', hydrocarbon_group('CN=C=O'))

CodePudding user response:

insert return smiles at the bottom of the function it should work.

CodePudding user response:

Here's a slightly different way to do what you're asking. It uses two steps as follows:

  • In a list comprehension (essentially a one-line loop that creates a list of results for each iteration), record the result of a search for each of your key characters '=1#C' in smiles, and use the first successful search by accessing the element of the search results at index 0 (append a different character such as ' ' to the search results to cover the possibility that smiles is either empty or does not contain any of the key characters).
  • Using Python's match statement and case blocks, test for each of the key characters sequentially until the first match.
def hydrocarbon_group(smiles):        
    x = ''.join([c[:c in smiles] for c in '=1#C'] [' '])[0]
    match x:
        case "=": 
            return "alkene"
        case "1": 
            return "cycloalkane"
        case "#": 
            return "alkyne"
        case "C": 
            return "alkane"
        case _: 
            return None

print('methane is', hydrocarbon_group("C"))
print('propene is', hydrocarbon_group("C=CC"))
print('ethyne is', hydrocarbon_group('C#C'))
print('cyclobutane is', hydrocarbon_group('C#C'))
print('methyl isocyanate is', hydrocarbon_group('CN=C=O'))

Output:

methane is alkane
propene is alkene
ethyne is alkyne
cyclobutane is alkyne
methyl isocyanate is alkene

The reasons for preferring this approach to the one in your original code are:

  • Does not rely on nested if/else blocks.
  • match/case syntax provides a concise, readable expression of what conclusion to draw based on which key character was encountered.
  • For someone who is new to Python, the "exercise" of trying out the match statement helps build new language skills.

Arguments against this approach could include:

  • The list comprehension for detecting the first key character encountered in the desired sequence is too cryptic.
  • if/elif/else would provide an alternative that is also readable (though not as concise as the match statement) and does not require the list comprehension.
  • Related