Home πŸ„πŸ»β€β™€οΈ [μžλ°”μŠ€ν¬λ¦½νŠΈ] μƒνƒœν•™
Post
Cancel

πŸ„πŸ»β€β™€οΈ [μžλ°”μŠ€ν¬λ¦½νŠΈ] μƒνƒœν•™

πŸ“„ 문제

싀버⅑ μƒνƒœν•™

κ΅¬ν˜„ν•΄μ•Ό ν•˜λŠ” 것은

νŒŒλΌλ―Έν„°λ‘œ 전달받은 1️⃣λͺ©λ‘μ„ μ˜€λ¦„μ°¨μˆœμœΌλ‘œ μ •λ ¬ν•˜κ³  2οΈβƒ£μ •λ ¬λœ λͺ©λ‘μ„ μˆœνšŒν•˜λ©° ν•΄λ‹Ή μš”μ†Œμ˜ μž…λ ₯ 횟수λ₯Ό Map에 μ—…λ°μ΄νŠΈν•œλ‹€.

2οΈβƒ£μ˜ μž‘μ—…μ΄ μ™„λ£Œλ˜λ©΄ ν•΄λ‹Ή Map을 λ°˜λ³΅μ‹œν‚€λ©° μž…λ ₯ κ°’ λͺ©λ‘μ—μ„œ 각각의 λ‚˜λ¬΄κ°€ μ°¨μ§€ν•˜λŠ” λΉ„μœ¨μ„ κ³„μ‚°ν•˜μ—¬ 좜λ ₯ν•œλ‹€.

🏹 μ‹œλ„

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
const fs = require("fs");
const inputs = fs.readFileSync("dev/stdin").toString().trim().split("\n");

//1️⃣λͺ©λ‘μ„ μ˜€λ¦„μ°¨μˆœμœΌλ‘œ μ •λ ¬ν•œλ‹€.
const orderedInputs = inputs.sort();

const totalCount = orderedInputs.length;

//2️⃣-1 λ‚˜λ¬΄λ³„ 차지 λΉ„μœ¨μ„ μ €μž₯ν•˜κΈ° μœ„ν•œ Map λ³€μˆ˜ 생성
const dictionary = new Map();

//2️⃣-2 μ •λ ¬λœ λͺ©λ‘μ„ μˆœνšŒν•˜λ©° ν•΄λ‹Ή μš”μ†Œμ˜ μž…λ ₯ 횟수λ₯Ό μ—…λ°μ΄νŠΈν•œλ‹€.
orderedInputs.forEach((input) =>
  dictionary.set(input, (dictionary.get(input) || 0) + 1)
);

//3️⃣ 2οΈβƒ£μ˜ μž‘μ—…μœΌλ‘œ Map λ³€μˆ˜μΈ dictionaryμ—λŠ” λ‚˜λ¬΄λ³„ μž…λ ₯ νšŸμˆ˜κ°€ μ €μž₯λ˜μ–΄ μžˆμœΌλ―€λ‘œ λ°±λΆ„μœ¨μ„ κ΅¬ν•˜μ—¬ 좜λ ₯ν•œλ‹€.
// λ°±λΆ„μœ¨μ€ μ†Œμˆ˜μ  λ„€λ²ˆμ§Έ μžλ¦¬κΉŒμ§€ 반올림 ν•΄μ•Ό ν•˜λ―€λ‘œ toFixed λ©”μ„œλ“œλ₯Ό μ‚¬μš©ν•œλ‹€.
const result = [];
for (let [key, value] of dictionary) {
  result.push(`${key} ${((value / totalCount) * 100).toFixed(4)}`);
}

console.log(result.join("\n"));
This post is licensed under CC BY 4.0 by the author.

🍊 2022-08-23

🍊 2022-08-26