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

separate the numbers implementation #5

Merged
merged 2 commits into from
Jul 19, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
42 changes: 42 additions & 0 deletions codereview/beautifulNumberValidator.md
Original file line number Diff line number Diff line change
@@ -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

<! -- language: lang-java -->

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
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package algorithms.interfaces;

public interface BeautifulNumberValidator {
boolean isValidBeautifulNumber(final String s);
}
Original file line number Diff line number Diff line change
@@ -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"));
}
}