Loading...
More Scala Posts
def isPalindrome(x: Int): Boolean = {val original = x.toStringval reversed = original.reverseoriginal == reversed}val result1 = isPalindrome(121)println(result1) // Should print: trueval result2 = isPalindrome(-121)println(result2) // Should print: falseval result3 = isPalindrome(10)println(result3) // Should print: falseval result4 = isPalindrome(1221)println(result4) // Should print: true
def yourFunction(nums: Array[Int]): Boolean = {for(i <- 0 until nums.length) {for(j <- i + 1 until nums.length) {if (nums(i) == nums(j)) {return true // Return true only when a duplicate is found}}}false}// Your function hereval result1 = yourFunction(Array(1, 2, 3, 1))println(result1) // Should print: trueval result2 = yourFunction(Array(1, 2, 3, 4))println(result2) // Should print: falseval result3 = yourFunction(Array(1, 1, 1, 3, 3, 4, 3, 2, 4, 2))println(result3) // Should print: true
def reverseString(s: Array[Char]): Array[Char] = {for (i <- 0 until s.length / 2) {// Hint: Use your swap logic hereval temp = s(i)s(i) = s(s.length - 1 - i)s(s.length - 1 - i) = temp}s // Return the modified array}val result = reverseString(Array('H', 'a', 'n', 'n', 'a', 'h'))println(result.mkString(", ")) // Should print: h, a, n, n, a, H