Home ๋ชจ์Šค๋ถ€ํ˜ธ (1)
Post
์ทจ์†Œ

๋ชจ์Šค๋ถ€ํ˜ธ (1)

๐Ÿ”– ๋ชจ์Šค๋ถ€ํ˜ธ (1)

๐Ÿ“Œ ๋ฌธ์ œ

๋ฌธ์ œ ์„ค๋ช…

๋จธ์“ฑ์ด๋Š” ์นœ๊ตฌ์—๊ฒŒ ๋ชจ์Šค๋ถ€ํ˜ธ๋ฅผ ์ด์šฉํ•œ ํŽธ์ง€๋ฅผ ๋ฐ›์•˜์Šต๋‹ˆ๋‹ค. ๊ทธ๋ƒฅ์€ ์ฝ์„ ์ˆ˜ ์—†์–ด ์ด๋ฅผ ํ•ด๋…ํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ๋งŒ๋“ค๋ ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ๋ฌธ์ž์—ด letter๊ฐ€ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ฃผ์–ด์งˆ ๋•Œ, letter๋ฅผ ์˜์–ด ์†Œ๋ฌธ์ž๋กœ ๋ฐ”๊พผ ๋ฌธ์ž์—ด์„ return ํ•˜๋„๋ก solution ํ•จ์ˆ˜๋ฅผ ์™„์„ฑํ•ด๋ณด์„ธ์š”. ๋ชจ์Šค๋ถ€ํ˜ธ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

1
2
3
4
5
6
7
morse = { 
    '.-':'a','-...':'b','-.-.':'c','-..':'d','.':'e','..-.':'f',
    '--.':'g','....':'h','..':'i','.---':'j','-.-':'k','.-..':'l',
    '--':'m','-.':'n','---':'o','.--.':'p','--.-':'q','.-.':'r',
    '...':'s','-':'t','..-':'u','...-':'v','.--':'w','-..-':'x',
    '-.--':'y','--..':'z'
}

์ œํ•œ์‚ฌํ•ญ
  • 1 โ‰ค letter์˜ ๊ธธ์ด โ‰ค 1,000
  • return๊ฐ’์€ ์†Œ๋ฌธ์ž์ž…๋‹ˆ๋‹ค.
  • letter์˜ ๋ชจ์Šค๋ถ€ํ˜ธ๋Š” ๊ณต๋ฐฑ์œผ๋กœ ๋‚˜๋ˆ„์–ด์ ธ ์žˆ์Šต๋‹ˆ๋‹ค.
  • letter์— ๊ณต๋ฐฑ์€ ์—ฐ์†์œผ๋กœ ๋‘ ๊ฐœ ์ด์ƒ ์กด์žฌํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.
  • ํ•ด๋…ํ•  ์ˆ˜ ์—†๋Š” ํŽธ์ง€๋Š” ์ฃผ์–ด์ง€์ง€ ์•Š์Šต๋‹ˆ๋‹ค.
  • ํŽธ์ง€์˜ ์‹œ์ž‘๊ณผ ๋์—๋Š” ๊ณต๋ฐฑ์ด ์—†์Šต๋‹ˆ๋‹ค.

์ž…์ถœ๋ ฅ ์˜ˆ
letterresult
โ€œโ€ฆ. . .-.. .-.. โ€”โ€โ€œhelloโ€
โ€.โ€“. -.โ€“ - โ€ฆ. โ€” -.โ€โ€œpythonโ€

์ž…์ถœ๋ ฅ ์˜ˆ ์„ค๋ช…

์ž…์ถœ๋ ฅ ์˜ˆ #1

  • โ€ฆ. = h
  • . = e
  • .-.. = l
  • .-.. = l
  • โ€” = o
  • ๋”ฐ๋ผ์„œ โ€œhelloโ€๋ฅผ return ํ•ฉ๋‹ˆ๋‹ค.

์ž…์ถœ๋ ฅ ์˜ˆ #2

  • .โ€“. = p
  • -.โ€“ = y
  • - = t
  • โ€ฆ. = h
  • โ€” = o
  • -. = n
  • ๋”ฐ๋ผ์„œ โ€œpythonโ€์„ return ํ•ฉ๋‹ˆ๋‹ค.

  • a ~ z์— ํ•ด๋‹นํ•˜๋Š” ๋ชจ์Šค๋ถ€ํ˜ธ๊ฐ€ ์ˆœ์„œ๋Œ€๋กœ ๋‹ด๊ธด ๋ฐฐ์—ด์ž…๋‹ˆ๋‹ค.
  • {".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."}

โœ๏ธ ํ’€์ด

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
function solution(letter) {
    var answer = '';
    const morse = { 
        '.-':'a','-...':'b','-.-.':'c','-..':'d','.':'e','..-.':'f',
        '--.':'g','....':'h','..':'i','.---':'j','-.-':'k','.-..':'l',
        '--':'m','-.':'n','---':'o','.--.':'p','--.-':'q','.-.':'r',
        '...':'s','-':'t','..-':'u','...-':'v','.--':'w','-..-':'x',
        '-.--':'y','--..':'z'
    };
    let letterArray = letter.split(' ');
  
    for(let e of letterArray)  {
        for(const v in morse)
            if(e === v) 
                answer += morse[v];
    }
    
    return answer;
}

์ž…๋ ฅ ๋ฐ›์€ ๋ถ€์Šค๋ถ€ํ˜ธ ๋ฌธ์ž์—ด๋ฅผ ๋‹จ์–ด๋กœ ๋ณ€ํ™˜ํ•˜๋Š” ๋ฌธ์ œ์ด๋‹ค.

์ผ๋‹จ ๋ฌธ์ž์—ด์„ split()๋ฉ”์„œ๋“œ๋ฅผ ํ†ตํ•ด ๊ณต๋ฐฑ์„ ๊ธฐ์ค€์œผ๋กœ ๋‚˜๋ˆ„์–ด ์ฃผ์—ˆ๋‹ค.

๊ทธ๋Ÿฐ ๋‹ค์Œ forโ€ฆof ๋ฌธ์„ ํ†ตํ•ด ๋ฐฐ์—ด์˜ ์š”์†Œ๋“ค์„ ๋ฐ˜๋ณตํ•˜๊ณ , forโ€ฆin์„ ํ†ตํ•ด morse ๊ฐ์ฒด๋ฅผ ๋ฐ˜๋ณตํ•œ๋‹ค. ๋ฐฐ์—ด์„ ์ˆœํšŒํ•˜๋ฉด์„œ ๊ฐ์ฒด์— ์ ‘๊ทผํ•˜์—ฌ ๋ฐฐ์—ด์˜ ์š”์†Œ๊ฐ€ ๊ฐ์ฒด์˜ ํ‚ค์™€ ๊ฐ™์„ ๊ฒฝ์šฐ, answer์— ๊ฐ์ฒด์˜ ๊ฐ’์„ ๋”ํ•ด ํ• ๋‹นํ•œ๋‹ค.

๐Ÿ” ๋‹ค๋ฅธ ์‚ฌ๋žŒ ํ’€์ด

1
2
3
4
5
6
7
8
9
10
11
// ๋‹ค๋ฅธ ์‚ฌ๋žŒ ํ’€์ด
function solution(letter) {
    let morse = { 
        '.-':'a','-...':'b','-.-.':'c','-..':'d','.':'e','..-.':'f',
        '--.':'g','....':'h','..':'i','.---':'j','-.-':'k','.-..':'l',
        '--':'m','-.':'n','---':'o','.--.':'p','--.-':'q','.-.':'r',
        '...':'s','-':'t','..-':'u','...-':'v','.--':'w','-..-':'x',
        '-.--':'y','--..':'z'
    };
    return letter.split(' ').map(v=>morse[v]).join('');
}

for๋ฌธ์„ ์‚ฌ์šฉํ•˜์ง€ ์•Š๊ณ  map() ๋ฉ”์„œ๋“œ๋ฅผ ํ™œ์šฉํ•˜์—ฌ ํ‘ผ ๋ฌธ์ œ์ด๋‹ค. split() ๋ฉ”์„œ๋“œ๋ฅผ ํ†ตํ•ด ๋ฐ›์€ ๋งค๊ฐœ๋ณ€์ˆ˜ ๋ฌธ์ž์—ด๋“ค์„ ์ž๋ฅด๊ณ  map()๋ฉ”์„œ๋“œ๋ฅผ ํ†ตํ•ด ๋ฐฐ์—ด์„ ์ˆœํšŒํ•˜๋ฉด์„œ ๋ฐฐ์—ด์˜ ์š”์†Œ๋ฅผ ํ‚ค๊ฐ’๊ณผ ๊ฐ™์€ ๊ฒฝ์šฐ ๋ฐ˜ํ™˜ํ•˜๊ณ , join() ๋ฉ”์„œ๋“œ๋ฅผ ํ†ตํ•ด ๋ฌธ์ž์—ด๋“ค์„ ๋‹ค์‹œ ํ•ฉ์ณ ์ฃผ์—ˆ๋‹ค.

๐Ÿ’ป ์ถœ๋ ฅ ๊ฒฐ๊ณผ

image-20221126170730195

This post is licensed under CC BY 4.0 by the author.

๊ฐœ๋ฏธ ๊ตฐ๋‹จ

๊ตฌ์Šฌ์„ ๋‚˜๋ˆ„๋Š” ๊ฒฝ์šฐ์˜ ์ˆ˜