├── .gitattributes ├── Solutions ├── CamelCase.scala ├── Day 5: Loops.scala ├── Day 10: Binary Numbers.scala ├── Day 9: Recursion.scala ├── Day 7: Arrays.scala ├── Arrays - DS.scala ├── Staircase.scala ├── Intro to Tutorial Challenges.scala ├── A Very Big Sum.scala ├── Day 1: Data Types.scala ├── Game of Thrones - I.scala ├── Kangaroo.scala ├── Super Reduced String.scala ├── Pangrams.scala ├── Big Sorting.scala ├── Day 11: 2D Arrays.scala ├── Day 3: Intro to Conditional Statements.scala ├── Grading Students.scala ├── Birthday Cake Candles.scala ├── Mars Exploration.scala ├── Day 6: Let's Review.scala ├── Day 8: Dictionaries and Maps.scala ├── Mini-Max Sum.scala ├── HackerRank in a String!-Part2.scala ├── Time Conversion.scala ├── Day 2: Operators.scala ├── Diagonal Difference.scala ├── Caesar Cipher.scala ├── Day 4: Class vs. Instance.scala ├── Compare the Triplets.scala ├── Plus Minus.scala ├── Apple and Orange.scala ├── Weighted Uniform Strings.scala └── HackerRank in a String!.scala └── README.md /.gitattributes: -------------------------------------------------------------------------------- 1 | $ cat .gitattributes 2 | *.rb linguist-language=Scala 3 | 4 | * text=auto 5 | -------------------------------------------------------------------------------- /Solutions/CamelCase.scala: -------------------------------------------------------------------------------- 1 | 2 | object Solution { 3 | 4 | def main(args: Array[String]) { 5 | val sc = new java.util.Scanner (System.in); 6 | var s = scala.io.StdIn.readLine(); 7 | println(s.length() - s.replaceAll("[A-Z]", "").length() + 1) 8 | } 9 | } 10 | 11 | -------------------------------------------------------------------------------- /Solutions/Day 5: Loops.scala: -------------------------------------------------------------------------------- 1 | object Solution { 2 | 3 | def main(args: Array[String]) { 4 | val sc = new java.util.Scanner (System.in); 5 | var n = sc.nextInt(); 6 | 7 | for(i <- 1 to 10) 8 | { 9 | println(s"$n x $i = "+(n*i).toString) 10 | } 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /Solutions/Day 10: Binary Numbers.scala: -------------------------------------------------------------------------------- 1 | object Solution { 2 | 3 | def main(args: Array[String]) { 4 | val sc = new java.util.Scanner (System.in); 5 | var n = sc.nextInt(); 6 | 7 | var temp = n.toBinaryString.toInt 8 | var listOfOnes = temp.toString.split("0").toList 9 | println(listOfOnes.max.size) 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /Solutions/Day 9: Recursion.scala: -------------------------------------------------------------------------------- 1 | object Solution { 2 | 3 | def factorial(n: Int): Int = { 4 | if(n==1) return 1 5 | return factorial(n-1) * n 6 | } 7 | 8 | def main(args: Array[String]) { 9 | val sc = new java.util.Scanner (System.in); 10 | var n = sc.nextInt(); 11 | val result = factorial(n); 12 | println(result) 13 | } 14 | } 15 | 16 | -------------------------------------------------------------------------------- /Solutions/Day 7: Arrays.scala: -------------------------------------------------------------------------------- 1 | object Solution { 2 | 3 | def main(args: Array[String]) { 4 | val sc = new java.util.Scanner (System.in); 5 | var n = sc.nextInt(); 6 | var arr = new Array[Int](n); 7 | for(arr_i <- 0 to n-1) { 8 | arr(arr_i) = sc.nextInt(); 9 | } 10 | arr.reverse.foreach((i: Int) => printf(i.toString + " ")) 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /Solutions/Arrays - DS.scala: -------------------------------------------------------------------------------- 1 | object Solution { 2 | 3 | def main(args: Array[String]) { 4 | val sc = new java.util.Scanner (System.in); 5 | var n = sc.nextInt(); 6 | var arr = new Array[Int](n); 7 | for(arr_i <- 0 to n-1) { 8 | arr(arr_i) = sc.nextInt(); 9 | } 10 | 11 | for(i <- Range(n-1, -1, -1)){ 12 | printf(arr(i).toString + " ") 13 | } 14 | } 15 | } 16 | 17 | -------------------------------------------------------------------------------- /Solutions/Staircase.scala: -------------------------------------------------------------------------------- 1 | object Solution { 2 | 3 | def main(args: Array[String]) { 4 | val sc = new java.util.Scanner (System.in); 5 | var n = sc.nextInt(); 6 | 7 | val N = n.toInt 8 | 9 | for(i <- Range(0,N)) 10 | { 11 | var output1 = "" 12 | var output2 = "" 13 | 14 | output1 = " "*((N-1) - i) 15 | output2 = "#"*(i+1) 16 | 17 | println(output1 + output2) 18 | } 19 | } 20 | } 21 | 22 | -------------------------------------------------------------------------------- /Solutions/Intro to Tutorial Challenges.scala: -------------------------------------------------------------------------------- 1 | object Solution { 2 | 3 | def main(args: Array[String]) { 4 | val sc = new java.util.Scanner (System.in); 5 | var V = sc.next(); 6 | var n = sc.nextInt(); 7 | var ar = new Array[Int](n); 8 | for(ar_i <- 0 to n-1) { 9 | ar(ar_i) = sc.nextInt(); 10 | } 11 | 12 | for(i <- 0 to n-1) 13 | { 14 | if(V.toInt == ar(i).toInt) println(i) 15 | } 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /Solutions/A Very Big Sum.scala: -------------------------------------------------------------------------------- 1 | object Solution { 2 | 3 | def aVeryBigSum(n: Int, ar: Array[Long]): Long = { 4 | // Complete this function 5 | return ar.sum 6 | } 7 | 8 | def main(args: Array[String]) { 9 | val sc = new java.util.Scanner (System.in); 10 | var n = sc.nextInt(); 11 | var ar = new Array[Long](n); 12 | for(ar_i <- 0 to n-1) { 13 | ar(ar_i) = sc.nextLong(); 14 | } 15 | val result = aVeryBigSum(n, ar); 16 | println(result) 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /Solutions/Day 1: Data Types.scala: -------------------------------------------------------------------------------- 1 | object Solution { 2 | def main(args: Array[String]) { 3 | val i = 4 4 | val d = 4.0 5 | val s = "HackerRank " 6 | val input1 = scala.io.StdIn.readLine() 7 | val input2 = scala.io.StdIn.readLine() 8 | val input3 = scala.io.StdIn.readLine() 9 | 10 | var v1: Int = (i.toInt + input1.toInt) 11 | var v2: Double = (d.toDouble + input2.toDouble) 12 | var v3: String = (s + input3) 13 | 14 | println(v1) 15 | println(v2) 16 | println(v3) 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /Solutions/Game of Thrones - I.scala: -------------------------------------------------------------------------------- 1 | object Solution { 2 | 3 | def main(args: Array[String]) { 4 | val sc = new java.util.Scanner (System.in); 5 | var s = sc.next(); 6 | 7 | var occurence = collection.mutable.Map[String, Int]() 8 | val xs = s.toSet.toList 9 | val sAsList = s.toList 10 | 11 | for(c <- 0 to xs.size-1) occurence.put(xs(c).toString, sAsList.filter(_.toString == xs(c).toString).length.toInt) 12 | 13 | if(occurence.values.filter(x => (x % 2 == 1)).size > 1) println("NO") else println("YES") 14 | 15 | } 16 | 17 | } 18 | -------------------------------------------------------------------------------- /Solutions/Kangaroo.scala: -------------------------------------------------------------------------------- 1 | object Solution { 2 | 3 | def main(args: Array[String]) { 4 | val sc = new java.util.Scanner (System.in); 5 | var x1 = sc.nextInt(); 6 | var v1 = sc.nextInt(); 7 | var x2 = sc.nextInt(); 8 | var v2 = sc.nextInt(); 9 | 10 | if(v2 == v1) println("NO") 11 | else if (v1 > v2) { 12 | if ((Math.abs(v2 - v1) != 0) && (x2 - x1) % (Math.abs(v2 - v1)) == 0 ) println("YES") 13 | else println("NO") 14 | } 15 | else if (v1 < v2){ 16 | println("NO") 17 | } 18 | else println("YES") 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /Solutions/Super Reduced String.scala: -------------------------------------------------------------------------------- 1 | object Solution { 2 | 3 | def main(args: Array[String]) { 4 | val sc = new java.util.Scanner (System.in); 5 | var s = sc.next(); 6 | 7 | var str = s.toString 8 | 9 | var i = 1 10 | while(i < str.length) 11 | { 12 | if((str.charAt(i)).toString == (str.charAt(i-1)).toString) 13 | { 14 | str = str.substring(0, i-1) + str.substring(i+1); 15 | i = 0; 16 | } 17 | i = i + 1 18 | } 19 | 20 | if(str.length > 0) println(str) 21 | else println("Empty String") 22 | } 23 | 24 | } 25 | 26 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # HackerRank-Solved-In-Scala 2 | The Challenges of HackerRank Solved in Scala 3 | 4 | All the solutions provided here, are solely developed by me (Somdip Dey), and the challenges were pursued while learning Scala on the way. 5 | 6 | The name of the solution is the name of the challenge itself. 7 | For example, the solution with name "Arrays - DS.scala" is also of the same name, 'Arrays - DS', as the problem on HackerRank website. 8 | 9 | Follow me on HackerRank @somdipdey [https://www.hackerrank.com/somdipdey] 10 | 11 | ![Somdip Dey's Hackerrank profile](https://user-images.githubusercontent.com/8515608/31318280-7fc043c2-ac47-11e7-9998-ea29745276d7.png "Somdip Dey's HackerRank profile") 12 | -------------------------------------------------------------------------------- /Solutions/Pangrams.scala: -------------------------------------------------------------------------------- 1 | object Solution { 2 | 3 | def main(args: Array[String]) { 4 | val input = scala.io.StdIn.readLine() 5 | var noSpacedInput = input.replaceAll(" ", "") 6 | 7 | var alphabetSet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".toSet 8 | var mutAlphabetSet = collection.mutable.Set(alphabetSet.toArray:_*) 9 | 10 | for(i <- 0 to noSpacedInput.length-1) 11 | { 12 | val tempChar = Character.toUpperCase(noSpacedInput.charAt(i).toChar) 13 | mutAlphabetSet -= tempChar 14 | } 15 | 16 | if(mutAlphabetSet.isEmpty) println("pangram") 17 | else println("not pangram") 18 | } 19 | 20 | } 21 | -------------------------------------------------------------------------------- /Solutions/Big Sorting.scala: -------------------------------------------------------------------------------- 1 | object Solution { 2 | 3 | def main(args: Array[String]) { 4 | val sc = new java.util.Scanner (System.in); 5 | var n = sc.nextInt(); 6 | var unsorted = new Array[String](n); 7 | for(unsorted_i <- 0 to n-1) { 8 | unsorted(unsorted_i) = sc.next(); 9 | } 10 | 11 | var u_sorted = new Array[scala.math.BigInt](n) 12 | 13 | for(i <- 0 to n-1) 14 | { 15 | u_sorted(i) = BigInt(unsorted(i).toString) 16 | } 17 | var sorted = u_sorted.sorted 18 | 19 | for(i <- 0 to n-1) 20 | { 21 | println(sorted(i).toString) 22 | } 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /Solutions/Day 11: 2D Arrays.scala: -------------------------------------------------------------------------------- 1 | object Solution { 2 | 3 | def main(args: Array[String]) { 4 | val sc = new java.util.Scanner (System.in); 5 | var arr = Array.ofDim[Int](6,6); 6 | for(arr_i <- 0 to 6-1) { 7 | for(arr_j <- 0 to 6-1){ 8 | arr(arr_i)(arr_j) = sc.nextInt(); 9 | } 10 | } 11 | var A = scala.collection.mutable.ArrayBuffer.empty[Int] 12 | 13 | for (i <- 0 to 3) { 14 | for (j <- 0 to 3) { 15 | A += arr(i)(j) + arr(i)(j+1) + arr(i)(j+2) + arr(i+1)(j+1) + arr(i+2)(j) + arr(i+2)(j+1) + arr(i+2)(j+2) 16 | } 17 | } 18 | println(A.sorted.max) 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /Solutions/Day 3: Intro to Conditional Statements.scala: -------------------------------------------------------------------------------- 1 | object Solution { 2 | 3 | def main(args: Array[String]) { 4 | val sc = new java.util.Scanner (System.in); 5 | var N = sc.nextInt(); 6 | 7 | println(isItWeird(N)) 8 | } 9 | 10 | def isItWeird(n: Int): String = (n % 2 == 0, n >= 2, n <= 5, n >= 6, n <= 20, n > 20) match { 11 | case(true, true, true, false, true, false) => "Not Weird" 12 | case(true, false, false, true, true, false) => "Weird" 13 | case(true, true, false, true, false, true) => "Not Weird" 14 | case(false, false, false, false, false, false) => "Weird" 15 | case(_,_,_,_,_,_) => "Weird" 16 | } 17 | } 18 | 19 | -------------------------------------------------------------------------------- /Solutions/Grading Students.scala: -------------------------------------------------------------------------------- 1 | object Solution { 2 | 3 | def main(args: Array[String]) { 4 | val sc = new java.util.Scanner (System.in); 5 | var n = sc.nextInt(); 6 | var grades = new Array[Int](n); 7 | for(grades_i <- 0 to n-1) { 8 | grades(grades_i) = sc.nextInt(); 9 | } 10 | 11 | for(i <- Range(0,grades.length)) 12 | { 13 | println(evaluateGrade(grades(i))) 14 | } 15 | } 16 | 17 | def evaluateGrade(n: Int): Int = (n % 5 > 2, n < 38) match { 18 | case (true, true) => n 19 | case (false, true) => n 20 | case (true, false) => n + (5 - (n % 5)) 21 | case (false, false) => n 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /Solutions/Birthday Cake Candles.scala: -------------------------------------------------------------------------------- 1 | object Solution { 2 | 3 | def birthdayCakeCandles(n: Int, ar: Array[Int]): Int = { 4 | var temp:Array[Int] = ar.sorted 5 | var result = 0 6 | 7 | for(i <- Range((temp.length - 1), -1, -1)) 8 | { 9 | if(temp(temp.length - 1) == temp(i)) result = result + 1 10 | } 11 | 12 | return result 13 | } 14 | 15 | 16 | def main(args: Array[String]) { 17 | val sc = new java.util.Scanner (System.in); 18 | var n = sc.nextInt(); 19 | var ar = new Array[Int](n); 20 | for(ar_i <- 0 to n-1) { 21 | ar(ar_i) = sc.nextInt(); 22 | } 23 | val result = birthdayCakeCandles(n, ar); 24 | println(result) 25 | } 26 | } 27 | 28 | -------------------------------------------------------------------------------- /Solutions/Mars Exploration.scala: -------------------------------------------------------------------------------- 1 | object Solution { 2 | 3 | def main(args: Array[String]) { 4 | val sc = new java.util.Scanner (System.in); 5 | var S = sc.next(); 6 | var screwedSOS = 0 7 | for(i <- Range(0, S.length, 3)) 8 | { 9 | if(S.charAt(i).toString + S.charAt(i+1).toString + S.charAt(i+2).toString != "SOS") 10 | { 11 | if(S.charAt(i).toString != "S") screwedSOS = screwedSOS + 1 12 | if(S.charAt(i+1).toString != "O") screwedSOS = screwedSOS + 1 13 | if(S.charAt(i+2).toString != "S") screwedSOS = screwedSOS + 1 14 | } 15 | } 16 | println(screwedSOS) 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /Solutions/Day 6: Let's Review.scala: -------------------------------------------------------------------------------- 1 | object Solution { 2 | 3 | def main(args: Array[String]) { 4 | val sc = new java.util.Scanner (System.in); 5 | val n = sc.nextInt(); 6 | var arr = new Array[String](n); 7 | for(arr_i <- 0 to n-1) { 8 | arr(arr_i) = sc.next(); 9 | } 10 | 11 | for(i <- 0 to n-1) 12 | { 13 | var s = arr(i) 14 | var leftSide = "" 15 | var rightSide = "" 16 | for(j <- 0 to s.length -1) 17 | { 18 | if(j % 2 == 0) leftSide = leftSide + s.charAt(j).toString 19 | else if(j % 2 == 1) rightSide = rightSide + s.charAt(j).toString 20 | } 21 | println(leftSide + " " + rightSide) 22 | } 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /Solutions/Day 8: Dictionaries and Maps.scala: -------------------------------------------------------------------------------- 1 | object Solution { 2 | 3 | def main(args: Array[String]) { 4 | val num = scala.io.StdIn.readLine() 5 | var thisMap = collection.mutable.Map.empty[String, Long] 6 | for(i <- 0 to num.toInt-1) { 7 | val tempKeyValue = scala.io.StdIn.readLine(); 8 | val keyAndValue = tempKeyValue.split(" "); 9 | thisMap += (keyAndValue(0).toString -> keyAndValue(1).toLong) 10 | } 11 | 12 | val inputLines = Iterator.continually(scala.io.StdIn.readLine()).takeWhile(_ != null).mkString(" ").split(" ") 13 | 14 | for(i <- 0 to inputLines.size -1) 15 | { 16 | if(thisMap contains inputLines(i)) println(inputLines(i) + "=" + thisMap(inputLines(i))) 17 | else println("Not found") 18 | } 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /Solutions/Mini-Max Sum.scala: -------------------------------------------------------------------------------- 1 | object Solution { 2 | 3 | def main(args: Array[String]) { 4 | val sc = new java.util.Scanner (System.in); 5 | var arr = new Array[Long](5); 6 | for(arr_i <- 0 to 5-1) { 7 | arr(arr_i) = sc.nextInt(); 8 | } 9 | println(Min(arr) + " " + Max(arr)) 10 | 11 | } 12 | 13 | def Max(a: Array[Long]): Long = { 14 | var max: Long = 0 15 | for(i <- Range(0, a.length)) 16 | { 17 | val thismax = a.sum - a(i) 18 | 19 | if(max < thismax) max = thismax.toLong 20 | } 21 | 22 | return max 23 | } 24 | 25 | def Min(a: Array[Long]): Long = { 26 | var min: Long = 99999999999L; 27 | 28 | for(i <- Range(0, a.length)) 29 | { 30 | val thismax = a.sum - a(i) 31 | 32 | if(min > thismax) min = thismax.toLong 33 | } 34 | 35 | return min 36 | } 37 | 38 | } 39 | 40 | -------------------------------------------------------------------------------- /Solutions/HackerRank in a String!-Part2.scala: -------------------------------------------------------------------------------- 1 | object Solution { 2 | 3 | def main(args: Array[String]) { 4 | val sc = new java.util.Scanner (System.in); 5 | var q = sc.nextInt(); 6 | var a0 = 0; 7 | var arr = new Array[String](q); 8 | var str = "hackerrank" 9 | 10 | for(arr_i <- 0 to q-1) { 11 | arr(arr_i) = sc.next(); 12 | } 13 | 14 | for(i <- 0 to arr.length-1) 15 | { 16 | if(arr(i).length < str.length) println("NO") 17 | else 18 | { 19 | var j = 0 20 | for(k <- 0 to arr(i).length-1) 21 | { 22 | if(j < str.length && arr(i).charAt(k) == str.charAt(j)) j = j + 1 23 | } 24 | if(j == str.length) println("YES") else println("NO") 25 | } 26 | } 27 | 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /Solutions/Time Conversion.scala: -------------------------------------------------------------------------------- 1 | object Solution { 2 | 3 | def timeConversion(s: String): String = { 4 | if((s slice (8,10)) == "PM") 5 | { 6 | if((s slice (0, 2)) != "12"){ 7 | var hhString = (s slice (0, 2) ) 8 | var hh = hhString.toInt 9 | hh = hh + 12 10 | var result = hh.toString + (s slice (2, 8) ) 11 | return result 12 | } 13 | else return s slice (0, 8) 14 | } 15 | else { 16 | if((s slice (0, 2)) != "12"){ 17 | return s slice (0, 8) 18 | } 19 | else{ 20 | return "00" + (s slice (2, 8) ) 21 | } 22 | } 23 | } 24 | 25 | def main(args: Array[String]) { 26 | val sc = new java.util.Scanner (System.in); 27 | var s = sc.next(); 28 | val result = timeConversion(s); 29 | println(result) 30 | } 31 | } 32 | 33 | -------------------------------------------------------------------------------- /Solutions/Day 2: Operators.scala: -------------------------------------------------------------------------------- 1 | object Solution { 2 | def main(args: Array[String]) = { 3 | 4 | //val sc = new java.util.Scanner (System.in); 5 | 6 | var inputPrice = scala.io.StdIn.readLine() 7 | var inputTipPerct = scala.io.StdIn.readLine() 8 | var inputTaxPerct = scala.io.StdIn.readLine() 9 | 10 | var mealCost: Double = inputPrice.toDouble 11 | var tipPercent: Double = inputTipPerct.toDouble 12 | var taxPercent: Double = inputTaxPerct.toDouble 13 | 14 | var amountTip = percentCalculator(mealCost, tipPercent) 15 | var amountTax = percentCalculator(mealCost, taxPercent) 16 | 17 | var totalCost = mealCost.toDouble + amountTip.toDouble + amountTax.toDouble 18 | 19 | printf("The total meal cost is %d dollars.", Math.round(totalCost)) 20 | } 21 | 22 | def percentCalculator(cost: Double, percent: Double): Double = { 23 | return (cost * (percent / 100)) 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /Solutions/Diagonal Difference.scala: -------------------------------------------------------------------------------- 1 | object Solution { 2 | 3 | def main(args: Array[String]) { 4 | val sc = new java.util.Scanner (System.in); 5 | var n = sc.nextInt(); 6 | var a = Array.ofDim[Int](n,n); 7 | for(a_i <- 0 to n-1) { 8 | for(a_j <- 0 to n-1){ 9 | a(a_i)(a_j) = sc.nextInt(); 10 | } 11 | } 12 | 13 | val N:Int = n.toInt 14 | var diagonalRightSide = sumDiagonal(a, N, true) 15 | var diagonalLeftSide = sumDiagonal(a, N, false) 16 | 17 | var temp = (diagonalRightSide.toInt - diagonalLeftSide.toInt) 18 | 19 | var abs = Math.abs(temp) 20 | 21 | println(abs) 22 | } 23 | 24 | def sumDiagonal(a:Array[Array[Int]], n:Int, rightSide:Boolean): Int = { 25 | var sum:Int = 0 26 | if(rightSide){ 27 | for(i <- Range(0,n)){ 28 | sum = sum + a(i)(i) 29 | } 30 | } 31 | else{ 32 | for(i <- Range(0,n)){ 33 | sum = sum + a(i)((n-1)-i) 34 | } 35 | } 36 | 37 | return sum 38 | } 39 | 40 | } 41 | 42 | -------------------------------------------------------------------------------- /Solutions/Caesar Cipher.scala: -------------------------------------------------------------------------------- 1 | object Solution { 2 | 3 | def main(args: Array[String]) { 4 | val sc = new java.util.Scanner (System.in); 5 | var n = sc.nextInt(); 6 | var s = sc.next(); 7 | var k = sc.nextInt(); 8 | var e: String = "" 9 | 10 | for(i <- Range(0,s.length)) 11 | { 12 | if(s.charAt(i).toInt >= 65 && s.charAt(i).toInt <= 90) 13 | { 14 | if(s.charAt(i).toInt + (k % 26) <= 90) e = e + (s.charAt(i).toInt + (k % 26)).toChar.toString 15 | else e = e + (s.charAt(i).toInt + (k % 26) - 90 + 64).toChar.toString 16 | } 17 | else if(s.charAt(i).toInt >= 97 && s.charAt(i).toInt <= 122) 18 | { 19 | if(s.charAt(i).toInt + (k % 26) <= 122) e = e + (s.charAt(i).toInt + (k % 26)).toChar.toString 20 | else e = e + (s.charAt(i).toInt + (k % 26) - 122 + 96).toChar.toString 21 | } 22 | else e = e + s.charAt(i).toString 23 | } 24 | 25 | println(e) 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /Solutions/Day 4: Class vs. Instance.scala: -------------------------------------------------------------------------------- 1 | object Solution { 2 | 3 | def main(args: Array[String]) { 4 | var T=scala.io.StdIn.readInt() 5 | var i=0 6 | for(i<-1 to T){ 7 | var age=scala.io.StdIn.readInt() 8 | var p=new Person(age) 9 | p.amIOld() 10 | var j=0 11 | for(j<-1 to 3){ 12 | p.yearPasses() 13 | } 14 | p.amIOld() 15 | System.out.println() 16 | 17 | } 18 | 19 | 20 | } 21 | } 22 | 23 | //class Person starts here --> 24 | 25 | class Person { 26 | 27 | var age: Int = 0 28 | 29 | def this(initialAge:Int) = { 30 | this() 31 | 32 | if(initialAge < 0) { 33 | age = 0 34 | println("Age is not valid, setting age to 0.") 35 | } 36 | else { 37 | age = initialAge 38 | 39 | } 40 | 41 | 42 | } 43 | 44 | def amIOld(): Unit = { 45 | if(age < 13) println("You are young.") 46 | else if(age >= 13 && age< 18) println("You are a teenager.") 47 | else println("You are old.") 48 | } 49 | 50 | def yearPasses(): Unit = { 51 | age = age + 1 52 | } 53 | 54 | } 55 | -------------------------------------------------------------------------------- /Solutions/Compare the Triplets.scala: -------------------------------------------------------------------------------- 1 | object Solution { 2 | 3 | def main(args: Array[String]) { 4 | val sc = new java.util.Scanner (System.in); 5 | var a0 = sc.nextInt(); 6 | var a1 = sc.nextInt(); 7 | var a2 = sc.nextInt(); 8 | var b0 = sc.nextInt(); 9 | var b1 = sc.nextInt(); 10 | var b2 = sc.nextInt(); 11 | 12 | var AliceScore: Int = 0 13 | var BobScore: Int = 0 14 | 15 | if(isValid(a0)) 16 | { 17 | if(a0 > b0) AliceScore = AliceScore + 1 18 | else if(a0 < b0) BobScore = BobScore + 1 19 | } 20 | 21 | if(isValid(a1)) 22 | { 23 | if(a1 > b1) AliceScore = AliceScore + 1 24 | else if(a1 < b1) BobScore = BobScore + 1 25 | } 26 | 27 | if(isValid(a2)) 28 | { 29 | if(a2 > b2) AliceScore = AliceScore + 1 30 | else if(a2 < b2) BobScore = BobScore + 1 31 | } 32 | 33 | println(AliceScore.toString + " " + BobScore.toString) 34 | 35 | } 36 | 37 | def isValid(thisNum: Int): Boolean = { 38 | val m = thisNum match { 39 | case x if 1 until 100 contains x => true 40 | case _ => false 41 | } 42 | return m 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /Solutions/Plus Minus.scala: -------------------------------------------------------------------------------- 1 | object Solution { 2 | 3 | def main(args: Array[String]) { 4 | val sc = new java.util.Scanner (System.in); 5 | var n = sc.nextInt(); 6 | var arr = new Array[Int](n); 7 | for(arr_i <- 0 to n-1) { 8 | arr(arr_i) = sc.nextInt(); 9 | } 10 | var pos = countPositive(arr) 11 | var neg = countNegetive(arr) 12 | var zeros = countZeros(arr) 13 | 14 | var d1:Double = (pos.toDouble / n.toDouble) 15 | var d2:Double = (neg.toDouble / n.toDouble) 16 | var d3:Double = (zeros.toDouble / n.toDouble) 17 | 18 | printf("%1.6f \n", d1) 19 | printf("%1.6f \n", d2) 20 | printf("%1.6f \n", d3) 21 | } 22 | 23 | 24 | def countPositive(a: Array[Int]): Int = { 25 | var num = 0 26 | 27 | for(i <- Range(0,a.length)) 28 | { 29 | if(a(i) > 0) num = num + 1 30 | } 31 | 32 | return num 33 | } 34 | 35 | def countNegetive(a: Array[Int]): Int = { 36 | var num = 0 37 | 38 | for(i <- Range(0,a.length)) 39 | { 40 | if(a(i) < 0) num = num + 1 41 | } 42 | 43 | return num 44 | } 45 | 46 | def countZeros(a: Array[Int]): Int = { 47 | var num = 0 48 | 49 | for(i <- Range(0,a.length)) 50 | { 51 | if(a(i) == 0) num = num + 1 52 | } 53 | 54 | return num 55 | } 56 | 57 | } 58 | -------------------------------------------------------------------------------- /Solutions/Apple and Orange.scala: -------------------------------------------------------------------------------- 1 | object Solution { 2 | 3 | def main(args: Array[String]) { 4 | val sc = new java.util.Scanner (System.in); 5 | var s = sc.nextInt(); 6 | var t = sc.nextInt(); 7 | var a = sc.nextInt(); 8 | var b = sc.nextInt(); 9 | var m = sc.nextInt(); 10 | var n = sc.nextInt(); 11 | var apple = new Array[Int](m); 12 | for(apple_i <- 0 to m-1) { 13 | apple(apple_i) = sc.nextInt(); 14 | } 15 | var orange = new Array[Int](n); 16 | for(orange_i <- 0 to n-1) { 17 | orange(orange_i) = sc.nextInt(); 18 | } 19 | 20 | var applesThatReachHouse = 0 21 | var orangesThatReachHouse = 0 22 | 23 | for(apple_i <- Range(0, apple.length)) { 24 | if(s <= (apple(apple_i) + a) && (apple(apple_i) + a) <= t ) { 25 | applesThatReachHouse = applesThatReachHouse + 1 26 | } 27 | } 28 | 29 | for(orange_i <- Range(0, orange.length)) { 30 | if(s <= (orange(orange_i) + b) && (orange(orange_i) + b) <= t ) { 31 | orangesThatReachHouse = orangesThatReachHouse + 1 32 | } 33 | } 34 | 35 | println(applesThatReachHouse) 36 | println(orangesThatReachHouse) 37 | } 38 | } 39 | 40 | -------------------------------------------------------------------------------- /Solutions/Weighted Uniform Strings.scala: -------------------------------------------------------------------------------- 1 | object Solution { 2 | 3 | def main(args: Array[String]) { 4 | val sc = new java.util.Scanner (System.in); 5 | var s = sc.next(); 6 | var n = sc.nextInt(); 7 | var prev = -1 8 | var numArray = collection.mutable.MutableList[Int]() 9 | var digitArray = collection.mutable.MutableList[Int]() 10 | var a0 = 0; 11 | while(a0 < n){ 12 | var x = sc.nextInt(); 13 | numArray += x 14 | a0+=1; 15 | } 16 | 17 | var i = 0 18 | while(i < s.length) 19 | { 20 | if(i+1 < s.length) 21 | { 22 | if(s.charAt(i) == s.charAt(i+1)) 23 | { 24 | digitArray += s.charAt(i).toInt - 96 25 | var j = 1 26 | while(s.charAt(i) == s.charAt(i+1)) 27 | { 28 | digitArray += (s.charAt(i).toInt - 96)*(j+1) 29 | j = j + 1 30 | i = i + 1 31 | } 32 | } 33 | else digitArray += s.charAt(i).toInt - 96 34 | } 35 | else digitArray += s.charAt(i).toInt - 96 36 | i = i + 1 37 | } 38 | 39 | numArray.foreach((x) => if(digitArray contains x) println("Yes") else println("No") ) 40 | } 41 | } 42 | 43 | -------------------------------------------------------------------------------- /Solutions/HackerRank in a String!.scala: -------------------------------------------------------------------------------- 1 | object Solution { 2 | 3 | def main(args: Array[String]) { 4 | val sc = new java.util.Scanner (System.in); 5 | var q = sc.nextInt(); 6 | var a0 = 0; 7 | var arr = new Array[String](q); 8 | var str = "hackerrank" 9 | 10 | for(arr_i <- 0 to q-1) { 11 | arr(arr_i) = sc.next(); 12 | } 13 | 14 | for(i <- Range(0, arr.length)){ 15 | var s = arr(i).toString 16 | if(s.count(_ == 'h') >= 1 & s.count(_ == 'a') >=2 && s.count(_ == 'c') >= 1 && s.count(_ == 'k') >= 2 && s.count(_ == 'e') >= 1 && s.count(_ == 'n') >= 1 && s.count(_ == 'r') >= 2 ) { 17 | var patrnSeq = 0 18 | for(j <- Range(0,str.length, 2)){ 19 | if(str.charAt(j).toString != "a" || str.charAt(j).toString != "r" || 20 | str.charAt(j+1).toString != "a" || str.charAt(j+1).toString != "r") 21 | { 22 | if(s.indexOf(str.charAt(j).toString) < s.indexOf(str.charAt(j+1).toString)) patrnSeq = patrnSeq + 1 23 | } 24 | else 25 | { 26 | if(s.lastIndexOf(str.charAt(j).toString) > s.lastIndexOf(str.charAt(j+1).toString)) patrnSeq = patrnSeq 27 | } 28 | } 29 | if(patrnSeq == 4) println("YES") 30 | else println("NO") 31 | } 32 | else println("NO") 33 | } 34 | 35 | } 36 | } 37 | --------------------------------------------------------------------------------