Registrations Closing Soon for DevOps Certification Training by CloudxLab | Registrations Closing in

  Enroll Now

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:


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
  • 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.

No hints are availble for this assesment

Answer is not availble for this assesment

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

Loading comments...