-
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.
[Docs] 20240228 programers coding test level 0
- Loading branch information
Showing
3 changed files
with
221 additions
and
0 deletions.
There are no files selected for viewing
75 changes: 75 additions & 0 deletions
75
_posts/development/algorithm/lv0/2024-03-06-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,75 @@ | ||
--- | ||
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`가 주어질 때, 첫 번째로 나오는 음수의 인덱스를 return하도록 solution 함수를 완성해주세요. 음수가 없다면 -1을 return합니다. | ||
|
||
### 제한사항 | ||
|
||
--- | ||
|
||
- 5 ≤ `num_list`의 길이 ≤ 100 | ||
- 10 ≤ `num_list`의 원소 ≤ 100 | ||
|
||
### 입출력 예 | ||
|
||
--- | ||
|
||
| num_list | result | | ||
| --------------------------- | ------ | | ||
| [12, 4, 15, 46, 38, -2, 15] | 5 | | ||
| [13, 22, 53, 24, 15, 6] | -1 | | ||
|
||
### 입출력 예 설명 | ||
|
||
--- | ||
|
||
입출력 예 #1 | ||
|
||
- 5번 인덱스에서 음수가 처음 등장하므로 5를 return합니다. | ||
|
||
입출력 예 #2 | ||
|
||
- 음수가 없으므로 -1을 return합니다. | ||
|
||
### 풀이 | ||
|
||
--- | ||
|
||
```jsx | ||
function solution(num_list) { | ||
const answer = num_list.reduce( | ||
(acc, cur, idx) => { | ||
if (cur < 0 && !acc.isNegative) { | ||
return (acc = { findIdx: idx, isNegative: true }); | ||
} | ||
return acc; | ||
}, | ||
{ findIdx: -1, isNegative: false } | ||
); | ||
|
||
return answer.findIdx; | ||
} | ||
``` | ||
|
||
### 느낀점 | ||
|
||
--- | ||
|
||
생각보다 나는 시간이 많이 걸렸다. 무조건 reduce로 해결해야겠다고 생각을 해서 어떻게 하지 고민을 하다가 결국 초기 값을 만들어서 해결했으나 뭔가 빙빙 돌아서 푼거같다고 생각이 든다. javascript method중에 findIndex도 있었으나 나는 생각이 나지 않았다. 다음에는 사용할 수 있을꺼 같다고 생각을 한다. |
73 changes: 73 additions & 0 deletions
73
_posts/development/algorithm/lv0/2024-03-06-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,73 @@ | ||
--- | ||
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} | ||
|
||
## 문제 | ||
|
||
### **문제 설명** | ||
|
||
--- | ||
|
||
연산 ⊕는 두 정수에 대한 연산으로 두 정수를 붙여서 쓴 값을 반환합니다. 예를 들면 다음과 같습니다. | ||
|
||
- 12 ⊕ 3 = 123 | ||
- 3 ⊕ 12 = 312 | ||
|
||
양의 정수 `a`와 `b`가 주어졌을 때, `a` ⊕ `b`와 `b` ⊕ `a` 중 더 큰 값을 return 하는 solution 함수를 완성해 주세요. | ||
|
||
단, `a` ⊕ `b`와 `b` ⊕ `a`가 같다면 `a` ⊕ `b`를 return 합니다. | ||
|
||
### 제한사항 | ||
|
||
--- | ||
|
||
- 1 ≤ `a`, `b` < 10,000 | ||
|
||
### 입출력 예 | ||
|
||
--- | ||
|
||
| a | b | result | | ||
| --- | --- | ------ | | ||
| 9 | 91 | 991 | | ||
| 89 | 8 | 898 | | ||
|
||
### 입출력 예 설명 | ||
|
||
--- | ||
|
||
입출력 예 #1 | ||
|
||
- `a` ⊕ `b` = 991 이고, `b` ⊕ `a` = 919 입니다. 둘 중 더 큰 값은 991 이므로 991을 return 합니다. | ||
|
||
입출력 예 #2 | ||
|
||
- `a` ⊕ `b` = 898 이고, `b` ⊕ `a` = 889 입니다. 둘 중 더 큰 값은 898 이므로 898을 return 합니다. | ||
|
||
### 풀이 | ||
|
||
--- | ||
|
||
```jsx | ||
function solution(a, b) { | ||
const firstNum = Number(a.toString() + b.toString()); | ||
const secondNum = Number(b.toString() + a.toString()); | ||
return firstNum > secondNum ? firstNum : secondNum; | ||
} | ||
``` | ||
|
||
### 느낀점 | ||
|
||
--- | ||
|
||
다른 분들은 Math.max를 이용해서 푸셨는데 나는 정말 사용안하는 함수여서 잘 몰랐던거 같다. 이번 기회에 새로운 것에 대해 알게 되었고 좀 더 Math함수를 살펴봐야할꺼 같다. |
73 changes: 73 additions & 0 deletions
73
_posts/development/algorithm/lv0/2024-03-06-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,73 @@ | ||
--- | ||
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} | ||
|
||
## 문제 | ||
|
||
### **문제 설명** | ||
|
||
--- | ||
|
||
문자열 `my_string`과 정수 배열 `index_list`가 매개변수로 주어집니다. `my_string`의 `index_list`의 원소들에 해당하는 인덱스의 글자들을 순서대로 이어 붙인 문자열을 return 하는 solution 함수를 작성해 주세요. | ||
|
||
### 제한사항 | ||
|
||
--- | ||
|
||
- 1 ≤ `my_string`의 길이 ≤ 1,000 | ||
- `my_string`의 원소는 영소문자로 이루어져 있습니다. | ||
- 1 ≤ `index_list`의 길이 ≤ 1,000 | ||
- 0 ≤ `index_list`의 원소 < `my_string`의 길이 | ||
|
||
### 입출력 예 | ||
|
||
--- | ||
|
||
| my_string | index_list | result | | ||
| -------------------- | ---------------------------------------- | ------------- | | ||
| "cvsgiorszzzmrpaqpe" | [16, 6, 5, 3, 12, 14, 11, 11, 17, 12, 7] | "programmers" | | ||
| "zpiaz" | [1, 2, 0, 0, 3] | "pizza" | | ||
|
||
### 입출력 예 설명 | ||
|
||
--- | ||
|
||
입출력 예 #1 | ||
|
||
- 예제 1번의 `my_string`에서 인덱스 3, 5, 6, 11, 12, 14, 16, 17에 해당하는 글자는 각각 g, o, r, m, r, a, p, e이므로 `my_string`에서 `index_list`에 들어있는 원소에 해당하는 인덱스의 글자들은 각각 순서대로 p, r, o, g, r, a, m, m, e, r, s입니다. 따라서 "programmers"를 return 합니다. | ||
|
||
입출력 예 #2 | ||
|
||
- 예제 2번의 `my_string`에서 인덱스 0, 1, 2, 3에 해당하는 글자는 각각 z, p, i, a이므로 `my_string`에서 `index_list`에 들어있는 원소에 해당하는 인덱스의 글자들은 각각 순서대로 p, i, z, z, a입니다. 따라서 "pizza"를 return 합니다. | ||
|
||
### 풀이 | ||
|
||
--- | ||
|
||
```jsx | ||
function solution(my_string, index_list) { | ||
const arrString = my_string.split(''); | ||
const answer = index_list.reduce((acc, cur) => { | ||
acc.push(arrString.find((_, idx) => idx === cur)); | ||
return acc; | ||
}, []); | ||
|
||
return answer.join(''); | ||
} | ||
``` | ||
|
||
### 느낀점 | ||
|
||
--- | ||
|
||
문제를 보고 어떻게 풀어야할지 감이 안왔지만 차근차근 풀다보니 문제를 어떻게 풀이 해결법이 나온거 같다. 문자열을 변환하고 배열을 변환하고 많이 했지만 문제를 해결했고 추후에 다시 살펴보면서 더 나은 코드로 만들어야겠다. |