[BE] ํ์ฑ๋ฏผ ๐ฅ ์ค๋์ ์ง๊ฟ์?#12
[BE] ํ์ฑ๋ฏผ ๐ฅ ์ค๋์ ์ง๊ฟ์?#12anxi01 wants to merge 8 commits intoLeets-Official:mainfrom
Conversation
|
|
||
| printResult(generateRandomGroups(memberList, maximumGroupSize)); | ||
|
|
||
| while (true) { |
There was a problem hiding this comment.
์์์
printResult(generateRandomGroups(memberList, maximumGroupSize));
์ฝ๋๊ฐ ์ค๋ณต๋ฉ๋๋ค! while์ฝ๋๋ฅผ ์๋์ ๊ฐ์ด ์์ ํ๋ฉด ์ข์ ๊ฒ ๊ฐ์ต๋๋ค!
while (true) {
printResult(generateRandomGroups(memberList, maximumGroupSize));
System.out.print("๋ค์ ๊ตฌ์ฑํ์๊ฒ ์ต๋๊น? (y or n): ");
String answer = sc.next();
if (answer.equals("n")) {
System.out.println("์๋ฆฌ๋ฅผ ์ด๋ํด ์๋ก์๊ฒ ์ธ์ฌํด์ฃผ์ธ์.");
break;
}
}
There was a problem hiding this comment.
์ค ์ข์ ๋ฐฉ๋ฒ์ด๋ค์!
๊ฐ์ฌํฉ๋๋ค :)
| } | ||
|
|
||
| // ๋๋ค ์ง๊ฟ ์ถ์ฒจํ๋ ํจ์ ์ ๋๋ค. | ||
| public List<List<String>> generateRandomGroups(List<String> memberList, int maximumGroupSize) { |
There was a problem hiding this comment.
์ด ํจ์ ์ฝ๋๋ฅผ GPT๋ฅผ ํตํด ๊ฐ์ ํ๋ฉด
์๋์ ๊ฐ์ด ๊ฐ์ ๊ฐ๋ฅํ๋ฐ
Collections.shuffle(memberList);
List<List<String>> result = new ArrayList<>();
int memberCount = memberList.size();
int groupCount = memberCount / maximumGroupSize;
int remainder = memberCount % maximumGroupSize;
int currentIndex = 0;
for (int i = 0; i < groupCount; i++) {
List<String> group = new ArrayList<>(memberList.subList(currentIndex, currentIndex + maximumGroupSize));
result.add(group);
currentIndex += maximumGroupSize;
}
// ๋จ์ ๋ฉค๋ฒ๋ค์ ์ฒ๋ฆฌํฉ๋๋ค.
List<String> lastGroup = new ArrayList<>(memberList.subList(currentIndex, currentIndex + remainder));
result.add(lastGroup);
๋จ์ ๋ฉค๋ฒ ๋ฐ๋ก ์ฒ๋ฆฌ ํ๊ธฐ ์ซ์ด์ ์๋์ ๊ฐ์ ๋ฐฉ๋ฒ์ ์๊ฐํด ๋ณด์์ต๋๋ค. ์๋ง ๋ ์ข์ ๋ฐฉ๋ฒ์ด ์์ ๊ฑฐ ๊ฐ์๋ฐ, ์ ๋ฐฉ๋ฒ ๊ณต์ ํด ๋๋ฆฝ๋๋ค!
Collections.shuffle(memberList);
List<List<String>> result = new ArrayList<>();
int memberCount = memberList.size();
int groupCount = memberCount / maximumGroupSize;
int leftMemberCount = memberCount;
int currentIndex = 0;
for (int i = 0; i < groupCount+1; i++) {
int groupSize = Math.min(maximumGroupSize, leftMemberCount);
List<String> group = new ArrayList<>(memberList.subList(currentIndex, currentIndex + groupSize));
result.add(group);
currentIndex += maximumGroupSize;
leftMemberCount -= maximumGroupSize;
}
There was a problem hiding this comment.
๋จ์์๋ ๋ฉค๋ฒ์ ์ต๋ ๊ทธ๋ฃน ์๋ฅผ ๋น๊ตํด์ฃผ๋ฉด ๋๋๊ตฐ์ !
์ข์ ์๊ฒฌ ๊ฐ์ฌํฉ๋๋ค !
ay-eonii
left a comment
There was a problem hiding this comment.
1์ฃผ์ฐจ ๋ฏธ์
๊ณ ์ํ์
จ์ต๋๋ค ๐
์๊ตฌ์ฌํญ์ ์ ๋ฐ์ํด์ ๊ตฌํํด์ฃผ์
จ๋ค์!
| System.out.print("๋ค์ ๊ตฌ์ฑํ์๊ฒ ์ต๋๊น? (y or n): "); | ||
| String answer = sc.next(); | ||
|
|
||
| if (answer.equals("y")) { |
There was a problem hiding this comment.
๋ง์ฝ answer์ด null ์ด๋ผ๋ฉด ์ด๋ค ๋ฌธ์ ๊ฐ ์์๊น์?
There was a problem hiding this comment.
null์ผ ๊ฒฝ์ฐ ์ข
๋ฃ๋๋ ๋ฌธ์ ๊ฐ ๋ฐ์ํ๋ค์,,
y, n ๊ทธ๋ฆฌ๊ณ ์ด ๋ ๊ฐ๊ณผ ๋ค๋ฅธ ๊ฒฝ์ฐ๋ฅผ ์์ธ์ฒ๋ฆฌ ํด์ผ๊ฒ ์ต๋๋ค !
๊ฐ์ฌํด์ !
There was a problem hiding this comment.
y ๋๋ n ์ธ๊ฒฝ์ฐ ์์ธ์ฒ๋ฆฌ๋ ์ค์ํฉ๋๋ค๐
๊ทธ๋ฐ๋ฐ answer์ด null ์ด๋ผ๋ฉด NPE๊ฐ ๋ ๊ฒ ๊ฐ์์.
| if (answer.equals("y")) { | |
| if ("y".equals(answer)) { |
์ด๋ ๊ฒ ๋ณ๊ฒฝํด๋ NPE๊ฐ ๋ฐ์ํ ๊ฐ๋ฅ์ฑ์ด ์์๊น์?
There was a problem hiding this comment.
euqals ๋ฉ์๋๋ nullableํ ๊ฐ์ฒด๋ฅผ ์ธ์๋ก ๋ฐ๊ธฐ ๋๋ฌธ์ NPE๊ฐ ๋ฐ์ํ์ง ์์ ๊ฒ ๊ฐ์๋ฐ ์ด๋ป๊ฒ ์๊ฐํ์๋์ง ๊ถ๊ธํฉ๋๋ค !
There was a problem hiding this comment.
euqals ๋ฉ์๋๋ nullableํ ๊ฐ์ฒด๋ฅผ ์ธ์๋ก ๋ฐ๊ธฐ ๋๋ฌธ์ NPE๊ฐ ๋ฐ์ํ์ง ์์ ๊ฒ ๊ฐ์๋ฐ
๋ต ๋ง์์!
answer.equals("y")๋ NPE๊ฐ ๋ฐ์ํ ๊ฐ๋ฅ์ฑ์ด ์์ง๋ง ์์๋ฅผ ๋ฐ๊ฟ์ "y".equals(answer)๋ก ์์ฑํ๋ค๋ฉด NPE๊ฐ ๋ฐ์ํ์ง ์๊ฒ ์ฃ ! ๊ทธ๋์ ์ ํ์๋ฅผ ์ ํธํฉ๋๋ค ๐
There was a problem hiding this comment.
์ ํ ์๊ฐํด๋ณธ์ ์์๋๋ฐ ๊นจ๋ซ๊ณ ๊ฐ๋๋ค !
๊ฐ์ฌํด์ !
| for (List<String> row : result) { | ||
| sb.append("[ "); | ||
| for (int j = 0; j < row.size(); j++) { | ||
| sb.append(row.get(j)); | ||
|
|
||
| if (j < row.size() - 1) { | ||
| sb.append(" | "); | ||
| } | ||
| } | ||
| sb.append(" ]\n"); | ||
| } |
There was a problem hiding this comment.
StringJoiner๋ฅผ ์์๋ณด๋ฉด ๋์์ด ๋ ๊ฒ ๊ฐ๋ค์ ๐
There was a problem hiding this comment.
์ถ๊ฐ์ ์ผ๋ก, \n ๊ฐํ๋ฌธ์๋ฅผ ํ๋์ฝ๋ฉํ์ ๋์ ๋ฌธ์ ์ ์ ๋ฌด์์ด ์์๊น์?
There was a problem hiding this comment.
StringJoiner์ ์ฒ์ ๋ค์ด๋ณด๋๋ฐ ํ๋ฒ ์์๋ณผ๊ฒ์ !
There was a problem hiding this comment.
ํ๋์ฝ๋ฉํ๋ฉด ์ฝ๋๊ฐ ์ง์ ๋ถํด๋ณด์ด๋ ๊ฒ๋ ๋ฌธ์ ์ ์ด๋ผ ํ ์ ์์ ๊ฑฐ ๊ฐ์๋ฐ
์์ฐ๋์ด ์๊ฐํ์๋ ๋ฌธ์ ์ ์ ๋ฌด์์ธ์ง ๊ถ๊ธํด์ !
There was a problem hiding this comment.
UNIX : \n
Windows : \r\n
์ด์์ฒด์ ๋ง๋ค ๊ฐํ๋ฌธ์๊ฐ ๋ค๋ฅด๊ธฐ ๋๋ฌธ์ ์ํ๋ ๋๋ก ์ค๋ฐ๊ฟ์ด ๋์ง ์์ ๊ฐ๋ฅ์ฑ์ด ์์ด๋ณด์ฌ์!
There was a problem hiding this comment.
์ค์ค,, ์ด์์ฒด์ ์ ๋ํ ์๊ฐ์ ์ ํ ํด๋ณธ ์ ์๋๋ฐ,, ์ด๋ฐ ์ผ์ด ๋ฐ์ํ ์๋ ์๊ตฐ์,,! ์ฐธ๊ณ ํ๊ฒ ์ต๋๋ค! ๊ฐ์ฌํด์ ใ ใ
|
|
||
| // ๋์ ํจ์์ ๋๋ค. | ||
| public void run() { | ||
| public void run() throws Exception { |
There was a problem hiding this comment.
์ง๊ธ์ ์์ธ๋ฅผ ๋์ง๊ณ ๋ง ์์ง๋ง, ์คํ๊ฐ ๋ ๋๋ง๋ค ํ๋ก๊ทธ๋จ์ด ์ข ๋ฃ๋๋ฉด ๋ถํธํ์ง ์์๊น์ ?๐
There was a problem hiding this comment.
์์ธ ์ฒ๋ฆฌ ๋์ boolean์ผ๋ก ์ค์ ํด์ ์ข
๋ฃ๋์ง ์๊ณ ์ฌ์
๋ ฅ ๋ฐ์ ์ ์๊ฒ ํด์ผ๊ฒ ๋ค์ !
์์ธ๋ฅผ ๋์ง๋ ๊ฒ๋ง์ด ๋ฐฉ๋ฒ์ธ์ค ์์๋๋ฐ ์๋ ค์ฃผ์
์ ๊ฐ์ฌํฉ๋๋ค :)
๋ฏธ์ ๊ฒฐ๊ณผ
ํ ์คํธ
์คํ ๊ฒฐ๊ณผ
๊ธฐ๋ฅ ๋ช ์ธ์
๋๋ค ์ง๊ฟ ์ถ์ฒจ ๋ก์ง