File tree Expand file tree Collapse file tree 2 files changed +64
-0
lines changed
main/java/io/github/gunkim/algorithm/basic
test/java/io/github/gunkim/algorithm/basic Expand file tree Collapse file tree 2 files changed +64
-0
lines changed Original file line number Diff line number Diff line change 1+ package io .github .gunkim .algorithm .basic ;
2+
3+ public class ReverseNumber {
4+ private final int value ;
5+
6+ public ReverseNumber (int value ) {
7+ this .value = value ;
8+ }
9+
10+ public int reverse () {
11+ if (value == 0 ) {
12+ return value ;
13+ }
14+ int number = Math .abs (value );
15+ int reversed = 0 ;
16+ while (number > 0 ) {
17+ reversed = (reversed * 10 ) + (number % 10 );
18+ number /= 10 ;
19+ }
20+
21+ return value < 0 ? -reversed : reversed ;
22+ }
23+ }
Original file line number Diff line number Diff line change 1+ package io .github .gunkim .algorithm .basic ;
2+
3+ import org .junit .jupiter .api .DisplayName ;
4+ import org .junit .jupiter .api .Test ;
5+
6+ import static org .assertj .core .api .Assertions .assertThat ;
7+
8+ @ DisplayName ("숫자 뒤집기" )
9+ class ReverseNumberTest {
10+ @ Test
11+ void testPositiveNumber () {
12+ var reverseNumber = new ReverseNumber (123 );
13+ int result = reverseNumber .reverse ();
14+
15+ assertThat (result ).isEqualTo (321 );
16+ }
17+
18+ @ Test
19+ void testNegativeNumber () {
20+ var reverseNumber = new ReverseNumber (-456 );
21+ int result = reverseNumber .reverse ();
22+
23+ assertThat (result ).isEqualTo (-654 );
24+ }
25+
26+ @ Test
27+ void testSingleDigit () {
28+ var reverseNumber = new ReverseNumber (7 );
29+ int result = reverseNumber .reverse ();
30+
31+ assertThat (result ).isEqualTo (7 );
32+ }
33+
34+ @ Test
35+ void testZero () {
36+ var reverseNumber = new ReverseNumber (0 );
37+ int result = reverseNumber .reverse ();
38+
39+ assertThat (result ).isEqualTo (0 );
40+ }
41+ }
You can’t perform that action at this time.
0 commit comments