From 8ec0a525572b9de5ceb7eabeee050852a0deac94 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EC=84=B1=ED=9B=88?= Date: Tue, 15 Oct 2024 16:36:52 +0900 Subject: [PATCH] =?UTF-8?q?2=EC=A3=BC=EC=B0=A8=20=EA=B3=BC=EC=A0=9C=20?= =?UTF-8?q?=EC=A0=9C=EC=B6=9C=ED=95=A9=EB=8B=88=EB=8B=A4.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/PROBLEM1.md | 11 +++++++++++ docs/PROBLEM2.md | 30 +++++++++++++++++++++++++++++ docs/PROBLEM3.md | 31 +++++++++++++++++++++++++++--- docs/PROBLEM4.md | 39 ++++++++++++++++++++++++++++++++++---- docs/PROBLEM5.md | 25 ++++++++++++++++++++---- docs/PROBLEM6.md | 20 +++++++++++++++++--- docs/PROBLEM7.md | 17 ++++++++++++++++- src/problem1.js | 29 +++++++++++++++++++++++++++- src/problem2.js | 15 +++++++++++++++ src/problem3.js | 17 +++++++++++++++++ src/problem4.js | 19 +++++++++++++++++++ src/problem5.js | 13 +++++++++++++ src/problem6.js | 49 ++++++++++++++++++++++++++++++++++++++++++++++-- src/problem7.js | 47 ++++++++++++++++++++++++++++++++++++++++++++++ 14 files changed, 344 insertions(+), 18 deletions(-) diff --git a/docs/PROBLEM1.md b/docs/PROBLEM1.md index 9c369b6..1373002 100644 --- a/docs/PROBLEM1.md +++ b/docs/PROBLEM1.md @@ -23,3 +23,14 @@ | [97, 98] | [197, 198] | 0 | | [131, 132] | [211, 212] | 1 | | [99, 102] | [211, 212] | -1 | + + diff --git a/docs/PROBLEM2.md b/docs/PROBLEM2.md index 7d0acd6..0e54838 100644 --- a/docs/PROBLEM2.md +++ b/docs/PROBLEM2.md @@ -22,3 +22,33 @@ | "browoanoommnaon" | "brown" | | "zyelleyz" | "" | + diff --git a/docs/PROBLEM4.md b/docs/PROBLEM4.md index f2ff9ed..3ce5e87 100644 --- a/docs/PROBLEM4.md +++ b/docs/PROBLEM4.md @@ -4,9 +4,9 @@ 엄마 말씀 word가 매개변수로 주어질 때, 아래 청개구리 사전을 참고해 반대로 변환하여 return 하도록 solution 메서드를 완성하라. -| A | B | C | D | E | F | G | H | I | J | K | L | M | N | O | P | Q | R | S | T | U | V | W | X | Y | Z | +| A | B | C | D | E | F | G | H | I | J | K | L | M | N | O | P | Q | R | S | T | U | V | W | X | Y | Z | | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | -| Z | Y | X | W | V | U | T | S | R | Q | P | O | N | M | L | K | J | I | H | G | F | E | D | C | B | A | +| Z | Y | X | W | V | U | T | S | R | Q | P | O | N | M | L | K | J | I | H | G | F | E | D | C | B | A | ### 제한사항 @@ -16,6 +16,37 @@ ### 실행 결과 예시 -| word | result | -| --- | --- | +| word | result | +| ------------ | ------------ | | "I love you" | "R olev blf" | + +그러면 아스키 코드 또는 배열로 정해놓고 전체에서 빼서 돌리는 것도 있겠다... + +알파벳 : 26개 0부터 25 +0이면 25 = 25 +10이면 15 = 25 +12이면 13 = 25 + +m과 n이 제일 가깝고 서로 바뀜 + +아 n과 25-n의 인덱스 ! + +배열을 푸시해서 만들고 +word의 인덱스를 하나하나 map으로 25-n을 해서 다시 만들고 문자열로 만들면 되겠다! +그리고 소문자로 받았으니 나중에 따로 대문자로 만들어주면 될꺼같은데? + +문자열 => 97~122 니까 아스키 코드로 받은 수를 122에 빼면 인덱스 번호가 될듯? + + 근데 아스키 코드 안쓰니까 너무 복잡하다... + 그냥 아스키 코드 쓰면서 하자 + 아스키 코드로 변환해주는 함수는 charCodeAt() + 아스키 코드를 문자로 변환하는 함수는 fromCharCode() + + 받아와서 charCodeAt() 하고 122-코드번호 하면 처음에 만든 배열에서 + 문자 받아오고 + +// 해결 : 띄어쓰기(3) , 대소문자(2) , 알파벳 역전(1) +// 1.다 바꾼 상태 +// 2.공백 대문자 소문자 +// 3.공백이면 32 그대로 / 소문자면 122 - 인덱스 번호 / 대문자면 122 -인덱스번호 소문자 소문자에 toUpperCase() +// 4.문자열 합치기 diff --git a/docs/PROBLEM5.md b/docs/PROBLEM5.md index d98a1c0..d3a2383 100644 --- a/docs/PROBLEM5.md +++ b/docs/PROBLEM5.md @@ -10,7 +10,24 @@ ### 실행 결과 예시 -| money | result | -| --- | --- | -| 50237 | [1, 0, 0, 0, 0, 2, 0, 3, 7] | -| 15000 | [0, 1, 1, 0, 0, 0, 0, 0, 0] | +| money | result | +| ----- | --------------------------- | +| 50237 | [1, 0, 0, 0, 0, 2, 0, 3, 7] | +| 15000 | [0, 1, 1, 0, 0, 0, 0, 0, 0] | + +9자리 배열 +5만원 0 or 1 , 1만원 +오천원 0 or 1, 천원 +오백원 0 or 1, 백원 +오십원 0 or 1, 십원 +일원 + +수가 크니까 +결국 나머지로 가야해 +50000으로 나누고 그 수 받고 나머지로 다시 실행 +그 나머지를 10000으로 나누고 그 수 받고 나머지로 다시 실행 +.........쭉 + +맵 돌리려면 먼저 나누고 나머지 구해야 하는데...return 때문에 안되네 순서가.. + +돈을 두개로 나누기 diff --git a/docs/PROBLEM6.md b/docs/PROBLEM6.md index 963375a..57d21a1 100644 --- a/docs/PROBLEM6.md +++ b/docs/PROBLEM6.md @@ -4,7 +4,6 @@ 혼란을 막기 위해 크루들의 닉네임 중 **같은 글자가 연속적으로 포함** 될 경우 해당 닉네임 사용을 제한하려 한다. 이를 위해 같은 글자가 연속적으로 포함되는 닉네임을 신청한 크루들에게 알려주는 시스템을 만들려고 한다. - 신청받은 닉네임 중 **같은 글자가 연속적으로 포함** 되는 닉네임을 작성한 지원자의 이메일 목록을 return 하도록 solution 메서드를 완성하라. ### 제한사항 @@ -18,6 +17,21 @@ ### 실행 결과 예시 -| forms | result | -| --- | --- | +| forms | result | +| ----------------------------------------------------------------------------------------------------------------------------------------------------- | --------------------------------------------------- | | [ ["jm@email.com", "제이엠"], ["jason@email.com", "제이슨"], ["woniee@email.com", "워니"], ["mj@email.com", "엠제이"], ["nowm@email.com", "이제엠"] ] | ["jason@email.com", "jm@email.com", "mj@email.com"] | + +같은 글자 두개 이상 잡아내기 +객체 배열 이다 + +이름만 받아서 두개 이상 잡아내고 +해당 인덱스에 있는 이메일 출력 하면 끝 + +result는 이메일에 해당하는 부분의 문자열을 오름차순으로 정렬하고 중복은 제거한다. + +하나의 문자열이 포함된지 한번 필터 +하나의 문자열 + 원하는 문자열 필터 +필터 된 것을 가지고 다시 이메일을 찾는 과정 +=> 배열에서 문자를 다시 가져와서 +객체배열에 다시 필터 +인덱스 번호로 이메일 가져오기 diff --git a/docs/PROBLEM7.md b/docs/PROBLEM7.md index 6bffb3f..75a4baa 100644 --- a/docs/PROBLEM7.md +++ b/docs/PROBLEM7.md @@ -1,7 +1,8 @@ ## 🚀 기능 요구 사항 레벨 2의 팀 프로젝트 미션으로 SNS(Social Networking Service)를 만들고자 하는 팀이 있다. 팀에 속한 크루 중 평소 알고리즘에 관심이 많은 미스터코는 친구 추천 알고리즘을 구현하고자 아래와 같은 규칙을 세웠다. -- 사용자와 함께 아는 친구의 수 = 10점 + +- 사용자와 함께 아는 친구의 수 = 10점 - 사용자의 타임 라인에 방문한 횟수 = 1점 사용자 아이디 user와 친구 관계를 담은 이차원 배열 friends, 사용자 타임 라인 방문 기록 visitors가 매개변수로 주어질 때, 미스터코의 친구 추천 규칙에 따라 점수가 가장 높은 순으로 정렬하여 최대 5명을 return 하도록 solution 메서드를 완성하라. 이때 추천 점수가 0점인 경우 추천하지 않으며, 추천 점수가 같은 경우는 이름순으로 정렬한다. @@ -23,3 +24,17 @@ | user | friends | visitors | result | | ------ | ------------------------------------------------------------------------------------------------------------------------------- | --------------------------------------------- | ------------------------- | | "mrko" | [ ["donut", "andole"], ["donut", "jun"], ["donut", "mrko"], ["shakevan", "andole"], ["shakevan", "jun"], ["shakevan", "mrko"] ] | ["bedi", "bedi", "donut", "bedi", "shakevan"] | ["andole", "jun", "bedi"] | + +// 1.추천 친구 리스트 ( 객체 형식) +// -사용자가 아는 친구 배제 +// 2.추천 함수 +// -함께 아는 친구 + 10 +// -방문친구 + 1 +// 로 각 점수를 넣어줌 (객체) + +// 3.추천 친구 객체보고 순위 정하기 +// -0점 빼고 +// -최대 5명 +// -점수를 오름차순으로 +// -점수가 같으면 이름순 +// 배열로 결과 출력 \ No newline at end of file diff --git a/src/problem1.js b/src/problem1.js index 9a14f2c..3667318 100644 --- a/src/problem1.js +++ b/src/problem1.js @@ -1,5 +1,32 @@ function problem1(pobi, crong) { - var answer; + if (pobi[0] + 1 === pobi[1] && crong[0] + 1 === crong[1]) { + const big = (a) => { + let result1 = 0; + let result2 = 1; + + Array.from(String(a)).forEach((num) => { + result1 += parseInt(num); + result2 *= parseInt(num); + }); + return result1 >= result2 ? result1 : result2; + }; + + const total = (leftRight) => { + return big(leftRight[0]) >= big(leftRight[1]) + ? big(leftRight[0]) + : big(leftRight[1]); + }; + + if (total(pobi) > total(crong)) { + answer = 1; + } else if (total(pobi) < total(crong)) { + answer = 2; + } else { + answer = 0; + } + } else { + answer = -1; + } return answer; } diff --git a/src/problem2.js b/src/problem2.js index cebd07c..ab8ed73 100644 --- a/src/problem2.js +++ b/src/problem2.js @@ -1,5 +1,20 @@ function problem2(cryptogram) { var answer; + let setArray = Array.from(cryptogram); + + for (let i = 0; i < cryptogram.length; i++) { + let newArray; + newArray = setArray.filter((item, index, Aarry) => { + return item !== Aarry[index + 1] && item !== Aarry[index - 1]; + }); + + if (newArray.join("") !== setArray.join("")) { + setArray = newArray; + } else { + answer = newArray.join(""); + return answer; + } + } return answer; } diff --git a/src/problem3.js b/src/problem3.js index 1baed28..e23ff25 100644 --- a/src/problem3.js +++ b/src/problem3.js @@ -1,5 +1,22 @@ function problem3(number) { var answer; + let setArray = []; + + for (let i = 1; i <= number; i++) { + setArray.push(String(i)); + } + + const newArray1 = setArray.filter((num) => { + return num.includes(3) || num.includes(6) || num.includes(9); + }); + + const newArray2 = newArray1.join("").split(""); + + const total = newArray2.filter((num) => { + return num.includes(3) || num.includes(6) || num.includes(9); + }); + + answer = total.length; return answer; } diff --git a/src/problem4.js b/src/problem4.js index ee1d3bd..76aa5c0 100644 --- a/src/problem4.js +++ b/src/problem4.js @@ -1,5 +1,24 @@ function problem4(word) { var answer; + const setArray = Array.from(word); + console.log(setArray); + + const setAlphabat = setArray.map((str) => { + return str.charCodeAt(); + }); + + const newArray = setAlphabat.map((item) => { + if (item < 65) { + return (item = String.fromCharCode(32)); + } else if (65 <= item && item <= 90) { + return (item = String.fromCharCode(122 + 65 - item).toUpperCase()); + } else { + return (item = String.fromCharCode(122 + 97 - item)); + } + }); + + answer = newArray.join(""); + return answer; } diff --git a/src/problem5.js b/src/problem5.js index 9368e87..b7207c4 100644 --- a/src/problem5.js +++ b/src/problem5.js @@ -1,5 +1,18 @@ function problem5(money) { var answer; + + const setMoney = [50000, 10000, 5000, 1000, 500, 100, 50, 10, 1]; + + let calMoney = money; + const cal = setMoney.map((mon) => { + let moneyDivide = calMoney; + let moneyRemain = calMoney; + calMoney = moneyDivide % mon; + return parseInt(moneyRemain / mon); + }); + + answer = cal; + return answer; } diff --git a/src/problem6.js b/src/problem6.js index 3f842b5..8e00242 100644 --- a/src/problem6.js +++ b/src/problem6.js @@ -1,6 +1,51 @@ function problem6(forms) { - var answer; - return answer; + var answer = []; + // 배열에서 닉네임만 빼오기 + const filtering1 = forms.map((word) => { + return word[1]; + }); + + const filtering2 = String(filtering1) + .split("") + .map((item, index, array) => { + if (item !== "," && array.includes(item, index + 1)) { + return `${item}${array[index + 1]}`; + } + }); + + const filtering3 = filtering2.filter((item) => { + return item !== undefined && !item.includes(","); + }); + + const set = [...new Set(filtering3)]; + + const pairing = set.filter((pair) => { + const dupl = filtering1.filter((word) => word.includes(pair)); + return dupl.length > 1; + }); + + const filtering4 = pairing.map((word) => { + const check = []; + for (let i = 0; i < filtering1.length; i++) { + if (filtering1[i].includes(word)) { + check.push(i); + } + } + return check; + }); + + const final = [...new Set(filtering4.flat())]; + console.log(final); + const email = forms.map((word) => { + return word[0]; + }); + + for (let i = 0; i < final.length; i++) { + answer.push(email[final[i]]); + } + console.log(email); + + return answer.sort(); } module.exports = problem6; diff --git a/src/problem7.js b/src/problem7.js index ee1bb9d..14f3c33 100644 --- a/src/problem7.js +++ b/src/problem7.js @@ -1,5 +1,52 @@ function problem7(user, friends, visitors) { var answer; + const setArray = [...new Set([...friends.flat(), ...visitors])]; + //모든 사람 모아서 중첩 삭제 + const filtering = friends.flatMap((item) => { + if (item[0] == "mrko") { + return item[1]; + } + if (item[1] == "mrko") { + return item[0]; + } + return []; + }); + filtering.push(user); + + const filteringfriends = setArray.filter((item) => !filtering.includes(item)); + console.log(filteringfriends); + + const friendsObj = Object.fromEntries( + filteringfriends.map((key) => [key, 0]) + ); + + ///////1번 완 + filtering.splice(filtering.length - 1, 1); + + filteringfriends.forEach((item) => { + filtering.forEach((friend) => { + if ( + friends.some( + ([person1, person2]) => + (person1 === item && person2 === friend) || + (person1 === friend && person2 === item) + ) + ) { + friendsObj[item] += 10; + } + }); + }); + visitors.forEach((visitor) => { + if (friendsObj.hasOwnProperty(visitor)) { + friendsObj[visitor] += 1; + } + }); + + const score = Object.entries(friendsObj).filter(([, score]) => score > 0); + const finalFriends = score + .sort((a, b) => b[1] - a[1] || a[0].localeCompare(b[0])) + .map(([name]) => name); + answer = finalFriends; return answer; }