Text reading is an integral component in our counting algorithms, and it is also ubiquitous in daily applications. The following is a very bad algorithm for reading from a text file, although it is correct and straightforward. It reads from a file line by line, and concatenate them into text. Then we split it using a regular expression. The split part is fast in linear complexity (we will learn it in Comp-2140), hence that is not our concern. Note that trim() is needed to remove empty strings. static String [] readTextBAD ( String PATH ) throws Exception { BufferedReader br =new BufferedReader (new FileReader ( PATH ) ) ; String text =""; String line =""; while (( line = br . readLine () ) != null ) text = text +" "+ line . trim () ; String tokens []= text . trim () . split ("[^a-zA -Z]+") ; return tokens ; } 5.4.1 Time complexity of readT extBAD Write below the time complexity of readTextBAD in terms of the text length n (number of lines), and explain your answer. Refer to repeat1 example in the textbook for the cause of low speed and its time complexity
Text reading is an integral component in our counting algorithms, and it is also ubiquitous in daily applications. The
following is a very bad
a file line by line, and concatenate them into text. Then we split it using a regular expression. The split part is
fast in linear complexity (we will learn it in Comp-2140), hence that is not our concern. Note that trim() is needed
to remove empty strings.
static String [] readTextBAD ( String PATH ) throws Exception {
BufferedReader br =new BufferedReader (new FileReader ( PATH ) ) ;
String text ="";
String line ="";
while (( line = br . readLine () ) != null )
text = text +" "+ line . trim () ;
String tokens []= text . trim () . split ("[^a-zA -Z]+") ;
return tokens ;
}
5.4.1 Time complexity of readT extBAD
Write below the time complexity of readTextBAD in terms of the text length n (number of lines), and explain your
answer. Refer to repeat1 example in the textbook for the cause of low speed and its time complexity.
Step by step
Solved in 4 steps with 3 images