Skip to content
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

๐Ÿš€ 1๋‹จ๊ณ„ - ๋ฌธ์ž์—ด ๋ง์…ˆ ๊ณ„์‚ฐ๊ธฐ #776

Open
wants to merge 11 commits into
base: sumiini
Choose a base branch
from

Conversation

sumiini
Copy link

@sumiini sumiini commented Jan 26, 2025

์•ˆ๋…•ํ•˜์„ธ์š”.
์š”๊ตฌ์‚ฌํ•ญ๋ณ„๋กœ ์ปค๋ฐ‹ํ•˜๋ ค ํ–ˆ์œผ๋‚˜, ๋ชจ๋“  ํ…Œ์ŠคํŠธ ์กฐ๊ฑด์— ๋งŒ์กฑ์‹œ์ผœ์•ผ ํ•œ๋‹ค๋Š” ์ƒ๊ฐ์— ๋ถ„๋ฆฌํ•˜์—ฌ ์ปค๋ฐ‹ํ•˜๋Š”๊ฒŒ ์–ด๋ ค์› ์Šต๋‹ˆ๋‹ค.

add ๋‚ด ๋งค๊ฐœ๋ณ€์ˆ˜ text๋Š” ๋ณ€๊ฒฝ ๋ถˆ๊ฐ€ํ•œ ๊ฐ’์ด๋ฏ€๋กœ final ์„ ์–ธ์ด ๋งž๋‹ค๊ณ  ์ƒ๊ฐํ–ˆ์Šต๋‹ˆ๋‹ค.
๋‚ด๋ถ€์˜ private ํ•จ์ˆ˜๋“ค ์ค‘ text๋ฅผ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ๋ฐ›๋Š” ๊ฒƒ๋“ค๋„ ๋ชจ๋‘ final์„ ์„ ์–ธํ•ด์•ผํ•˜๋Š”์ง€? ์•„๋‹ˆ๋ฉด, ์ด๋ฏธ add ๋งค๊ฐœ๋ณ€์ˆ˜์—์„œ final text๋กœ ๋“ค์–ด์™€์„œ ์ƒ๊ด€์—†๋Š”์ง€ ๊ถ๊ธˆํ•ฉ๋‹ˆ๋‹ค.

@sah3122
Copy link

sah3122 commented Jan 27, 2025

add ๋‚ด ๋งค๊ฐœ๋ณ€์ˆ˜ text๋Š” ๋ณ€๊ฒฝ ๋ถˆ๊ฐ€ํ•œ ๊ฐ’์ด๋ฏ€๋กœ final ์„ ์–ธ์ด ๋งž๋‹ค๊ณ  ์ƒ๊ฐํ–ˆ์Šต๋‹ˆ๋‹ค.
๋‚ด๋ถ€์˜ private ํ•จ์ˆ˜๋“ค ์ค‘ text๋ฅผ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ๋ฐ›๋Š” ๊ฒƒ๋“ค๋„ ๋ชจ๋‘ final์„ ์„ ์–ธํ•ด์•ผํ•˜๋Š”์ง€? ์•„๋‹ˆ๋ฉด, ์ด๋ฏธ add ๋งค๊ฐœ๋ณ€์ˆ˜์—์„œ final text๋กœ ๋“ค์–ด์™€์„œ ์ƒ๊ด€์—†๋Š”์ง€ ๊ถ๊ธˆํ•ฉ๋‹ˆ๋‹ค.

private ํ•จ์ˆ˜์— ํ•œ์ •๋˜์ง€ ์•Š๊ณ  ์ผ๋ฐ˜์ ์œผ๋กœ ๊ฐ’์ด ๋ถˆ๋ณ€ํ•œ์ง€ ๋ณ€๊ฒฝ ๊ฐ€๋Šฅํ•œ์ง€๋งŒ ํŒ๋‹จ ํ•ด๋ณด๋ฉด ๋ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค.
๋งค๊ฐœ ๋ณ€์ˆ˜๊ฐ€ ๋ถˆ๋ณ€ํ•˜๋‹ค๋ฉด final ์„ ์–ธ์„ ํ•˜๊ณ , ์ง€์—ญ ๋ณ€์ˆ˜๋ฅผ ์„ ์–ธํ•˜๋Š” ์ƒํ™ฉ์—์„œ๋„ ๋ถˆ๋ณ€ ๊ฐ’์„ ์œ ์ง€ํ•œ๋‹ค๋ฉด final ์„ ์–ธ์„ ํ•˜์—ฌ ๋ถˆ๋ณ€์„ฑ์„ ์œ ์ง€ ํ•˜๋Š”๊ฒƒ์ด ์˜ฌ๋ฐ”๋ฅด๋‹ค๊ณ  ์ƒ๊ฐํ•ด์š” ๐Ÿ˜„

Copy link

@sah3122 sah3122 left a comment

Choose a reason for hiding this comment

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

๋ฌธ์ž์—ด ๊ณ„์‚ฐ๊ธฐ ๊ตฌํ˜„ ์ž˜ํ•ด์ฃผ์…จ์Šต๋‹ˆ๋‹ค ๐Ÿ‘
๋‹ค์Œ ๋ฏธ์…˜์„ ์ง„ํ–‰ํ•˜๊ธฐ ์ „ ๋ช‡๊ฐ€์ง€ ์ƒ๊ฐ๊ฑฐ๋ฆฌ๋ฅผ ๋‚จ๊ฒจ๋‘์—ˆ์œผ๋‹ˆ ํ™•์ธ ๋ถ€ํƒ๋“œ๋ ค์š” ๐Ÿ˜„

private static final String DELIMITER = "//(.)\n(.*)";

public int add(final String text) {
if (text == null || text.isEmpty()) return 0;
Copy link

Choose a reason for hiding this comment

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

Suggested change
if (text == null || text.isEmpty()) return 0;
if (text == null || text.isEmpty()) {
return 0;
}

https://google.github.io/styleguide/javaguide.html#s4.1.1-braces-always-used

๊ตฌ๊ธ€ ์Šคํƒ€์ผ ๊ฐ€์ด๋“œ๋ฅผ ์ฐธ๊ณ ํ•˜์—ฌ ์ค‘๊ด„ํ˜ธ๋ฅผ ์ƒ๋žตํ•˜์ง€ ์•Š๊ณ  ์ž‘์„ฑํ•ด๋ณด์„ธ์š” ๐Ÿ˜„

try {
String[] numbers = parse(text);
return sumOfNumbers(numbers);
} catch (Exception e) {
Copy link

Choose a reason for hiding this comment

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

์ตœ์ƒ์œ„ Exception์„ catch ํ•œ๋‹ค๋ฉด ์–ด๋–ค ๋ฌธ์ œ์ ์ด ์žˆ์„๊นŒ์š” ?

}

private String[] parse(String text) {
Matcher m = Pattern.compile(DELIMITER).matcher(text);
Copy link

Choose a reason for hiding this comment

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

๋งค๋ฒˆ Pattern.compile(DELIMITER)์„ ํ†ตํ•ด ๊ฐ์ฒด๊ฐ€ ์ƒ์„ฑ๋˜์ง€ ์•Š๊ณ  ์žฌ์‚ฌ์šฉํ• ์ˆ˜ ์žˆ๋„๋ก ๋ณ€๊ฒฝํ•ด๋ณด๋Š”๊ฑด ์–ด๋–จ๊นŒ์š” ?

.sum();
}

private int validate(String number) {
Copy link

Choose a reason for hiding this comment

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

์ด๋ฒˆ ๊ณผ์ •์„ ์ง„ํ–‰ํ•˜๋ฉฐ ์ตœ๋Œ€ํ•œ ์ž‘์€ ๋‹จ์œ„์˜ ํด๋ž˜์Šค๋ฅผ ๋„์ถœํ•ด๋ณด๋Š”๊ฒƒ์„ ๊ถŒ์žฅ๋“œ๋ฆฝ๋‹ˆ๋‹ค.
์–‘์ˆ˜๋ฅผ ๋ณด์žฅํ• ์ˆ˜ ์žˆ๋Š” ํด๋ž˜์Šค๋ฅผ ์ถ”๊ฐ€ํ•ด๋ณด์„ธ์š” ๐Ÿ˜„

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class StringCalculator {
Copy link

Choose a reason for hiding this comment

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

๋ฌธ์ž์—ด ๊ณ„์‚ฐ๊ธฐ ๊ตฌํ˜„ ์ž˜ํ•ด์ฃผ์…จ์Šต๋‹ˆ๋‹ค ๐Ÿ‘
ํ•œ๊ฐ€์ง€ ๊ฐœ์„ ์‚ฌํ•ญ์œผ๋กœ ํ˜„์žฌ StringCalculator ๊ฐ€ ๋„ˆ๋ฌด ๋งŽ์€ ์—ญํ• ์„ ๋‹ด๋‹นํ•˜๊ณ  ์žˆ๋‹ค๊ณ  ์ƒ๊ฐ๋ฉ๋‹ˆ๋‹ค.
๋ฌธ์ž์—ด ํŒŒ์‹ฑ์„ ๋‹ด๋‹นํ•˜๋Š” ํด๋ž˜์Šค, ๊ณ„์‚ฐ์„ ๋‹ด๋‹นํ•˜๋Š” ํด๋ž˜์Šค ๋“ฑ์œผ๋กœ ์—ญํ• ์„ ๋‚˜๋ˆ„๋Š” ์—ฐ์Šต์„ ํ•ด๋ณด์„ธ์š” !

private int validate(String number) {
try {
int num = Integer.parseInt(number);
if (num < 0) {
Copy link

Choose a reason for hiding this comment

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

๋งค์ง๋„˜๋ฒ„๋Š” ์˜๋ฏธ๋ฅผ ๊ฐ€์ง€๋Š” ์ƒ์ˆ˜๋กœ ์ •์˜ํ•˜์—ฌ ๊ฐ€๋…์„ฑ์„ ๋†’ํ˜€๋ณด์„ธ์š” ๐Ÿ˜„
https://www.slipp.net/questions/356

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants