BAEL-5563 Reversing a number (#12153)
* BAEL-5563 Reverse a number initial commit * BAEL-5563 Final improvements before PR * BAEL-5563 Reverse a number - Draft improvements * BAEL-5563 Reverse a number - Recursive method improvements * BAEL-5563 Reverse a number - Recursive method improvements * BAEL-5563 Reverse a number - Remove unused imports Co-authored-by: Timothy Kruger - Lenny <gapptechnology@gmail.com> Co-authored-by: claud <180181@virtualwindow.co.za>
This commit is contained in:
parent
0bbd9bdc64
commit
9727e555ce
|
@ -0,0 +1,45 @@
|
||||||
|
package com.baeldung.reversenumber;
|
||||||
|
|
||||||
|
public class ReverseNumber {
|
||||||
|
|
||||||
|
public static int reverseNumberWhileLoop(int number) {
|
||||||
|
int reversedNumber = 0;
|
||||||
|
int numberToReverse = Math.abs(number);
|
||||||
|
|
||||||
|
while (numberToReverse > 0) {
|
||||||
|
int mod = numberToReverse % 10;
|
||||||
|
reversedNumber = reversedNumber * 10 + mod;
|
||||||
|
numberToReverse /= 10;
|
||||||
|
}
|
||||||
|
|
||||||
|
return number < 0 ? reversedNumber * -1 : reversedNumber;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static int reverseNumberForLoop(int number) {
|
||||||
|
int reversedNumber = 0;
|
||||||
|
int numberToReverse = Math.abs(number);
|
||||||
|
|
||||||
|
for (; numberToReverse > 0; numberToReverse /= 10) {
|
||||||
|
int mod = numberToReverse % 10;
|
||||||
|
reversedNumber = reversedNumber * 10 + mod;
|
||||||
|
}
|
||||||
|
|
||||||
|
return number < 0 ? reversedNumber * -1 : reversedNumber;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static int reverseNumberRecWrapper(int number) {
|
||||||
|
int output = reverseNumberRec(Math.abs(number), 0);
|
||||||
|
return number < 0 ? output * -1 : output;
|
||||||
|
}
|
||||||
|
private static int reverseNumberRec(int numberToReverse, int recursiveReversedNumber) {
|
||||||
|
|
||||||
|
if (numberToReverse > 0) {
|
||||||
|
int mod = numberToReverse % 10;
|
||||||
|
recursiveReversedNumber = recursiveReversedNumber * 10 + mod;
|
||||||
|
numberToReverse /= 10;
|
||||||
|
return reverseNumberRec(numberToReverse, recursiveReversedNumber);
|
||||||
|
}
|
||||||
|
|
||||||
|
return recursiveReversedNumber;
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,34 @@
|
||||||
|
package com.baeldung.reversenumber;
|
||||||
|
|
||||||
|
import org.assertj.core.api.Assertions;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
|
||||||
|
class ReverseNumberUnitTest {
|
||||||
|
|
||||||
|
private static final int ORIGINAL_NUMBER = 123456789;
|
||||||
|
private static final int REVERSED_NUMBER = 987654321;
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void whenReverseNumberWhileLoop_thenOriginalEqualToReverse() {
|
||||||
|
Assertions.assertThat(ReverseNumber.reverseNumberWhileLoop(ORIGINAL_NUMBER)).isEqualTo(REVERSED_NUMBER);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void whenReverseNumberForLoop_thenOriginalEqualToReverse() {
|
||||||
|
Assertions.assertThat(ReverseNumber.reverseNumberForLoop(ORIGINAL_NUMBER)).isEqualTo(REVERSED_NUMBER);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void whenReverseNumberRec_thenOriginalEqualToReverse() {
|
||||||
|
Assertions.assertThat(ReverseNumber.reverseNumberRecWrapper(ORIGINAL_NUMBER)).isEqualTo(REVERSED_NUMBER);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void whenReverseNegativeNumber_thenNumberShouldReverse() {
|
||||||
|
Assertions.assertThat(ReverseNumber.reverseNumberWhileLoop(ORIGINAL_NUMBER * -1)).isEqualTo(REVERSED_NUMBER * -1);
|
||||||
|
Assertions.assertThat(ReverseNumber.reverseNumberForLoop(ORIGINAL_NUMBER * -1)).isEqualTo(REVERSED_NUMBER * -1);
|
||||||
|
Assertions.assertThat(ReverseNumber.reverseNumberRecWrapper(ORIGINAL_NUMBER * -1)).isEqualTo(REVERSED_NUMBER * -1);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue