Skip to content

Comments

[unn04012] 25.01.13 #23

Merged
JooKangsan merged 10 commits intomainfrom
unn04012
Jan 16, 2025
Merged

[unn04012] 25.01.13 #23
JooKangsan merged 10 commits intomainfrom
unn04012

Conversation

@unn04012
Copy link
Collaborator

📌 푼 문제

난이도 문제 이름 문제 링크
초급 문자열 뒤집기 (https://school.programmers.co.kr/learn/courses/30/lessons/120822)
초급 컨트롤 제트 (https://school.programmers.co.kr/learn/courses/30/lessons/120853)
초급 영어가 싫어요 (https://school.programmers.co.kr/learn/courses/30/lessons/120894)
초급 문자열 계산하기 (https://school.programmers.co.kr/learn/courses/30/lessons/120902)
중급 크레인 인형뽑기 게임 (https://school.programmers.co.kr/learn/courses/30/lessons/64061)

📝 간단한 풀이 과정

문자열뒤집기

주어진 문자열을 배열로 변환하여 뒤집은 후 join메서드로 다시 문자열로 만들었습니다.

function solution(my_string) {
  return my_string.split('').reverse().join('');
}

컨트롤 제트

  1. �입력값을 공백을 기준으로 배열로 생성
  2. 생성된 배열을 순회하면서 z일 경우에는 stack에서 제거
  3. z가 아닐 경우에는 스택에 push합니다.
function solution(s) {
  const stack = [];
  let answer = 0;

  s.split(' ').forEach((e) => {
    // 숫자가 아니면
    if (Number.isNaN(Number(e))) {
      answer -= stack.pop();
    } else {
      const num = Number(e);
      answer += num;
      stack.push(num);
    }
  });

  return answer;
}

영어가 싫어요

hash를 이용하여 푸는 방법말고는 떠오르지 않아서 어떤 방법으로 스택을 활용할지는 모르곘습니다..!

function solution(numbers) {
  let answer = numbers;
  const englishObj = {
    zero: 0,
    one: 1,
    two: 2,
    three: 3,
    four: 4,
    five: 5,
    six: 6,
    seven: 7,
    eight: 8,
    nine: 9,
  };

  for (const [key, value] of Object.entries(englishObj)) {
    if (numbers.includes(key)) {
      answer = answer.replaceAll(key, value);
    }
  }

  return Number(answer);
}

문자열 계산하기

공백을 기준으로 나누었으며 스택 push, pop을 활용하는데 초점을 두어서 reverse()메서드를 활용함으로써 연산순서를 맞추었습니다

function solution(my_string) {
  let answer = 0;

  const expression = my_string.split(' ').reverse();

  while (expression.length) {
    const element = expression.pop();

    if (element === '+') {
      answer += Number(expression.pop());
      expression.push(answer);
    } else if (element === '-') {
      answer -= Number(expression.pop());
      expression.push(answer);
    } else {
      // 숫자인 경우
      answer = Number(element);
    }
  }

  return answer;
}

크레인 인형 뽑기 게임

  • 열 방향으로 �순회하며 크레인 동작을 구현했습니다.
  • 스택에 넣기전에 동일한 인형인지 확인을 했습니다.
function solution(board, moves) {
  let answer = 0;
  const stack = [];
  const width = board.length;

  const pullCrane = (order) => {
    const w = order - 1;
    let h = 0;

    while (width > h) {
      const doll = board[h][w];
      if (doll !== 0) {
        if (stack[stack.length - 1] === doll) {
          stack.pop();
          answer += 2;
        } else {
          stack.push(board[h][w]);
        }

        board[h][w] = 0;
        break;
      }
      h++;
    }
  };

  moves.forEach((e) => pullCrane(e));

  return answer;
}

Copy link
Collaborator

@JustDevRae JustDevRae left a comment

Choose a reason for hiding this comment

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

수고 많으셨습니다. 작성하신 코드들이 간결하고 직관적이여서 리뷰하기 편했습니다!

Comment on lines +16 to +20
for (const [key, value] of Object.entries(englishObj)) {
if (numbers.includes(key)) {
answer = answer.replaceAll(key, value);
}
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

오 해쉬로도 풀 수 있군요.. 배워갑니다!

@JooKangsan JooKangsan requested review from jiyeeeah and removed request for geniusgo January 15, 2025 08:08
@JooKangsan JooKangsan merged commit 8e545d2 into main Jan 16, 2025
4 checks passed
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.

3 participants