-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
900 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
83 changes: 83 additions & 0 deletions
83
_posts/development/algorithm/lv0/2024-02-27-algorithm-lv0-이어-붙인-수.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
--- | ||
layout: post | ||
title: '[프로그래머스 / lv0] 이어 붙인 수' | ||
subtitle: '[프로그래머스 / lv0] 이어 붙인 수' | ||
category: dev | ||
tag: algorithm | ||
image: | ||
path: /assets/img/algorithm.png | ||
--- | ||
|
||
<!-- prettier-ignore --> | ||
* this ordered seed list will be replaced by the toc | ||
{:toc} | ||
|
||
## 문제 | ||
|
||
--- | ||
|
||
### **문제 설명** | ||
|
||
--- | ||
|
||
정수가 담긴 리스트 `num_list`가 주어집니다. `num_list`의 홀수만 순서대로 이어 붙인 수와 짝수만 순서대로 이어 붙인 수의 합을 return하도록 solution 함수를 완성해주세요. | ||
|
||
### 제한사항 | ||
|
||
--- | ||
|
||
- 2 ≤ `num_list`의 길이 ≤ 10 | ||
- 1 ≤ `num_list`의 원소 ≤ 9 | ||
- `num_list`에는 적어도 한 개씩의 짝수와 홀수가 있습니다. | ||
|
||
### 입출력 예 | ||
|
||
--- | ||
|
||
| num_list | result | | ||
| --------------- | ------ | | ||
| [3, 4, 5, 2, 1] | 393 | | ||
| [5, 7, 8, 3] | 581 | | ||
|
||
### 입출력 예 설명 | ||
|
||
--- | ||
|
||
입출력 예 #1 | ||
|
||
- 홀수만 이어 붙인 수는 351이고 짝수만 이어 붙인 수는 42입니다. 두 수의 합은 393입니다. | ||
|
||
입출력 예 #2 | ||
|
||
- 홀수만 이어 붙인 수는 573이고 짝수만 이어 붙인 수는 8입니다. 두 수의 합은 581입니다. | ||
|
||
### 풀이 | ||
|
||
--- | ||
|
||
```jsx | ||
function solution(num_list) { | ||
const { even, odd } = num_list.reduce( | ||
(acc, cur) => { | ||
let curEven = ''; | ||
let curOdd = ''; | ||
if (cur % 2 === 0) { | ||
curEven = cur.toString(); | ||
} else { | ||
curOdd = cur.toString(); | ||
} | ||
return { even: acc.even + curEven, odd: acc.odd + curOdd }; | ||
}, | ||
{ even: '', odd: '' } | ||
); | ||
|
||
const answer = Number(even) + Number(odd); | ||
return answer; | ||
} | ||
``` | ||
|
||
### 느낀점 | ||
|
||
--- | ||
|
||
reduce를 사용해 구조분해 할당으로 짝수, 홀수를 받아서 사용하면 좀 더 가독성이 높지 않을까라는 생각을하여 해당 방법으로 코드를 작성했다. |
68 changes: 68 additions & 0 deletions
68
_posts/development/algorithm/lv0/2024-02-27-algorithm-lv0-flag에-따라-다른-값-반환하기.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
--- | ||
layout: post | ||
title: '[프로그래머스 / lv0] flag에 따라 다른 값 반환하기' | ||
subtitle: '[프로그래머스 / lv0] flag에 따라 다른 값 반환하기' | ||
category: dev | ||
tag: algorithm | ||
image: | ||
path: /assets/img/algorithm.png | ||
--- | ||
|
||
<!-- prettier-ignore --> | ||
* this ordered seed list will be replaced by the toc | ||
{:toc} | ||
|
||
## 문제 | ||
|
||
--- | ||
|
||
### **문제 설명** | ||
|
||
--- | ||
|
||
두 정수 `a`, `b`와 boolean 변수 `flag`가 매개변수로 주어질 때, `flag`가 true면 `a` + `b`를 false면 `a` - `b`를 return 하는 solution 함수를 작성해 주세요. | ||
|
||
### 제한사항 | ||
|
||
--- | ||
|
||
- 1,000 ≤ `a`, `b` ≤ 1,000 | ||
|
||
### 입출력 예 | ||
|
||
--- | ||
|
||
| a | b | flag | result | | ||
| --- | --- | ----- | ------ | | ||
| -4 | 7 | true | 3 | | ||
| -4 | 7 | false | -11 | | ||
|
||
### 입출력 예 | ||
|
||
--- | ||
|
||
입출력 예 #1 | ||
|
||
- 예제 1번에서 `flag`가 true이므로 `a` + `b` = (-4) + 7 = 3을 return 합니다. | ||
|
||
입출력 예 #2 | ||
|
||
- 예제 2번에서 `flag`가 false이므로 `a` - `b` = (-4) - 7 = -11을 return 합니다. | ||
|
||
### 풀이 | ||
|
||
--- | ||
|
||
```jsx | ||
function solution(a, b, flag) { | ||
const sum = a + b; | ||
const sub = a - b; | ||
return flag ? sum : sub; | ||
} | ||
``` | ||
|
||
### 느낀점 | ||
|
||
--- | ||
|
||
바로 삼항연산자를쓰고 계산식을 변수 선언안하고 사용할 수 있었지만 나는 그래도 변수로 빼서 하는게 가독성이 좋다고 생각해서 따로 빼서 삼항연산자를 썼다. |
72 changes: 72 additions & 0 deletions
72
_posts/development/algorithm/lv0/2024-02-27-algorithm-lv0-n번째-원소까지.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
--- | ||
layout: post | ||
title: '[프로그래머스 / lv0] n번째 원소까지' | ||
subtitle: '[프로그래머스 / lv0] n번째 원소까지' | ||
category: dev | ||
tag: algorithm | ||
image: | ||
path: /assets/img/algorithm.png | ||
--- | ||
|
||
<!-- prettier-ignore --> | ||
* this ordered seed list will be replaced by the toc | ||
{:toc} | ||
|
||
## 문제 | ||
|
||
--- | ||
|
||
### **문제 설명** | ||
|
||
--- | ||
|
||
정수 리스트 `num_list`와 정수 `n`이 주어질 때, `num_list`의 첫 번째 원소부터 `n` 번째 원소까지의 모든 원소를 담은 리스트를 return하도록 solution 함수를 완성해주세요. | ||
|
||
### 제한사항 | ||
|
||
- 2 ≤ `num_list`의 길이 ≤ 30 | ||
- 1 ≤ `num_list`의 원소 ≤ 9 | ||
- 1 ≤ `n` ≤ `num_list`의 길이 \_\_\_ | ||
|
||
### 입출력 예 | ||
|
||
--- | ||
|
||
| num_list | n | result | | ||
| --------------- | --- | --------- | | ||
| [2, 1, 6] | 1 | [2] | | ||
| [5, 2, 1, 7, 5] | 3 | [5, 2, 1] | | ||
|
||
### 입출력 예 설명 | ||
|
||
--- | ||
|
||
입출력 예 #1 | ||
|
||
- [2, 1, 6]의 첫 번째 원소부터 첫 번째 원소까지의 모든 원소는 [2]입니다. | ||
|
||
입출력 예 #2 | ||
|
||
- [5, 2, 1, 7, 5]의 첫 번째 원소부터 세 번째 원소까지의 모든 원소는 [5, 2, 1]입니다. | ||
|
||
### 풀이 | ||
|
||
--- | ||
|
||
```jsx | ||
function solution(num_list, n) { | ||
const answer = num_list.reduce((acc, cur, idx) => { | ||
if (idx < n) { | ||
return [...acc, cur]; | ||
} | ||
return acc; | ||
}, []); | ||
return answer; | ||
} | ||
``` | ||
|
||
### 느낀점 | ||
|
||
--- | ||
|
||
다른 사람의 풀이를 보니 slice를 사용하던데 나는 reduce를 사용해서 너무 빙빙 돌아서 푼게 아닌가라는 생각이 든다. |
50 changes: 50 additions & 0 deletions
50
_posts/development/algorithm/lv0/2024-02-27-algorithm-lv0-대문자로-바꾸기.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
--- | ||
layout: post | ||
title: '[프로그래머스 / lv0] 대문자로 바꾸기' | ||
subtitle: '[프로그래머스 / lv0] 대문자로 바꾸기' | ||
category: dev | ||
tag: algorithm | ||
image: | ||
path: /assets/img/algorithm.png | ||
--- | ||
|
||
<!-- prettier-ignore --> | ||
* this ordered seed list will be replaced by the toc | ||
{:toc} | ||
|
||
## 문제 | ||
|
||
--- | ||
|
||
### **문제 설명** | ||
|
||
--- | ||
|
||
알파벳으로 이루어진 문자열 `myString`이 주어집니다. 모든 알파벳을 대문자로 변환하여 return 하는 solution 함수를 완성해 주세요. | ||
|
||
### 제한사항 | ||
|
||
--- | ||
|
||
- 1 ≤ `myString`의 길이 ≤ 100,000 | ||
- `myString`은 알파벳으로 이루어진 문자열입니다. | ||
|
||
### 입출력 예 | ||
|
||
--- | ||
|
||
### 풀이 | ||
|
||
--- | ||
|
||
```jsx | ||
function solution(myString) { | ||
return myString.toUpperCase(); | ||
} | ||
``` | ||
|
||
### 느낀점 | ||
|
||
--- | ||
|
||
현업에서 toUpperCase()나 toLowerCase()를 종종 사용하였기 때문에 문제를 보는 순간 방법이 바로 떠올랐다. |
Oops, something went wrong.