스칼라 23

curry의 변환을 역으로 수행하는 고차 함수를 구현하라.

def uncurry[A, B, C](f: A => B => C): (A, B) => C = { (a, b) => f(a).apply(b) }회고.좀 헤맸다. a와 b를 인자로 받으면,함수 f를 호출하여 결과로 B를 인자로 받고 C를 리턴하는 함수를 리턴 받고,리턴 받은 함수에 b를 인자로 넘겨서 C를 리턴 받도록 처리함. 풀고보니 간단한데, 함수형 프로그래밍에 익숙치 않아서 헤맸던듯..

Array[A]가 주어진 비교 함수에 의거해서 정렬되어 있는지 점검하는 함수를 구현하라.

package scala /** * Created by kwo2002 on 2015-08-04. */ object Chapter2 { def isSorted[A](as: Array[A], ordered: (A, A) => Boolean): Boolean = { def go(n: Int): Boolean = { if (n >= as.length - 1) { true } else { if (ordered(as(n), as(n + 1))) { go(n + 1) } else { false } } } go(0) } def main(args: Array[String]) { print(isSorted[Int](Array(1, 3, 2, 9), (a, b) => if (a < b) true else false )) }..

n번째 피보나치 수를 돌려주는 재귀 함수를 작성하라

/** * Created by kwo2002 on 2015-08-04. */ object Fibonacci { def fibonacci(n: Int): Int = { def go(a: Int, b: Int): Int = { if(b >= n) { b }else { print(b + " ") go(b, a + b) } } print(0 + " ") go(0, 1) } def main(args: Array[String]) { println(fibonacci(25)) } } 회고1. n번째 피보나치 수를 돌려주는 재귀 함수 자체는 아주 쉬웠으나,피보나치 수열을 print 해주는건 의외로 약간의 고민이 필요했음. 2. 재귀 호출로 go의 b인자가 결국 피보나치 수열인건데, 첫번째 피보나치 수열 (위에서는 0) ..

같은 문자가 반복될 경우, 그 횟수를 사용해 문자열을 압축하는 메소드를 구현하라.(3)

def zipString2(str: String): String = { def go(i: Int, li: Int, cnt: Int, zipStr: StringBuilder): String = { if (i >= str.length) { zipStr.append(str.charAt(li)) zipStr.append(cnt) zipStr.toString() } else { if (str.charAt(li) == str.charAt(i)) { go(i + 1, li, cnt + 1, zipStr) } else { zipStr.append(str.charAt(li)) zipStr.append(cnt) go(i + 1, i, 1, zipStr) } } } val zipStr: String = go(1, 0, 1,..

같은 문자가 반복될 경우, 그 횟수를 사용해 문자열을 압축하는 메소드를 구현하라.(2)

import scala.StringBuilder /** * Created by kwo2002 on 2015-08-03. */ object ZipString { def zipString(str: String): String = { def go(n: Int, cnt: Int, zipString: StringBuilder): String = { if (cnt == 1) { zipString.append(str.charAt(n)) } if (n >= str.length - 1) { zipString.append(cnt) zipString.toString() } else { if (str.charAt(n) == str.charAt(n + 1)) { go(n + 1, cnt + 1, zipString) } else..

같은 문자가 반복될 경우, 그 횟수를 사용해 문자열을 압축하는 메소드를 구현하라.

/** * Created by kwo2002 on 2015-08-03. */ object ZipString { def zipString(str: String): String = { def go(n: Int, cnt: Int, zipString: StringBuilder): String = { if (cnt == 1) { zipString.append(str.charAt(n)) } if (n >= str.length - 1) { if (cnt > 1) { zipString.append(cnt) } zipString.toString() } else { if (str.charAt(n) == str.charAt(n + 1)) { go(n + 1, cnt + 1, zipString) } else { if (cnt..

주어진 문자배열내의 공백을 모두 %20으로 바꾸는 메소드를 작성하라.

/** * Created by kwo2002 on 2015-07-30. */ object RelpaceSpace { def replaceSpace(cArr: Array[Char], n: Int): Array[Char] = { //새로운 배열 사이즈를 얻기 위한 loop def getNewArraySize(i: Int, newSize: Int): Int = { if (i >= n) newSize else { if (cArr(i) == ' ') { getNewArraySize(i + 1, newSize + 2) } else { getNewArraySize(i + 1, newSize) } } } def repChar(i: Int, i2: Int, newArr: Array[Char]): Array[Char] =..

문자열 두개를 받아 그 중 하나가 다른 하나의 순열인지 판단하라(2)

/** * Created by kwo2002 on 2015-07-29. * 문자열 두개를 받아 그 중 하나가 다른 하나의 순열인지 판단하라 */ object Permutation { def permutation1(a: String, b: String): Boolean = { if (a.trim.length != b.trim.length) { false } else { a.sorted.equalsIgnoreCase(b.sorted) } } def permutation2(a: String, b: String, p: Array[Char] => Array[Int]): Boolean = { if (a.trim.length != b.trim.length) { false } else { val aArr: Array[..