-
Notifications
You must be signed in to change notification settings - Fork 1.2k
๐ 2๋จ๊ณ - ๋ฌธ์์ด ๋ง์ ๊ณ์ฐ๊ธฐ #5891
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
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,56 @@ | ||||||||||||||
import java.util.Arrays; | ||||||||||||||
import java.util.Objects; | ||||||||||||||
import java.util.regex.Matcher; | ||||||||||||||
import java.util.regex.Pattern; | ||||||||||||||
|
||||||||||||||
public class StringAddCalculator { | ||||||||||||||
|
||||||||||||||
private static final String DELIMITER = "[,:;]"; | ||||||||||||||
private static final String DELIMITER_REGEX = "//(.)\n(.*)"; | ||||||||||||||
private static final Pattern PATTERN = Pattern.compile(DELIMITER_REGEX); | ||||||||||||||
|
||||||||||||||
public static int splitAndSum(String text) { | ||||||||||||||
if (Objects.isNull(text) || text.isBlank()) { | ||||||||||||||
return 0; | ||||||||||||||
} | ||||||||||||||
return sum(toInt(split(text))); | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
private static String[] split(String text) { | ||||||||||||||
Matcher matcher = PATTERN.matcher(text); | ||||||||||||||
if (!matcher.find()) { | ||||||||||||||
return text.split(DELIMITER); | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
String customDelimiter = matcher.group(1); | ||||||||||||||
String delimitedText = matcher.group(2); | ||||||||||||||
return delimitedText.split(customDelimiter); | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
private static int[] toInt(String[] texts) { | ||||||||||||||
return Arrays.stream(texts) | ||||||||||||||
.mapToInt(StringAddCalculator::toIntOrThrow) | ||||||||||||||
.toArray(); | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
private static int toIntOrThrow(String text) { | ||||||||||||||
try { | ||||||||||||||
int parsedValue = toInt(text); | ||||||||||||||
if (parsedValue < 0) { | ||||||||||||||
throw new NumberFormatException(); | ||||||||||||||
} | ||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ๋ต ํด๋น ์กฐ๊ฑด์ ์ ์ ํ ์์ธ์ธ์ง์ ๋ํ ํ๋จ์ ๋์น ๊ฒ ๊ฐ์ต๋๋ค. ๋ง์ํด์ฃผ์ ๋๋ก ์์ ํ๊ฒ ์ต๋๋ค. ๊ฐ์ฌํฉ๋๋ค! |
||||||||||||||
return parsedValue; | ||||||||||||||
} catch (NumberFormatException e) { | ||||||||||||||
throw new RuntimeException(); | ||||||||||||||
} | ||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ๋ณ๋ try/catch ๋ฌธ์ ํ ํ์ ์์๊ฒ ๊ฐ์์!
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ์! @hyoojuu ๋
NumberFormatException ์ Runtime ์ ์์ํ๊ณ ์๋ ์์ธ์ด๊ธฐ ๋๋ฌธ์ ๋ค์ ์์ธ๋ฅผ ์ ํํ ํ์๊ฐ ์์๊ฒ ๊ฐ์๋ณด์ฌ์ ํผ๋๋ฐฑ ๋จ๊ฒผ์ต๋๋น ๐ There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ์ ๋ต,, ์์ํ๊ณ ์๋ ๊ฒ์ ์๊ณ ์์๋๋ฐ |
||||||||||||||
} | ||||||||||||||
|
||||||||||||||
private static int toInt(String text) { | ||||||||||||||
return Integer.parseInt(text); | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
private static int sum(int[] values) { | ||||||||||||||
return Arrays.stream(values) | ||||||||||||||
.sum(); | ||||||||||||||
} | ||||||||||||||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,15 +24,13 @@ void setUp() { | |
|
||
@Test | ||
void ์ปฌ๋ ์ _ํฌ๊ธฐ_๊ตฌํ๊ธฐ() { | ||
int size = this.numbers.size(); | ||
|
||
assertThat(size).isEqualTo(3); | ||
assertThat(this.numbers).hasSize(3); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ๐ |
||
} | ||
|
||
@ParameterizedTest | ||
@ValueSource(ints = {1, 2, 3}) | ||
void ์ซ์_ํฌํจ์ฌ๋ถ_ํ์ธ(int value) { | ||
assertThat(this.numbers.contains(value)).isTrue(); | ||
assertThat(this.numbers).contains(value); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ๐ |
||
} | ||
|
||
@ParameterizedTest | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import org.junit.jupiter.api.Test; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.assertj.core.api.Assertions.assertThatThrownBy; | ||
|
||
public class StringAddCalculatorTest { | ||
|
||
@Test | ||
public void splitAndSum_null_๋๋_๋น๋ฌธ์() { | ||
int result = StringAddCalculator.splitAndSum(null); | ||
assertThat(result).isEqualTo(0); | ||
|
||
result = StringAddCalculator.splitAndSum(""); | ||
assertThat(result).isEqualTo(0); | ||
} | ||
|
||
@Test | ||
public void splitAndSum_์ซ์ํ๋() throws Exception { | ||
int result = StringAddCalculator.splitAndSum("1"); | ||
assertThat(result).isEqualTo(1); | ||
} | ||
|
||
@Test | ||
public void splitAndSum_์ผํ๊ตฌ๋ถ์() throws Exception { | ||
int result = StringAddCalculator.splitAndSum("1,2"); | ||
assertThat(result).isEqualTo(3); | ||
} | ||
|
||
@Test | ||
public void splitAndSum_์ผํ_๋๋_์ฝ๋ก _๊ตฌ๋ถ์() throws Exception { | ||
int result = StringAddCalculator.splitAndSum("1,2:3"); | ||
assertThat(result).isEqualTo(6); | ||
} | ||
|
||
@Test | ||
public void splitAndSum_custom_๊ตฌ๋ถ์() throws Exception { | ||
int result = StringAddCalculator.splitAndSum("//;\n1;2;3"); | ||
assertThat(result).isEqualTo(6); | ||
} | ||
|
||
@Test | ||
public void splitAndSum_negative() throws Exception { | ||
assertThatThrownBy(() -> StringAddCalculator.splitAndSum("-1,2,3")) | ||
.isInstanceOf(RuntimeException.class); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
์ด๋ฒ ๋ฏธ์ ์์ ์ค์ํ ๊ธฐ๋ฅ์ ๋ด๋นํ๋ split ๋ฉ์๋์์ ๋๊ฐ์ง์ ํ์๋ฅผ ํ๊ณ ์๋๊ฒ ๊ฐ์์!
split ์ด๋ผ๋ ๋ฉ์๋๋ 2๋ฒ์ ํด๋นํ๋ ํ์๋ง ์กด์ฌํ๊ฒ ๋ฆฌํฉํ ๋ง ํ๋๊ฑด ์ด๋จ๊น์? ๐ค
์ ์ฒ๋ผ ๋ฆฌํฉํ ๋ง์ด ๋๋ ค๋ฉด ์๋์ ๊ฐ์ด ๋ฉ์๋๋ก ๋๋๋ ๋ฐฉ๋ฒ์ด ์์๊ฒ ๊ฐ์์ :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
๋ต ๋ง์ํด์ฃผ์ ๋๋ก 1, 2 ๊ฐ๊ฐ์ ํ์๋ง ํ๋ ๋ฉ์๋๋ก ๋ถ๋ฆฌํด๋ณด๊ฒ ์ต๋๋ค!