DevLog ๐Ÿ˜ถ

[์šฐ์•„ํ•œํ…Œํฌ์ฝ”์Šค] 5๊ธฐ ๋ฐฑ์—”๋“œ 1์ฃผ์ฐจ ํ”„๋ฆฌ์ฝ”์Šค ํšŒ๊ณ ๋ก ๋ณธ๋ฌธ

์šฐ์•„ํ•œํ…Œํฌ์ฝ”์Šค/ํ”„๋ฆฌ์ฝ”์Šค

[์šฐ์•„ํ•œํ…Œํฌ์ฝ”์Šค] 5๊ธฐ ๋ฐฑ์—”๋“œ 1์ฃผ์ฐจ ํ”„๋ฆฌ์ฝ”์Šค ํšŒ๊ณ ๋ก

dolmeng2 2022. 11. 9. 22:40

- 2์ฃผ์ฐจ๊ฐ€ ๋๋‚˜์„œ์•ผ ์ž‘์„ฑํ•˜๋Š” 1์ฃผ์ฐจ ํ”„๋ฆฌ์ฝ”์Šค ํšŒ๊ณ ๋ก...(?) 

- 1์ฃผ์ฐจ์—๋Š” ์ •์‹ ์—†์ด ๊ตฌํ˜„ํ•˜๊ณ  ์ปค๋ฐ‹ํ•˜๊ณ  ๋ฆฌํŒฉํ† ๋งํ•˜๊ณ  ๊ทธ๋Ÿฌ๋Š๋ผ ํ•œ ์ฃผ๊ฐ€ ๊ฐ€๋Š”์ง€๋„ ๋ชจ๋ฅด๊ฒŒ ๋๋‚ฌ๋˜ ๊ฒƒ ๊ฐ™๋‹ค. 

๋‹ค๋ฅธ ๋ถ„๋“ค์ด ๋งค์ฃผ ํšŒ๊ณ ๋ก์„ ์ž‘์„ฑํ•˜์‹œ๋Š” ๊ฒŒ ๊ฝค ๋ฉ‹์žˆ์–ด ๋ณด์ด๊ธฐ๋„ ํ–ˆ๊ณ , ๊ฐœ์ธ์ ์œผ๋กœ ์—ฌ๋ฆ„/๊ฒจ์šธ๋งˆ๋‹ค ํšŒ๊ณ ๋ก์„ ์ž‘์„ฑํ•˜๊ณ  ์žˆ์—ˆ์–ด์„œ ์šฐํ…Œ์ฝ” ์ฐธ์—ฌ ๊ธฐ๊ฐ„ ๋™์•ˆ์— ๋‚˜๋„ ํšŒ๊ณ ๋ก์„ ๋‚จ๊ฒจ๋ณด๊ณ ์ž ํ•œ๋‹ค :D ์ฝ”๋“œ ์งœ๋ฉด์„œ ๋„ˆ๋ฌด ๋ฐฐ์šฐ๋Š” ์ ์ด ๋งŽ์•„์„œ ๊ธฐ์˜๋‹ค.

 

- 1์ฃผ์ฐจ ๊ณผ์ œ ์ฝ”๋“œ๋Š” ์•„๋ž˜์—์„œ!

https://github.com/Cl8D/java-onboarding/tree/cl8d

 

GitHub - Cl8D/java-onboarding: ์˜จ๋ณด๋”ฉ ๋ฏธ์…˜์„ ์ง„ํ–‰ํ•˜๋Š” ์ €์žฅ์†Œ

์˜จ๋ณด๋”ฉ ๋ฏธ์…˜์„ ์ง„ํ–‰ํ•˜๋Š” ์ €์žฅ์†Œ. Contribute to Cl8D/java-onboarding development by creating an account on GitHub.

github.com

 

- ์‚ฌ์‹ค 1์ฃผ์ฐจ ๊ณผ์ œ๋Š” ์ฝ”ํ…Œ ๋ฌธ์ œ๋ž‘ ๊ฑฐ์˜ ๋ณ„๋‹ค๋ฅผ ๊ฒŒ ์—†์—ˆ๋‹ค๊ณ  ๋ณธ๋‹ค.

๋‚œ์ด๋„๋งŒ ๋ณด๋ฉด... 1๋ฒˆ์€ ๊ตฌํ˜„ํ•  ๊ฒŒ ์กฐ๊ธˆ ์žˆ๊ณ , 2~5๋ฒˆ์€ ํ‰์ดํ•˜๊ณ  6~7๋ฒˆ์ด ์กฐ๊ธˆ ์–ด๋ ค์› ๋˜ ๊ฒƒ ๊ฐ™๋‹ค.

- ๊ทผ๋ฐ ์‚ฌ์‹ค์ƒ ์šฐํ…Œ์ฝ”๋Š” ๋‚œ์ด๋„๋ฅผ ๋”ฐ์ง€๊ธฐ๋ณด๋‹จ, ์ด๊ฑธ ์–ด๋–ป๊ฒŒ ํ•˜๋ฉด '๋” ๊น”๋”ํ•˜๊ฒŒ, ํด๋ฆฐ ์ฝ”๋“œ๋ฅผ ์“ธ ์ˆ˜ ์žˆ์„๊นŒ?'์˜ ๊ณ ๋ฏผ์ด ๊ฐ€์žฅ ํฐ ๊ฒƒ ๊ฐ™๋‹ค.

๊ธฐ๋Šฅ ๊ตฌํ˜„์€ ๋งˆ์Œ ๋จน์œผ๋ฉด 1์‹œ๊ฐ„ ๋‚ด์—๋„ ๊ฐ€๋Šฅํ•˜์ง€๋งŒ, ์ˆ˜์ •ํ•˜๋Š”๋ฐ ๋ฉฐ์น ์”ฉ ๊ฑธ๋ฆฌ๊ธฐ ๋•Œ๋ฌธ...๐Ÿ˜ต‍๐Ÿ’ซ

 

 

| ๊ณผ์ œ ์‹œ์ž‘ ์ „

- ์ฒซ ๊ณผ์ œ์ธ ๋งŒํผ ๊ธด์žฅ ์ž”๋œฉ ํ•˜๊ณ  ์—ฌ๋Ÿฌ ๊ฐ€์ง€ ์ฐพ์•„๋ดค๋˜ ๊ธฐ์–ต์ด ๋‚œ๋‹ค.

๋ณ€์ˆ˜ ๋„ค์ด๋ฐ, ๋ฉ”์„œ๋“œ ๋„ค์ด๋ฐ, ์ปค๋ฐ‹ ์ปจ๋ฒค์…˜, ๋ช…๋ช…๊ทœ์น™, ํด๋ฆฐ์ฝ”๋“œ ์ฒดํฌ๋ฆฌ์ŠคํŠธ๊นŒ์ง€...

์‚ฌ์‹ค ์ง€๊ธˆ๊นŒ์ง€ ๊ฐœ๋ฐœํ•  ๋•Œ ์ž์—ฐ์Šค๋Ÿฝ๊ฒŒ ์ด๋Ÿฐ ์‹์œผ๋กœ ์จ์•ผ์ง€~๋Š” ํ–ˆ์ง€๋งŒ ๋ง‰์ƒ ๊ฐ ์žก๊ณ  ํ•˜๋ ค๊ณ  ํ•˜๋‹ˆ๊นŒ ๋„ˆ๋ฌด ์–ด์ง€๋Ÿฌ์› ๋‹ค.

- ํ•˜์ง€๋งŒ... ์ง€๊ธˆ์ด ์•„๋‹ˆ๋ฉด ๋‹ค์‹œ๋Š” ์ฝ”๋”ฉ ์Šต๊ด€์„ ์ž˜ ๋ชป ์žก์„ ๊ฒƒ ๊ฐ™๋‹ค๋Š” ์ƒ๊ฐ ํ•˜๋‚˜๋กœ ์ง€ํ‚ค๋ ค๊ณ  ์—„์ฒญ ์• ์ผ๋‹ค.

๊ฐ๊ฐ ์ฐธ๊ณ ํ–ˆ๋˜ ๋ธ”๋กœ๊ทธ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

 

[๋ช…๋ช… ๊ทœ์น™]

 

[Java] ๋ช…๋ช… ๊ทœ์น™

1. ๋ฐ˜์˜์–ด ๋ฐ˜์˜์–ด ์„ค๋ช… get / set ๋ฐ›๋‹ค / ๋ฐ›๋‹ค add / remove ์ถ”๊ฐ€ / ์ œ๊ฑฐ create / destroy ์ฐฝ์กฐ / ํŒŒ๊ดด start / stop ์‹œ๋™ / ์ •์ง€ insert / delete ์‚ฝ์ž… / ์‚ญ์ œ increment / decrement ์ฆ๊ฐ€ / ๊ฐ์†Œ old / new ๊ตฌ / ์‹  begin / end ์‹œ์ž‘

ozofweird.tistory.com

 

[๋ณ€์ˆ˜ ๋„ค์ด๋ฐ]

 

์ข‹์€ ์ฝ”๋“œ๋ฅผ ์œ„ํ•œ ์ž๋ฐ” ๋ณ€์ˆ˜๋ช… ๋„ค์ด๋ฐ

JAVA…

tecoble.techcourse.co.kr

 

[๋ฉ”์„œ๋“œ ๋„ค์ด๋ฐ]

 

์ข‹์€ ์ฝ”๋“œ๋ฅผ ์œ„ํ•œ ์ž๋ฐ” ๋ฉ”์„œ๋“œ ๋„ค์ด๋ฐ

tecoble.techcourse.co.kr

 

- ์—ฌ๊ธฐ์„œ ์˜๋„์ ์œผ๋กœ ์ง€ํ‚ค๋ ค๊ณ  ํ•˜๋Š” ๊ฑด ์ˆซ์ž ๊ทธ๋Œ€๋กœ ์•ˆ ๋ฐ•๋Š” ๊ฑฐ. ์ด๊ฑฐ๋Š” 2์ฃผ์ฐจ์—์„œ๋„ ์‹ ๊ฒฝ์“ฐ๋ ค๊ณ  ๋…ธ๋ ฅํ–ˆ๋‹ค.

- ๋˜ํ•œ, ์ด๋ฆ„์„ ๊ธธ๊ฒŒ ๊ฐ€์ ธ๊ฐ€๋Š” ๊ฑฐ์— ๋‘๋ ค์›Œํ•˜์ง€ ์•Š๋Š” ๊ฒƒ. ์˜คํžˆ๋ ค ๋ฉ”์„œ๋“œ๊ฐ€ ํ•˜๋Š” ์ผ์„ ์ž˜ ๋‹ด์„ ์ˆ˜ ์žˆ๋Š” ๊ฒƒ ๊ฐ™์•„์„œ ์‹ ๊ฒฝ์“ฐ๋Š” ๋ถ€๋ถ„์ด๋‹ค.

๋งŒ์•ฝ ๋ฉ”์„œ๋“œ๊ฐ€ ๋„ˆ๋ฌด ๋‹ค์–‘ํ•œ ์ผ์„ ํ•ด์„œ ๋ฉ”์„œ๋“œ๋ช…์ด ๊ธธ์–ด์ง„๋‹ค? > ์ด๋Ÿฌ๋ฉด ๋ฉ”์„œ๋“œ ๋ถ„๋ฆฌ๊ฐ€ ํ•„์š”ํ•˜๋‹ค๋Š” ๋ถ€๋ถ„์ด๊ธฐ ๋•Œ๋ฌธ์— ์ฒดํฌํ•˜๋ฉด ๋œ๋‹ค.

- ๊ทธ๋ฆฌ๊ณ  getXXX, createXXX... ๊ฐ™์ด ๋™์‚ฌ๋กœ ์‹œ์ž‘ํ•˜๊ฒŒ ํ•˜๋Š” ๊ฑฐ. ๊ฐœ์ธ์ ์œผ๋กœ ์กฐํšŒ๋Š” get์œผ๋กœ ๊ณ ์ •ํ•˜๋Š” ๊ฒŒ ๋ณด๊ธฐ ์ข‹์€ ๊ฒƒ ๊ฐ™๋‹ค.

 

 

[์ปค๋ฐ‹ ์ปจ๋ฒค์…˜]

 

[GitHub] ๊นƒ ์ปค๋ฐ‹ ๋ฉ”์„ธ์ง€ ์ปจ๋ฒค์…˜ (Git Commit Message Convention)

Introduction This style guide acts as the official guide to follow in your projects. Udacity evaluators will use this guide to grade your projects. There are many opinions on the "ideal" style in the world of development. Therefore, in order to reduce the

seolahchloe.tistory.com

 

 

[Git] ์ปค๋ฐ‹ ๋ฉ”์‹œ์ง€ ๊ทœ์•ฝ ์ •๋ฆฌ (the AngularJS commit conventions)

the AngularJS commit conventions๋ฅผ ๋ฒˆ์—ญํ•œ ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๊ณต๋ถ€ํ•˜๋ฉด์„œ ๋ฒˆ์—ญํ–ˆ์Šต๋‹ˆ๋‹ค. ์ž…๋ง›๋Œ€๋กœ ๋ฒˆ์—ญ๋œ ๋ถ€๋ถ„์ด๋‚˜ ์˜ค์—ญ์ด ์žˆ์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

velog.io

- ๊ฐœ์ธ์ ์œผ๋กœ 2๋ฒˆ์งธ ๋ธ”๋กœ๊ทธ์˜ ๋‚ด์šฉ์„ ๋„ˆ๋ฌด ์ž˜ ์ฝ์—ˆ๋‹ค. ํŠนํžˆ scope์— ๊ด€ํ•œ ๋ถ€๋ถ„.

์‚ฌ์‹ค feat, chore, fix ๋ง๊ณ  ์ง€๊ธˆ๊นŒ์ง€๋Š” ๊ฐœ๋ฐœํ•˜๋ฉด์„œ ๊ฑฐ์˜ ์•ˆ ์ผ๋˜ ๊ฒƒ ๊ฐ™์€๋ฐ 1์ฃผ์ฐจ ๊ณผ์ œํ•˜๋ฉด์„œ ๋ฌธ์ œ ๋ฒˆํ˜ธ๋ฅผ scope๋กœ ๋‘๊ณ  ์ปค๋ฐ‹ํ•˜๋‹ˆ๊นŒ ๊ฐ€๋…์„ฑ์ด ํ™•์‹คํžˆ ์ข‹์•„์กŒ๋‹ค๋Š” ๊ฑธ ๋Š๊ผˆ๋‹ค.

- ๋‚˜๋งŒ ๊ทธ๋ ‡๊ฒŒ ๋Š๋ผ๋Š” ๊ฑฐ๋ฉด ๋ง๊ณ ....

- ๊ทธ๋ฆฌ๊ณ  ์ปค๋ฐ‹ ๋ฉ”์‹œ์ง€์—๋Š” ์†Œ๋ฌธ์ž๋งŒ ์“ฐ๋Š” ๊ฒƒ๋„ ์žŠ์ง€ ๋ง๊ธฐ. ์ฒ˜์Œ์— ๋ฌด์˜์‹์ ์œผ๋กœ ๋Œ€๋ฌธ์ž๋„ ๊ฐ™์ด ๋„ฃ์–ด์„œ ๊ณ ์ƒํ–ˆ๋‹ค. 

(๋ช‡ ๋ธ”๋กœ๊ทธ์—์„œ๋Š” ์ฒซ ๋ฌธ์ž๋Š” ๋Œ€๋ฌธ์ž๋ผ๊ณ ๋„ ํ–ˆ์ง€๋งŒ... ๋Œ€๋‹ค์ˆ˜๋Š” ์†Œ๋ฌธ์ž๋กœ ์“ฐ๋Š” ๊ฒŒ ๋งž๋‹ค๊ณ  ํ•ด์„œ ์šฐ์„  ๋‚˜๋Š” ์†Œ๋ฌธ์ž๋กœ ๊ณ ์ •ํ•ด์„œ ์‚ฌ์šฉ ์ค‘์ด๋‹ค)

 

 

[ํด๋ฆฐ ์ฝ”๋“œ ์ฒดํฌ๋ฆฌ์ŠคํŠธ]

 

GitHub - woowacourse/woowacourse-docs: ์šฐ์•„ํ•œํ…Œํฌ์ฝ”์Šค ๋ฌธ์„œ๋ฅผ ๊ด€๋ฆฌํ•˜๋Š” ์ €์žฅ์†Œ

์šฐ์•„ํ•œํ…Œํฌ์ฝ”์Šค ๋ฌธ์„œ๋ฅผ ๊ด€๋ฆฌํ•˜๋Š” ์ €์žฅ์†Œ. Contribute to woowacourse/woowacourse-docs development by creating an account on GitHub.

github.com

- ๊ฐœ์ธ์ ์œผ๋กœ ์ •๋ง ๋„ˆ๋ฌด ์ค‘์š”ํ•˜๋‹ค๊ณ  ์ƒ๊ฐํ•˜๋Š” ๋ถ€๋ถ„. ํŠนํžˆ else ์•ˆ ์“ฐ๋Š” ๊ฑฐ๋ž‘, ์ธ๋ดํŠธ ์ค„์ด๋Š” ๊ฑฐ.

1์ฃผ์ฐจ ๋•Œ๋Š” ๊ทธ ๋‘ ๊ฐœ๋ž‘ ์ถ”๊ฐ€์ ์œผ๋กœ ๋ฉ”์„œ๋“œ์˜ ์ธ์ž์— 3๊ฐœ ์ด์ƒ์œผ๋กœ ์•ˆ ๋“ค์–ด๊ฐ€๋„๋ก ๋…ธ๋ ฅํ–ˆ๋˜ ๊ฒƒ ๊ฐ™๋‹ค.

 


 

| 1๋ฒˆ ๋ฌธ์ œ

- ํฌ๊ฒŒ ๊ณ ๋ฏผํ–ˆ๋˜ ๋ถ€๋ถ„์€ '์ œํ•œ์‚ฌํ•ญ์„ ์ฝ”๋“œ๋กœ ๋„ฃ๋Š” ๊ฒŒ ๋งž๋Š”๊ฐ€?' ์ด ๋ถ€๋ถ„์ด์—ˆ๋˜ ๊ฒƒ ๊ฐ™๋‹ค.

๋ณดํ†ต ๋ฐฑ์ค€์ด๋‚˜ ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค์—์„œ๋Š” ์ œํ•œ์‚ฌํ•ญ์— ์œ„๋ฐฐ๋˜๋Š” ์ž…๋ ฅ๊ฐ’์€ ์•ˆ ๋“ค์–ด์˜ค๊ธฐ๋„ ํ•˜๊ณ , ์—ฌ๊ธฐ์„œ๋„ ์‚ฌ์‹ค ๊ทธ๋Ÿด ๊ฒƒ ๊ฐ™์•˜๊ธฐ ๋•Œ๋ฌธ...

ํ•˜์ง€๋งŒ! ๊ตฌํ˜„ ํ•˜๋‚˜ ๋” ํ•œ๋‹ค๊ณ  ๋‚˜์  ๊ฑด ์ „ํ˜€ ์—†๊ธฐ ๋•Œ๋ฌธ์— ๋ฐ”๋กœ ๊ตฌํ˜„ํ–ˆ๋‹ค ใ…Žใ…Ž 

package onboarding;

        import java.util.Arrays;
        import java.util.List;
        import java.util.stream.Stream;

class Problem1 {
    static final int FIRST_PAGE = 1;
    static final int LAST_PAGE = 400;

    public static int solution(List<Integer> pobi, List<Integer> crong) {
        // ์ž…๋ ฅ ํŽ˜์ด์ง€ ๊ฒ€์ฆ ํ›„ ์˜ˆ์™ธ ์‚ฌํ•ญ์ผ ๊ฒฝ์šฐ -1์„ ๋ฆฌํ„ดํ•œ๋‹ค.
        if (isNotPageValid(pobi) || isNotPageValid(crong)) {
            return -1;
        }

        // ํฌ๋น„์˜ ์™ผ์ชฝ, ์˜ค๋ฅธ์ชฝ ํŽ˜์ด์ง€ ๋ฒˆํ˜ธ๋ฅผ ๊ตฌํ•œ๋‹ค.
        int pobiLeftPage = pobi.get(0);
        int pobiRightPage = pobi.get(1);

        // ํฌ๋กฑ์˜ ์™ผ์ชฝ, ์˜ค๋ฅธ์ชฝ ํŽ˜์ด์ง€ ๋ฒˆํ˜ธ๋ฅผ ๊ตฌํ•œ๋‹ค.
        int crongLeftPage = crong.get(0);
        int crongRightPage = crong.get(1);

        // ํฌ๋น„์˜ ๊ฒฐ๊ณผ ๋ฆฌ์ŠคํŠธ๋ฅผ ๊ตฌํ•œ๋‹ค.
        List<Integer> pobiResultList = List.of(getSumValue(pobiLeftPage), getSumValue(pobiRightPage),
                getMultiplyValue(pobiLeftPage), getMultiplyValue(pobiRightPage));

        int pobiScore = getMaxValue(pobiResultList);

        // ํฌ๋กฑ์˜ ๊ฒฐ๊ณผ ๋ฆฌ์ŠคํŠธ๋ฅผ ๊ตฌํ•œ๋‹ค.
        List<Integer> crongResultList = List.of(getSumValue(crongLeftPage), getSumValue(crongRightPage),
                getMultiplyValue(crongLeftPage), getMultiplyValue(crongRightPage));

        int crongScore = getMaxValue(crongResultList);

        return compareResult(pobiScore, crongScore);
    }

    /**
     * ์ž…๋ ฅ ํŽ˜์ด์ง€์— ๋Œ€ํ•œ ๊ฒ€์ฆ์„ ์ง„ํ–‰ํ•œ๋‹ค.
     *
     * @param pageNums ์‚ฌ์šฉ์ž์˜ ํŽ˜์ด์ง€ ๋ฒˆํ˜ธ ๋ฆฌ์ŠคํŠธ
     * @return ์˜ฌ๋ฐ”๋ฅด์ง€ ์•Š์€ ํŽ˜์ด์ง€ ๋ฆฌ์ŠคํŠธ๋ผ๋ฉด true, ์•„๋‹ˆ๋ผ๋ฉด false
     */
    private static boolean isNotPageValid(List<Integer> pageNums) {
        int leftPage = pageNums.get(0);
        int rightPage = pageNums.get(1);

        /* ์ž…๋ ฅ ํŽ˜์ด์ง€์— ๋Œ€ํ•ด์„œ ๊ฒ€์ฆ์„ ์ง„ํ–‰ํ•œ๋‹ค. ๋‹ค์Œ์€ ์˜ˆ์™ธ ์‚ฌํ•ญ์œผ๋กœ ๊ฐ„์ฃผํ•œ๋‹ค.
            1. ํŽ˜์ด์ง€์˜ ๋ฒ”์œ„๊ฐ€ 1~400 ์‚ฌ์ด๊ฐ€ ์•„๋‹Œ ๊ฒฝ์šฐ
            2. ํŽ˜์ด์ง€๊ฐ€ ์‹œ์ž‘์ด๋‚˜ ๋งˆ์ง€๋ง‰ ๋ฉด์ธ ๊ฒฝ์šฐ
            3. ์™ผ์ชฝ ํŽ˜์ด์ง€๊ฐ€ ํ™€์ˆ˜๊ฐ€ ์•„๋‹ˆ๊ณ , ์˜ค๋ฅธ์ชฝ ํŽ˜์ด์ง€๊ฐ€ ์ง์ˆ˜๊ฐ€ ์•„๋‹Œ ๊ฒฝ์šฐ
            4. ์™ผ์ชฝ, ์˜ค๋ฅธ์ชฝ ํŽ˜์ด์ง€์˜ ์ฐจ๊ฐ€ 1์ด ์•„๋‹Œ ๊ฒฝ์šฐ (์—ฐ์†๋œ ํŽ˜์ด์ง€๊ฐ€ ์•„๋‹˜)
         */
        return isNotPageRange(leftPage, rightPage) || isPageFirstOrLast(leftPage) ||
                isNotPageOddAndEven(leftPage, rightPage) || isNotPageContinuous(leftPage, rightPage);
    }

    /**
     * ์ž…๋ ฅ ํŽ˜์ด์ง€์˜ ๋ฒ”์œ„๊ฐ€ 1~400 ์‚ฌ์ด๊ฐ€ ์•„๋‹Œ์ง€ ํŒ๋‹จํ•œ๋‹ค.
     *
     * @param leftPage ์™ผ์ชฝ ํŽ˜์ด์ง€
     * @param rightPage ์˜ค๋ฅธ์ชฝ ํŽ˜์ด์ง€
     * @return 1~400 ์‚ฌ์ด๊ฐ€ ์•„๋‹ˆ๋ผ๋ฉด true, ๋งž๋‹ค๋ฉด false
     */
    private static boolean isNotPageRange(int leftPage, int rightPage) {
        return leftPage < FIRST_PAGE || leftPage >= LAST_PAGE || rightPage <= FIRST_PAGE || rightPage > LAST_PAGE;
    }

    /**
     * ์™ผ์ชฝ ํŽ˜์ด์ง€ ๋ฒˆํ˜ธ๊ฐ€ ์‹œ์ž‘ ๋ฉด์ด๊ฑฐ๋‚˜ ๋งˆ์ง€๋ง‰ ๋ฉด์ธ์ง€ ํŒ๋‹จํ•œ๋‹ค.
     *
     * @param leftPage ์™ผ์ชฝ ํŽ˜์ด์ง€
     * @return ์™ผ์ชฝ ํŽ˜์ด์ง€์˜ ๊ฐ’์ด 1์ด๊ฑฐ๋‚˜ 399๋ผ๋ฉด true, ์•„๋‹ˆ๋ผ๋ฉด false
     */
    private static boolean isPageFirstOrLast(int leftPage) {
        return leftPage == FIRST_PAGE || leftPage == LAST_PAGE-1;
    }

    /**
     * ์™ผ์ชฝ ํŽ˜์ด์ง€๊ฐ€ ํ™€์ˆ˜, ์˜ค๋ฅธ์ชฝ ํŽ˜์ด์ง€๊ฐ€ ์ง์ˆ˜๊ฐ€ ์•„๋‹Œ์ง€ ํŒ๋‹จํ•œ๋‹ค.
     *
     * @param leftPage ์™ผ์ชฝ ํŽ˜์ด์ง€
     * @param rightPage ์˜ค๋ฅธ์ชฝ ํŽ˜์ด์ง€
     * @return ์™ผ์ชฝ ํŽ˜์ด์ง€๊ฐ€ ํ™€์ˆ˜๊ฐ€ ์•„๋‹ˆ๊ณ  ์˜ค๋ฅธ์ชฝ ํŽ˜์ด์ง€๊ฐ€ ์ง์ˆ˜๊ฐ€ ์•„๋‹ˆ๋ฉด true, ์•„๋‹ˆ๋ผ๋ฉด false
     */
    private static boolean isNotPageOddAndEven(int leftPage, int rightPage) {
        return leftPage % 2 != 1 && rightPage % 2 != 0;
    }

    /**
     * ์™ผ์ชฝ, ์˜ค๋ฅธ์ชฝ ํŽ˜์ด์ง€๊ฐ€ ์—ฐ์†์ ์ด์ง€ ์•Š์€์ง€ ํ™•์ธํ•œ๋‹ค.
     *
     * @param leftPage ์™ผ์ชฝ ํŽ˜์ด์ง€
     * @param rightPage ์˜ค๋ฅธ์ชฝ ํŽ˜์ด์ง€
     * @return ์˜ค๋ฅธ์ชฝ, ์™ผ์ชฝ ํŽ˜์ด์ง€์˜ ์ฐจ๊ฐ€ 1์ด ์•„๋‹ˆ๋ผ๋ฉด true, ์•„๋‹ˆ๋ผ๋ฉด false
     */
    private static boolean isNotPageContinuous(int leftPage, int rightPage) {
        return rightPage - leftPage != 1;
    }

    /**
     * ์ž…๋ ฅ ๋ฐ›์€ ํŽ˜์ด์ง€ ๋ฒˆํ˜ธ์˜ ๊ฐ ์ž๋ฆฌ์ˆ˜์˜ ํ•ฉ์„ ๊ตฌํ•œ๋‹ค.
     *
     * @param page ํŽ˜์ด์ง€ ๋ฒˆํ˜ธ
     * @return ๊ฐ ์ž๋ฆฌ์ˆ˜์˜ ํ•ฉ
     */
    private static int getSumValue(int page) {
        int[] pageArr = getPageValueArray(page);

        return Arrays.stream(pageArr).sum();
    }

    /**
     * ์ž…๋ ฅ ๋ฐ›์€ ํŽ˜์ด์ง€ ๋ฒˆํ˜ธ์˜ ๊ฐ ์ž๋ฆฌ์ˆ˜์˜ ๊ณฑ์„ ๊ตฌํ•œ๋‹ค.
     *
     * @param page ํŽ˜์ด์ง€ ๋ฒˆํ˜ธ
     * @return ๊ฐ ์ž๋ฆฌ์ˆ˜์˜ ๊ณฑ
     */
    private static int getMultiplyValue(int page) {
        int[] pageArr = getPageValueArray(page);

        int multiVal = 1;
        for(int pageVal : pageArr) {
            multiVal *= pageVal;
        }

        return multiVal;
    }

    /**
     * ์ž…๋ ฅ ๋ฐ›์€ ํŽ˜์ด์ง€์˜ ๊ฐ ์ž๋ฆฟ์ˆ˜๋ฅผ ๋‹ด์€ ๋ฐฐ์—ด์„ ๊ตฌํ•œ๋‹ค.
     *
     * @param page ํŽ˜์ด์ง€ ๋ฒˆํ˜ธ
     * @return ๊ฐ ์ž๋ฆฟ์ˆ˜๊ฐ€ ๋‹ด๊ธด ๋ฐฐ์—ด
     */
    private static int[] getPageValueArray(int page) {
        String[] digitStrArr = String.valueOf(page).split("");

        return Stream.of(digitStrArr).mapToInt(Integer::parseInt).toArray();
    }

    /**
     * ํŽ˜์ด์ง€ ๋ฒˆํ˜ธ์— ๋”ฐ๋ฅธ ์ž๋ฆฌ์ˆ˜ ๊ณ„์‚ฐ ๊ฒฐ๊ณผ ์ค‘ ๊ฐ€์žฅ ํฐ ๊ฐ’์„ ๋ฆฌํ„ดํ•œ๋‹ค.
     *
     * @param resultArr ์ž๋ฆฌ์ˆ˜ ๊ณ„์‚ฐ ๊ฒฐ๊ณผ๋ฅผ ๋‹ด์€ ๋ฆฌ์ŠคํŠธ
     * @return ๊ฐ€์žฅ ํฐ ๊ฐ’
     */
    private static int getMaxValue(List<Integer> resultArr) {
        int maxVal = 0;
        for(int resultVal : resultArr) {
            maxVal = Math.max(maxVal, resultVal);
        }

        return maxVal;
    }

    /**
     * ๋‘ ์‚ฌ๋žŒ์˜ ์ ์ˆ˜๋ฅผ ๋น„๊ตํ•˜์—ฌ ๊ฒฐ๊ณผ๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค.
     *
     * @param pobiScore ํฌ๋น„์˜ ์ ์ˆ˜
     * @param crongScore ํฌ๋กฑ์˜ ์ ์ˆ˜
     * @return ํฌ๋น„๊ฐ€ ์ด๊ธฐ๋ฉด 1, ํฌ๋กฑ์ด ์ด๊ธฐ๋ฉด 2, ๋ฌด์Šน๋ถ€๋Š” 0์„ ๋ฆฌํ„ดํ•œ๋‹ค.
     */
    private static int compareResult(int pobiScore, int crongScore) {
        if (pobiScore > crongScore) {
            return 1;
        }
        if (pobiScore < crongScore) {
            return 2;
        }
        return 0;
    }
}

- ๊ทธ๋ƒฅ ๊ฐ„๋‹จํ•˜๊ฒŒ ์ƒ์ˆ˜ํ™” ์‹ ๊ฒฝ์“ฐ๊ณ , ํ•˜๋‚˜์˜ ์ผ๋งŒ ํ•˜๋„๋ก ๋ฉ”์„œ๋“œ๋ฅผ ์ตœ๋Œ€ํ•œ ์ž‘๊ฒŒ ๋ถ„๋ฆฌํ–ˆ๋‹ค.

Stream.of.mapToInt ์‚ฌ์šฉํ•ด์„œ ์ž๋ฆฟ์ˆ˜ ๋ฐฐ์—ด ๋งŒ๋“  ๊ฒŒ ๊ฐ€์žฅ ๊ธฐ์–ต์— ๋‚จ๋Š” ๋ฌธ์ œ์ด๋‹ค.

 


 

| 2๋ฒˆ ๋ฌธ์ œ

- 2๋ฒˆ ๋ฌธ์ œ๋Š” ์ƒ๊ฐ๋ณด๋‹ค ๊นŒ๋‹ค๋กœ์› ๋‹ค. ํ(๋ฑ)๋ฅผ ์‚ฌ์šฉํ•ด์„œ ํ’€๊นŒ ๊ณ ๋ฏผํ•˜๋‹ค๊ฐ€ ๊ทธ๋Ÿผ ๋„ˆ๋ฌด ๋ณต์žกํ•ด์งˆ ๊ฒƒ ๊ฐ™์•˜๊ธฐ ๋•Œ๋ฌธ...

๊ทธ๋ž˜์„œ while๋ฌธ์„ ๋Œ๊นŒ, ์•„๋‹ˆ๋ฉด 2์ค‘ for๋ฌธ์„ ๋Œ๊นŒ ๊ณ ๋ฏผํ•˜๋‹ค๊ฐ€ ๋‘˜ ๋‹ค ๋งˆ์Œ์— ์•ˆ ๋“ค์–ด์„œ ์—„์ฒญ ๊ณ ๋ฏผํ•˜๋‹ค ์ •๊ทœ์‹์œผ๋กœ ํ’€์—ˆ๋‹ค!

๊ฐœ์ธ์ ์œผ๋กœ ํ’€๊ณ  ๋‚˜์„œ ๋„ˆ๋ฌด ๋ฟŒ๋“ฏํ–ˆ๋˜ ๋ฌธ์ œ ใ…Žใ…Ž  

package onboarding;

        import onboarding.common.InputStringUtil;
        import onboarding.common.ValidationUtil;

public class Problem2 {
    public static String solution(String cryptogram) {
        // ์ž…๋ ฅ ์•”ํ˜ธ๋ฌธ ๋Œ€ํ•œ ์ œํ•œ์‚ฌํ•ญ์„ ๊ฒ€์ฆํ•œ๋‹ค.
        verifyCryptogram(cryptogram);

        // ์ž…๋ ฅ ๋ฌธ์ž์—ด์„ charํ˜• ๋ฐฐ์—ด๋กœ ๋ถ„ํ•ดํ•œ๋‹ค.
        char[] cryptogramArr = InputStringUtil.getCharArr(cryptogram);

        // ๋ฐฐ์—ด์„ ์ˆœํšŒํ•˜๋ฉฐ ํ•ด๋‹น ์›์†Œ๋ฅผ ๊ธฐ์ค€์œผ๋กœ ์ค‘๋ณต ๋ฌธ์ž์—ด์„ ์ฒดํฌํ•œ๋‹ค.
        for(Character element : cryptogramArr) {
            // ํ•ด๋‹น ๋ฌธ์ž๊ฐ€ 2๋ฒˆ ์ด์ƒ ๋ฐ˜๋ณต๋œ๋‹ค๋ฉด ์ œ๊ฑฐํ•˜์—ฌ ์›๋ณธ ์•”ํ˜ธ๋ฌธ์„ ๋ณ€๊ฒฝํ•œ๋‹ค.
            cryptogram = removeDuplicateElement(cryptogram, element);
        }

        return cryptogram;
    }

    /**
     * ์ž…๋ ฅ๋ฐ›์€ ์•”ํ˜ธ๋ฌธ์— ๋Œ€ํ•œ ๊ฒ€์ฆ ์ž‘์—…์„ ์ง„ํ–‰ํ•œ๋‹ค.
     *
     * @param cryptogram ์ž…๋ ฅ ์•”ํ˜ธ๋ฌธ
     */
    private static void verifyCryptogram(String cryptogram) {
        // ์•”ํ˜ธ๋ฌธ์˜ ๊ธธ์ด๋Š” 1~1000 ์‚ฌ์ด๋กœ ์ œํ•œ๋˜์–ด ์žˆ๋‹ค.
        ValidationUtil cryptogramValidation = new ValidationUtil();
        cryptogramValidation.addVarName("cryptogram");
        cryptogramValidation.checkStrLowerCase(cryptogram, 1, 1000);
    }

    /**
     * 2๋ฒˆ ์ด์ƒ ์ค‘๋ณต๋œ ๋ฌธ์ž์—ด์„ ์ œ๊ฑฐํ•œ๋‹ค.
     *
     * @param cryptogram ์ œ๊ฑฐ ์ง„ํ–‰ํ•  ๋ฌธ์ž์—ด
     * @param element ์ œ๊ฑฐ์˜ ๊ธฐ์ค€์ด ๋˜๋Š” ์š”์†Œ
     * @return String - ์ œ๊ฑฐ ์ž‘์—…์ด ์™„๋ฃŒ๋œ ๋ฌธ์ž์—ด์„ ๋ฆฌํ„ดํ•œ๋‹ค.
     */
    private static String removeDuplicateElement(String cryptogram, Character element) {
        return cryptogram.replaceAll(element + "{2,}", "");
    }
}

- ๋‹ค์†Œ 1์ฃผ์ฐจ ๊ณผ์ œ๋Š” ์ฃผ์„์„ ์—„์ฒญ ๋‹ฌ๋‹ค ๋ณด๋‹ˆ๊นŒ ์ฝ”๋“œ๊ฐ€ ๊ธธ๋‹ค...

์•„๋ฌดํŠผ...! ์ •๊ทœ์‹์„ ํ†ตํ•ด์„œ {2, } -> 2๋ฒˆ ์ด์ƒ ๋ฐ˜๋ณตํ•˜๋Š” ๋ฌธ์ž์— ๋Œ€ํ•ด์„œ ์ „๋ถ€ ์‚ญ์ œ ์ฒ˜๋ฆฌ๋ฅผ ์ง„ํ–‰ํ•ด์ฃผ์—ˆ๋‹ค.

์•ž์—์„œ ์‚ญ์ œ๋ฅผ ์ฒ˜๋ฆฌํ•˜๋ฉด ๋ฐ˜๋ณต๋ฌธ์„ ํ†ตํ•ด ๊ทธ ๋‹ค์Œ ์š”์†Œ๋ฅผ ๋Œ์•„๋„ ๋˜ ๋‹ค์‹œ ์ค‘๋ณต๋˜๋Š” ๋ฌธ์ž๋ฅผ ์ œ๊ฑฐํ•ด์ค„ ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— 1์ค‘ for๋ฌธ์œผ๋กœ๋„ ํ’€ ์ˆ˜ ์žˆ์–ด์„œ ์ข‹์•˜๋‹ค.

- ์ถ”๊ฐ€์ ์œผ๋กœ ๊ฒ€์ฆ์— ๊ด€ํ•œ ๋ถ€๋ถ„์€ ๋”ฐ๋กœ ์•„๋ž˜์— ์ž‘์„ฑํ•  ์˜ˆ์ •์ด๋‹ค.


| 3๋ฒˆ ๋ฌธ์ œ

- 3๋ฒˆ์€ ์ง„์งœ ๊ธฐ์ดˆ ๋ฌธ์ œ์—ฌ์„œ ํฌ๊ฒŒ ์–ด๋ ค์šธ ๊ฒƒ ์—†์ด ํ’€์—ˆ๋‹ค.

package onboarding;

        import onboarding.common.ValidationUtil;

public class Problem3 {
    static final String[] NUM_FOR_CLAPS = {"3", "6", "9"};

    public static int solution(int number) {
        int answer = 0;

        // ์ž…๋ ฅ๋ฐ›์€ ์ˆซ์ž์— ๋Œ€ํ•œ ๊ฒ€์ฆ ์ž‘์—…์„ ์ง„ํ–‰ํ•œ๋‹ค.
        verifyNumber(number);

        // 1๋ถ€ํ„ฐ ์ž…๋ ฅ์œผ๋กœ ์ฃผ์–ด์ง„ ๋ฒˆํ˜ธ๊นŒ์ง€ ์ˆœํšŒ๋ฅผ ์ง„ํ–‰ํ•œ๋‹ค.
        for (int num = 1; num <= number; num++) {
            // ๊ฐ ๋ฒˆํ˜ธ๋ฅผ ๋ฌธ์ž์—ด๋กœ ๋ณ€๊ฒฝํ•˜๊ณ , ์›๋ณธ ๊ธธ์ด๋ฅผ ๊ตฌํ•œ๋‹ค.
            String strNum = String.valueOf(num);
            int originLen = strNum.length();

            // ๋ฐ•์ˆ˜ ํšŸ์ˆ˜๋ฅผ ๊ณ„์† ๋”ํ•ด์ค€๋‹ค.
            answer += getClapCount(strNum, originLen);
        }
        return answer;
    }

    /**
     * ์ž…๋ ฅ๋ฐ›์€ ์ˆซ์ž์— ๋Œ€ํ•œ ๊ฒ€์ฆ ์ž‘์—…์„ ์ง„ํ–‰ํ•œ๋‹ค.
     *
     * @param number ์ž…๋ ฅ๋ฐ›์€ ์ˆซ์ž
     */
    private static void verifyNumber(int number) {
        ValidationUtil numValidation = new ValidationUtil();
        numValidation.addVarName("number");
        // ์ˆซ์ž์˜ ๋ฒ”์œ„๋Š” 1~10000 ์‚ฌ์ด๋กœ ์ œํ•œ๋˜์–ด ์žˆ๋‹ค.
        numValidation.checkNumRange(number, 1, 10000);
    }

    /**
     * ๋ฐ•์ˆ˜ ํšŸ์ˆ˜๋ฅผ ๊ตฌํ•œ๋‹ค.
     *
     * @param strNum ๊ธฐ์ค€ ๋ฌธ์ž์—ด
     * @param originLen ๊ธฐ์ค€ ๋ฌธ์ž์—ด์˜ ์›๋ณธ ๊ธธ์ด
     * @return int - 3/6/9๊ฐ€ ํฌํ•จ๋˜์—ˆ์„ ๋•Œ ์นœ ๋ฐ•์ˆ˜ ํšŸ์ˆ˜๋ฅผ ๋ฆฌํ„ดํ•œ๋‹ค.
     */
    private static int getClapCount(String strNum, int originLen) {
        int clapCount = 0;
        for(String clapNum : NUM_FOR_CLAPS) {
            int newLen = checkClapNumAndGetLength(strNum, clapNum);

            // ์ œ๊ฑฐ๋œ ๊ธธ์ด๋งŒํผ์ด ๋ฐ•์ˆ˜๋ฅผ ์นœ ํšŸ์ˆ˜๊ฐ€ ๋œ๋‹ค.
            if (newLen != -1) {
                clapCount += (originLen - newLen);
            }
        }

        return clapCount;
    }

    /**
     * ๊ธฐ์ค€ ๋ฌธ์ž์—ด์— 3/6/9๊ฐ€ ํฌํ•จ์ด ๋˜์–ด ์žˆ๋‹ค๋ฉด ์ œ๊ฑฐ ํ›„ ๊ธธ์ด๋ฅผ ๊ตฌํ•œ๋‹ค.
     *
     * @param strNum ๊ธฐ์ค€ ๋ฌธ์ž์—ด
     * @param clapNum 369 ๊ฒŒ์ž„์˜ ๊ธฐ์ค€์ด ๋˜๋Š” ๋ฒˆํ˜ธ (3/6/9)
     * @return int - ์ œ๊ฑฐ ์™„๋ฃŒ๋œ ๋ฌธ์ž์—ด์˜ ๊ธธ์ด๋ฅผ ๋ฆฌํ„ดํ•œ๋‹ค. ๋ณ€๊ฒฝ์‚ฌํ•ญ์ด ์—†์„ ๊ฒฝ์šฐ -1์„ ๋ฆฌํ„ดํ•œ๋‹ค.
     */
    private static int checkClapNumAndGetLength(String strNum, String clapNum) {
        if (strNum.contains(clapNum)) {
            String removedStr = removeClapNum(strNum, clapNum);
            return removedStr.length();
        }

        return -1;
    }

    /**
     * ๋ฌธ์ž์—ด์— ์กด์žฌํ•˜๋Š” clapNum (3/6/9)๋ฅผ ์ œ๊ฑฐํ•œ๋‹ค.
     *
     * @param strNum ๊ธฐ์ค€ ๋ฌธ์ž์—ด
     * @param clapNum 369 ๊ฒŒ์ž„์˜ ๊ธฐ์ค€์ด ๋˜๋Š” ๋ฒˆํ˜ธ (3/6/9)
     * @return String - ์ œ๊ฑฐ๊ฐ€ ์™„๋ฃŒ๋œ ๋ฌธ์ž์—ด์„ ๋ฆฌํ„ดํ•œ๋‹ค.
     */
    private static String removeClapNum(String strNum, String clapNum) {
        return strNum.replaceAll(clapNum, "");
    }
}

- ๋‹ค๋ฅธ ๋ถ„๋“ค ์ฝ”๋“œ๋ฅผ ๋ณด๋‹ˆ๊นŒ ์ˆ˜ํ•™์ ์œผ๋กœ ์ ‘๊ทผํ•˜์‹  ๋ถ„๋“ค๋„ ๊ณ„์…จ๋Š”๋ฐ, ๋‚˜๋Š” ๊ทธ์ •๋„๋กœ ๋จธ๋ฆฌ๊ฐ€ ๊ตด๋Ÿฌ๊ฐ€์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์—...

์–ด์ฐจํ”ผ 3, 6, 9๋ผ๋Š” ๊ธธ์ด๊ฐ€ 1์ธ ๋ฌธ์ž์— ๋Œ€ํ•ด์„œ๋งŒ ์ œ๊ฑฐ ์ฒ˜๋ฆฌ๋ฅผ ํ•˜๋ฉด, ๊ทธ๊ฒŒ ๊ณง ๋ฐ•์ˆ˜ ํšŸ์ˆ˜๊ฐ€ ๋˜๊ธฐ ๋•Œ๋ฌธ์— ๊ธธ์ด๊ฐ€ ์–ด๋Š ์ •๋„ ๋‹ฌ๋ผ์กŒ๋Š”์ง€๋กœ ํŒ๋‹จํ–ˆ๋‹ค.

์ด๊ฒƒ๋„ ๊ฝค... ๋…ํŠนํ•˜๊ฒŒ ํ’€์—ˆ๋‹ค๊ณ  ์ƒ๊ฐํ•˜๋Š”๋ฐ ๋‚˜๋งŒ ๊ทธ๋Ÿด์ง€๋„...?


| 4๋ฒˆ ๋ฌธ์ œ

- 4๋ฒˆ์€ ์•„์Šคํ‚ค๋ฅผ ํ™œ์šฉํ•˜๋Š” ๋ฌธ์ œ! ์•„์Šคํ‚ค์ฝ”๋“œํ‘œ ๋ณด๋ฉด์„œ ํ’€์—ˆ๋‹ค ใ…‹ใ…‹ใ…‹ ใ… ใ…  

package onboarding;

public class Problem4 {
    static final char START_ASCII_BY_UPPER_CASE = 'A';
    static final char END_ASCII_BY_UPPER_CASE = 'Z';
    static final char START_ASCII_BY_LOWER_CASE = 'a';
    static final char END_ASCII_BY_LOWER_CASE = 'z';

    public static String solution(String word) {
        StringBuilder answer = new StringBuilder("");

        // ์ž…๋ ฅ ๋ฌธ์ž์—ด์„ ๋ฐฐ์—ด๋กœ ๋ณ€ํ™˜ํ•œ๋‹ค.
        char[] wordArr = getCharArr(word);

        for(Character wordVal : wordArr) {
            // ๋งŒ์•ฝ ์•ŒํŒŒ๋ฒณ์ด ์•„๋‹ˆ๋ผ๋ฉด ๊ณต๋ฐฑ์œผ๋กœ ๊ฐ„์ฃผํ•œ๋‹ค.
            if(isNotAlphabetic(wordVal)) {
                answer.append(' ');
                continue;
            }

            // ์•ŒํŒŒ๋ฒณ์ด๋ผ๋ฉด ๋ณ€ํ™˜ ์ž‘์—…์„ ์ง„ํ–‰ํ•œ๋‹ค.
            char reversedVal = changeReversed(wordVal);
            answer.append(reversedVal);
        }

        return answer.toString();
    }

    /**
     * ์ฒญ๊ฐœ๊ตฌ๋ฆฌ ์‚ฌ์ „์— ๋”ฐ๋ผ ๋‹จ์–ด๋ฅผ ๋ณ€ํ™˜ํ•œ๋‹ค.
     *
     * @param value ์›๋ณธ ๋‹จ์–ด ์š”์†Œ
     * @return char - ์‚ฌ์ „์— ๋”ฐ๋ผ ๋ณ€ํ™˜๋œ ์š”์†Œ๋ฅผ ๋ฆฌํ„ดํ•œ๋‹ค. ๋Œ€๋ฌธ์ž๋Š” ๋Œ€๋ฌธ์ž๋กœ, ์†Œ๋ฌธ์ž๋Š” ์†Œ๋ฌธ์ž๋กœ ๋ฆฌํ„ด๋œ๋‹ค.
     */

    private static char changeReversed(char value) {
        if (Character.isUpperCase(value))
            return (char) (END_ASCII_BY_UPPER_CASE - (value - START_ASCII_BY_UPPER_CASE));
        return (char) (END_ASCII_BY_LOWER_CASE - (value - START_ASCII_BY_LOWER_CASE));
    }

    /**
     * ์š”์†Œ๊ฐ€ ์•ŒํŒŒ๋ฒณ์ด ์•„๋‹Œ์ง€ ํŒ๋‹จํ•œ๋‹ค.
     *
     * @param value ํŒ๋‹จํ•  ์š”์†Œ
     * @return ์•ŒํŒŒ๋ฒณ์ด ์•„๋‹ˆ๋ผ๋ฉด true, ๋งž๋‹ค๋ฉด false
     */
    private static boolean isNotAlphabetic(Character value) {
        return !Character.isAlphabetic(value);
    }

    /**
     * ํŒŒ๋ผ๋ฏธํ„ฐ๋กœ ๋ฐ›์€ ๋ฌธ์ž์—ด์„ charํ˜• ๋ฐฐ์—ด๋กœ ๋ถ„ํ•ดํ•˜์—ฌ ๋ฆฌํ„ดํ•œ๋‹ค.
     *
     * @param str ๋ฌธ์ž์—ด
     * @return char[] - ์ž…๋ ฅ์œผ๋กœ ๋ฐ›์€ ๋ฌธ์ž์—ด์— ๋Œ€ํ•ด charํ˜• ๋ฐฐ์—ด๋กœ ์ชผ๊ฐ  ๊ฒฐ๊ณผ๋ฅผ ๋ฆฌํ„ดํ•œ๋‹ค.
     */
    private static char[] getCharArr(String str) {
        return str.toCharArray();
    }
}

 - ์‚ฌ์‹ค ์‚ฌ์ „์— ๊ทธ๋ƒฅ ์ฒญ๊ฐœ๊ตฌ๋ฆฌ ์‚ฌ์ „์˜ ๊ฐ’์„ ์ง€์ •ํ•ด๋‘๊ณ  ํ’€์—ˆ๋‹ค๋ฉด ๋” ๊ฐ„๋‹จํ•˜๊ฒ ์ง€๋งŒ... ์ฝ”๋“œ๊ฐ€ ๊ฐ„์ง€๊ฐ€ ์—†๋‹ค. ๊ทธ๋Ÿฐ ๊ฑด ์šฉ๋‚ฉ ๋ถˆ๊ฐ€.


| 5๋ฒˆ ๋ฌธ์ œ

- 5๋ฒˆ๋„ ์ง„์งœ ๊ธฐ์ดˆ ๋ฌธ์ œ์˜€๊ธฐ ๋•Œ๋ฌธ์— ์–ด๋ ต์ง€ ์•Š๊ฒŒ ํ’€ ์ˆ˜ ์žˆ์—ˆ๋‹ค.

package onboarding;

        import onboarding.common.ValidationUtil;

        import java.util.ArrayList;
        import java.util.List;

public class Problem5 {

    static final int[] MONETARY_UNIT = {50000, 10000, 5000, 1000, 500, 100, 50, 10, 1};

    public static List<Integer> solution(int money) {
        List<Integer> answer = new ArrayList<>();

        // ์ž…๋ ฅ๋œ ๊ธˆ์•ก์— ๋Œ€ํ•œ ๊ฒ€์ฆ์„ ์ง„ํ–‰ํ•œ๋‹ค.
        moneyValidation(money);

        // ๋‚ด๋ฆผ์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌ๋œ ํ™”ํ ๋‹จ์œ„๋ฅผ ์ˆœํšŒํ•œ๋‹ค.
        for(int unit : MONETARY_UNIT) {
            // ๊ธฐ์กด ๊ธˆ์•ก์ด ๋‹จ์œ„๋ณด๋‹ค ์ž‘์œผ๋ฉด ํ•ด๋‹น ํ™”ํ์˜ ๊ฐœ์ˆ˜๋Š” 0์œผ๋กœ ๊ฐ„์ฃผํ•œ๋‹ค.
            if (money < unit) {
                answer.add(0);
                continue;
            }
            // ๊ทธ๊ฒŒ ์•„๋‹ˆ๋ผ๋ฉด ๊ธฐ์กด ๊ธˆ์•ก์—์„œ ํ™”ํ ๋‹จ์œ„๋กœ ๋‚˜๋ˆˆ ๋ชซ์ด ๊ฐœ์ˆ˜๊ฐ€ ๋œ๋‹ค.
            answer.add(money/unit);
            // ๊ธฐ์กด ๊ธˆ์•ก์€ ํ™”ํ ๋‹จ์œ„๋กœ ๋‚˜๋ˆˆ ๋‚˜๋จธ์ง€๋กœ ๊ฐฑ์‹ ํ•œ๋‹ค.
            money %= unit;
        }

        return answer;
    }

    /**
     * ์ž…๋ ฅ์œผ๋กœ ๋“ค์–ด์˜จ ๊ธˆ์•ก์— ๋Œ€ํ•ด ๊ฒ€์ฆํ•œ๋‹ค.
     *
     * @param money ์ž…๋ ฅ๋ฐ›์€ ๊ธˆ์•ก
     */
    private static void moneyValidation(int money) {
        ValidationUtil moneyValidation = new ValidationUtil();
        moneyValidation.addVarName("money");
        // ๊ธˆ์•ก์€ 1~1000000 ์‚ฌ์ด์˜ ๊ฐ’์ด์–ด์•ผ ํ•œ๋‹ค. 
        moneyValidation.checkNumRange(money, 1, 1000000);
    }
}

- ์•ฝ๊ฐ„... ๋ฐฑ์ค€์—์„œ ๋ณผ ๊ฒƒ ๊ฐ™์€ ๋ฌธ์ œ์˜€๋‹ค.


| 6๋ฒˆ ๋ฌธ์ œ

- 6๋ฒˆ๋ถ€ํ„ฐ ์ ์  ๋‚œ์ด๋„๊ฐ€ ์–ด๋ ค์›Œ์กŒ๋˜ ๋Š๋‚Œ... 

ํ•˜์ง€๋งŒ ๊ฐœ์ธ์ ์œผ๋กœ Map, Set ๊ฐ™์€ ์ž๋ฃŒ๊ตฌ์กฐ๋ฅผ ์ด์šฉํ•œ ๋ฌธ์ œ ํ‘ธ๋Š” ๊ฑธ ๋„ˆ๋ฌด ์ข‹์•„ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ํ’€๋ฉด์„œ ์žฌ๋ฐŒ์—ˆ๋‹ค ใ…Žใ…Ž

package onboarding;

        import onboarding.common.ValidationUtil;
        import onboarding.exception.InputRangeException;
        import onboarding.exception.InputTypeException;

        import java.util.*;
        import java.util.regex.Pattern;

public class Problem6 {
    // ๋ถ€๋ถ„ ๋‹‰๋„ค์ž„ ๋ฌธ์ž์—ด๊ณผ ์ด๋ฉ”์ผ ์ •๋ณด๋ฅผ ๋‹ด์€ ๋งต์ด๋‹ค.
    static Map<String, String> partNameWithEmail;

    // ๊ฒฐ๊ณผ ์ด๋ฉ”์ผ์„ ๋‹ด๋Š” TreeSet์ด๋‹ค.
    static Set<String> answer;

    public static List<String> solution(List<List<String>> forms) {
        partNameWithEmail = new HashMap<>();
        answer = new TreeSet<>();

        // ํฌ๋ฃจ์˜ ์ธ์› ์ˆ˜์— ๋Œ€ํ•ด์„œ ๊ฒ€์ฆํ•œ๋‹ค.
        verifyCrewNum(forms);

        for(List<String> form : forms) {
            // ํฌ๋ฃจ๋“ค์˜ ์ด๋ฉ”์ผ๊ณผ ๋‹‰๋„ค์ž„ ์ •๋ณด๋ฅผ ๊ฐ๊ฐ ์ถ”์ถœํ•œ๋‹ค.
            String email = form.get(0);
            // ์ด๋ฉ”์ผ์— ๋Œ€ํ•œ ๋„๋ฉ”์ธ ๊ฒ€์ฆ์„ ์ง„ํ–‰ํ•œ๋‹ค.
            checkEmailCond(email);

            String nickname = form.get(1);
            // ๋‹‰๋„ค์ž„์— ๋Œ€ํ•œ ์กฐ๊ฑด์„ ๊ฒ€์ฆํ•œ๋‹ค.
            checkNicknameCond(nickname);

            // ์ด๋ฉ”์ผ์„ ํ•œ ๊ธ€์ž์”ฉ ๋Š์–ด์„œ ๋ฐฐ์—ด๋กœ ๋งŒ๋“ ๋‹ค.
            String[] nameArr = nickname.split("");

            // 2๊ธ€์ž์”ฉ ๋Š์–ด์„œ ๋ถ€๋ถ„ ๋‹‰๋„ค์ž„์„ ์ƒ์„ฑํ•œ ๋’ค ์ค‘๋ณต์„ ์ฒดํฌํ•œ๋‹ค.
            createPartNicknameAndCheck(email, nameArr);
        }

        // ๊ฒฐ๊ณผ๋ฅผ ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌํ•œ๋‹ค.
        List<String> answerList = new ArrayList<>(answer);
        answerList.sort(String::compareTo);

        return answerList;
    }

    /**
     * ์ž…๋ ฅ๋ฐ›์€ ํฌ๋ฃจ์˜ ์ธ์›์ˆ˜์— ๋Œ€ํ•ด์„œ ๊ฒ€์ฆํ•œ๋‹ค.
     *
     * @param forms ํฌ๋ฃจ ๋ฆฌ์ŠคํŠธ
     */
    private static void verifyCrewNum(List<List<String>> forms) {
        ValidationUtil crewSizeValidation = new ValidationUtil();
        crewSizeValidation.addVarName("crew member size");
        // ํฌ๋ฃจ์˜ ์‚ฌ์ด์ฆˆ๋Š” 1~10000 ์‚ฌ์ด์ด๋‹ค.
        crewSizeValidation.checkNumRange(forms.size(), 1, 10000);
    }

    /**
     * ๋‹‰๋„ค์ž„์— ๋Œ€ํ•œ ํ˜•์‹์„ ๊ฒ€์ฆํ•œ๋‹ค.
     *
     * @param nickname ์ž…๋ ฅ๋ฐ›์€ ๋‹‰๋„ค์ž„
     */
    private static void checkNicknameCond(String nickname) {
        // ๋‹‰๋„ค์ž„์€ ํ•œ๊ธ€๋กœ๋งŒ ์ด๋ฃจ์–ด์ ธ ์žˆ์œผ๋ฉฐ, ๊ธธ์ด๋Š” 1~19 ์‚ฌ์ด์—ฌ์•ผ ํ•œ๋‹ค.
        String regex = "^[ใ„ฑ-ใ…Žใ…-ใ…ฃ๊ฐ€-ํžฃ]{1,19}$";
        if (!Pattern.matches(regex, nickname)) {
            throw new InputRangeException("๋‹‰๋„ค์ž„์€ ํ•œ๊ธ€๋กœ๋งŒ ์ด๋ฃจ์–ด์ ธ์•ผ ํ•˜๋ฉฐ, ๊ธธ์ด๋Š” 1~19์ž๊นŒ์ง€ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค.");
        }
    }

    /**
     * ์ด๋ฉ”์ผ์— ๋Œ€ํ•œ ํ˜•์‹์„ ๊ฒ€์ฆํ•œ๋‹ค.
     *
     * @param email ์ž…๋ ฅ๋ฐ›์€ ์ด๋ฉ”์ผ
     */
    private static void checkEmailCond(String email) {
        // ์ด๋ฉ”์ผ์€ @email.com ๋„๋ฉ”์ธ์œผ๋กœ ๊ตฌ์„ฑ๋˜์–ด ์žˆ์œผ๋ฉฐ, ์ „์ฒด ๊ธธ์ด๋Š” 11~20์ž ๋ฏธ๋งŒ์ด๋‹ค.
        // @email.com์ด 10์ž์ด๊ธฐ ๋•Œ๋ฌธ์—, ์•ž๋‹จ์˜ ์•„์ด๋””๋Š” 1~9์ž๊นŒ์ง€ ๋  ์ˆ˜ ์žˆ๋‹ค.
        String regex = "^[a-zA-Z0-9]{1,9}@email[.]com$";

        if (!Pattern.matches(regex, email)) {
            throw new InputTypeException("์ด๋ฉ”์ผ์€ 11~20์ž ์‚ฌ์ด์ด๋ฉฐ, ๋„๋ฉ”์ธ์€ @email.com๋กœ ์ œํ•œ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค.");
        }
    }

    /**
     * ๋‹‰๋„ค์ž„ ๋ฐฐ์—ด๋กœ๋ถ€ํ„ฐ 2๊ธ€์ž์”ฉ ์ž๋ฅด๊ณ  ์ค‘๋ณต์„ ์ฒดํฌํ•œ๋‹ค.
     *
     * @param email ์‚ฌ์šฉ์ž์˜ ์ด๋ฉ”์ผ
     * @param nameArr ์‚ฌ์šฉ์ž์˜ ๋‹‰๋„ค์ž„ ๋ฐฐ์—ด
     */
    private static void createPartNicknameAndCheck(String email, String[] nameArr) {
        for (int i = 0; i < nameArr.length-1; i++) {
            String partNickname = nameArr[i] + nameArr[i+1];
            // ๋ถ€๋ถ„ ๋‹‰๋„ค์ž„ ๋ฌธ์ž์—ด์— ๋Œ€ํ•œ ์ค‘๋ณต ์ฒดํฌ๋ฅผ ์ง„ํ–‰ํ•œ๋‹ค.
            checkDuplicatedAndAddResult(email, partNickname);
            partNameWithEmail.put(partNickname, email);
        }
    }

    /**
     * ๋ถ€๋ถ„ ๋‹‰๋„ค์ž„ ๋ฌธ์ž์—ด์— ๋Œ€ํ•œ ์ค‘๋ณต ์ฒดํฌ๋ฅผ ์ง„ํ–‰ํ•œ ๋’ค ๊ฒฐ๊ณผ Set์— ๋‹ด๋Š”๋‹ค.
     *
     * @param email ์‚ฌ์šฉ์ž์˜ ์ด๋ฉ”์ผ
     * @param partNickname ์‚ฌ์šฉ์ž์˜ ๋ถ€๋ถ„ ๋‹‰๋„ค์ž„ ๋ฌธ์ž์—ด
     */
    private static void checkDuplicatedAndAddResult(String email, String partNickname) {
        if(partNameWithEmail.containsKey(partNickname)) {
            // ํ•ด๋‹น ์‚ฌ์šฉ์ž์˜ ์ด๋ฉ”์ผ์„ ๊ฒฐ๊ณผ Set์— ์ถ”๊ฐ€ํ•œ๋‹ค.
            answer.add(email);
            // ์ค‘๋ณต๋œ ๋ถ€๋ถ„ ๋ฌธ์ž์—ด์„ ๊ฐ€์ง„ ์‚ฌ์šฉ์ž์˜ ์ด๋ฉ”์ผ์„ ๊ฒฐ๊ณผ Set์— ์ถ”๊ฐ€ํ•œ๋‹ค.
            answer.add(partNameWithEmail.get(partNickname));
        }
    }
}

- ์—ญ์‹œ ๊ธฐ์–ต์— ๋‚จ๋Š” ๊ฑด ์ •๊ทœ์‹...

์ œํ•œ์‚ฌํ•ญ ๊ฒ€์ฆ์„ ์ฝ”๋“œ์— ๋„ฃ์ง€ ์•Š์•˜๋‹ค๋ฉด ์‚ฌ์‹ค ํ•„์š”์—†๋Š” ๋ถ€๋ถ„์ด์—ˆ์ง€๋งŒ, ๋„ฃ๊ฒŒ ๋˜๋ฉด์„œ ์ •๊ทœ์‹๋„ ์—„์ฒญ ์ฐพ์•„๊ฐ€๋ฉด์„œ ๊ณต๋ถ€ํ–ˆ๋‹ค. ใ…‹ใ…‹ใ…‹ใ…‹

^[]{} : ์ด๋Ÿฐ ์‹์œผ๋กœ ์•ž์— ํŒจํ„ด์„ ๋„ฃ๊ณ , ๋’ค์— ๊ธธ์ด๋ฅผ ๋„ฃ์œผ๋ฉด ํ•ด๋‹น ๊ธธ์ด + ํ˜•์‹๊นŒ์ง€ ๊ฒ€์ฆํ•  ์ˆ˜ ์žˆ๋Š” ๊ฒŒ ๋„ˆ๋ฌด ์‹ ๊ธฐํ–ˆ๋‹ค...

์•„์ง ์ •๊ทœ์‹ ์“ฐ๋Š” ๊ฑด ์•ˆ ์ต์ˆ™ํ•ด์„œ, ์•ž์œผ๋กœ ๋” ์ต์ˆ™ํ•ด์ง€๋„๋ก ๋…ธ๋ ฅํ•ด์•ผ ๋  ๊ฒƒ ๊ฐ™๋‹ค.


| 7๋ฒˆ ๋ฌธ์ œ

- 7๋ฒˆ!! ์‚ฌ์‹ค ๊ฐ€์žฅ ์‹œ๊ฐ„์„ ๋งŽ์ด ์ผ๋˜ ๋ฌธ์ œ์ด๋‹ค. ์™œ๋ƒ๋ฉด ์ฒ˜์Œ์— ๋ฌธ์ œ ์ž์ฒด๋ฅผ ์ดํ•ด ๋ชป ํ•ด์„œ ์‹œ๊ฐ„ ๋‚ญ๋น„๋ฅผ ํ–ˆ๊ธฐ ๋•Œ๋ฌธ... ๐Ÿ˜…

'์‚ฌ์šฉ์ž์™€ ํ•จ๊ป˜ ์•„๋Š” ์นœ๊ตฌ์˜ ์ˆ˜' << ์‚ฌ์‹ค ์ด ๋ง์„ ์ž˜ ๋ณด๋ฉด ์ดํ•ดํ•  ์ˆ˜ ์žˆ๋Š”๋ฐ ์ด๋•Œ๋Š” ์‚ฌ์šฉ์ž์˜ ์นœ๊ตฌ์˜ ์นœ๊ตฌ๋ผ๋Š” ๊ฑด๊ฐ€...? ์ด๋Ÿฌ๋ฉด์„œ ์—„์ฒญ ํ—ท๊ฐˆ๋ คํ–ˆ๋‹ค.

๊ทธ๋ƒฅ ์‚ฌ์šฉ์ž์™€ ์นœ๊ตฌ๊ฐ€ ์•„๋‹Œ ์ „์ฒด ์œ ์ € ์ค‘์—์„œ ์‚ฌ์šฉ์ž์™€ ์นœ๊ตฌ๊ฐ€ ์–ด๋Š ์ •๋„ ๊ฒน์น˜๋Š”์ง€ ์ฒดํฌํ•˜๋ฉด ๋˜๋Š” ๊ฑฐ์˜€๋Š”๋ฐ... ใ…Žใ…Ž

๊ทธ๋ž˜๋„ ์ด๊ฒƒ๋„ Map ์“ฐ๋Š” ๊ฑฐ์—ฌ์„œ ๊ตฌํ˜„ํ•˜๋ฉด์„œ ์žฌ๋ฐŒ์—ˆ๋‹ค. ใ…Žใ…Ž

package onboarding;

        import onboarding.common.ValidationUtil;

        import java.util.*;

public class Problem7 {

    // ์ „์ฒด ์‚ฌ์šฉ์ž์˜ ์นœ๊ตฌ ๊ด€๊ณ„๋ฅผ ์ •์˜ํ•  Map์„ ์„ ์–ธํ•œ๋‹ค.
    // key ๊ฐ’์œผ๋กœ ์‚ฌ์šฉ์ž์˜ ์•„์ด๋””, value ๊ฐ’์œผ๋กœ ๋ฆฌ์ŠคํŠธ๋กœ ํ•ด๋‹น ์‚ฌ์šฉ์ž์˜ ์นœ๊ตฌ ๋ฆฌ์ŠคํŠธ๊ฐ€ ๋“ค์–ด๊ฐ„๋‹ค.
    static Map<String, List<String>> friendMap;

    // ์ ์ˆ˜ ์ •๋ณด๋ฅผ ๊ด€๋ฆฌํ•  Map์„ ์„ ์–ธํ•œ๋‹ค.
    // key ๊ฐ’์œผ๋กœ ์‚ฌ์šฉ์ž์˜ ์•„์ด๋””, value ๊ฐ’์œผ๋กœ ์ ์ˆ˜๊ฐ€ ๋“ค์–ด๊ฐ„๋‹ค.
    static Map<String, Integer> scoreMap;

    // ์‚ฌ์šฉ์ž์˜ ์นœ๊ตฌ ๋ชฉ๋ก ๋ฆฌ์ŠคํŠธ๋ฅผ ๊ด€๋ฆฌํ•œ๋‹ค.
    static List<String> userFriendList;

    public static List<String> solution(String user, List<List<String>> friends, List<String> visitors) {
        friendMap = new HashMap<>();
        scoreMap = new HashMap<>();

        // ์ž…๋ ฅ๋ฐ›์€ ๋ฆฌ์ŠคํŠธ๋“ค์— ๋Œ€ํ•œ ๊ฒ€์ฆ ์ž‘์—…์„ ์ง„ํ–‰ํ•œ๋‹ค.
        verifyInputList(user, friends, visitors);

        // ์ „์ฒด ์‚ฌ์šฉ์ž์— ๋Œ€ํ•œ ์นœ๊ตฌ ๊ด€๊ณ„๋ฅผ ์ •์˜ํ•œ๋‹ค.
        for(List<String> relationship : friends) {
            // ๊ฐ ์›์†Œ์˜ ์‚ฌ์ด์ฆˆ๊ฐ€ 2์ธ์ง€ ๊ฒ€์ฆํ•œ๋‹ค.
            verifyRelationship(relationship);
            defineFriendship(relationship);
        }

        // ์‚ฌ์šฉ์ž์˜ ์นœ๊ตฌ ๋ชฉ๋ก์„ ์กฐํšŒํ•œ๋‹ค.
        userFriendList = friendMap.get(user);

        // ์นœ๊ตฌ ๊ด€๊ณ„ Map์—์„œ ์‚ฌ์šฉ์ž์˜ ์ •๋ณด๋ฅผ ์ œ๊ฑฐํ•œ๋‹ค. (๊ธฐ์ค€์ด ์‚ฌ์šฉ์ž์ด๊ธฐ ๋•Œ๋ฌธ)
        friendMap.remove(user);

        // ๋‹ค๋ฅธ ์‚ฌ์šฉ์ž์˜ ์นœ๊ตฌ ๋ฆฌ์ŠคํŠธ๋ฅผ ์กฐํšŒํ•˜์—ฌ ์‚ฌ์šฉ์ž์˜ ์นœ๊ตฌ ๋ฆฌ์ŠคํŠธ์™€ ๋น„๊ตํ•œ๋‹ค.
        for(String diffUser : friendMap.keySet()) {
            getFriendsOfDiffUserAndCompare(diffUser);
        }

        // ์‚ฌ์šฉ์ž์˜ ํƒ€์ž„๋ผ์ธ์„ ๋ฐฉ๋ฌธํ•œ ์‚ฌ์šฉ์ž์— ๋Œ€ํ•ด 1์ ์”ฉ ๋ถ€์—ฌํ•œ๋‹ค.
        addOneScoreToVisitors(visitors);

        // ์‚ฌ์šฉ์ž์™€ ์ด๋ฏธ ์นœ๊ตฌ์ธ ์‚ฌ์šฉ์ž๋Š” ์ถ”์ฒœ์—์„œ ์ œ์™ธํ•œ๋‹ค.
        removeAlreadyUserFriend();

        // ์ ์ˆ˜๊ฐ€ 0์ ์ธ ์‚ฌ์šฉ์ž๋Š” ์ œ๊ฑฐํ•œ๋‹ค.
        for(String recommendUser : scoreMap.keySet()) {
            removeUserWithZeroScore(recommendUser);
        }

        // ์ ์ˆ˜์™€ ์ด๋ฆ„ ์ˆœ์„œ๋Œ€๋กœ ์ •๋ ฌ์„ ์ง„ํ–‰ํ•œ๋‹ค.
        List<String> recommendUserList = new ArrayList<>(scoreMap.keySet());
        recommendUserList.sort(Problem7::sortByScoreAndName);

        // ์ตœ์ข…์ ์œผ๋กœ ์ถ”์ฒœํ•  ์ธ์›์„ ์„ ๋ณ„ํ•œ๋‹ค.
        return selectRecommendUsers(recommendUserList);
    }

    /**
     * ์นœ๊ตฌ ๊ด€๊ณ„ ๋ฆฌ์ŠคํŠธ์— ๋Œ€ํ•œ ๊ฒ€์ฆ ์ž‘์—…์„ ์ง„ํ–‰ํ•œ๋‹ค.
     *
     * @param relationship ์นœ๊ตฌ ๊ด€๊ณ„ ๋ฆฌ์ŠคํŠธ
     */
    private static void verifyRelationship(List<String> relationship) {
        ValidationUtil relationshipValidation = new ValidationUtil();
        relationshipValidation.addVarName("relationship size");
        // ๊ฐ ์›์†Œ์˜ ์‚ฌ์ด์ฆˆ๋Š” 2์—ฌ์•ผ ํ•œ๋‹ค.
        relationshipValidation.checkNumRange(relationship.size(), 2, 2);
    }

    /**
     * ์ž…๋ ฅ๋ฐ›์€ ๋ฆฌ์ŠคํŠธ๋“ค์— ๋Œ€ํ•œ ๊ฒ€์ฆ ์ž‘์—…์„ ์ง„ํ–‰ํ•œ๋‹ค.
     *
     * @param user ์‚ฌ์šฉ์ž ์•„์ด๋””
     * @param friends ์นœ๊ตฌ ๊ด€๊ณ„ ์ •๋ณด
     * @param visitors ์‚ฌ์šฉ์ž ํƒ€์ž„๋ผ์ธ ๋ฐฉ๋ฌธ ๊ธฐ๋ก
     */
    private static void verifyInputList(String user, List<List<String>> friends, List<String> visitors) {
        // ์ž…๋ ฅ์œผ๋กœ ๋ฐ›์€ user ๋ฌธ์ž์—ด์— ๋Œ€ํ•ด ๊ฒ€์ฆํ•œ๋‹ค.
        ValidationUtil userValidation = new ValidationUtil();
        userValidation.addVarName("user length");
        userValidation.checkNumRange(user.length(), 1, 30);

        // friends์˜ ์‚ฌ์ด์ฆˆ์— ๋Œ€ํ•ด์„œ ๊ฒ€์ฆํ•œ๋‹ค.
        ValidationUtil friendsValidation = new ValidationUtil();
        friendsValidation.addVarName("friends size");
        friendsValidation.checkNumRange(friends.size(), 1, 10000);

        // visitors ์‚ฌ์ด์ฆˆ์— ๋Œ€ํ•ด์„œ ๊ฒ€์ฆํ•œ๋‹ค.
        ValidationUtil visitorsValidation = new ValidationUtil();
        visitorsValidation.addVarName("visitors size");
        visitorsValidation.checkNumRange(visitors.size(), 0, 10000);
    }

    /**
     * ์ตœ์ข…์ ์œผ๋กœ ์ถ”์ฒœํ•  ์‚ฌ์šฉ์ž๋ฅผ ๋ฝ‘๋Š”๋‹ค.
     *
     * @param recommendUserList ์ •๋ ฌ๋œ ์‚ฌ์šฉ์ž ๋ฆฌ์ŠคํŠธ
     * @return ์ถ”์ฒœํ•  ์‚ฌ์šฉ์ž ๋ฆฌ์ŠคํŠธ๋ฅผ ๋ฆฌํ„ดํ•œ๋‹ค.
     */
    private static List<String> selectRecommendUsers(List<String> recommendUserList) {
        List<String> answer = new ArrayList<>();

        // ๊ธฐ์กด ์ถ”์ฒœ ๋ฆฌ์ŠคํŠธ์— ๋‹ด๊ธด ์ธ์›์ด 5๋ช…๋ณด๋‹ค ์ž‘์œผ๋ฉด ๊ทธ๋Œ€๋กœ, ์•„๋‹ˆ๋ผ๋ฉด ์ตœ๋Œ€ 5๋ช…๊นŒ์ง€ ์„ ๋ณ„ํ•œ๋‹ค.
        int numberOfUser = Math.min(recommendUserList.size(), 5);

        for(int i=0; i<numberOfUser; i++) {
            answer.add(recommendUserList.get(i));
        }

        return answer;
    }

    /**
     * ์ถ”์ฒœ ๋ฆฌ์ŠคํŠธ๋ฅผ ์ ์ˆ˜(๋‚ด๋ฆผ์ฐจ์ˆœ)์™€ ์ด๋ฆ„(์‚ฌ์ „์ˆœ)์— ๋”ฐ๋ผ ์ •๋ ฌํ•œ๋‹ค.
     *
     * @param userA ๋น„๊ตํ•  ์‚ฌ์šฉ์ž A
     * @param userB ๋น„๊ตํ•  ์‚ฌ์šฉ์ž B
     * @return ์ •๋ ฌ ๊ฒฐ๊ณผ์— ๋”ฐ๋ฅธ -1, 0(๋ณ€๊ฒฝํ•˜์ง€ ์•Š์Œ), 1(์ž๋ฆฌ ๋ณ€๊ฒฝ)์ด ๋ฆฌํ„ด๋œ๋‹ค.
     */
    private static int sortByScoreAndName(String userA, String userB) {
        // ์ ์ˆ˜๊ฐ€ ๊ฐ™๋‹ค๋ฉด ์ด๋ฆ„์ˆœ์œผ๋กœ ์ •๋ ฌํ•œ๋‹ค.
        if (Objects.equals(scoreMap.get(userA), scoreMap.get(userB))) {
            return userA.compareTo(userB);
        }
        // ๊ทธ๊ฒŒ ์•„๋‹ˆ๋ผ๋ฉด ์ ์ˆ˜๋ฅผ ๊ธฐ์ค€์œผ๋กœ ๋‚ด๋ฆผ์ฐจ์ˆœ ์ •๋ ฌํ•œ๋‹ค.
        return scoreMap.get(userB) - scoreMap.get(userA);
    }

    /**
     * ์ ์ˆ˜๊ฐ€ 0์ ์ธ ์‚ฌ์šฉ์ž๋Š” ์ ์ˆ˜ Map์—์„œ ์ œ๊ฑฐํ•œ๋‹ค.
     *
     * @param recommendUser ์ถ”์ฒœ ํ›„๋ณด ์‚ฌ์šฉ์ž ์•„์ด๋””
     */
    private static void removeUserWithZeroScore(String recommendUser) {
        if (scoreMap.get(recommendUser) == 0) {
            scoreMap.remove(recommendUser);
        }
    }

    /**
     * ์‚ฌ์šฉ์ž์™€ ์ด๋ฏธ ์นœ๊ตฌ์ธ ๊ฒฝ์šฐ ์ œ๊ฑฐํ•œ๋‹ค.
     */
    private static void removeAlreadyUserFriend() {
        for(String userFriend : userFriendList) {
            scoreMap.remove(userFriend);
        }
    }

    /**
     * ์‚ฌ์šฉ์ž์˜ ํƒ€์ž„๋ผ์ธ์„ ๋ฐฉ๋ฌธํ•œ ์‚ฌ์šฉ์ž์—๊ฒŒ 1์ ์”ฉ ๋ถ€์—ฌํ•œ๋‹ค.
     *
     * @param visitors ์‚ฌ์šฉ์ž์˜ ํƒ€์ž„๋ผ์ธ ๋ฐฉ๋ฌธ์ž
     */
    private static void addOneScoreToVisitors(List<String> visitors) {
        for(String visitor : visitors) {
            scoreMap.put(visitor, scoreMap.getOrDefault(visitor,0) + 1);
        }
    }

    /**
     * ๋‹ค๋ฅธ ์‚ฌ์šฉ์ž์˜ ์นœ๊ตฌ ๋ฆฌ์ŠคํŠธ๋ฅผ ์กฐํšŒํ•˜์—ฌ ์‚ฌ์šฉ์ž์˜ ์นœ๊ตฌ ๋ฆฌ์ŠคํŠธ์™€ ๋น„๊ตํ•œ๋‹ค.
     *
     * @param diffUser ๋‹ค๋ฅธ ์‚ฌ์šฉ์ž์˜ ์•„์ด๋””
     */
    private static void getFriendsOfDiffUserAndCompare(String diffUser) {
        List<String> diffFriendList = friendMap.get(diffUser);
        for(String friendOfDiffUser : diffFriendList) {
            addTenScore(diffUser, friendOfDiffUser);
        }
    }

    /**
     * ์‚ฌ์šฉ์ž์˜ ์นœ๊ตฌ ๋ฆฌ์ŠคํŠธ์™€ ๋™์ผํ•œ ์นœ๊ตฌ๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ๋‹ค๋ฉด, ํ•ด๋‹น ์‚ฌ์šฉ์ž์—๊ฒŒ 10์ ์„ ๋ถ€์—ฌํ•œ๋‹ค.
     *
     * @param diffUser ๋‹ค๋ฅธ ์‚ฌ์šฉ์ž ์•„์ด๋””
     * @param friendOfDiffUser ๋‹ค๋ฅธ ์‚ฌ์šฉ์ž์˜ ์นœ๊ตฌ
     */
    private static void addTenScore(String diffUser, String friendOfDiffUser) {
        if (userFriendList.contains(friendOfDiffUser)) {
            scoreMap.put(diffUser, scoreMap.getOrDefault(diffUser, 0) + 10);
        }
    }

    /**
     * ์ „์ฒด ์‚ฌ์šฉ์ž์˜ ์นœ๊ตฌ ๋ชฉ๋ก์„ ์นœ๊ตฌ ๊ด€๊ณ„ Map์— ์ €์žฅํ•œ๋‹ค.
     *
     * @param relationship ์นœ๊ตฌ ๊ด€๊ณ„ ์ •๋ณด
     */
    private static void defineFriendship(List<String> relationship) {
        String userA = relationship.get(0);
        String userB = relationship.get(1);

        // ์•„์ด๋””์— ๋Œ€ํ•œ ๊ฒ€์ฆ์„ ์ง„ํ–‰ํ•œ๋‹ค.
        idValidation(userA, userB);

        // ๋งŒ์•ฝ ์ฒ˜์Œ ์‚ฝ์ž…๋œ๋‹ค๋ฉด ์ดˆ๊ธฐํ™” ์ž‘์—…์„ ์ง„ํ–‰ํ•ด์ค€๋‹ค.
        List<String> friendListOfUserA = friendMap.getOrDefault(userA, new ArrayList<>());
        // ์นœ๊ตฌ ์ •๋ณด๋ฅผ ์ถ”๊ฐ€ํ•œ๋‹ค.
        friendListOfUserA.add(userB);

        List<String> friendListOfUserB = friendMap.getOrDefault(userB, new ArrayList<>());
        friendListOfUserB.add(userA);

        // ์นœ๊ตฌ ๊ด€๊ณ„ Map์— ์ถ”๊ฐ€ํ•œ๋‹ค.
        friendMap.put(userA, friendListOfUserA);
        friendMap.put(userB, friendListOfUserB);
    }

    /**
     * ์‚ฌ์šฉ์ž์˜ ์•„์ด๋””์— ๋Œ€ํ•œ ๊ฒ€์ฆ ์ž‘์—…์„ ์ง„ํ–‰ํ•œ๋‹ค.
     *
     * @param userA ์‚ฌ์šฉ์ž A
     * @param userB ์‚ฌ์šฉ์ž B
     */
    private static void idValidation(String userA, String userB) {
        // ์‚ฌ์šฉ์ž์˜ ์•„์ด๋””์˜ ๊ธธ์ด๋Š” 1~30 ์‚ฌ์ด์—ฌ์•ผ ํ•˜๋ฉฐ, ์†Œ๋ฌธ์ž๋กœ ์ด๋ฃจ์–ด์ ธ ์žˆ๋‹ค.
        ValidationUtil userAValidation = new ValidationUtil();
        userAValidation.addVarName("userId");
        userAValidation.checkStrLowerCase(userA, 1, 30);

        ValidationUtil userBValidation = new ValidationUtil();
        userBValidation.addVarName("userId");
        userBValidation.checkStrLowerCase(userB, 1, 30);
    }
}

- ๊ฐœ์ธ์ ์œผ๋กœ ์ด๋ ‡๊ฒŒ ์š”๊ตฌ์‚ฌํ•ญ ์ž”๋œฉ ์ฃผ๊ณ  ์ž๋ฃŒํ˜•์— ๋„ฃ๋‹ค ๋บ๋‹ค ํ•˜๋Š” ๋ฌธ์ œ๊ฐ€ ์žฌ๋ฐŒ๋Š” ๊ฒƒ ๊ฐ™๋‹ค.

์ด ๋ฌธ์ œ๋ฅผ ์ฒ˜์Œ ์ ‘ํ•œ ์‚ฌ๋žŒ์ด์—ˆ๋‹ค๋ฉด ์ด ๋ฌธ์ œ ํ•˜๋‚˜ ํ•ด๊ฒฐํ•จ์œผ๋กœ์„œ Map์€ ์™„๋ฒฝ ๋งˆ์Šคํ„ฐ ํ•  ์ˆ˜ ์žˆ์ง€ ์•Š์„๊นŒ... ๋ผ๊ณ  ์ƒ๊ฐํ–ˆ๋‹ค!

 


| ๊ณตํ†ต ํด๋ž˜์Šค

- ๋ฆฌํŒฉํ† ๋ง ํ•˜๋ฉด์„œ ๋งŒ๋“ค์—ˆ๋˜ ๊ณตํ†ต ํด๋ž˜์Šค!

package onboarding.common;

/**
 * ์ž…๋ ฅ์œผ๋กœ ๋“ค์–ด์˜จ ๋ฌธ์ž์—ด์— ๋Œ€ํ•œ ํ†ตํ•ฉ ๊ด€๋ฆฌ ๊ธฐ๋Šฅ์„ ์ œ๊ณตํ•˜๋Š” ํด๋ž˜์Šค.
 */
public class InputStringUtil {

    /**
     * ๋ฌธ์ž์—ด์„ charํ˜• ๋ฐฐ์—ด๋กœ ๋ถ„ํ•ดํ•˜์—ฌ ๋ฆฌํ„ดํ•œ๋‹ค.
     *
     * @param str ๋ฌธ์ž์—ด
     * @return char[] - ์ž…๋ ฅ์œผ๋กœ ๋ฐ›์€ ๋ฌธ์ž์—ด์— ๋Œ€ํ•ด charํ˜• ๋ฐฐ์—ด๋กœ ์ชผ๊ฐ  ๊ฒฐ๊ณผ๋ฅผ ๋ฆฌํ„ดํ•œ๋‹ค.
     */
    public static char[] getCharArr(String str) {
        return str.toCharArray();

    }
}

- ๋ฌธ์ž์—ด์„ ๋ฐฐ์—ด๋กœ ๋ฐ›๋Š” ๋ถ€๋ถ„์ด ์ƒ๋‹นํžˆ ๋งŽ์•„์„œ ๊ทธ๋ƒฅ ๋ถ„๋ฆฌํ–ˆ๋Š”๋ฐ... ์‚ฌ์‹ค ์ด ์ •๋„๊นŒ์ง€๋Š” ๋ถ„๋ฆฌ ์•ˆ ํ•ด๋„ ๋์ง€ ์•Š์•˜์„๊นŒ ์‹ถ๋‹ค. ใ…Žใ…Žใ…Ž

package onboarding.common;

        import onboarding.exception.InputRangeException;
        import onboarding.exception.InputTypeException;

        import java.util.regex.Pattern;

/**
 * ์ž…๋ ฅ์œผ๋กœ ๋“ค์–ด์˜จ ๊ฐ’์— ๋Œ€ํ•ด ๊ณตํ†ต ๊ฒ€์ฆ ๊ธฐ๋Šฅ์„ ์ œ๊ณตํ•˜๋Š” ํด๋ž˜์Šค.
 */
public class ValidationUtil {

    private String varName;

    /**
     * ์˜ค๋ฅ˜ ๋ฉ”์‹œ์ง€ ์ „๋‹ฌ ์‹œ ์‚ฌ์šฉํ•  ์ž…๋ ฅ ๋ณ€์ˆ˜ ์ด๋ฆ„ ์„ค์ • ๋ฉ”์„œ๋“œ
     *
     * @param varName ์ž…๋ ฅํ•œ ๋ณ€์ˆ˜ ์ด๋ฆ„
     */
    public void addVarName(String varName) {
        this.varName = varName;
    }

    /**
     * ์ž…๋ ฅ๋ฐ›์€ number์˜ ๋ฒ”์œ„์— ๋Œ€ํ•œ ๊ฒ€์ฆ์„ ์ง„ํ–‰ํ•œ๋‹ค.
     *
     * @param number ์ž…๋ ฅ์œผ๋กœ ๋“ค์–ด์˜จ number
     * @param start ์‹œ์ž‘ ๋ฒ”์œ„
     * @param end ๋ ๋ฒ”์œ„
     */
    public void checkNumRange(int number, int start, int end) {
        if (number < start || number > end) {
            if (start == end)
                throw new InputRangeException(this.varName + "์€(๋Š”) " + start + "๋กœ ์ œํ•œ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค.");

            throw new InputRangeException(this.varName + "์€(๋Š”) " + start + "~" + end + " ์‚ฌ์ด๋กœ ์ œํ•œ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค.");
        }
    }

    /**
     * ์ž…๋ ฅ๋ฐ›์€ ๋ฌธ์ž์—ด์ด ์†Œ๋ฌธ์ž๋กœ ๊ตฌ์„ฑ๋˜์–ด ์žˆ๋Š”์ง€ ๊ฒ€์ฆํ•œ๋‹ค.
     *
     * @param str ์ž…๋ ฅ๋ฐ›์€ ๋ฌธ์ž์—ด
     * @param startLen ์‹œ์ž‘ ๊ธธ์ด
     * @param endLen ๋ ๊ธธ์ด
     */
    public void checkStrLowerCase(String str, int startLen, int endLen) {
        String regex = "^[a-z]{" + startLen + "," + endLen + "}$";

        if (!Pattern.matches(regex, str)) {
            throw new InputTypeException(this.varName + "์˜ ๊ธธ์ด๋Š” " + startLen + "~" + endLen +
                    " ์‚ฌ์ด๋กœ ์ œํ•œ๋˜์–ด ์žˆ์œผ๋ฉฐ, ์†Œ๋ฌธ์ž๋กœ๋งŒ ๊ตฌ์„ฑ๋˜์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.");
        }
    }
}

- ๊ฐœ์ธ์ ์œผ๋กœ ์ด๋ฒˆ ์ฃผ์ฐจ์—์„œ ๋˜ ๊ณ ๋ฏผํ–ˆ๋˜ ๊ฒ€์ฆ ํด๋ž˜์Šค!

๊ฒ€์ฆ ๋ฉ”์‹œ์ง€๋ฅผ ์ฒ˜๋ฆฌํ•  ๋•Œ ๋ณ€์ˆ˜ ์ด๋ฆ„์„ ํ•จ๊ป˜ ์ถœ๋ ฅํ•ด์ฃผ๊ณ  ์‹ถ์–ด์„œ ํด๋ž˜์Šค ๋‚ด๋ถ€ ๋ณ€์ˆ˜๋กœ varName์„ ์„ ์–ธํ•ด ๊ด€๋ฆฌํ–ˆ๋‹ค.

๊ฐœ์ธ์ ์œผ๋กœ ์†Œ๋ฌธ์ž ๊ฒ€์ฆ๋„ ํŒŒ๋ผ๋ฏธํ„ฐ๋กœ ๊ธธ์ด๋ฅผ ๋ฐ›์•„์„œ ์ฒ˜๋ฆฌํ•ด๋‘” ๊ฒŒ ๋งˆ์Œ์— ๋“ ๋‹ค!

 


| ์†Œ๊ฐ

- ๊นƒํ—™์—๋„ ์˜ฌ๋ ค๋‘” ๊ฑฐ์ง€๋งŒ ๋”ฐ๋กœ ์ €์žฅํ•ด๋‘๊ณ  ์‹ถ์–ด์„œ ์ ์–ด๋‘๋Š” ์†Œ๊ฐ :D

๋งˆํฌ๋‹ค์šด์ด ํ‹ฐ์Šคํ† ๋ฆฌ์—์„œ ๋‹ค์†Œ ๊นจ์ง€๋Š” ๊ฒฝํ–ฅ์ด ์žˆ๋Š” ๊ฒŒ ์‹ซ์–ด์„œ ์บก์ณ๋กœ ์˜ฌ๋ ค๋‘”๋‹ค.

 

1์ฃผ์ฐจ๋Š” ์ด๋Ÿฐ ์‹์œผ๋กœ ์ฝ”๋“œ ๊ตฌํ˜„์„ ํ–ˆ์—ˆ๋Š”๋ฐ... ์ผ์ฃผ์ผ ๋™์•ˆ ์ฝ”๋“œ ๋ฐ”๋ผ๋ณด๋ฉด์„œ ์ˆ˜์ •ํ•œ ๊ฒฐ๊ณผ์ธ ๋งŒํผ ๊ฝค ๋ฟŒ๋“ฏํ–ˆ๋‹ค.

์‚ฌ์‹ค ์–ผ๋ฅธ 2์ฃผ์ฐจ ํšŒ๊ณ  ์จ์•ผ๊ฒ ๋‹ค๋Š” ์ƒ๊ฐ๋ฟ์ด์ง€๋งŒ ใ…Žใ…Žใ…Ž ์•ž์œผ๋กœ๋Š” ๋ฐ€๋ฆฌ์ง€ ์•Š๊ณ  ์จ์•ผ๊ฒ ๋‹ค...

์•ž์œผ๋กœ ๋‚จ์€ ์‹œ๊ฐ„๋„ ํŒŒ์ดํŒ…! ๊ผญ ํ•ฉ๊ฒฉํ–ˆ์œผ๋ฉด ๐Ÿ˜Œ

 

Comments