Skip to content
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

dsa-week1-assignment #1

Closed
wants to merge 6 commits into from
Closed

dsa-week1-assignment #1

wants to merge 6 commits into from

Conversation

hayasha
Copy link

@hayasha hayasha commented Aug 8, 2023

준비해주신 깃 관련 문서들을 읽었는데 이렇게 하는 것이 맞는지 잘 모르겠습니다.
감사합니다.

@hannut91
Copy link
Contributor

hannut91 commented Aug 8, 2023

맞게 잘 제출해 주셨습니다 ㅎㅎ 고생하셨어요

Copy link
Contributor

@hannut91 hannut91 left a comment

Choose a reason for hiding this comment

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

고생 많으셨습니다. 이번 한주를 통해서 무엇을 배우셨나요? 여기에 간단하게 적어주셔도 좋고, 코드숨 주간회고 채널에 회고를 작성해 주세요

@@ -1,18 +1,35 @@
const solution = (numbers) => {
const solution = (numbers) => (numbers.length ? numbers.reduce((acc, curr) => acc + curr) : 0);
Copy link
Contributor

Choose a reason for hiding this comment

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

꼬리재귀는 reduce로 변환이 가능하다는 것을 이용해서 해결해 주셨네요.

예를들어 코틀린 같은 다른 프로그래밍 언어에서는 배열에 가장 앞에 있는 값으로 reduce를 하는 것과
초기값이 주어지고 reduce를 하는 것을 fold로 구분합니다.
자바스크립트는 기본값을 주어지면 fold, 아니면 reduce로 동작하는데요. reduce는 빈 배열일 경우 에러가 발생합니다.
그래서 배열이 비어있는지 확인하신 것 같아요.

fold를 이용하면 빈 배열일 때도 기본값을 사용하도록 할 수 있어요.

const solution = (numbers) => numbers.reduce((acc, curr) => acc + curr, 0);

주로 fold는 타입이 달라질 때 사용해요. 지금은 같은 타입인 숫자지만, 기본 값으로 아무거나 들어갈 수 있으니까요. 이 차이점을 알아두시면 좋을 것 같습니다.

Comment on lines +3 to 11
const solution2 = (numbers) => {
let acc = 0;

while (numbers.length > 0) {
acc += numbers.pop();
}

return acc;
};
Copy link
Contributor

Choose a reason for hiding this comment

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

numbers목록에서 하나씩 제거해 가면서 값을 더하셨네요.

pop메서드는 주어진 배열 자체를 수정하는 것으로, 만약에 이 solution2에 전달한 numbers를 다른 곳에서 사용하고 있다면
값이 변경되어 당황스러운 일이 발생할 수 있어요.

따라서 numbers를 고치지 않고 값을 참조만 하는게 좋아보입니다.

const solution2 = (numbers) => {
  let index = 0;
  let acc = 0;

  while (index < numbers.length) {
    acc += numbers[index];
    index++;
  }

  return acc;
};

Comment on lines +1 to +8
const solution = (binNum) => binNum
.split('')
.reverse()
.map((x, idx) => {
if (x === '0') { return 0; }
return (2 ** idx);
})
.reduce((acc, curr) => acc + curr);
Copy link
Contributor

Choose a reason for hiding this comment

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

binNum
  .split('')
  .map((it, index) => it === '0' ? 0 : (2 ** (binNum.length - 1 - index)))
  .reduce((acc, cur) => acc + cur);

앞에서부터 차례대로 하도록 하면 reverse로 뒤집지 않아도 해결할 수 있어요

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.

2 participants