Enrollments closing soon for Post Graduate Certificate Program in Applied Data Science & AI By IIT Roorkee | 3 Seats Left

  Apply Now

Scala Project - Churn Email Inbox with Scala

3 / 7

Scala Project - Churn Emails - Count the Number of Subject Lines

To check if a particular line in a file starts with a set of strings we use the startsWith function in Scala.

The below code prints the lines starting with From:

import scala.io.Source

def count_from_lines() : Int = {
    var countLines = 0
    val filename = "/cxldata/datasets/project/mbox-short.txt"
    for (line <- Source.fromFile(filename).getLines) {
        if (line.startsWith("From:")) {
            countLines = countLines + 1
        }
    }
    return(countLines)
}
INSTRUCTIONS
  • Define a function count_subject_lines
  • Open the file mbox-short.txt which is located at /cxldata/datasets/project/mbox-short.txt and read it to get each line using getLines function
  • Write the logic to count the total number of lines starting with Subject:
  • Return the count of the number of lines
  • Call the function in the next cell

Note: If your logic is correct then your function should return 27.

See Answer

No hints are availble for this assesment


Note - Having trouble with the assessment engine? Follow the steps listed here

Loading comments...