-
-
Save AllenSH12/6163ec67bae14c945364 to your computer and use it in GitHub Desktop.
This file contains 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
//1 | |
val n = 10 | |
val range = 0 until n | |
var a = new Array[Int](n) | |
for (i <- 0 until n) a(i) = range(rnd.nextInt(range.length)) | |
//2 | |
var a = Array(1, 2, 3, 4, 5) | |
for (i <- 0 until a.length) { | |
if (i % 2 == 0 && i < a.length - 1) { | |
val tmp = a(i) | |
a(i) = a(i + 1) | |
a(i + 1) = tmp | |
} | |
} | |
//3 | |
val a = Array(1, 2, 3, 4, 5) | |
var newArray = for (i <- 0 until a.length) yield { | |
if (i % 2 == 0) { | |
if (i == a.length - 1) { | |
a(i) | |
} else { | |
a(i+1) | |
} | |
} else { | |
a(i-1) | |
} | |
} | |
//waaay neater approach from SO: | |
val betterWay = a.grouped(2).flatMap(_.reverse).toArray | |
//4 nifty... | |
val a = Array(1, 2, 3, 4, 5, 0, -1, 4, 0, -5, -2, -9, 0) | |
val sorted = a.filter(x => x > 0) ++ a.filter(x => x <= 0) | |
//5 nifty... | |
val a = Array(2.0, 3.5, 4.6, 91.65, 13.124) | |
val avg = a.sum / a.length | |
//6 pretty sure I missed the point here | |
val a = Array(1, 2, 3, 4, 5, 0, -1, 4, 0, -5, -2, -9, 0) | |
val aDescending = a.sortWith(_ > _) | |
import collection.mutable.ArrayBuffer | |
val a = ArrayBuffer(1, 2, 3, 4, 5, 0, -1, 4, 0, -5, -2, -9, 0) | |
val aDescending = a.sortWith(_ > _) | |
//7 | |
val a = Array(1, 2, 3, 4, 5, 0, -1, 4, 0, -5, -2, -9, 0) | |
val distinct = a.distinct | |
//8 | |
import collection.mutable.ArrayBuffer | |
val a = ArrayBuffer(1, 2, 3, 4, 5, 0, -1, 4, 0, -5, -2, -9, 0) | |
var indexes = for (i <- 0 until a.size if a(i) < 0) yield i | |
indexes = indexes.drop(1) | |
for (j <- (0 until indexes.size).reverse) a.remove(indexes(j)) | |
//9 | |
import java.util.TimeZone | |
var timezones = TimeZone.getAvailableIDs() | |
timezones = timezones.filter(timezone => timezone.indexOf("America/") == 0) | |
timezones = timezones.map(timezone => timezone.replace("America/", "")) | |
timezones = timezones.sortWith(_ < _) | |
//10 | |
import java.awt.datatransfer._ | |
import collection.mutable.ArrayBuffer | |
import scala.collection.JavaConversions._ | |
val flavors = SystemFlavorMap.getDefaultFlavorMap().asInstanceOf[SystemFlavorMap] | |
val buffer = asScalaBuffer(flavors.getNativesForFlavor(DataFlavor.imageFlavor)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment