/** * 1. Write a code snippet that sets a to an array of n random integers between 0 (inclusive) and n (exclusive) */ scala> def randomInRange(n:Int) = for(i <- 0 to n) yield Random.nextInt(n) randomInRange: (n: Int)scala.collection.immutable.IndexedSeq[Int] scala> randomInRange(100) res7: scala.collection.immutable.IndexedSeq[Int] = Vector(68, 47, 1, 20, 35, 93, 53, 24, 75, 46, 23, 61, 98, 62, 60, 38, 82, 69, 10, 82, 96, 30, 14, 50, 20, 42, 44, 11, 31, 18, 70, 13, 25, 77, 85, 89, 55, 45, 16, 44, 13, 50, 71, 58, 86, 16, 7, 79, 60, 31, 79, 52, 56, 86, 31, 85, 32, 84, 14, 54, 79, 56, 7, 68, 62, 87, 77, 4, 47, 28, 36, 23, 88, 19, 28, 41, 47, 3, 92, 88, 29, 22, 52, 8, 14, 76, 83, 45, 42, 45, 36, 77, 50, 12, 89, 28, 70, 97, 35, 70, 32) /** * 2. Write a loop that swaps adjacent elements of an array of integers. For example, Array(1, 2, 3, 4, 5) becomes Array(2, 1, 4, 3, 5). */ // http://stackoverflow.com/a/10160082/379235 scala> val a = Array(1, 2, 3, 4, 5) a: Array[Int] = Array(1, 2, 3, 4, 5) scala> a.grouped(2).flatMap(_.reverse).toArray res17: Array[Int] = Array(2, 1, 4, 3, 5) /** * 3. Repeat the preceding assignment, but produce a new array with the swapped values. Use for/yield. */ scala> (for {b <- a.grouped(2); c <- b.reverse} yield c).toArray res16: Array[Int] = Array(2, 1, 4, 3, 5) /** * 4. Given an array of integers, produce a new array that contains all positive values * of the original array, in their original order, followed by all values that are zero or negative, * in their original order */ scala> val b = Array(-1, 2,3,4, -10, 0, -12) b: Array[Int] = Array(-1, 2, 3, 4, -10, 0, -12) scala> val(positive, negative) = b partition(_ > 0) positive: Array[Int] = Array(2, 3, 4) negative: Array[Int] = Array(-1, -10, 0, -12) scala> positive ++ negative res11: Array[Int] = Array(2, 3, 4, -1, -10, 0, -12) // better approach - https://stackoverflow.com/questions/32109281/produce-new-array-where-all-positive-comes-first-then-negative-or-zero-but-in-s /** * 5. How do you compute the average of an Array[Double]? */ scala> val doubleArray = Array(12.3, 12.3) doubleArray: Array[Double] = Array(12.3, 12.3) scala> doubleArray.sum/doubleArray.length res15: Double = 12.3 /** * 6. How do you rearrange the elements of an Array[Int] so that they appear in reverse sorted order? * How do you do the same with an ArrayBuffer[Int]? */ scala> val arrayInt = Array(1,2,3,4,5,6,7,8,9) arrayInt: Array[Int] = Array(1, 2, 3, 4, 5, 6, 7, 8, 9) scala> arrayInt sortWith(_>_) res32: Array[Int] = Array(9, 8, 7, 6, 5, 4, 3, 2, 1) scala> import scala.collection.mutable.ArrayBuffer import scala.collection.mutable.ArrayBuffer scala> val arrayBufferInt = ArrayBuffer(1,3,2,4,5,6,7,9,8) arrayBufferInt: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(1, 3, 2, 4, 5, 6, 7, 9, 8) scala> arrayBufferInt sortWith(_>_) res33: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(9, 8, 7, 6, 5, 4, 3, 2, 1) /** * 7. Write a code snippet that produces all values from an array with duplicates removed. (Hint: Look at Scaladoc.) */ scala> val arrayWithDups = Array(2,2,4,5,6,6,3,3,1,1,1,1,0) arrayWithDups: Array[Int] = Array(2, 2, 4, 5, 6, 6, 3, 3, 1, 1, 1, 1, 0) scala> arrayWithDups.distinct res35: Array[Int] = Array(2, 4, 5, 6, 3, 1, 0) /* Not Attempting 08 */ /** * 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. */ scala> java.util.TimeZone.getAvailableIDs filter(t => t.contains("America/")) map (t => t.split('/') last) sortWith(_ < _) warning: there was one feature warning; re-run with -feature for details res45: Array[String] = Array(Adak, Anchorage, Anguilla, Antigua, Araguaina, Aruba, Asuncion, Atikokan, Atka, Bahia, Bahia_Banderas, Barbados, Belem, Belize, Beulah, Blanc-Sablon, Boa_Vista, Bogota, Boise, Buenos_Aires, Buenos_Aires, Cambridge_Bay, Campo_Grande, Cancun, Caracas, Catamarca, Catamarca, Cayenne, Cayman, Center, Chicago, Chihuahua, ComodRivadavia, Coral_Harbour, Cordoba, Cordoba, Costa_Rica, Creston, Cuiaba, Curacao, Danmarkshavn, Dawson, Dawson_Creek, Denver, Detroit, Dominica, Edmonton, Eirunepe, El_Salvador, Ensenada, Fort_Wayne, Fortaleza, Glace_Bay, Godthab, Goose_Bay, Grand_Turk, Grenada, Guadeloupe, Guatemala, Guayaquil, Guyana, Halifax, Havana, Hermosillo, Indianapolis, Indianapolis, Inuvik, Iqaluit, Jamaica, Jujuy, Jujuy, Juneau, Knox, Knox_IN, Kralendijk, La_Paz, L... scala> /** Not attempting 10 */