'코드트레이닝'에 해당되는 글 1건

  1. 2008/05/01 foleafs [dW] 초보 개발자 코드 트레이닝, Part 2: 알고리즘과 성능

현재 IBM dW 내에 운영되고 있는 초보 개발자 코드 트레이닝!!
대학생들의 코딩 능력 향상에 많은 도움이 될 것 같습니다!
다들 참여해 보시고! 좋은 선물도 받으세요! ^^;
http://www.ibm.com/developerworks/kr/li ··· 80429%2F

퀴즈 1. 프로시저 Both 를 완성해 봅시다.

우선 한 가지 문제를 해결해 볼까요? Listing 1 은 두 ArrayList 데이터 내에서 공통 원소만을 뽑아 모으는 프로시저, either 를 자바 언어를 이용해 만든 것입니다. 만일 두 ArrayList 내의 모든 원소를 합쳐 모으는 프로시저, both 가 필요하다면 어떻게 하면 좋을까요?

Listing1. FirstQuizSet

import java.util.*;
  public class FirstQuizSet {
    public static ArrayList both(ArrayList xs, ArrayList ys) {
      // TODO: 이 곳을 채우는 문제입니다.
    }

  public static ArrayList either(ArrayList xs, ArrayList ys) {
    ArrayList eitherList = new ArrayList();
    Object y = null; Iterator elems = ys.iterator();
    while (elems.hasNext()) {
      y = elems.next();
      if (xs.contains(y)) eitherList.add(y);
    }
    return eitherList;
  }

  public static ArrayList toArrayList(int[] xs) {
    ArrayList ys = new ArrayList();
    for (int i = 0; i < xs.length; i++) ys.add(new Integer(xs[i]));
    return ys;
  }

  public static void main(String[] args) {
    int[] xVec = { 1, 3, 4, 5, 6, 9, 7 };
    int[] yVec = { 4, 5, 8, 9, 10, 15, -1 };
    ArrayList xs = toArrayList(xVec); ArrayList ys = toArrayList(yVec);
    System.out.println( both(xs, ys) ); System.out.println( either(xs, ys) );
  }
}

Listing 1 의 실행 결과 예시
[1, 3, 4, 5, 6, 9, 7, 8, 10, 15, -1] ß both 의 실행 결과
[4, 5, 9] ß either 의 실행 결과

첫 번째 퀴즈인 both 를 해결하셨나요? 실행 결과는 제대로 나오구요? 축하드립니다.


총.. 3개의 퀴즈가 더 있으니.. dW 홈페이지로 이동 후 참여 하세요 !
크리에이티브 커먼즈 라이센스
Creative Commons License
2008/05/01 11:49 2008/05/01 11:49
받은 트랙백이 없고, 댓글이 없습니다.

댓글+트랙백 RSS :: http://dbilove.com/tt/foleafs/rss/response/165