diff --git a/knoda/src/B074_CommentSequence.java b/knoda/src/B074_CommentSequence.java new file mode 100644 index 0000000000000000000000000000000000000000..aaa6f8b4b8134f3ed32d6df4676996d78a4b94af --- /dev/null +++ b/knoda/src/B074_CommentSequence.java @@ -0,0 +1,92 @@ +package hellojunit; + +import java.util.ArrayList; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; +import java.util.Scanner; +import java.util.Set; + +public class B074_CommentSequence { + + public static void main(String[] args) { + final Scanner scan = new Scanner(System.in); + final int commentNum = scan.nextInt(); + final List> commentList = new ArrayList>(); + final int commentInfoNum = 3; // コメントの情報数(コメントID、返信先ID、いいねの数、の3つ) + + // コメント(コメントID、返信先ID、いいねの数)の情報を入力する + for (int i = 0; i < commentNum; i++) { + final List comment = new ArrayList(); + for (int j = 0; j < commentInfoNum; j++) { + comment.add(scan.next()); + } + commentList.add(comment); + } + CommentSequence commentSequence = new CommentSequence(); + // 最も注目度の高いコメントIDを取得する + System.out.println(commentSequence.getPopularCommentID(commentList)); + scan.close(); + } + +} + +//最も注目度の高いコメントIDを取得するクラス +class CommentSequence { + private final int COMMENT_ID_INDEX = 0; // コメントIDの要素番号 + private final int REPLY_COMMENT_ID_INDEX = 1; // 返信先IDの要素番号 + private final int GOOD_INDEX = 2; // いいねの数の要素番号 + private final int MAX_COMMENT_ID = 100; // コメントIDの最大値 + + // 最も注目の高いコメントIDを取得するメソッド + public int getPopularCommentID(final List> commentList) { + // notExistCommentGoodMap : 返信先が存在しないコメントのIDといいねの数を格納したマップ + Map notExistCommentGoodMap = new LinkedHashMap(); + + for (final List comment : commentList) { + final int visibility = getVisibility(comment, commentList); + if ("None".equals(comment.get(REPLY_COMMENT_ID_INDEX))) { + // 返信先IDが存在しないコメントを格納する + notExistCommentGoodMap.put(Integer.parseInt(comment.get(COMMENT_ID_INDEX)), visibility); + } + } + + Set notExistCommentIDSet = notExistCommentGoodMap.keySet(); + int popularVisibility = 0; // 最も高いコメントの注目度 + int popularCommentID = MAX_COMMENT_ID; // 最も注目度の高いコメントID + // 最も注目度の高いコメントIDを取得する + for (final Integer commentID : notExistCommentIDSet) { + if (popularVisibility < notExistCommentGoodMap.get(commentID)) { + popularVisibility = notExistCommentGoodMap.get(commentID); + popularCommentID = commentID; + } else if (popularVisibility == notExistCommentGoodMap.get(commentID)) { + // 最も注目度の高いコメントが複数存在するとき、それらの中で最も小さいコメントIDを取得する + if (popularCommentID > commentID) { + popularCommentID = commentID; + } + } + } + return popularCommentID; + } + + // コメントのいいねの数を取得するメソッド + private int getVisibility(final List comment, final List> commentList) { + int visibility = Integer.parseInt(comment.get(GOOD_INDEX)); + // System.out.println(visibility); + // replyCommentList : リプライコメントを格納したリスト + final List> replyCommentList = new ArrayList>(); + + // リプライコメントを取得する + for (final List replyComment : commentList) { + if (comment.get(COMMENT_ID_INDEX).equals(replyComment.get(REPLY_COMMENT_ID_INDEX))) { + replyCommentList.add(replyComment); + } + } + + // リプライコメントのリプライコメントを取得し、注目度を算出する(再帰呼び出し) + for (final List replyComment : replyCommentList) { + visibility += getVisibility(replyComment, commentList); + } + return visibility; + } +} diff --git a/knoda/test/B074_CommentSequenceTest.java b/knoda/test/B074_CommentSequenceTest.java new file mode 100644 index 0000000000000000000000000000000000000000..204ae6c6aba22a712141748f496a0f3c321ca6cb --- /dev/null +++ b/knoda/test/B074_CommentSequenceTest.java @@ -0,0 +1,65 @@ +package hellojunit; + +import static org.hamcrest.CoreMatchers.*; +import static org.junit.Assert.*; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +import org.junit.Test; + +public class B074_CommentSequenceTest { + + @Test + public void コメントが3つ以上連結しているとき() { + final List> commentList = new ArrayList>(Arrays.asList( + new ArrayList(Arrays.asList("3", "None", "5")), + new ArrayList(Arrays.asList("7", "3", "3")), + new ArrayList(Arrays.asList("8", "7", "4")), + new ArrayList(Arrays.asList("10", "8", "2")), + new ArrayList(Arrays.asList("5", "10", "6")) + )); + CommentSequence commentSequence = new CommentSequence(); + int expected = 3; + int actual = commentSequence.getPopularCommentID(commentList); + assertThat(actual, is(expected)); + } + + @Test + public void 注目度の高いコメントが複数存在するとき() { + final List> commentList = new ArrayList>(Arrays.asList( + new ArrayList(Arrays.asList("10", "None", "10")), + new ArrayList(Arrays.asList("5", "None", "5")), + new ArrayList(Arrays.asList("8", "5", "5")), + new ArrayList(Arrays.asList("4", "None", "6")), + new ArrayList(Arrays.asList("7", "4", "4")) + )); + CommentSequence commentSequence = new CommentSequence(); + int expected = 4; + int actual = commentSequence.getPopularCommentID(commentList); + assertThat(actual, is(expected)); + } + + @Test + public void コメントが3つ以上連結しているときとそうでないコメントが混在するとき() { + final List> commentList = new ArrayList>(Arrays.asList( + new ArrayList(Arrays.asList("22", "37", "77")), + new ArrayList(Arrays.asList("37", "29", "81")), + new ArrayList(Arrays.asList("11", "None", "32")), + new ArrayList(Arrays.asList("64", "93", "18")), + new ArrayList(Arrays.asList("46", "37", "12")), + new ArrayList(Arrays.asList("51", "None", "9")), + new ArrayList(Arrays.asList("84", "37", "15")), + new ArrayList(Arrays.asList("29", "11", "64")), + new ArrayList(Arrays.asList("73", "64", "71")), + new ArrayList(Arrays.asList("93", "51", "50")) + )); + CommentSequence commentSequence = new CommentSequence(); + int expected = 11; + int actual = commentSequence.getPopularCommentID(commentList); + assertThat(actual, is(expected)); + } + + +}