Square(n) Sum def squareSum(xs: List[Int]): Int = xs.map(x => x*x).sum String repeat def repeatStr(times: Int, str: String) = str * times Counting sheep... def countSheep(sheep: Array[Boolean]): Int = sheep.count(_ == true) Even or Odd def evenOrOdd(number: Int): String = if (number % 2 == 0) "Even" else "Odd" Convert boolean values to strings 'Yes' or 'No'. def boolToWord(boolean: Boolean): String = if (boolean) "Yes" else "No" Reversed Strings def solution(word: String): String = word.reverse Subtract the sum def subtractSum(n: Int) = "apple" // почему так - доказывайте сами, мне лень Transportation on vacation def cost(days: Int): Int = if(days >=7) days*40-50 else if(days>=3) days*40-20 else 40*days Century From Year def centuryFromYear(year: Int): Int = (year-1)/100+1 or def centuryFromYear(year: Int): Int = (year + 99) / 100 Convert a Number to a String! def numberToString(n: Int): String = n.toString Twice as old def twice_as_old(dad: Int, son: Int) = (dad - 2 * son).abs or def twice_as_old(dad: Int, son: Int) = math.abs(2 * son - dad) Beginner Series #2 Clock def past(h: Int, m: Int, s: Int): Int = h*3600000+m*60000+s*1000 Simple multiplication def multiply(n: Int): Int = if(n%2==0) n*8 else n*9 Is n divisible by x and y? def isDivisible(n: Int, x: Int, y: Int): Boolean = n % x == 0 && n % y == 0 Return Negative def makeNegative(n: Int): Int = -n.abs or def makeNegative(n: Int): Int = -math.abs(n) Grasshopper - Terminal game move function def move(pos: Int, roll: Int): Int = pos + 2 * roll Expressions Matter def expressionMatter(a: Int, b: Int, c: Int): Int = List(a+b+c, (a+b)*c,a*(b+c),a*b*c, a*b+c, a+b*c).max Opposite number def opposite(number: Double): Double = -number Opposites Attract def lovefunc(flower1: Int, flower2: Int): Boolean = (flower1 + flower2) % 2 == 1 or def lovefunc(flower1: Int, flower2: Int): Boolean = flower1 % 2 != flower2 % 2 Grasshopper - Summation def summation(n: Int): Int = n * (n + 1) / 2 Remove First and Last Character def removeChars(s: String): String = s.substring(1, s.length - 1) or def removeChars(s: String): String = s.tail.init Find the smallest integer in the array def findSmallestInt(nums:List[Int]):Int = nums.min Returning Strings def greet(name: String): String = "Hello, "+name+" how are you doing today?" or def greet(name: String): String = s"Hello, $name how are you doing today?" Reversed Words def reverseWords(str: String): String = str.split(' ').reverse.mkString(" ") You only need one - Beginner def check(seq:List[Any], elem: Any) = seq.contains(elem) Sum of positive def positiveSum(arr: Array[Int]): Int = arr.filter(x => x > 0).sum or def positiveSum(arr: Array[Int]): Int = arr.filter(_ > 0).sum Beginner - Lost Without a Map def maps(xs: List[Int]): List[Int] = xs.map(_ * 2) My head is at the wrong end! def fixTheMeerkat(arr: List[String]): List[String] = arr.reverse Count of positives / sum of negatives def countPositivesSumNegatives(xs: Array[Int]): (Int, Int) = (xs.count(_ > 0), xs.filter(_ < 0).sum) Remove String Spaces def noSpace(s: String): String = s.replace(" ", "") Regular Ball Super Ball class Ball(val ballType: String = "regular") or class Ball(bType: String="regular") { var ballType:String = bType } A wolf in sheep's clothing def warnTheSheepCheck(queue: Array[String]): String = if (queue.last == "wolf") "Pls go away and stop eating my sheep" else s"Oi! Sheep number ${queue.reverse.indexOf("wolf")}! You are about to be eaten by a wolf!" Beginner - Reduce but Grow def grow(xs: List[Long]): Long = xs.product or def grow(xs: List[Long]): Long = if(xs.isEmpty) return 1 xs.head*grow(xs.tail) Removing Elements def removeEveryOther[T](list: List[T]): List[T] = list.grouped(2).map(_.head).toList or def removeEveryOther[T](list: List[T]): List[T] = list.zipWithIndex.collect { case (x, i) if i%2==0 => x } Grasshopper - Check for factor def checkForFactor(base: Int, factor: Int): Boolean = base % factor == 0 Count Odd Numbers below n def oddCount(n: Long): Long = n / 2 If you can't sleep, just count sheep!! def countingSheep(n: Int): String = (1 to n).map(i => s"$i sheep...").mkString Pillars def distance(num_pill: Int, dist: Int, width: Int ): Int = if (num_pill == 1) 0 else ((num_pill - 1) * dist * 100) + ((num_pill - 2) * width) Thinkful - Number Drills: Pixelart planning def isDivisible(wallLength: Int, pixelSize: Int): Boolean = wallLength % pixelSize == 0