Skip to content
Question

Q.Write a method/function ISTOUPCOUNT() in python to read contents from a text file WRITER.TXT, to count and display the occurrence of the word "IS" or "TO" or "UP". For example : If the content of the file is IT IS UP TO US TO TAKE CARE OF OUR SURROUNDING. IT IS NOT POSSIBLE ONLY FOR THE GOVERNMENT TO TAKE RESPONSIBILITY The method/function should display Count of IS TO and UP is 6

(OR)
Write a method/function AEDISP() in python to read lines from a text file WRITER.TXT, and display those lines, which are starting either with A or starting with E. For example : If the content of the file is A CLEAN ENVIRONMENT IS NECESSARY FOR OUR GOOD HEALTH. WE SHOULD TAKE CARE OF OUR ENVIRONMENT. EDUCATIONAL INSTITUTIONS SHOULD TAKE THE LEAD. The method should display A CLEAN ENVIRONMENT IS NECESSARY FOR OUR GOOD HEALTH. EDUCATIONAL INSTITUTIONS SHOULD TAKE THE LEAD.
CBSECBSE Class XII Board 2019Subjective· 2mImportance★★★★★
🔒 Locked · start free trial →

You're viewing a preview — the full solution, concept, methods & PYQ mapping are locked.

Start your 14-day free trial to unlock the full solution →

Part (a): ISTOUPCOUNT() counts words equal to "IS"/"TO"/"UP" and prints Count of IS TO and UP is 6 for the sample.

Part (b): AEDISP() prints only the lines that begin with 'A' or 'E'.

Part (a)

Read the whole file, break it into words with split(), and test each word against the set IS, TO, UP. (The check is on whole words, so UPWARD would not count as UP.)

def ISTOUPCOUNT():
    f = open("WRITER.TXT", "r")
    data = f.read()
    f.close()
    count = 0
    for word in data.split():
        if word in ("IS", "TO", "UP"):
            count += 1
    print("Count of IS TO and UP is", count)

For the sample content IT IS UP TO US TO TAKE CARE ... GOVERNMENT TO TAKE RESPONSIBILITY, the matches are IS (2), TO (3), UP (1) = 6:

Count of IS TO and UP is 6 …

Unlock everything free for 14 days

  • Full step-by-step solutions
  • Concept-first explanations
  • Methods, shortcuts & mistakes
  • PYQ mapping + timed mock tests

Full access for 14 days. No credit card required.