본문 바로가기

CodingBat

(5)
CodingBat Java: Array-3 (linearIn) Given two arrays of ints sorted in increasing order, outer and inner, return true if all of the numbers in inner appear in outer. The best solution makes only a single "linear" pass of both arrays, taking advantage of the fact that both arrays are already in sorted order. linearIn([1, 2, 4, 6], [2, 4]) → true linearIn([1, 2, 4, 6], [2, 3, 4]) → false linearIn([1, 2, 4, 4, 6], [2, 4]) → true Solu..
CodingBat Java: String-3 (mirrorEnds) Given a string, look for a mirror image (backwards) string at both the beginning and end of the given string. In other words, zero or more characters at the very begining of the given string, and at the very end of the string in reverse order (possibly overlapping). For example, the string "abXYZba" has the mirror end "ab". mirrorEnds("abXYZba") → "ab" mirrorEnds("abca") → "a" mirrorEnds("aba") ..
CodingBat Java: Logic-2 (evenlySpaced) problem - Given three ints, a b c, one of them is small, one is medium and one is large. Return true if the three values are evenly spaced, so the difference between small and medium is the same as the difference between medium and large. evenlySpaced(2, 4, 6) → true evenlySpaced(4, 6, 2) → true evenlySpaced(4, 6, 3) → false Solution 이번 문제는 하나는 가장 작고, 하나는 중간값, 하나는 가장 큰 값으로 구성된 3개의 숫자가 주어진다. 그리고 ..
CodingBat Java: Warmup-2 (noTriples) problem - Given an array of ints, we'll say that a triple is a value appearing 3 times in a row in the array. Return true if the array does not contain any triples. noTriples([1, 1, 2, 2, 1]) → true noTriples([1, 1, 2, 2, 2, 1]) → false noTriples([1, 1, 1, 2, 2, 2, 1]) → false Solution 이번 문제는 연속된 세 개의 숫자를 가지는 배열일 경우 false를 반환하는 문제이다. 특별한 풀이과정은 없었으며, 배열의 인덱스를 전진하면서 연속된 세 개의 숫자가 있을 때까지 카운트하여 있으면 f..
CodingBat Java: Warmup-1 (close 10) problem - Given 2 int values, return whichever value is nearest to the value 10, or return 0 in the event of a tie. Note that Math.abs(n) returns the absolute value of a number. close10(8, 13) → 8 close10(13, 8) → 8 close10(13, 7) → 0 Solution 이번 문제는 두 정수를 입력받았을 때 어떤 정수가 10에 더 가까운지를 측정하여 return값을 반환하는 문제이다(같을 경우는 0을 반환한다). 이는 수학의 절대값 개념을 통해 10에서 두 정수를 각각 빼서 구할 수 있는데, java에서는 Math.abs() 함수를 사용하여 ..