Skip to content

Commit

Permalink
[Feat] Add Palindrome features
Browse files Browse the repository at this point in the history
  • Loading branch information
SSung023 committed May 4, 2023
1 parent aaf534f commit d8ab036
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down Expand Up @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -32,6 +34,7 @@ class OutsourceServiceTest {
@Autowired UserRepository userRepository;
@Autowired TaskRepository taskRepository;
@Autowired AssignmentRepository assignmentRepository;
@Autowired IoPairRepository ioPairRepository;
@Autowired OutsourceService outsourceService;


Expand Down Expand Up @@ -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<IOPairs> 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<String> lists = List.of(sentence.split(" "));
int cnt = 0;
Expand Down

0 comments on commit d8ab036

Please sign in to comment.