Created
April 28, 2015 00:46
-
-
Save fran0x/297acd6ae9c262d9da82 to your computer and use it in GitHub Desktop.
s4di - Chapter 3 - Exercises
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package chapter3 | |
import util._ | |
import scala.collection.mutable.ArrayBuffer | |
import java.awt.datatransfer._ | |
import scala.collection.JavaConverters._ | |
import scala.collection.mutable.Buffer | |
object ExercisesCh3 { | |
// 1. array of random ints | |
val n = 9 //> n : Int = 9 | |
val arr = (for (i <- 0 until n) yield Random.nextInt(n) - 4).toArray | |
//> arr : Array[Int] = Array(4, -3, -4, -3, 2, -2, -1, 1, -1) | |
// 2. swap adjacent elements in array | |
for (i <- 0 until (if (arr.length % 2 == 0) arr.length else arr.length - 1, 2)) { | |
val tmp = arr(i + 1) | |
arr(i + 1) = arr(i) | |
arr(i) = tmp | |
} | |
arr //> res0: Array[Int] = Array(-3, 4, -3, -4, -2, 2, 1, -1, -1) | |
// 3. swap adjacent elements in array using yield | |
val swapI = for (i <- 0 until arr.length) yield { | |
if (i % 2 == 0) { if (i + 1 == arr.length) arr(i) else arr(i + 1) } else arr(i - 1) | |
//> swapI : scala.collection.immutable.IndexedSeq[Int] = Vector(4, -3, -4, -3, | |
//| 2, -2, -1, 1, -1) | |
} | |
// 4. first positives in original order, then rest in original order | |
val positivesFirst = arr.filter(_ > 0) ++ arr.filter(_ <= 0) | |
//> positivesFirst : Array[Int] = Array(4, 2, 1, -3, -3, -4, -2, -1, -1) | |
// 5. average of array of doubles | |
val buffD = for (i <- 0 until n) yield Random.nextDouble() | |
//> buffD : scala.collection.immutable.IndexedSeq[Double] = Vector(0.2045885726 | |
//| 179577, 0.8741192942635029, 0.9474730699062074, 0.013852047484936292, 0.8644 | |
//| 440771032579, 0.47384990715809827, 0.30323618399261465, 0.5198772704062483, | |
//| 0.43745313626007765) | |
val arrD = buffD.toArray //> arrD : Array[Double] = Array(0.2045885726179577, 0.8741192942635029, 0.9474 | |
//| 730699062074, 0.013852047484936292, 0.8644440771032579, 0.47384990715809827, | |
//| 0.30323618399261465, 0.5198772704062483, 0.43745313626007765) | |
arrD.sum / arrD.length //> res1: Double = 0.5154326176881001 | |
// 6. rearrange array of doubles (instead of ints) to appear in sorted reverse order; same with arraybuffer | |
arrD.sortWith(_ > _) //> res2: Array[Double] = Array(0.9474730699062074, 0.8741192942635029, 0.86444 | |
//| 40771032579, 0.5198772704062483, 0.47384990715809827, 0.43745313626007765, | |
//| 0.30323618399261465, 0.2045885726179577, 0.013852047484936292) | |
buffD.sorted.reverse //> res3: scala.collection.immutable.IndexedSeq[Double] = Vector(0.947473069906 | |
//| 2074, 0.8741192942635029, 0.8644440771032579, 0.5198772704062483, 0.4738499 | |
//| 0715809827, 0.43745313626007765, 0.30323618399261465, 0.2045885726179577, 0 | |
//| .013852047484936292) | |
// 7. remove duplicates from array | |
arr //> res4: Array[Int] = Array(-3, 4, -3, -4, -2, 2, 1, -1, -1) | |
(arr ++ arr).distinct //> res5: Array[Int] = Array(-3, 4, -4, -2, 2, 1, -1) | |
// 8. rewrite "Transforming Arrays" (page 34) using drop method for the index of the first match | |
val arrB = ArrayBuffer[Int](1, 2, -3, 4, -5, 6, 7, 8, -9) | |
//> arrB : scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(1, 2, -3, 4 | |
//| , -5, 6, 7, 8, -9) | |
var indexes = (for (i <- 0 until arrB.length if arrB(i) < 0) yield i) | |
//> indexes : scala.collection.immutable.IndexedSeq[Int] = Vector(2, 4, 8) | |
indexes = indexes drop (1) | |
for (j <- indexes.reverse) arrB.remove(j) | |
arrB //> res6: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(1, 2, -3, 4, | |
//| 6, 7, 8) | |
// 9. Make a collection of all time zones returned by java.util.TimeZone.getAvailableIDs | |
// that are in America. Strip off the "America/" prefix and sort the result. | |
val tz = java.util.TimeZone.getAvailableIDs().filter(_.startsWith("America/")).map(s => s.stripPrefix("America/")) | |
//> tz : Array[String] = Array(Adak, Anchorage, Anguilla, Antigua, Araguaina, | |
//| Argentina/Buenos_Aires, Argentina/Catamarca, Argentina/ComodRivadavia, Arge | |
//| ntina/Cordoba, Argentina/Jujuy, Argentina/La_Rioja, Argentina/Mendoza, Arge | |
//| ntina/Rio_Gallegos, Argentina/Salta, Argentina/San_Juan, Argentina/San_Luis | |
//| , Argentina/Tucuman, Argentina/Ushuaia, Aruba, Asuncion, Atikokan, Atka, Ba | |
//| hia, Bahia_Banderas, Barbados, Belem, Belize, Blanc-Sablon, Boa_Vista, Bogo | |
//| ta, Boise, Buenos_Aires, Cambridge_Bay, Campo_Grande, Cancun, Caracas, Cata | |
//| marca, Cayenne, Cayman, Chicago, Chihuahua, Coral_Harbour, Cordoba, Costa_R | |
//| ica, Creston, Cuiaba, Curacao, Danmarkshavn, Dawson, Dawson_Creek, Denver, | |
//| Detroit, Dominica, Edmonton, Eirunepe, El_Salvador, Ensenada, Fort_Wayne, F | |
//| ortaleza, Glace_Bay, Godthab, Goose_Bay, Grand_Turk, Grenada, Guadeloupe, G | |
//| uatemala, Guayaquil, Guyana, Halifax, Havana, Hermosillo, Indiana/Indianapo | |
//| lis, Indiana/Knox, Indi | |
//| Output exceeds cutoff limit. | |
// 10. make object of type SystemFlavorMap; call getNativesForFlavor with DataFlavor.imageFlavor and get Scala buffer | |
val flavors = SystemFlavorMap.getDefaultFlavorMap().asInstanceOf[SystemFlavorMap] | |
//> flavors : java.awt.datatransfer.SystemFlavorMap = java.awt.datatransfer.Sy | |
//| stemFlavorMap@6d4b1c02 | |
val natives = flavors.getNativesForFlavors(Array(DataFlavor.imageFlavor)) | |
//> natives : java.util.Map[java.awt.datatransfer.DataFlavor,String] = {java.a | |
//| wt.datatransfer.DataFlavor[mimetype=image/x-java-image;representationclass= | |
//| java.awt.Image]=PNG} | |
val buff = collection.JavaConversions.asScalaBuffer(new java.util.LinkedList(natives.values())) | |
//> buff : scala.collection.mutable.Buffer[String] = Buffer(PNG) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment