-
Notifications
You must be signed in to change notification settings - Fork 14
[BE] ์ ์ํ ๐ฅ ์ค๋์ ์ง๊ฟ์? #13
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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(" ", ""); //๊ณต๋ฐฑ ์ ๊ฑฐ | ||
| 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) { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ์ง์ ๋๋ค ์ซ์๋ฅผ ๋ฝ์ ์์ผ์ จ๊ตฐ์!! ๋๋จํ์ญ๋๋ค!! ํ์ง๋ง ์ง๊ธ ์ฝ๋๋ ๋งค์ฐ ๋นํจ์จ ์ ์ ๋๋ค. ๋ฌผ๋ก ๋ฐ์ดํฐ๊ฐ ์ ์ผ๋ ๋น ๋ฅด๊ฒ ์ฐ์ฐํ๊ฒ ์ง๋ง, ์ด๋ฏธ ์ ํํ ๋ฐ์ดํฐ๊ฐ ์ ๋์ฌ๋๊น์ง ๊ณ์ ๋๋ค์ผ๋ก ๋ฝ๊ธฐ ๋๋ฌธ์ ๋ถํ์ํ ์ฐ์ฐ์ด ๋ง์ต๋๋ค. ๋๋ฌธ์ ์๋ ์ฝ๋๋ฅผ ์ฌ์ฉํ์๋ฉด ์ฝ๊ฒ ์์ ์ ์์ต๋๋ค. ์๋๋ ์ ํจ์์ ๋ด๋ถ ์ฝ๋์ ๋๋ค. ๋ง์ฝ ์ง์ ๊ตฌํ ํ์ ๋คํ๋ฉด, ๋๋คํ ์์๋ก ๋ฝ๊ธฐ ๋ณด๋ค, ๋๋คํ ์์น์ ๋งจ ๋ค ์์น ๋ฐ์ดํฐ ๊ต์ฒด๊ฐ ๋์ฑ ํจ์จ ์ ์
๋๋ค. } |
||
| 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); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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(); | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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() { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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"); | ||
| }); | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
trim()ํจ์๊ฐ ๊ณต๋ฐฑ์ ๊ฑฐ ๋ฌธ์์ด ํจ์์ ๋๋ค! ์จ๋ณด์ ๋ ์ข์๊ฒ ๊ฐ์์~