Untitled

mail@pastecode.io avatar
unknown
plain_text
3 years ago
5.1 kB
2
Indexable
Never
# Global variables initiation.
maillist = []
knownquestionslist = []

def CheckMails():
    rows = []
    inputrow = ""
    mail = []
    # Program recieves inputs rows in a loop - until "STOP" row is given. a List of rows is created.
    while (inputrow != "STOP"):
        inputrow = str(input())
        if (inputrow == "STOP"):
            break
        else:
            rows.append(inputrow)
    # Creating a new list - each member is a list (List of mails - 4 rows each).
    numofmails = int(len(rows)/4)
    for i in range(numofmails):
        mail = []
        for j in range(4):
            mail.append(rows[j+(4*i)])
        maillist.append(mail)
    approvedmails = []
    for i in range(len(maillist)):
        if (IsPolite(maillist[i]) == True):
            approvedmails.append(maillist[i])
    outputdetails = []
    for i in range(len(approvedmails)):
        outputdetails.append(AddQ(approvedmails[i][1], knownquestionslist))
        outputdetails.append(approvedmails[i][3])
    for i in range(len(knownquestionslist)):
        print(knownquestionslist[i])
        for j in range(len(outputdetails)):
            if j%2==0:
                if outputdetails[j]==i:
                    if (j<(len(outputdetails)-2)):
                        print(outputdetails[j+1], end =", ")
                    else:
                        print(outputdetails[j+1])


# Defining a function that returns True/False depending if a given mail is polite or not.
def IsPolite(mail):
    # first check - checks if first row of email is "Dear Moshe,".
    if (mail[0] == "Dear Moshe,"):
        dearcheck = True
    else:
        dearcheck = False
    # Second check - checks if second row of email ends with a question mark.
    question = mail[1]
    if (question[len(question)-1] == "?"):
        questionmarkcheck = True
    else:
        questionmarkcheck = False
    # Third check - checks if third row of email is "Thank You,".
    if (mail[2] == "Thank you,"):
        thankyoucheck = True
    else:
        thankyoucheck = False
    # Forth check - checks if forth row of email is a valid name.
    fullname = mail[3]
    names = fullname.split(" ")
    if len(names) == 2:
        firstname = names[0]
        lastname = names[1]
        if ((firstname[0].isupper() == True) and (lastname[0].isupper() == True)):
            counter1 = 0
            counter2 = 0
            for i in range(len(firstname)):
                if (firstname[i].islower() == True):
                    counter1 += 1
            for i in range(len(lastname)):
                if (lastname[i].islower() == True):
                    counter2 += 1
            if ((counter1 == (len(firstname)-1)) and (counter2 == (len(lastname)-1))):
                namecheck = True
            else:
                namecheck = False
        else:
            namecheck = False
    else:
        namecheck = False
    # Final decision - returns True if all checks are ok and false if one or more is not ok.
    if ((dearcheck == True) and (questionmarkcheck == True) and (thankyoucheck == True) and (namecheck == True)):
        return True
    else:
        return False

# Defining a function that checks whether a given question is known or not. returns the question index in the knownquestionlist. if a question is unknown - it add it to the list.
def AddQ(Q, Qllst):
    # Splitting a given question to pieces (list).
    wordlst = Q.split()
    # Making every word in the list lowercased.
    for i in range(len(wordlst)):
        wordlst[i] = wordlst[i].lower()
    # Making Capital letter at the beginning + 1 space between every word + question mark at the end)
    fixedquestion = ""
    for i in range(len(wordlst)):
        if (i == 0):
            tempfirstwrdlst = list(wordlst[i])
            tempfirstwrdlst[0] = tempfirstwrdlst[0].upper()
            tempfirstwrdstr = ""
            for j in range(len(tempfirstwrdlst)):
                tempfirstwrdstr += tempfirstwrdlst[j]    
            fixedquestion += tempfirstwrdstr
            fixedquestion += " "
        elif ((i != 0) and (i != (len(wordlst)-1))):
            fixedquestion += wordlst[i]
            fixedquestion += " "
        elif (i == (len(wordlst)-1)):
            fixedquestion += wordlst[i]
    # Searching if the question in known or not.
    if (Qllst==[]):
        knownquestionslist.append(fixedquestion)
        #print(f"added the first question to the list, the index is 0, the question was {fixedquestion}")
        return 0
    else:
        for i in range(len(Qllst)):
            if (fixedquestion == Qllst[i]):
                #print(f"found the question, the index is {i}, the question was {fixedquestion}")
                return i
            else:
                knownquestionslist.append(fixedquestion)
                #print(f"added an unknown question to the list, the index is {i+1}, the question was {fixedquestion}")
                return i+1

CheckMails()