Skip to content

[정렬] 예제 및 실전문제 해결 (이윤성) #12

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

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@
- 어떤 문제를 풀어서 올릴지는 매 주 토요일 만나는 시간에 정한다.
- 매 주 수요일 ~ 금요일 동안 서로 코드에 대한 리뷰를 남긴다.
- 코드를 돌려보고, 반례, 예외찾기를 우선으로 해본다.
- 매 주 토요일 만나, 실전 문제를 해결한다.
- 실전 문제를 해결하고, 스터디원들에게 설명하기 위해 의사코드를 간략히 작성해 설명한다.

## 🙋🏻‍♂️ Participants

|이윤성|이시현|김종민|노희승|
|---|---|---|---|
|이윤성|이시현|김종민|노희승|박영규|
|---|---|---|---|---|

## 🚀 Topics and Goals

Expand Down
4 changes: 4 additions & 0 deletions Sort/yunseong/input/p2.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
3
15
27
12
3 changes: 3 additions & 0 deletions Sort/yunseong/input/p3.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
2
홍길동 95
이순신 77
3 changes: 3 additions & 0 deletions Sort/yunseong/input/p4.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
5 3
1 2 5 4 3
5 5 6 6 5
12 changes: 12 additions & 0 deletions Sort/yunseong/problem02.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// 입력 로직 선언
const fs = require('fs');
const input = fs.readFileSync('./input/p2.txt').toString().trim();
const [n, ...inputNumbers] = input.split('\n').map((e) => +e);

// 필요한 함수 선언

inputNumbers.sort((a, b) => b - a); // 내림차순 정렬 API
Copy link
Contributor Author

Choose a reason for hiding this comment

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

sort는 자바스크립트 배열의 내장 API로, 인자로 정렬기준을 알려주는 callback을 전달해줍니다.


const result = inputNumbers.join(' '); // 결과 배열을 공백으로 구분된 값을 나타내는 문자열로 나타냄. ex) 12 15 27 등등

console.log(result);
26 changes: 26 additions & 0 deletions Sort/yunseong/problem03.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// 입력받는 로직
const fs = require('fs');
const input = fs.readFileSync('./input/p3.txt').toString().trim();
const [i_n, ...i_arr] = input.split('\n');
const n = +i_n;

let students = [];

class Student {
constructor(name, grade) {
this.name = name;
this.grade = grade;
}
}

// 입력 받은 학생들 객체를 만들어 주는 로직
i_arr.forEach((element) => {
const [name, grade] = element.split(' ');
students.push(new Student(name, +grade));
});

students.sort((a, b) => a.grade - b.grade);

const result = students.map((student) => student.name).join(' ');
Copy link
Contributor Author

Choose a reason for hiding this comment

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

students 배열 객체에서 이름을 뽑아 결과 문자열을 만들어 주는 로직입니다.
배열 API를 적절하게 사용하면 로직을 간단하게 줄일 수 있습니다.
python 같은 경우도 map, 등의 리스트 API를 제공하니 잘 공부해보시기 바랍니다!
(자바는 아쉽게도 쓸려면 stream으로 변환한 후에 사용해야 될 것 같습니다! 따로 공부해보시기 바랍니다.)


console.log(result);
26 changes: 26 additions & 0 deletions Sort/yunseong/problem04.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// 입력받는 로직
const fs = require('fs');
const input = fs.readFileSync('./input/p4.txt').toString().trim();
const inputs = input.split('\n');
const [n, k] = inputs[0].split(' ').map((e) => +e);

let arrA = inputs[1].split(' ').map((e) => +e);
let arrB = inputs[2].split(' ').map((e) => +e);

// 정렬 기준을 나타내는 콜백 (sort에 전달)
const ascending = (a, b) => a - b;
const descending = (a, b) => b - a;

arrA.sort(ascending);
arrB.sort(descending);

let count = 0;
let max = 0;

for (let i = 0; i < k; i++) {
if (arrA[i] < arrB[i]) {
[arrB[i], arrA[i]] = [arrA[i], arrB[i]];
} else break;
}

console.log(arrA.reduce((arr, cur) => arr + cur));