Skip to content

Comments

[BE] ํ•œ์„ฑ๋ฏผ ๐Ÿ‘ฅ ์˜ค๋Š˜์˜ ์ง๊ฟ์€?#12

Open
anxi01 wants to merge 8 commits intoLeets-Official:mainfrom
anxi01:anxi01
Open

[BE] ํ•œ์„ฑ๋ฏผ ๐Ÿ‘ฅ ์˜ค๋Š˜์˜ ์ง๊ฟ์€?#12
anxi01 wants to merge 8 commits intoLeets-Official:mainfrom
anxi01:anxi01

Conversation

@anxi01
Copy link
Member

@anxi01 anxi01 commented Apr 10, 2024

๋ฏธ์…˜ ๊ฒฐ๊ณผ

ํ…Œ์ŠคํŠธ

แ„‰แ…ณแ„แ…ณแ„…แ…ตแ†ซแ„‰แ…ฃแ†บ 2024-04-10 แ„‹แ…ฉแ„’แ…ฎ 10 24 33

์‹คํ–‰ ๊ฒฐ๊ณผ

แ„‰แ…ณแ„แ…ณแ„…แ…ตแ†ซแ„‰แ…ฃแ†บ 2024-04-10 แ„‹แ…ฉแ„’แ…ฎ 10 26 02

๊ธฐ๋Šฅ ๋ช…์„ธ์„œ

๋žœ๋ค ์ง๊ฟ ์ถ”์ฒจ ๋กœ์ง

  • currentIndex๋ฅผ 1์”ฉ ์ฆ๊ฐ€ํ•˜๋ฉด์„œ ๊ทธ๋ฃน์— ๋ฉค๋ฒ„๋ฅผ ์ฑ„์›Œ result ๋ฆฌ์ŠคํŠธ์— ๋„ฃ์–ด์ค€๋‹ค.
  • ๊ทธ๋ฃน์— ๊ฐ€๋“ ์ฐจ์ง€ ๋ชปํ•  ๊ฒฝ์šฐ, ๋‚˜๋จธ์ง€ ๋ฉค๋ฒ„๋“ค์„ ๋ชจ์•„ ๋งˆ์ง€๋ง‰ ๊ทธ๋ฃน์— ๋„ฃ์–ด์ค€๋‹ค.

@anxi01 anxi01 self-assigned this Apr 10, 2024
Copy link
Member

@taeseokyang taeseokyang left a comment

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 (true) {
Copy link
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
Member Author

Choose a reason for hiding this comment

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

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

}

// ๋žœ๋ค ์ง๊ฟ ์ถ”์ฒจํ•˜๋Š” ํ•จ์ˆ˜ ์ž…๋‹ˆ๋‹ค.
public List<List<String>> generateRandomGroups(List<String> memberList, int maximumGroupSize) {
Copy link
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
Member Author

Choose a reason for hiding this comment

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

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

Copy link
Member

@ay-eonii ay-eonii left a comment

Choose a reason for hiding this comment

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

1์ฃผ์ฐจ ๋ฏธ์…˜ ๊ณ ์ƒํ•˜์…จ์Šต๋‹ˆ๋‹ค ๐Ÿ‘
์š”๊ตฌ์‚ฌํ•ญ์„ ์ž˜ ๋ฐ˜์˜ํ•ด์„œ ๊ตฌํ˜„ํ•ด์ฃผ์…จ๋„ค์š”!

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

if (answer.equals("y")) {
Copy link
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
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
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
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
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
Member Author

Choose a reason for hiding this comment

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

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

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

Choose a reason for hiding this comment

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

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

Copy link
Member

Choose a reason for hiding this comment

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

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

Copy link
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
Member Author

Choose a reason for hiding this comment

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

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

Copy link
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
Member Author

Choose a reason for hiding this comment

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

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


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

Choose a reason for hiding this comment

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

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

Copy link
Member Author

Choose a reason for hiding this comment

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

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants