Enrollments closing soon for Post Graduate Certificate Program in Applied Data Science & AI By IIT Roorkee | 3 Seats Left
Apply NowLogin using Social Account
     Continue with GoogleLogin using your credentials
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)
}
count_subject_lines
mbox-short.txt
which is located at /cxldata/datasets/project/mbox-short.txt
and read it to get each line using getLines
functionSubject:
Note: If your logic is correct then your function should return 27.
Taking you to the next exercise in seconds...
Want to create exercises like this yourself? Click here.
No hints are availble for this assesment
Note - Having trouble with the assessment engine? Follow the steps listed here
Loading comments...