Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
114 changes: 107 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,137 @@
public class LeetsMateApplication {

// ๋™์ž‘ ํ•จ์ˆ˜์ž…๋‹ˆ๋‹ค.
public void run() {
public void run() throws Exception {
Boolean isDone = false;
while(!isDone) {
Scanner sc = new Scanner(System.in);
System.out.println("์ฐธ์„์ž๋“ค์˜ ์ด๋ฆ„์„ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”. (,๋กœ ๊ตฌ๋ถ„)");
String str = sc.nextLine();
checkHasNoEnglish(str);
List<String> memberList = parseMembers(str);
System.out.println(Arrays.deepToString(memberList.toArray())); //2์ฐจ์›” ์–ด๋ ˆ์ด ๋ฐ”๋กœ ์ถœ๋ ฅ

int memberCount = memberNumber(memberList);

System.out.println("์ตœ๋Œ€ ์ง ์ˆ˜๋ฅผ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”");
int maximumGroupSize = sc.nextInt();
checkDataValidity(memberCount, maximumGroupSize); //๋ฉค๋ฒ„์ˆ˜์™€ ์ตœ๋Œ€ ์ง์ˆ˜ ๋ฐ์ดํ„ฐ๊ฐ€ ์œ ํšจํ•œ์ง€ ๊ฒ€์‚ฌ

List<List<String>> result = generateRandomGroups(memberList, maximumGroupSize);
System.out.println("์˜ค๋Š˜์˜ ์ง ์ถ”์ฒœ ๊ฒฐ๊ณผ์ž…๋‹ˆ๋‹ค.");
printResult(result);
//๊น€์„ฑ๋ฏผ,์กฐํ˜œ์›,๋…ธ์ •์™„,๊น€ํ˜œ์ง„,์–‘ํƒœ์„, ์ „์‹œํ˜„, ์•„์•„

System.out.println("์ถ”์ฒœ์„ ์™„๋ฃŒํ–ˆ์Šต๋‹ˆ๋‹ค.\n๋‹ค์‹œ ๊ตฌ์„ฑํ•˜์‹œ๊ฒ ์Šต๋‹ˆ๊นŒ? (y or n):");

char again = sc.next().charAt(0);
if(!(again == 'y' || again == 'Y')){
isDone = true; //n, N์„ ํฌํ•จํ•œ ๋‹ค๋ฅธ ์ž…๋ ฅ์€ ๋‹ค์‹œ์‹œ์ž‘x
}
}
System.out.println("์ž๋ฆฌ๋ฅผ ์ด๋™ํ•ด ์„œ๋กœ์—๊ฒŒ ์ธ์‚ฌํ•ด์ฃผ์„ธ์š”");

}

// ๋ฌธ์ž์—ด๋กœ๋œ ๋ฉค๋ฒ„๋“ค์„ ๋ฆฌ์ŠคํŠธ๋กœ ๋ถ„๋ฆฌํ•˜๋Š” ํ•จ์ˆ˜์ž…๋‹ˆ๋‹ค.
public List<String> parseMembers(String members) {
return new ArrayList<>();
//String[] memberList = members.split(",");
/*
for(int i = 0; i<members.length()){
if(members[i])
}*/

members = members.replaceAll(" ", ""); //๊ณต๋ฐฑ ์ œ๊ฑฐ

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.

trim()ํ•จ์ˆ˜๊ฐ€ ๊ณต๋ฐฑ์ œ๊ฑฐ ๋ฌธ์ž์—ด ํ•จ์ˆ˜์ž…๋‹ˆ๋‹ค! ์จ๋ณด์…”๋„ ์ข‹์„๊ฒƒ ๊ฐ™์•„์š”~

List<String> memberList = Arrays.asList(members.split(","));
return memberList;
}

// ์ด ๋ฉค๋ฒ„์ˆ˜๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.
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].*")) {//์˜์–ด๊ฐ€ ํฌํ•จ๋˜๋ฉด
System.out.println("has english");
throw new Exception("has english");
}
}

// ๋ฉค๋ฒ„์ˆ˜์™€ ์ตœ๋Œ€ ์ง์ˆ˜ ๋ฐ์ดํ„ฐ๊ฐ€ ์œ ํšจํ•œ์ง€ ๊ฒ€์‚ฌํ•˜๋Š” ํ•จ์ˆ˜์ž…๋‹ˆ๋‹ค. ์œ ํšจํ•˜์ง€ ์•Š๋‹ค๋ฉด ์˜ˆ์™ธ ์ถœ๋ ฅ
public void checkDataValidity(int memberCount, int maximumGroupSize) {
public void checkDataValidity(int memberCount, int maximumGroupSize) throws Exception {
if(memberCount<maximumGroupSize){
throw new Exception("num of members < maximum group size");
}
}

// ๋žœ๋ค ์ง๊ฟ ์ถ”์ฒจํ•˜๋Š” ํ•จ์ˆ˜ ์ž…๋‹ˆ๋‹ค.
//๋ฉค๋ฒ„ ์ˆ˜ 9๋ช…, maximumGroupSize : 3 -> 3๊ฐœ์กฐ
//๋ฉค๋ฒ„ ์ˆ˜ 9๋ช…, maximumGroupSize:4 -> 4*2+1, 4,
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.

์ง์ ‘ ๋žœ๋ค ์ˆซ์ž๋ฅผ ๋ฝ‘์•„ ์„ž์œผ์…จ๊ตฐ์š”!! ๋Œ€๋‹จํ•˜์‹ญ๋‹ˆ๋‹ค!!

ํ•˜์ง€๋งŒ ์ง€๊ธˆ ์ฝ”๋“œ๋Š” ๋งค์šฐ ๋น„ํšจ์œจ ์ ์ž…๋‹ˆ๋‹ค. ๋ฌผ๋ก  ๋ฐ์ดํ„ฐ๊ฐ€ ์ ์œผ๋‹ˆ ๋น ๋ฅด๊ฒŒ ์—ฐ์‚ฐํ•˜๊ฒ ์ง€๋งŒ, ์ด๋ฏธ ์„ ํƒํ•œ ๋ฐ์ดํ„ฐ๊ฐ€ ์•ˆ ๋‚˜์˜ฌ๋•Œ๊นŒ์ง€ ๊ณ„์† ๋žœ๋ค์œผ๋กœ ๋ฝ‘๊ธฐ ๋•Œ๋ฌธ์— ๋ถˆํ•„์š”ํ•œ ์—ฐ์‚ฐ์ด ๋งŽ์Šต๋‹ˆ๋‹ค.

๋•Œ๋ฌธ์— ์•„๋ž˜ ์ฝ”๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์‹œ๋ฉด ์‰ฝ๊ฒŒ ์„ž์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
Collections.shuffle(memberList);

์•„๋ž˜๋Š” ์œ„ ํ•จ์ˆ˜์˜ ๋‚ด๋ถ€ ์ฝ”๋“œ์ž…๋‹ˆ๋‹ค.

๋งŒ์•ฝ ์ง์ ‘ ๊ตฌํ˜„ ํ•˜์‹ ๋‹คํ•˜๋ฉด, ๋žœ๋คํ•œ ์ˆœ์„œ๋กœ ๋ฝ‘๊ธฐ ๋ณด๋‹ค, ๋žœ๋คํ•œ ์œ„์น˜์™€ ๋งจ ๋’ค ์œ„์น˜ ๋ฐ์ดํ„ฐ ๊ต์ฒด๊ฐ€ ๋”์šฑ ํšจ์œจ ์ ์ž…๋‹ˆ๋‹ค.
์ „์ž๋Š” ์ค‘๋ณต์ด ๋‚˜์™€ ๋ฐ˜๋ณต ํšŸ์ˆ˜๋Š” ์˜ˆ์ƒ ๋ชปํ•˜๊ฒŒ ์ปค์ง‘๋‹ˆ๋‹ค, ํ›„์ž๋Š” ๊ทธ์ € ์ •ํ•ด์ง„ ํšŸ์ˆ˜๋งŒํผ ๊ต์ฒด ํ•ฉ๋‹ˆ๋‹ค.

public static void shuffle(List<?> list, Random rnd) {
    int size = list.size();
    if (size < SHUFFLE_THRESHOLD || list instanceof RandomAccess) {
        for (int i=size; i>1; i--)
            swap(list, i-1, rnd.nextInt(i));
    } else {
        Object arr[] = list.toArray();

        // Shuffle array
        for (int i=size; i>1; i--)
            swap(arr, i-1, rnd.nextInt(i));

        // Dump array back into list
        ListIterator it = list.listIterator();
        for (int i=0; i<arr.length; i++) {
            it.next();
            it.set(arr[i]);
        }
    }

}

return new ArrayList<>();
int size = memberList.size();
Random r = new Random();
int index;

Collections.shuffle(memberList); //์ˆœ์„œ ๋žœ๋ค์œผ๋กœ ์„ž๊ธฐ
//randomly select members
//8/2 = 2, 2, 2, 2
// 9/2 = 2, 2, 2, 2, 1
// 8/4 = 4, 4
// 9/4 = 4, 3, 2
// 10/4 = 4, 4, 2 at least 2 people are in a group
int[] groupNum = new int[size/maximumGroupSize+10];
int times;
if (size%maximumGroupSize==0){
times = size/maximumGroupSize; //the number of total groups
}
else{
times = size/maximumGroupSize+1; //the number of total groups
}

//๊ทธ๋ฃน ๋‹น ์ธ์› ์ˆ˜ ์ •ํ•˜๊ธฐ
int left = size;
//get num of members per group
for(int i = 0; i<times; i++){
if(left>=maximumGroupSize){
groupNum[i] = maximumGroupSize;
left-=maximumGroupSize;
}
else{
groupNum[i] = left;
}
}

//at least 2 people in a group
if(left==1 && maximumGroupSize!=1 && maximumGroupSize !=2){
//left alone
groupNum[times-2]--;
groupNum[times-1]++; //change group
}
List<List<String>> result = new ArrayList<>();

//add member names as the num of members with groupnum[i]
int indexOfSelected = 0;
for(int i = 0; i<times; i++){
List<String> innerList = new ArrayList<>();
for(int j = 0; j<groupNum[i]; j++){
innerList.add(memberList.get(indexOfSelected)); //add in the same group
indexOfSelected++;
}// 4 4 4 3 2
result.add(innerList); //add the group to the result
}


return result;
}

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

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.

๊ฒฐ๊ณผ ๋ถ€๋ถ„์„ ์ถœ๋ ฅ์˜ˆ์‹œ์™€ ๋™์ผํ•˜๊ฒŒ ํ•ด๋ณด๋Š”๊ฑด ์–ด๋–จ๊นŒ์š”?

}

public static void main(String[] args) {
public static void main(String[] args) throws Exception {
LeetsMateApplication app = new LeetsMateApplication();
app.run();
}
Expand Down
8 changes: 4 additions & 4 deletions src/test/java/leets/leets_mate/LeetsMateApplicationTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,28 +18,28 @@ void setUp() {
}

@Test
void ์ž…๋ ฅ๋ฐ›์€_๋ฌธ์ž์—ด์„_ํŒŒ์‹ฑํ•˜์—ฌ_๋ฆฌ์ŠคํŠธ๋กœ_๋งŒ๋“ ๋‹ค() {
void parseMembers() {
String members = "๋ฆฌ์ธ ์—,์˜ค์‹ ,๊ฑธ,ํ™˜์˜ํ•ฉ๋‹ˆ๋‹ค";
List<String> actual = app.parseMembers(members);
assertThat(actual).containsExactly("๋ฆฌ์ธ ์—", "์˜ค์‹ ", "๊ฑธ", "ํ™˜์˜ํ•ฉ๋‹ˆ๋‹ค");
}

@Test
void ๋ฉค๋ฒ„์ˆ˜๋ฅผ_๋ฐ˜ํ™˜ํ•œ๋‹ค() {
void memberNumber() {
List<String> members = Arrays.asList("๋ฆฌ์ธ ์—", "์˜ค์‹ ", "๊ฑธ", "ํ™˜์˜ํ•ฉ๋‹ˆ๋‹ค");
int actual = app.memberNumber(members);
assertThat(actual).isEqualTo(4);
}

@Test
void ๋ฉค๋ฒ„์ˆ˜์™€_์ตœ๋Œ€_๋ฉค๋ฒ„์ˆ˜๋ฅผ_์ž˜๋ชป_์ž…๋ ฅํ•œ_๊ฒฝ์šฐ_์˜ˆ์™ธ๋ฅผ_๋ฐ˜ํ™˜ํ•œ๋‹ค() {
void checkHasNoEnglish() {

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.

๋ฐ‘์— ํ•จ์ˆ˜์™€ ์ด๋ฆ„์ด ๋ฐ”๋€๊ฑฐ ๊ฐ™์Šต๋‹ˆ๋‹ค :D

assertThrows(Exception.class, () -> {
app.checkDataValidity(3, 4);
});
}

@Test
void ๋ฉค๋ฒ„_๋ฌธ์ž์—ด์—_์˜์–ด๋ฅผ_์ž…๋ ฅํ•œ_๊ฒฝ์šฐ_์˜ˆ์™ธ๋ฅผ_๋ฐ˜ํ™˜ํ•œ๋‹ค() {
void checkDataValidity() {
assertThrows(Exception.class, () -> {
app.checkHasNoEnglish("welcome,to,leets");
});
Expand Down