/** * 1. Write a Scala code snippet that reverses the lines in a file (making the last line the first one, and so on). */ scala> import scala.io.Source import scala.io.Source scala> val source = Source.fromURL("http://www.gutenberg.org/cache/epub/4809/pg4809.txt", "UTF-8") source: scala.io.BufferedSource = non-empty iterator scala> source.getLines.toArray.reverse res10: Array[String] = Array(*END THE SMALL PRINT! FOR PUBLIC DOMAIN EBOOKS*Ver.02/11/02*END*, "", express permission.], they hardware or software or any other related product without, used in any sales of Project Gutenberg eBooks or other materials be, Michael S. Hart. Project Gutenberg is a TradeMark and may not be, when distributed free of all fees. Copyright (C) 2001, 2002 by, [Portions of this eBook's header and trailer may be reprinted only, "", hart@pobox.com, software or other items, please contact Michael Hart at:, If you are interested in contributing scanning equipment or, "", "Project Gutenberg Literary Archive Foundation.", Money should be paid to the:, public domain materials, or royalty free copyright licenses., The Project gratefully accepts contributions of money, tim... scala> /** * 2. Write a Scala program that reads a file with tabs, replaces each tab with spaces so that tab stops are at n-column boundaries, and writes the result to the same file. */ // File (with tabs) /** * $ cat ~/Downloads/fileWithTabs.txt a b c d e f */ scala> val tabPattern = """\t""".r tabPattern: scala.util.matching.Regex = \t scala> val source = Source.fromFile("/Users/harit/Downloads/fileWithTabs.txt") source: scala.io.BufferedSource = non-empty iterator scala> val lines = source.getLines.toArray lines: Array[String] = Array(a b c, d e f) scala> lines(0) res13: String = a b c scala> import java.io.PrintWriter import java.io.PrintWriter scala> val out = new PrintWriter("/Users/harit/Downloads/fileWithTabs.txt") out: java.io.PrintWriter = java.io.PrintWriter@106e6dfe scala> for(line <- replacedLines) {out.println(line)} scala> out.flush scala> out.close /** *“3. Write a Scala code snippet that reads a file and prints all words with more than 12 characters to the console. Extra credit if you can do this in a single line. */ scala> import scala.io.Source import scala.io.Source scala> Source.fromURL("http://www.gutenberg.org/cache/epub/4809/pg4809.txt", "UTF-8").mkString.split("\\s").filter(_.length > 12) res7: Array[String] = Array(redistributing, Volunteers*****, council--Policy, Orange--Corrupt, government--Efforts, reform--Influence, Armenteros--Painful, retire--Secret, Philip--Ominous, persecution--Execution, Antwerp--Horrible, Protestants--Remonstrance, Titelmann--Obduracy, Philip--Council, Trent--Quarrel, envoys--Order, Netherlands--Opposition, measure--Reluctance, Spain--Violent, instructions--Remarkable, Orange--Apoplexy, Viglius--Temporary, Hopper--Departure, Cambray--Character, Archbishop--Egmont, Spain--Flattery, bribery--Council, Doctors--Vehement, --Proceedings, principality--Egmont's, persecution--Indignation, Egmont--Habitual, dissimulation, King--Reproof, Orange--Assembly, Brussels--Result, deliberations, Philip--Universal, Netherlands--New, heretics--Interview, subject-... scala> /** * 4. Write a Scala program that reads a text file containing only floating-point numbers. Print the sum, average, maximum, and minimum of the numbers in the file. */ scala> import scala.io.Source import scala.io.Source scala> val numbers = Source.fromFile("/Users/harit/Downloads/floating.txt", "UTF-8").getLines.map(_.toDouble).toArray numbers: Array[Double] = Array(3.14, 2.34, 1.54, 8.34) scala> numbers.min res9: Double = 1.54 scala> numbers.max res10: Double = 8.34 scala> numbers.sum res11: Double = 15.36 scala> numbers.sum/numbers.size res12: Double = 3.84 /** * 5. Write a Scala program that writes the powers of 2 and their reciprocals to a file, with the exponent ranging from 0 to 20. Line up the columns:   1      1   2      0.5   4      0.25 ...      ... */ scala> val out = new java.io.PrintWriter("out.txt") out: java.io.PrintWriter = java.io.PrintWriter@259aec72 scala> for (i <- 0 to 20) { | val pow = scala.math.pow(2, i) | out.println(pow + "\t" + 1/pow) | } scala> out.close scala> /** * $ cat out.txt 1.0 1.0 2.0 0.5 4.0 0.25 8.0 0.125 16.0 0.0625 32.0 0.03125 64.0 0.015625 128.0 0.0078125 256.0 0.00390625 512.0 0.001953125 1024.0 9.765625E-4 2048.0 4.8828125E-4 4096.0 2.44140625E-4 8192.0 1.220703125E-4 16384.0 6.103515625E-5 32768.0 3.0517578125E-5 65536.0 1.52587890625E-5 131072.0 7.62939453125E-6 262144.0 3.814697265625E-6 524288.0 1.9073486328125E-6 1048576.0 9.5367431640625E-7 */ // skipping 6-10