/** * 1. Set up a map of prices for a number of gizmos that you covet. * Then produce a second map with the same keys and the prices at a 10 percent discount. */ scala> val items = Map("Tesla" -> 80000, "iWatch" -> 700) items: scala.collection.immutable.Map[String,Int] = Map(Tesla -> 80000, iWatch -> 700) scala> for ((item, value) <- items) yield(item, value - (value * 0.1)) res2: scala.collection.immutable.Map[String,Double] = Map(Tesla -> 72000.0, iWatch -> 630.0) /** * 2. Write a program that reads words from a file. Use a mutable map to count how often each word appears. */ // source file: https://www.gutenberg.org/cache/epub/35709/pg35709.txt scala> val words = scala.io.Source.fromFile("pg35709.txt").mkString.split("\\s+") words: Array[String] = Array(The, Project, Gutenberg, EBook, of, Making, Your, Camera, Pay,, by, Frederick, C., Davis, This, eBook, is, for, the, use, of, anyone, anywhere, at, no, cost, and, with, almost, no, restrictions, whatsoever., You, may, copy, it,, give, it, away, or, re-use, it, under, the, terms, of, the, Project, Gutenberg, License, included, with, this, eBook, or, online, at, www.gutenberg.net, Title:, Making, Your, Camera, Pay, Author:, Frederick, C., Davis, Release, Date:, March, 29,, 2011, [EBook, #35709], Language:, English, ***, START, OF, THIS, PROJECT, GUTENBERG, EBOOK, MAKING, YOUR, CAMERA, PAY, ***, Produced, by, The, Online, Distributed, Proofreading, Team, at, http://www.pgdp.net, (This, file, was, produced, from, images, generously, made, available, by, The, In... scala> val wordCount = scala.collection.mutable.HashMap[String, Int]() wordCount: scala.collection.mutable.HashMap[String,Int] = Map() scala> for (word <- words) { | val count = wordCount.getOrElse(word, 0) | wordCount(word) = count + 1 | } scala> word wordCount words scala> wordCount res1: scala.collection.mutable.HashMap[String,Int] = Map(arts -> 1, follow -> 3, request, -> 1, Lines. -> 1, demand -> 7, 1.E.4. -> 1, PRODUCT -> 2, 470 -> 1, Chicago, -> 3, scenic -> 1, J2 -> 1, untrimmed -> 1, photographs--not -> 1, basis. -> 1, "prints -> 1, instances. -> 1, Onion-Planter -> 1, trick -> 1, illustrating -> 3, prefer. -> 1, detected -> 1, non-exclusive. -> 1, famous -> 1, Competition -> 2, expense -> 1, created -> 2, renamed. -> 1, maggot -> 1, calendar-photographs, -> 1, widely-read -> 1, Publisher, -> 1, producers -> 1, Shapes -> 1, ARTICLES -> 2, yearly -> 2, retoucher -> 1, satisfy -> 2, agrees: -> 1, Gentleman_, -> 1, intellectual -> 2, hard -> 2, Porch. -> 1, sold.) -> 1, START -> 1, House -> 2, welcome -> 1, Dealers' -> 1, ... -> 2, pasted -> 1, _Cosmopolitan_ -... /** * 3. Repeat the preceding exercise with an immutable map. */ scala> val words = scala.io.Source.fromURL("https://www.gutenberg.org/cache/epub/35709/pg35709.txt").mkString.split("\\s+") words: Array[String] = Array(The, Project, Gutenberg, EBook, of, Making, Your, Camera, Pay,, by, Frederick, C., Davis, This, eBook, is, for, the, use, of, anyone, anywhere, at, no, cost, and, with, almost, no, restrictions, whatsoever., You, may, copy, it,, give, it, away, or, re-use, it, under, the, terms, of, the, Project, Gutenberg, License, included, with, this, eBook, or, online, at, www.gutenberg.net, Title:, Making, Your, Camera, Pay, Author:, Frederick, C., Davis, Release, Date:, March, 29,, 2011, [EBook, #35709], Language:, English, ***, START, OF, THIS, PROJECT, GUTENBERG, EBOOK, MAKING, YOUR, CAMERA, PAY, ***, Produced, by, The, Online, Distributed, Proofreading, Team, at, http://www.pgdp.net, (This, file, was, produced, from, images, generously, made, available, by, The, In... scala> var wordCount = Map[String, Int]() wordCount: scala.collection.immutable.Map[String,Int] = Map() scala> for (word <- words) { | val count = wordCount.getOrElse(word, 0) | wordCount = wordCount + (word -> (count + 1)) | } scala> wordCount res2: scala.collection.immutable.Map[String,Int] = Map("Should -> 1, serious -> 1, Farm-buildings -> 1, container, -> 1, comply -> 6, forgotten -> 1, stop, -> 2, Spell -> 1, Washington, -> 6, Advertises -> 1, rate -> 2, lights -> 1, submitted -> 1, engraver -> 3, generalize -> 2, Produced -> 2, Furniture: -> 1, Redistributing -> 1, non-fictionist, -> 1, Archive/American -> 2, wiser -> 1, "snap-shooters" -> 1, action. -> 3, 6, -> 1, supplementary -> 2, botany, -> 1, glean -> 1, locations -> 1, tenth, -> 1, Features -> 1, Advertising -> 1, Lens. -> 1, trade, -> 1, SHAPE -> 2, particularly -> 1, bird-dogs, -> 1, used -> 24, Mo., -> 1, eye -> 1, PUNITIVE -> 1, striking -> 2, gloss." -> 1, job--as -> 1, picture. -> 1, instance, -> 1, READ -> 1, Sets -> 1, Medical: -> 1, advertising-medium. -... scala> /** * 4. Repeat the preceding exercise with a sorted map, so that the words are printed in sorted order. */ scala> val words = scala.io.Source.fromURL("https://www.gutenberg.org/cache/epub/35709/pg35709.txt").mkString.split("\\s+") words: Array[String] = Array(The, Project, Gutenberg, EBook, of, Making, Your, Camera, Pay,, by, Frederick, C., Davis, This, eBook, is, for, the, use, of, anyone, anywhere, at, no, cost, and, with, almost, no, restrictions, whatsoever., You, may, copy, it,, give, it, away, or, re-use, it, under, the, terms, of, the, Project, Gutenberg, License, included, with, this, eBook, or, online, at, www.gutenberg.net, Title:, Making, Your, Camera, Pay, Author:, Frederick, C., Davis, Release, Date:, March, 29,, 2011, [EBook, #35709], Language:, English, ***, START, OF, THIS, PROJECT, GUTENBERG, EBOOK, MAKING, YOUR, CAMERA, PAY, ***, Produced, by, The, Online, Distributed, Proofreading, Team, at, http://www.pgdp.net, (This, file, was, produced, from, images, generously, made, available, by, The, In... scala> var wordCountSorted = scala.collection.immutable.SortedMap[String, Int]() wordCountSorted: scala.collection.immutable.SortedMap[String,Int] = Map() scala> for(word <- words) { | val count = wordCountSorted.getOrElse(word, 0) | wordCountSorted = wordCountSorted + (word -> (count + 1)) | } scala> wordCountSorted res2: scala.collection.immutable.SortedMap[String,Int] = Map("'Second -> 1, "1001 -> 1, "A -> 3, "All -> 2, "And -> 1, "Any -> 1, "But -> 1, "Defects," -> 1, "Do -> 1, "Draw -> 1, "Either -> 1, "First -> 5, "For -> 2, "From -> 1, "Here -> 1, "How -> 1, "In -> 2, "Information -> 1, "No -> 1, "Non-Exclusive" -> 1, "Not -> 1, "Open -> 1, "Pardon -> 1, "Pay-As-You-Leave" -> 1, "Plain -> 2, "Project -> 5, "Right -> 1, "Second -> 3, "Sell" -> 1, "Shoot -> 1, "Should -> 1, "Sky." -> 1, "Sneezing -> 1, "Speed-Pictures," -> 1, "Study -> 1, "The -> 10, "There -> 1, "There's -> 1, "Transference -> 1, "We -> 1, "Well, -> 1, "What -> 2, "Where -> 1, "Winter-Sports," -> 1, "Work -> 1, "_'How -> 1, "a -> 1, "arrow-points-to-the-scene-of-the-crime" -> 1, "author" -> 1, "barrels -> 1, "before -> 2, "by ... /** * 5. Repeat the preceding exercise with a java.util.TreeMap that you adapt to the Scala API. */ scala> import java.util.TreeMap import java.util.TreeMap scala> var wordCountTreeMap = new TreeMap[String, Int]() wordCountTreeMap: java.util.TreeMap[String,Int] = {} scala> for(word <- words) { | val count = wordCountTreeMap.getOrDefault(word, 0) | wordCountTreeMap.put(word, count+1) | } scala> wordCount wordCountSorted wordCountTreeMap scala> wordCountTreeMap res7: java.util.TreeMap[String,Int] = {"'Second=1, "1001=1, "A=3, "All=2, "And=1, "Any=1, "But=1, "Defects,"=1, "Do=1, "Draw=1, "Either=1, "First=5, "For=2, "From=1, "Here=1, "How=1, "In=2, "Information=1, "No=1, "Non-Exclusive"=1, "Not=1, "Open=1, "Pardon=1, "Pay-As-You-Leave"=1, "Plain=2, "Project=5, "Right=1, "Second=3, "Sell"=1, "Shoot=1, "Should=1, "Sky."=1, "Sneezing=1, "Speed-Pictures,"=1, "Study=1, "The=10, "There=1, "There's=1, "Transference=1, "We=1, "Well,=1, "What=2, "Where=1, "Winter-Sports,"=1, "Work=1, "_'How=1, "a=1, "arrow-points-to-the-scene-of-the-crime"=1, "author"=1, "barrels=1, "before=2, "by=1, "cashed=1, "dead"=1, "deliver=1, "delivers=2, "eaten=1, "either=1, "film-eater."=1, "flivver"=1, "for=1, "fuzzy."=1, "get=1, "he=1, "heart-trouble."=1, "hot=1, "it's=1, "my... /** * 6. Define a linked hash map that maps "Monday" to java.util.Calendar.MONDAY, and similarly for the other weekdays. Demonstrate that the elements are visited in insertion order. */ scala> import scala.collection.mutable.LinkedHashMap import scala.collection.mutable.LinkedHashMap scala> import java.util.Calendar._ import java.util.Calendar._ scala> val weekdays = LinkedHashMap[String, Int]("Monday" -> MONDAY, "Tuesday" -> TUESDAY, "Sunday" -> SUNDAY, "Friday" -> FRIDAY) weekdays: scala.collection.mutable.LinkedHashMap[String,Int] = Map(Monday -> 2, Tuesday -> 3, Sunday -> 1, Friday -> 6) scala> weekdays res0: scala.collection.mutable.LinkedHashMap[String,Int] = Map(Monday -> 2, Tuesday -> 3, Sunday -> 1, Friday -> 6) /** * 7. Print a table of all Java properties, like this: java.runtime.name             | Java(TM) SE Runtime Environment sun.boot.library.path         | /home/apps/jdk1.6.0_21/jre/lib/i386 java.vm.version               | 17.0-b16 java.vm.vendor                | Sun Microsystems Inc. java.vendor.url               | http://java.sun.com/ path.separator                | : java.vm.name                  | Java HotSpot(TM) Server VM You need to find the length of the longest key before you can print the table. */ // https://stackoverflow.com/questions/32217170/how-to-have-width-between-values scala> import scala.collection.JavaConversions.propertiesAsScalaMap import scala.collection.JavaConversions.propertiesAsScalaMap scala> val props: scala.collection.Map[String, String] = System.getProperties() props: scala.collection.Map[String,String] = Map(env.emacs -> "", java.runtime.name -> Java(TM) SE Runtime Environment, sun.boot.library.path -> /Library/Java/JavaVirtualMachines/jdk1.8.0_51.jdk/Contents/Home/jre/lib, java.vm.version -> 25.51-b03, gopherProxySet -> false, java.vm.vendor -> Oracle Corporation, java.vendor.url -> http://java.oracle.com/, path.separator -> :, java.vm.name -> Java HotSpot(TM) 64-Bit Server VM, file.encoding.pkg -> sun.io, user.country -> US, sun.java.launcher -> SUN_STANDARD, sun.os.patch.level -> unknown, java.vm.specification.name -> Java Virtual Machine Specification, user.dir -> /Users/harit, java.runtime.version -> 1.8.0_51-b16, java.awt.graphicsenv -> sun.awt.CGraphicsEnvironment, java.endorsed.dirs -> /Library/Java/JavaVirtualMachines/jdk1.8.0_51.jdk... scala> props.keys.map(_.length).max res24: Int = 29 scala> for ((k,v) <- props) yield println(k.padTo(29, " ").mkString + "|" + v) env.emacs | java.runtime.name |Java(TM) SE Runtime Environment sun.boot.library.path |/Library/Java/JavaVirtualMachines/jdk1.8.0_51.jdk/Contents/Home/jre/lib java.vm.version |25.51-b03 gopherProxySet |false java.vm.vendor |Oracle Corporation java.vendor.url |http://java.oracle.com/ path.separator |: java.vm.name |Java HotSpot(TM) 64-Bit Server VM file.encoding.pkg |sun.io user.country |US sun.java.launcher |SUN_STANDARD sun.os.patch.level |unknown java.vm.specification.name |Java Virtual Machine Specification user.dir |/Users/harit java.runtime.version |1.8.0_51-b16 java.awt.graphicsenv |sun.awt.CGraphicsEnvironment java.endorsed.dirs |/Library/Java/JavaVirtualMachines/jdk1.8.0_51.jdk/Contents/Home/jre/lib/endorsed os.arch |x86_64 java.io.tmpdir |/var/folders/39/rrqhhzfn42bf7qcwqg2kywtr0000gn/T/ line.separator | java.vm.specification.vendor |Oracle Corporation os.name |Mac OS X sun.jnu.encoding |UTF-8 java.library.path |/Users/harit/Library/Java/Extensions:/Library/Java/Extensions:/Network/Library/Java/Extensions:/System/Library/Java/Extensions:/usr/lib/java:. java.specification.name |Java Platform API Specification java.class.version |52.0 sun.management.compiler |HotSpot 64-Bit Tiered Compilers os.version |10.10.4 http.nonProxyHosts |local|*.local|169.254/16|*.169.254/16 user.home |/Users/harit user.timezone |America/Los_Angeles scala.home |/usr/local/Cellar/scala/2.11.7/libexec java.awt.printerjob |sun.lwawt.macosx.CPrinterJob file.encoding |UTF-8 java.specification.version |1.8 scala.usejavacp |true java.class.path |"" user.name |harit java.vm.specification.version|1.8 sun.java.command |scala.tools.nsc.MainGenericRunner java.home |/Library/Java/JavaVirtualMachines/jdk1.8.0_51.jdk/Contents/Home/jre sun.arch.data.model |64 user.language |en java.specification.vendor |Oracle Corporation awt.toolkit |sun.lwawt.macosx.LWCToolkit java.vm.info |mixed mode java.version |1.8.0_51 java.ext.dirs |/Users/harit/Library/Java/Extensions:/Library/Java/JavaVirtualMachines/jdk1.8.0_51.jdk/Contents/Home/jre/lib/ext:/Library/Java/Extensions:/Network/Library/Java/Extensions:/System/Library/Java/Extensions:/usr/lib/java sun.boot.class.path |/Library/Java/JavaVirtualMachines/jdk1.8.0_51.jdk/Contents/Home/jre/lib/resources.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_51.jdk/Contents/Home/jre/lib/rt.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_51.jdk/Contents/Home/jre/lib/sunrsasign.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_51.jdk/Contents/Home/jre/lib/jsse.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_51.jdk/Contents/Home/jre/lib/jce.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_51.jdk/Contents/Home/jre/lib/charsets.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_51.jdk/Contents/Home/jre/lib/jfr.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_51.jdk/Contents/Home/jre/classes:/usr/local/Cellar/scala/2.11.7/libexec/lib/akka-actor_2.11-2.3.10.jar:/usr/local/Cellar/scala/2.11.7/libexec/lib/config-1.2.1.jar:/usr/local/Cellar/scala/2.11.7/libexec/lib/jline-2.12.1.jar:/usr/local/Cellar/scala/2.11.7/libexec/lib/scala-actors-2.11.0.jar:/usr/local/Cellar/scala/2.11.7/libexec/lib/scala-actors-migration_2.11-1.1.0.jar:/usr/local/Cellar/scala/2.11.7/libexec/lib/scala-compiler.jar:/usr/local/Cellar/scala/2.11.7/libexec/lib/scala-continuations-library_2.11-1.0.2.jar:/usr/local/Cellar/scala/2.11.7/libexec/lib/scala-continuations-plugin_2.11.7-1.0.2.jar:/usr/local/Cellar/scala/2.11.7/libexec/lib/scala-library.jar:/usr/local/Cellar/scala/2.11.7/libexec/lib/scala-parser-combinators_2.11-1.0.4.jar:/usr/local/Cellar/scala/2.11.7/libexec/lib/scala-reflect.jar:/usr/local/Cellar/scala/2.11.7/libexec/lib/scala-swing_2.11-1.0.2.jar:/usr/local/Cellar/scala/2.11.7/libexec/lib/scala-xml_2.11-1.0.4.jar:/usr/local/Cellar/scala/2.11.7/libexec/lib/scalap-2.11.7.jar java.vendor |Oracle Corporation file.separator |/ java.vendor.url.bug |http://bugreport.sun.com/bugreport/ sun.io.unicode.encoding |UnicodeBig sun.cpu.endian |little socksNonProxyHosts |local|*.local|169.254/16|*.169.254/16 ftp.nonProxyHosts |local|*.local|169.254/16|*.169.254/16 sun.cpu.isalist | res42: Iterable[Unit] = ArrayBuffer((), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), ()) /** * 8. Write a function minmax(values: Array[Int]) that returns a pair containing the smallest and largest values in the array. */ scala> def minmax(values: Array[Int]):(Int, Int) = (values.min, values.max) minmax: (values: Array[Int])(Int, Int) scala> minmax(Array(1, 100, -1, 20)) res45: (Int, Int) = (-1,100) /** * 9. Write a function lteqgt(values: Array[Int], v: Int) that returns a triple * containing the counts of values less than v, equal to v, and greater than v. */ scala> def lteqgt(values: Array[Int], v: Int): (Int, Int, Int) = (values.count(_ < v), values.count(_ == v), values.count(_ > v)) lteqgt: (values: Array[Int], v: Int)(Int, Int, Int) scala> lteqgt(Array(0,0,1,1,1,2,2), 1) res47: (Int, Int, Int) = (2,3,2) /** * 10. What happens when you zip together two strings, such as "Hello".zip("World")? Come up with a plausible use case. */ scala> "Hello".zip("World") res52: scala.collection.immutable.IndexedSeq[(Char, Char)] = Vector((H,W), (e,o), (l,r), (l,l), (o,d)) // use-case? May be sum 2 arrays