티스토리 뷰
반응형
Day 27 챌린지는 강의가 없기 때문에
바로 문제풀이를 진행한다.
이번 문제는 문제에서 지정하는 클래스와 메서드를
직접 코드로 구현하는 문제였다.
문제 내용은 이해가 되었지만
클래스와 메서드를 코드로 구현하는 게 낯설어
인터넷의 도움을 받고 문제를 해결했다.
추후에 복습이 필요할 것 같다.
우선 전체 소스코드와 출처를 확인해보자. 👇👇
import java.util.*;
public class Solution {
public static int minimum_index(int[] seq) {
if (seq.length == 0) {
throw new IllegalArgumentException("Cannot get the minimum value index from an empty sequence");
}
int min_idx = 0;
for (int i = 1; i < seq.length; ++i) {
if (seq[i] < seq[min_idx]) {
min_idx = i;
}
}
return min_idx;
}
static class TestDataEmptyArray {
public static int[] get_array() {
int arr[] = {};
return arr;
}
}
static class TestDataUniqueValues {
public static int[] get_array() {
int arr[] = {1,2,3};
return arr;
}
public static int get_expected_result() {
return 0;
}
}
static class TestDataExactlyTwoDifferentMinimums {
public static int[] get_array() {
int arr[] = {1,2,1};
return arr;
}
public static int get_expected_result() {
return 0;
}
}
public static void TestWithEmptyArray() {
try {
int[] seq = TestDataEmptyArray.get_array();
int result = minimum_index(seq);
} catch (IllegalArgumentException e) {
return;
}
throw new AssertionError("Exception wasn't thrown as expected");
}
public static void TestWithUniqueValues() {
int[] seq = TestDataUniqueValues.get_array();
if (seq.length < 2) {
throw new AssertionError("less than 2 elements in the array");
}
Integer[] tmp = new Integer[seq.length];
for (int i = 0; i < seq.length; ++i) {
tmp[i] = Integer.valueOf(seq[i]);
}
if (!((new LinkedHashSet<Integer>(Arrays.asList(tmp))).size() == seq.length)) {
throw new AssertionError("not all values are unique");
}
int expected_result = TestDataUniqueValues.get_expected_result();
int result = minimum_index(seq);
if (result != expected_result) {
throw new AssertionError("result is different than the expected result");
}
}
public static void TestWithExactlyTwoDifferentMinimums() {
int[] seq = TestDataExactlyTwoDifferentMinimums.get_array();
if (seq.length < 2) {
throw new AssertionError("less than 2 elements in the array");
}
int[] tmp = seq.clone();
Arrays.sort(tmp);
if (!(tmp[0] == tmp[1] && (tmp.length == 2 || tmp[1] < tmp[2]))) {
throw new AssertionError("there are not exactly two minimums in the array");
}
int expected_result = TestDataExactlyTwoDifferentMinimums.get_expected_result();
int result = minimum_index(seq);
if (result != expected_result) {
throw new AssertionError("result is different than the expected result");
}
}
public static void main(String[] args) {
TestWithEmptyArray();
TestWithUniqueValues();
TestWithExactlyTwoDifferentMinimums();
System.out.println("OK");
}
}
함수를 구현하는 문제이기 때문에
함수의 내용 또한 문제에서 제공되기 때문에
별다른 설명은 않도록 하겠다.
그럼 복습 꼼꼼히 하고 우리는 다음 포스팅에서 다시 만나자. 😊
반응형
'해커랭크 챌린지' 카테고리의 다른 글
[hackerrank] hackerrank challenges Day 28 자바 코드 리뷰 (0) | 2022.07.21 |
---|---|
[hackerrank] hackerrank challenges Day 28 자바 강의 리뷰 (0) | 2022.07.21 |
[hackerrank] hackerrank challenges Day 25 자바 코드 리뷰 (0) | 2022.07.19 |
[hackerrank] hackerrank challenges Day 25 자바 강의 리뷰 (0) | 2022.07.19 |
[hackerrank] hackerrank challenges Day 26 자바 코드 리뷰 (0) | 2022.07.18 |
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- 풀이
- 자바
- JPA
- 그리디
- BAEKJOON
- ORM
- 백준
- 자바의 정석
- 정보처리산업기사
- 22 정보처리 산업기사
- 22 정보처리산업기사
- 해커랭크 챌린지
- 챌린지
- 해커랭크 자바
- 디버깅
- 정보처리 산업기사
- 정보처리산업기사 공부법
- 코드
- LinkedList
- stack
- hackerrank challenges
- 소스코드
- hackerrank
- Java
- queue
- 강의
- 개발자
- challenges
- 해커랭크 자바 챌린지
- 해커랭크
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
글 보관함