Skip to content

[1주차][그리디][동동] 학습한 내용 정리입니다 - #13

Merged
bigsaigon333 merged 21 commits into
fe-backpackers:bigsaigon333from
bigsaigon333:greedy
Oct 5, 2021
Merged

[1주차][그리디][동동] 학습한 내용 정리입니다#13
bigsaigon333 merged 21 commits into
fe-backpackers:bigsaigon333from
bigsaigon333:greedy

Conversation

@bigsaigon333

@bigsaigon333 bigsaigon333 commented Sep 30, 2021

Copy link
Copy Markdown
Contributor
주차 알고리즘 정리한 내용 링크
1주차 그리디 그리디 알고리즘의 접근법을 정리하였습니다 링크
1주차 그리디 이코테 03장 그리디 예제/실전문제를 풀었습니다 링크
1주차 그리디 반복적으로 사용되는 유틸함수들을 lib 에 정리하였습니다 링크
1주차 그리디 이코테 11장 그리디 문제를 풀었습니다 링크

@Sunmon

Sunmon commented Sep 30, 2021

Copy link
Copy Markdown

안녕하세요 동동님!
작성하신 코드에 보면 아래처럼 input을 받으시던데 질문이 있어서요~

const input = require("fs").readFileSync(0, "utf-8").trim().split("\n");

readFileSync가 파일을 읽는 함수더라구요, 그러면 이름이 '0'인 파일을 읽으신건가요??
코드를 복사해서 실행해봤는데 오류만 떠서 질문드립니다 ㅠㅠ

@bigsaigon333

bigsaigon333 commented Sep 30, 2021

Copy link
Copy Markdown
Contributor Author

@Sunmon 안녕하세요~ 안 그래도 제가 그거 관련해서 지금 정리하고 있었는데요~!

정리 다하고 나면 PR 본문에 링크 달아둘게요 ㅎㅎ..

(링크: 이건 제가 전에 쓴 블로그인데 참고 목적으로 남겨둡니다 ㅎㅎ)


여기서 0은 filename 이 아니라 file descriptor인데요 linux 계열에서는 standard input 인 /dev/stdin 을 나타냅니다.

즉, Node.js로 콘솔에서 실행시키고 나서 콘솔창에 직접 입력값을 넣어줘야해요

번거로울 수 있지만, BOJ에서 문제 풀다보니 저는 이게 익숙해져서 편하더라구요!

fs_readfilesync_path_options

@bigsaigon333
bigsaigon333 changed the base branch from main to bigsaigon333 October 3, 2021 04:32
Comment thread src/js-for-coding-test/PART-03/11-그리디-문제/03.js

@moonheekim0118 moonheekim0118 left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

동동님 ! 코드가 너무 깔끔하고 멋져요 👍 👍 👍 많이 배워갑니다 !! 저도 정리해놓으신거 잘 참고해서 공부하겠습니다 😄 감사합니다ㅎㅎ

Comment thread src/js-for-coding-test/PART-03/11-그리디-문제/03.js
Comment thread src/js-for-coding-test/PART-03/11-그리디-문제/03.js
Comment on lines 7 to 8

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Number 라고만 해도 숫자형으로 바뀌는거 처음알았어요.. 많이 배워갑니다!!

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

@moonheekim0118

Number는 원시 타입을 인자로 받아서 Number 타입으로 바꿔주는 함수에요

Array.prototype.map 는 매개변수로 배열의 각 요소에 적용할 콜백함수를 받는데, Number 함수를 전달하여 모두 Number타입으로 변환하였습니다 👍

.map(el => Number(el) 이렇게 쓰는거랑 동일하지만, 굳이 익명함수를 또 만들필요가 없는거 같아서 저는 이렇게 간결하게 쓰는걸 선호하고 있어요 😄

Comment on lines 7 to 8

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

reduce를 정말 잘쓰시네요.. 👍 👍

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

감사합니다~~ reduce가 어떨때는 가독성을 헤치기도 하지만, 이런 간단한 거는 reduce가 더 읽기 편하고 좋더라구요 👍

Comment on lines 6 to 9

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

이렇게 함수 분리해놓은거 인상깊어요 👍

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

@moonheekim0118

한때 함수형 프로그래밍에 꽂혀서 강의도 듣고 책도 보고 했는데...(오늘 아침에 운동 갔다 오면서 오랜만에 다시 들었습니다 ㅎㅎ)

그때의 함수형 프로그래밍의 영향으로 순수함수로 많이 작성하려고 하는거 같아요.

PS 에서 복잡성을 올리는 요소가 바로 사이드 이펙트라고 생각하거든요.
순수함수로 로직을 분리해내면, 데이터의 흐름이 더 잘 읽힌다고 생각합니다 😄

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

와 ㅎㅎ 저 for문 설명 해주실수 있나요? 이런 형태를 본적이 있는 것 같은데, 어떻게 로직이 진행되는질 잘 몰라서요 ㅠ 배우고 싶습니다

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

@sooster910

음.. for문의 흐름을 말씀하시는 걸까요~?

for문으로 썼지만... 사실 while문으로 쓰는게 더 맞는거 같네요 💦

let count = 0;
let i = 0;
// count가 k 보다 작은 동안 반복
while(count < k) {
   // food_times[i]가 0보다 크면 food_times[i]를 하나 감소시키고 count를 하나 증가시킨다.
    if (food_times[i] > 0) {
      food_times[i]--;
      count++;
    }
  // i 가 1 증가한다(food_times의 끝에 도달한 경우에는 0으로 된다)
  i = next(i, food_times);
}

정말 순진하게 그리디로만 풀어서, 효율성을 통과를 못했어요.. ㅠㅠ

리밋님꺼 보고 어떻게 푸는지 감은 잡았는데, 잘 안되네요 ㅠ 버퍼 기간에 다시 풀어봐야겠습니다!

Comment thread lib/index.js
export const Array3D = (X, Y, Z, value = 0) =>
Array(X)
.fill(0)
.map(() => Array2D(Y, Z, value));

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

자주 쓰는 함수는 이렇게 분리해두어도 좋은 방법 인 것 같습니다! 👍

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

@sooster910 이번 알고리즘 스터디의 주요 목표중 하나죠! 나만의 library 만들기~!

이번주 월요일에 코테 하나 쳤는데, IDE 사용 못하고 웹캠 및 휴대폰 카메라로 감시당해서, 써보지는 못했습니다만... 😭

@bigsaigon333
bigsaigon333 merged commit b7d3164 into fe-backpackers:bigsaigon333 Oct 5, 2021
@bigsaigon333
bigsaigon333 deleted the greedy branch October 5, 2021 15:42
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.

4 participants