-
항해 99 5기 TIL_39항해 99 2022. 2. 18. 04:05
▶ Today I Learned
<알고리즘>
[k번째 수]
소요 시간: 1시간 20분
해결 여부: 해결
https://programmers.co.kr/learn/courses/30/lessons/42748?language=javascript
/*k번째 수 i번째 숫자부터 j번째 숫자까지 자르고 정렬했을 때, k번째에 있는 수를 구하려 합니다. 배열 array, [i, j, k]를 원소로 가진 2차원 배열 commands가 매개변수로 주어질 때, commands의 모든 원소에 대해 앞서 설명한 연산을 적용했을 때 나온 결과를 배열에 담아 return 하도록 solution 함수를 작성해주세요. commands의 각 원소는 길이가 3입니다. */ let array = [1, 5, 2, 6, 3, 7, 4] let commands = [[2, 5, 3], [4, 4, 1], [1, 7, 3]] let answer = [] let ci = 0 let i = 0 let j = 0 let k = 0 while (ci < commands.length){ i = commands[ci][0] j = commands[ci][1] k = commands[ci][2] let d = [] console.log('i번부터 j번까지 자른 배열', array.slice(i-1, j)) let slicedArray = array.slice(i-1, j) let a = 0 for (a ; a < slicedArray.length ; a++){ if (slicedArray[a] > slicedArray[a+1]) { d = slicedArray[a] slicedArray[a] = slicedArray[a+1] slicedArray[a+1] = d a = -1 // 서로의 앞뒤만 고려하는 것이 아닌 전체 수 내에서의 대소를 비교하기 위해 앞뒤 비교후 인덱스를 -1로 지정해주어 다시 시작 } } console.log('slicedArray를 내림차순으로 정렬', slicedArray) console.log('거기서 k번째 숫자', slicedArray[k-1]) answer.push(slicedArray[k-1]) ci++ } console.log(answer)
sort()를 이용하면 더 간단히 풀 수 있었지만 쓰지 않고 구현해보고 싶었다.
과거 '내림차순' 문제의 풀이법을 상기하여 풀어냈다.
뿌듯쓰 :)
[구명보트]
소요 시간: 1시간
해결 여부: 미해결
https://programmers.co.kr/learn/courses/30/lessons/42885
let people = [70, 50, 80, 50, 50, 70] let limit = 100 // 예상 결과값 3 // 실제로 사람을 구한다면? // 무인도에서 우선 몸무게 별로 정렬하라고 함 (근데 몸무게는 실수일 수도 있음) // 그다음 최댓값과 최솟값을 구하고 그 둘을 더한 게 제한 아래면 ok, // 아냐 연산이 너무 많이 걸릴거 같아, 그 둘을 더한게 제한 아래가 아니면 또 답이 없음.. // 그럼 정렬 해놓고 중간에서 부터 연산해야 하나??? //그것도 아닌데 // Fact) 일단 리밋이 1명의 최대 몸무게 이상이니까 people의 몸무게는 100 이하 // 그럼 최소 몸무게를 구해서?? // 아니지 이거 몸무게 전체 총합 때린다음 limit으로 나누면 안되나?? /* let total = people.reduce((a, b) => a + b) console.log(Math.ceil(total/limit)) 그러나 사람 몸을 애초에 자유롭게 나눌 수가 없다. 그래서 이런 방식은 옳지 않으며 아래와 같은 예외가 있다. people이 [70, 50, 80, 50, 50, 70] 인 경우 연산결과 4, 필요 보트수는 실제로 최소 6 */ let i = 0 let count = 0 let peopleDes = people.sort((a,b) => a - b) // 내림 차순 정렬 console.log(peopleDes) let total = [] while(i < people.length) { console.log(total) if(total.reduce((a,b) => a + b, 0) <= limit) { total.push(people[i]) i++ } else { console.log(total, '빠지기 전') total.pop() console.log(total, '빠지고 난 후') count++ total = [] i-- console.log(i) } } console.log(count)
<홈페이지 구현>
이미지를 업로드 하기 위해 multer를 사용하다가 모르는 부분을 발견했다.
const fileFilter = (req, file, cb) => { const ext = path.extname(file.originalname); if ( ext !== ".jpg" && ext !== ".jpeg" && ext !== ".png" && ext !== ".gif" && ext !== ".jfif" ) return cb({ message: "이미지 파일만 전송가능합니다." }, false); cb(null, true); };
여기서 path.extname은 무엇일까?
구글링 해보니 확장자 명을 표시해주는 것이었다.
그래서 이미지 파일인지 아닌지를 &&가 이어진 식으로 확인하는 것이다.
https://www.w3schools.com/nodejs/met_path_extname.asp
▶ 느낀 점
또 하나의 프로젝트가 끝났다. 배포과정에서 에러가 꽤 발생했고 다같이 해결하고자 했지만 몇 시간 만에 다 하기에는
역부족이었던 듯 하다. 비록 완벽하게 마무리 짓지는 못했지만
팀원들과 열과 성을 다해 노력해본 경험은 분명 값진 것이었고 앞으로의 성장에 좋은 밑거름이 될 것이다.
다가오는 또 다른 주차도 힘내서 해보도록 하자 :)
+ 렛츠 미라클 모닝 ;)
▶ 공부 시 참고 링크들
https://nodejs.org/api/path.html#pathextnamepath
백엔드 보안을 위해 프록시 서버로 설정하는 것 또는 아래 방법 이용 추천 (백엔드 멘토님)
추후 보안을 위해 axios 사용 시 활용하면 좋은 기능 (프론트 멘토님 추천)
https://axios-http.com/docs/interceptors
https://mobicon.tistory.com/302
'항해 99' 카테고리의 다른 글
항해 99 5기 TIL_41 (0) 2022.02.20 항해 99 5기 TIL_40 (0) 2022.02.19 항해 99 5기 TIL_38 (0) 2022.02.17 항해 99 5기 TIL_37 (0) 2022.02.16 항해 99 5기 TIL_36 (0) 2022.02.15