samedi 27 juin 2020

Which method is more efficient and better to do this task, also why? Please guide

Method A : In this method I have performed decision making without converting the 'line' from a string to list.

def isVariable(line):
    if not ';' in line:
        return False

    if ('public' in line or 'private' in line or 'protected' in line) and ('int' in line or 'String' in line or 'float' in line):
        return True
    else:
        return False


line = 'private int name;'
print(isVariable(line))

Method B : This method involves performing decision making after converting the 'line' to a list.

def isVariable(line):
    if not ';' in line:
        return False

    modifiers = ['private','protected','public']
    datatypes = ['int','float','String']
    linelist = list(line.split())

    if linelist[0] in modifiers and linelist[1] in datatypes:
        return True
    else:
        return False

line = 'private int name;'
print(isVariable(line))

Aucun commentaire:

Enregistrer un commentaire