From 1cbd689d39d85eaaa494e97c3d136933691c83fd Mon Sep 17 00:00:00 2001 From: jaebradley Date: Wed, 19 Jul 2017 14:50:34 -0400 Subject: [PATCH 1/2] separate the numbers implementation --- .../BeautifulNumberValidatorImpl.java | 37 +++++++++++++++++++ .../interfaces/BeautifulNumberValidator.java | 5 +++ .../BeautifulNumberValidatorImplTest.java | 30 +++++++++++++++ 3 files changed, 72 insertions(+) create mode 100644 src/main/java/algorithms/implementations/BeautifulNumberValidatorImpl.java create mode 100644 src/main/java/algorithms/interfaces/BeautifulNumberValidator.java create mode 100644 src/test/java/algorithms/implementations/BeautifulNumberValidatorImplTest.java diff --git a/src/main/java/algorithms/implementations/BeautifulNumberValidatorImpl.java b/src/main/java/algorithms/implementations/BeautifulNumberValidatorImpl.java new file mode 100644 index 0000000..3910685 --- /dev/null +++ b/src/main/java/algorithms/implementations/BeautifulNumberValidatorImpl.java @@ -0,0 +1,37 @@ +package algorithms.implementations; + +import algorithms.interfaces.BeautifulNumberValidator; + +/** + * https://www.hackerrank.com/challenges/separate-the-numbers/problem + */ + +public class BeautifulNumberValidatorImpl implements BeautifulNumberValidator { + @Override + public boolean isValidBeautifulNumber(final String s) { + if (s.length() < 2) { + return false; + } + + for (int substringSize = 1; substringSize <= s.length() / 2; substringSize++) { + long value = Long.parseLong(s.substring(0, substringSize)); + + if (value == 0) { + break; + } + + StringBuilder sb = new StringBuilder(); + sb.append(value); + + while (sb.length() < s.length() && sb.toString().equals(s.substring(0, sb.length()))) { + sb.append(++value); + } + + if (sb.toString().equals(s)) { + return true; + } + } + + return false; + } +} diff --git a/src/main/java/algorithms/interfaces/BeautifulNumberValidator.java b/src/main/java/algorithms/interfaces/BeautifulNumberValidator.java new file mode 100644 index 0000000..f79928f --- /dev/null +++ b/src/main/java/algorithms/interfaces/BeautifulNumberValidator.java @@ -0,0 +1,5 @@ +package algorithms.interfaces; + +public interface BeautifulNumberValidator { + boolean isValidBeautifulNumber(final String s); +} diff --git a/src/test/java/algorithms/implementations/BeautifulNumberValidatorImplTest.java b/src/test/java/algorithms/implementations/BeautifulNumberValidatorImplTest.java new file mode 100644 index 0000000..52a6ad7 --- /dev/null +++ b/src/test/java/algorithms/implementations/BeautifulNumberValidatorImplTest.java @@ -0,0 +1,30 @@ +package algorithms.implementations; + +import algorithms.interfaces.BeautifulNumberValidator; +import org.junit.Test; + +import static org.junit.Assert.*; + +public class BeautifulNumberValidatorImplTest { + final BeautifulNumberValidator validator = new BeautifulNumberValidatorImpl(); + + @Test + public void itShouldBeAValidBeautifulNumber() { + assertTrue(validator.isValidBeautifulNumber("1234")); + } + + @Test + public void itShouldNotBeAValidBeautifulNumber() { + assertFalse(validator.isValidBeautifulNumber("101103")); + } + + @Test + public void itShouldNotBeAValidBeautifulNumberSinceItStartsWithZero() { + assertFalse(validator.isValidBeautifulNumber("010203")); + } + + @Test + public void itShouldNotBeAValidBeautifulNumberSinceItIsTooSmall() { + assertFalse(validator.isValidBeautifulNumber("1")); + } +} \ No newline at end of file From 41b7f230d010ba8b081d2d8de963e13fba91f231 Mon Sep 17 00:00:00 2001 From: jaebradley Date: Wed, 19 Jul 2017 15:18:38 -0400 Subject: [PATCH 2/2] add codereview markdown --- codereview/beautifulNumberValidator.md | 42 ++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 codereview/beautifulNumberValidator.md diff --git a/codereview/beautifulNumberValidator.md b/codereview/beautifulNumberValidator.md new file mode 100644 index 0000000..9d371d8 --- /dev/null +++ b/codereview/beautifulNumberValidator.md @@ -0,0 +1,42 @@ +## Problem +Adapted from [this HackerRank problem](https://www.hackerrank.com/challenges/separate-the-numbers/problem). + +[![Beautiful Number Description][1]][1] + +Instead of printing `YES` or `NO`, I just wanted to return a `boolean`. + +## Implementation + + + + public class BeautifulNumberValidator { + public static boolean isValidBeautifulNumber(String s) { + if (s.length() < 2) { + return false; + } + + for (int substringSize = 1; substringSize <= s.length() / 2; substringSize++) { + long value = Long.parseLong(s.substring(0, substringSize)); + + if (value == 0) { + break; + } + + StringBuilder sb = new StringBuilder(); + sb.append(value); + + while (sb.length() < s.length() && sb.toString().equals(s.substring(0, sb.length()))) { + sb.append(++value); + } + + if (sb.toString().equals(s)) { + return true; + } + } + + return false; + } + } + + + [1]: https://i.stack.imgur.com/p8HI7.png \ No newline at end of file