Skip to content

Instantly share code, notes, and snippets.

@Haaroon
Created December 29, 2020 16:00
Show Gist options
  • Save Haaroon/474a2a946c4e1bf8987ea191d4843c49 to your computer and use it in GitHub Desktop.
Save Haaroon/474a2a946c4e1bf8987ea191d4843c49 to your computer and use it in GitHub Desktop.
def randomArr(n: Int)={
val rnd = new scala.util.Random
val rands = new Array[Int](n)
for (i <- 0 until rands.length)
rands(i) = rnd.nextInt(n)
rands
}
def swapElements(ogArray: Array[Int]) = {
var adjacent_pos = 0
var temp = 0
for (i <- 0 until ogArray.length){
if (i <= ogArray.length/2){
adjacent_pos = ogArray.length - 1 - i
temp = ogArray(adjacent_pos)
ogArray(adjacent_pos) = ogArray(i)
ogArray(i) = temp
}
}
ogArray
}
/*
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.
*/
def posNegElements(startingArray: Array[Int]) = {
startingArray.filter(_ > 0) ++ startingArray.filter(_ <= 0)
}
a.sum/a.length
a.sorted(Ordering.Int.reverse)
a.distinct
val givenArray = ArrayBuffer(0,-1, 1,-2, 2, 3, -4, 5, 6, -6)
def removeFirstNegative(givenArray: ArrayBuffer[Int]) = {
val givenArray = ArrayBuffer(0,-1, 1,-2, 2, 3, -4, 5, 6, -6)
val pos = givenArray.zipWithIndex.filter(_._1 < 0).map(_._2).drop(1).zipWithIndex.map(x => x._1 - x._2)
for (elem <- pos) givenArray.remove(elem)
}
val ids = java.util.TimeZone.getAvailableIDs
ids.filter(_.startsWith("America/")).map(x => x.replaceFirst("America/", "")).sorted
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment