From d8ab03602938b54c60f7f521e988410488de5eda Mon Sep 17 00:00:00 2001 From: SSung023 <50323157+SSung023@users.noreply.github.com> Date: Thu, 4 May 2023 17:40:37 +0900 Subject: [PATCH] [Feat] Add Palindrome features --- .../controller/OutsourceController.java | 3 ++ .../oursource/service/OutsourceService.java | 39 +++++++++++++++ .../service/OutsourceServiceTest.java | 49 +++++++++++++++++++ 3 files changed, 91 insertions(+) diff --git a/src/main/java/sangmyung/chatprompt/oursource/controller/OutsourceController.java b/src/main/java/sangmyung/chatprompt/oursource/controller/OutsourceController.java index 8e1077d..bd5b61d 100644 --- a/src/main/java/sangmyung/chatprompt/oursource/controller/OutsourceController.java +++ b/src/main/java/sangmyung/chatprompt/oursource/controller/OutsourceController.java @@ -44,10 +44,13 @@ public CommonResponse getType1_Complex(@PageableDefault(size = 60, sort = "idx", @GetMapping("/outsource/type2") public CommonResponse getType2(@PageableDefault(size = 60, sort = "idx", direction = Sort.Direction.ASC) Pageable pageable){ outsourceService.extract_C27(pageable); + outsourceService.extract_D20(pageable); + outsourceService.extract_E20(pageable); outsourceService.extract_E26(pageable); outsourceService.extract_E27(pageable); + outsourceService.extract_E28(pageable); outsourceService.extract_F102(pageable); outsourceService.extract_F105(pageable); diff --git a/src/main/java/sangmyung/chatprompt/oursource/service/OutsourceService.java b/src/main/java/sangmyung/chatprompt/oursource/service/OutsourceService.java index 0ae614c..5fe1b3a 100644 --- a/src/main/java/sangmyung/chatprompt/oursource/service/OutsourceService.java +++ b/src/main/java/sangmyung/chatprompt/oursource/service/OutsourceService.java @@ -609,7 +609,17 @@ public void extract_E28(Pageable pageable){ User user = checkAssignedUser(taskPK); for (IOPairs pair : pairs) { + String input = pair.getInput1(); + String output = longestPalindrome(input); + Assignment assignment = Assignment.builder() + .input(input) + .output(output) + .taskId(taskPK) + .ioPairsIdx(pair.getIdx()) + .build(); + Assignment savedAssignment = assignmentRepository.save(assignment); + savedAssignment.addUser(user); } } @@ -935,4 +945,33 @@ private int getFrequency(String sentence, String target){ } return cnt; } + + // 회문 관련 코드 + private String longestPalindrome(String s) { + if (s == null || s.length() < 1) return ""; + int start = 0, end = 0; + for (int i = 0; i < s.length(); i++) { + int len1 = expandAroundCenter(s, i, i); + int len2 = expandAroundCenter(s, i, i + 1); + int len = Math.max(len1, len2); + if (len > end - start) { + start = i - (len - 1) / 2; + end = i + len / 2; + } + } + String result = s.substring(start, end + 1); + if (result.length() == 1){ + return String.valueOf(s.charAt(0)); + } + return result; + } + + private int expandAroundCenter(String s, int left, int right) { + int L = left, R = right; + while (L >= 0 && R < s.length() && s.charAt(L) == s.charAt(R)) { + L--; + R++; + } + return R - L - 1; + } } \ No newline at end of file diff --git a/src/test/java/sangmyung/chatprompt/oursource/service/OutsourceServiceTest.java b/src/test/java/sangmyung/chatprompt/oursource/service/OutsourceServiceTest.java index e022a73..4d2e382 100644 --- a/src/test/java/sangmyung/chatprompt/oursource/service/OutsourceServiceTest.java +++ b/src/test/java/sangmyung/chatprompt/oursource/service/OutsourceServiceTest.java @@ -15,6 +15,8 @@ import sangmyung.chatprompt.Util.exception.ErrorCode; import sangmyung.chatprompt.assignment.domain.Assignment; import sangmyung.chatprompt.assignment.repository.AssignmentRepository; +import sangmyung.chatprompt.task.domain.IOPairs; +import sangmyung.chatprompt.task.repository.IoPairRepository; import sangmyung.chatprompt.task.repository.TaskRepository; import sangmyung.chatprompt.user.domain.User; import sangmyung.chatprompt.user.repository.UserRepository; @@ -32,6 +34,7 @@ class OutsourceServiceTest { @Autowired UserRepository userRepository; @Autowired TaskRepository taskRepository; @Autowired AssignmentRepository assignmentRepository; + @Autowired IoPairRepository ioPairRepository; @Autowired OutsourceService outsourceService; @@ -542,12 +545,58 @@ public void E10Test(){ //then Assertions.assertThat(output).isEqualTo("에트프리 탄 이들람사 스노보드를 타고 있는 남자를 고하경구 있다"); } + + @Test + @DisplayName("E28번 테스트") + public void E28Test(){ + //given + Long taskPK = 96L; + PageRequest pageable = PageRequest.of(0, 60, Sort.by(Sort.Direction.ASC, "idx")); + List pairs = ioPairRepository.findPairsByTaskId(taskPK, pageable); + + + //when + String input = "aaaaaangggannna"; + String output = longestPalindrome(input); + + //then + Assertions.assertThat(output).isEqualTo("aaaaaa"); + } + + private String longestPalindrome(String s) { + if (s == null || s.length() < 1) return ""; + int start = 0, end = 0; + for (int i = 0; i < s.length(); i++) { + int len1 = expandAroundCenter(s, i, i); + int len2 = expandAroundCenter(s, i, i + 1); + int len = Math.max(len1, len2); + if (len > end - start) { + start = i - (len - 1) / 2; + end = i + len / 2; + } + } + String result = s.substring(start, end + 1); + if (result.length() == 1){ + return String.valueOf(s.charAt(0)); + } + return result; + } + + private int expandAroundCenter(String s, int left, int right) { + int L = left, R = right; + while (L >= 0 && R < s.length() && s.charAt(L) == s.charAt(R)) { + L--; + R++; + } + return R - L - 1; + } + private int getFrequency(String sentence, String target){ List lists = List.of(sentence.split(" ")); int cnt = 0;