Skip to content
Open
93 changes: 86 additions & 7 deletions src/main/java/leets/leets_mate/LeetsMateApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,37 +5,116 @@
public class LeetsMateApplication {

// ๋™์ž‘ ํ•จ์ˆ˜์ž…๋‹ˆ๋‹ค.
public void run() {
public void run() throws Exception {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

์ง€๊ธˆ์€ ์˜ˆ์™ธ๋ฅผ ๋˜์ง€๊ณ ๋งŒ ์žˆ์ง€๋งŒ, ์˜คํƒ€๊ฐ€ ๋‚  ๋•Œ๋งˆ๋‹ค ํ”„๋กœ๊ทธ๋žจ์ด ์ข…๋ฃŒ๋˜๋ฉด ๋ถˆํŽธํ•˜์ง€ ์•Š์„๊นŒ์š” ?๐Ÿ˜‹

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

์˜ˆ์™ธ ์ฒ˜๋ฆฌ ๋Œ€์‹  boolean์œผ๋กœ ์„ค์ •ํ•ด์„œ ์ข…๋ฃŒ๋˜์ง€ ์•Š๊ณ  ์žฌ์ž…๋ ฅ ๋ฐ›์„ ์ˆ˜ ์žˆ๊ฒŒ ํ•ด์•ผ๊ฒ ๋„ค์š” !
์˜ˆ์™ธ๋ฅผ ๋˜์ง€๋Š” ๊ฒƒ๋งŒ์ด ๋ฐฉ๋ฒ•์ธ์ค„ ์•Œ์•˜๋Š”๋ฐ ์•Œ๋ ค์ฃผ์…”์„œ ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค :)

Scanner sc = new Scanner(System.in);

System.out.println("[Leets ์˜ค๋Š˜์˜ ์ง์—๊ฒŒ]๋ฅผ ์‹œ์ž‘ํ•ฉ๋‹ˆ๋‹ค.\n");

System.out.println("์ฐธ์„์ž๋“ค์˜ ์ด๋ฆ„์„ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”. (,๋กœ ๊ตฌ๋ถ„)");
String members = sc.nextLine();

checkHasNoEnglish(members);

System.out.println("\n์ตœ๋Œ€ ์ง ์ˆ˜๋ฅผ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”.");
int maximumGroupSize = sc.nextInt();

List<String> memberList = parseMembers(members);

checkDataValidity(memberNumber(memberList), maximumGroupSize);

printResult(generateRandomGroups(memberList, maximumGroupSize));

while (true) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

์œ„์—์„œ
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;
        }
    }

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

์˜ค ์ข‹์€ ๋ฐฉ๋ฒ•์ด๋„ค์š”!
๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค :)

System.out.print("๋‹ค์‹œ ๊ตฌ์„ฑํ•˜์‹œ๊ฒ ์Šต๋‹ˆ๊นŒ? (y or n): ");
String answer = sc.next();

if (answer.equals("y")) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

๋งŒ์•ฝ answer์ด null ์ด๋ผ๋ฉด ์–ด๋–ค ๋ฌธ์ œ๊ฐ€ ์žˆ์„๊นŒ์š”?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

null์ผ ๊ฒฝ์šฐ ์ข…๋ฃŒ๋˜๋Š” ๋ฌธ์ œ๊ฐ€ ๋ฐœ์ƒํ•˜๋„ค์š”,,
y, n ๊ทธ๋ฆฌ๊ณ  ์ด ๋‘ ๊ฐ’๊ณผ ๋‹ค๋ฅธ ๊ฒฝ์šฐ๋ฅผ ์˜ˆ์™ธ์ฒ˜๋ฆฌ ํ•ด์•ผ๊ฒ ์Šต๋‹ˆ๋‹ค !
๊ฐ์‚ฌํ•ด์š” !

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

y ๋˜๋Š” n ์ธ๊ฒฝ์šฐ ์˜ˆ์™ธ์ฒ˜๋ฆฌ๋„ ์ค‘์š”ํ•ฉ๋‹ˆ๋‹ค๐Ÿ‘
๊ทธ๋Ÿฐ๋ฐ answer์ด null ์ด๋ผ๋ฉด NPE๊ฐ€ ๋‚  ๊ฒƒ ๊ฐ™์•„์š”.

Suggested change
if (answer.equals("y")) {
if ("y".equals(answer)) {

์ด๋ ‡๊ฒŒ ๋ณ€๊ฒฝํ•ด๋„ NPE๊ฐ€ ๋ฐœ์ƒํ•  ๊ฐ€๋Šฅ์„ฑ์ด ์žˆ์„๊นŒ์š”?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

euqals ๋ฉ”์„œ๋“œ๋Š” nullableํ•œ ๊ฐ์ฒด๋ฅผ ์ธ์ž๋กœ ๋ฐ›๊ธฐ ๋•Œ๋ฌธ์— NPE๊ฐ€ ๋ฐœ์ƒํ•˜์ง€ ์•Š์„ ๊ฒƒ ๊ฐ™์€๋ฐ ์–ด๋–ป๊ฒŒ ์ƒ๊ฐํ•˜์‹œ๋Š”์ง€ ๊ถ๊ธˆํ•ฉ๋‹ˆ๋‹ค !

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

euqals ๋ฉ”์„œ๋“œ๋Š” nullableํ•œ ๊ฐ์ฒด๋ฅผ ์ธ์ž๋กœ ๋ฐ›๊ธฐ ๋•Œ๋ฌธ์— NPE๊ฐ€ ๋ฐœ์ƒํ•˜์ง€ ์•Š์„ ๊ฒƒ ๊ฐ™์€๋ฐ

๋„ต ๋งž์•„์š”!
answer.equals("y")๋Š” NPE๊ฐ€ ๋ฐœ์ƒํ•  ๊ฐ€๋Šฅ์„ฑ์ด ์žˆ์ง€๋งŒ ์ˆœ์„œ๋ฅผ ๋ฐ”๊ฟ”์„œ "y".equals(answer)๋กœ ์ž‘์„ฑํ•œ๋‹ค๋ฉด NPE๊ฐ€ ๋ฐœ์ƒํ•˜์ง€ ์•Š๊ฒ ์ฃ ! ๊ทธ๋ž˜์„œ ์ „ ํ›„์ž๋ฅผ ์„ ํ˜ธํ•ฉ๋‹ˆ๋‹ค ๐Ÿ˜Š

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

์ „ํ˜€ ์ƒ๊ฐํ•ด๋ณธ์  ์—†์—ˆ๋Š”๋ฐ ๊นจ๋‹ซ๊ณ  ๊ฐ‘๋‹ˆ๋‹ค !
๊ฐ์‚ฌํ•ด์š” !

printResult(generateRandomGroups(memberList, maximumGroupSize));
} else {
System.out.println("์ž๋ฆฌ๋ฅผ ์ด๋™ํ•ด ์„œ๋กœ์—๊ฒŒ ์ธ์‚ฌํ•ด์ฃผ์„ธ์š”.");
break;
}
}
}

// ๋ฌธ์ž์—ด๋กœ๋œ ๋ฉค๋ฒ„๋“ค์„ ๋ฆฌ์ŠคํŠธ๋กœ ๋ถ„๋ฆฌํ•˜๋Š” ํ•จ์ˆ˜์ž…๋‹ˆ๋‹ค.
public List<String> parseMembers(String members) {
return new ArrayList<>();
return Arrays.asList(members.split(","));
}

// ์ด ๋ฉค๋ฒ„์ˆ˜๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.
public int memberNumber(List<String> members) {
return 0;
return members.size();
}

// ๋ฉค๋ฒ„ ๋ฌธ์ž์—ด์— ์˜์–ด๊ฐ€ ์žˆ๋Š”์ง€ ๊ฒ€์‚ฌํ•ฉ๋‹ˆ๋‹ค. ์˜์–ด๊ฐ€ ์žˆ๋‹ค๋ฉด ์˜ˆ์™ธ ์ถœ๋ ฅ
public void checkHasNoEnglish(String members) {
public void checkHasNoEnglish(String members) throws Exception {
if (members.matches(".*[a-zA-Z].*")) {
throw new Exception("[ERROR] ์ด๋ฆ„์€ ํ•œ๊ธ€๋กœ ์ž…๋ ฅํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค");
}
}

// ๋ฉค๋ฒ„์ˆ˜์™€ ์ตœ๋Œ€ ์ง์ˆ˜ ๋ฐ์ดํ„ฐ๊ฐ€ ์œ ํšจํ•œ์ง€ ๊ฒ€์‚ฌํ•˜๋Š” ํ•จ์ˆ˜์ž…๋‹ˆ๋‹ค. ์œ ํšจํ•˜์ง€ ์•Š๋‹ค๋ฉด ์˜ˆ์™ธ ์ถœ๋ ฅ
public void checkDataValidity(int memberCount, int maximumGroupSize) {
public void checkDataValidity(int memberCount, int maximumGroupSize) throws Exception {
if (memberCount < maximumGroupSize) {
throw new Exception("[ERROR] ์ตœ๋Œ€ ์ง ์ˆ˜๋Š” ์ด๋ฆ„์˜ ๊ฐฏ์ˆ˜๋ณด๋‹ค ํด ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค");
}
}

// ๋žœ๋ค ์ง๊ฟ ์ถ”์ฒจํ•˜๋Š” ํ•จ์ˆ˜ ์ž…๋‹ˆ๋‹ค.
public List<List<String>> generateRandomGroups(List<String> memberList, int maximumGroupSize) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

์ด ํ•จ์ˆ˜ ์ฝ”๋“œ๋ฅผ 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;
    }

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

๋‚จ์•„์žˆ๋Š” ๋ฉค๋ฒ„์™€ ์ตœ๋Œ€ ๊ทธ๋ฃน ์ˆ˜๋ฅผ ๋น„๊ตํ•ด์ฃผ๋ฉด ๋˜๋Š”๊ตฐ์š” !
์ข‹์€ ์˜๊ฒฌ ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค !

return new ArrayList<>();
Collections.shuffle(memberList);

List<List<String>> result = new ArrayList<>();

int memberCount = memberNumber(memberList);
int groupCount = memberCount / maximumGroupSize;
int remainingMembers = memberCount % maximumGroupSize;

int currentIndex = 0;
for (int i = 0; i < groupCount; i++) {
List<String> group = new ArrayList<>();
for (int j = 0; j < maximumGroupSize; j++) {
group.add(memberList.get(currentIndex));
currentIndex++;
}
result.add(group);
}

if (remainingMembers > 0) {
List<String> lastGroup = new ArrayList<>();
for (int i = currentIndex; i < memberCount; i++) {
lastGroup.add(memberList.get(i));
}
result.add(lastGroup);
}

return result;
}

// ๊ฒฐ๊ณผ๋ฅผ ํ”„๋ฆฐํŠธ ํ•˜๋Š” ํ•จ์ˆ˜์ž…๋‹ˆ๋‹ค.
public void printResult(List<List<String>> result) {
System.out.println("\n์˜ค๋Š˜์˜ ์ง ์ถ”์ฒœ ๊ฒฐ๊ณผ์ž…๋‹ˆ๋‹ค.");

StringBuilder sb = new StringBuilder();

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");
}
Comment on lines +101 to +111

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

StringJoiner๋ฅผ ์•Œ์•„๋ณด๋ฉด ๋„์›€์ด ๋  ๊ฒƒ ๊ฐ™๋„ค์š” ๐Ÿ˜Š

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

์ถ”๊ฐ€์ ์œผ๋กœ, \n ๊ฐœํ–‰๋ฌธ์ž๋ฅผ ํ•˜๋“œ์ฝ”๋”ฉํ–ˆ์„ ๋•Œ์˜ ๋ฌธ์ œ์ ์€ ๋ฌด์—‡์ด ์žˆ์„๊นŒ์š”?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

StringJoiner์€ ์ฒ˜์Œ ๋“ค์–ด๋ณด๋Š”๋ฐ ํ•œ๋ฒˆ ์•Œ์•„๋ณผ๊ฒŒ์š” !

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ํ•˜๋“œ์ฝ”๋”ฉํ•˜๋ฉด ์ฝ”๋“œ๊ฐ€ ์ง€์ €๋ถ„ํ•ด๋ณด์ด๋Š” ๊ฒƒ๋„ ๋ฌธ์ œ์ ์ด๋ผ ํ•  ์ˆ˜ ์žˆ์„ ๊ฑฐ ๊ฐ™์€๋ฐ
์•„์—ฐ๋‹˜์ด ์ƒ๊ฐํ•˜์‹œ๋Š” ๋ฌธ์ œ์ ์€ ๋ฌด์—‡์ธ์ง€ ๊ถ๊ธˆํ•ด์š” !

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

UNIX : \n
Windows : \r\n

์šด์˜์ฒด์ œ๋งˆ๋‹ค ๊ฐœํ–‰๋ฌธ์ž๊ฐ€ ๋‹ค๋ฅด๊ธฐ ๋•Œ๋ฌธ์— ์›ํ•˜๋Š” ๋Œ€๋กœ ์ค„๋ฐ”๊ฟˆ์ด ๋˜์ง€ ์•Š์„ ๊ฐ€๋Šฅ์„ฑ์ด ์žˆ์–ด๋ณด์—ฌ์š”!

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

์˜ค์˜ค,, ์šด์˜์ฒด์ œ์— ๋Œ€ํ•œ ์ƒ๊ฐ์€ ์ „ํ˜€ ํ•ด๋ณธ ์  ์—†๋Š”๋ฐ,, ์ด๋Ÿฐ ์ผ์ด ๋ฐœ์ƒํ•  ์ˆ˜๋„ ์žˆ๊ตฐ์š”,,! ์ฐธ๊ณ ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค! ๊ฐ์‚ฌํ•ด์š” ใ…Žใ…Ž


sb.append("\n์ถ”์ฒœ์„ ์™„๋ฃŒํ–ˆ์Šต๋‹ˆ๋‹ค.\n");
System.out.println(sb);
}

public static void main(String[] args) {
public static void main(String[] args) throws Exception {
LeetsMateApplication app = new LeetsMateApplication();
app.run();
}
Expand Down