# Upside down numbers in Java

## The challenge

Consider the numbers `6969` and `9116`. When you rotate them `180 degrees` (upside down), these numbers remain the same. To clarify, if we write them down on a paper and turn the paper upside down, the numbers will be the same. Try it and see! Some numbers such as `2` or `5` don’t yield numbers when rotated.

Given a range, return the count of upside down numbers within that range. For example, `solve(0,10) = 3`, because there are only `3` upside down numbers `>= 0 and < 10`. They are `0, 1, 8`.

More examples in the test cases.

## The solution in Java code

Option 1 (using `IntStream`):

```.wp-block-code{border:0;padding:0}.wp-block-code>div{overflow:auto}.shcb-language{border:0;clip:rect(1px,1px,1px,1px);-webkit-clip-path:inset(50%);clip-path:inset(50%);height:1px;margin:-1px;overflow:hidden;padding:0;position:absolute;width:1px;word-wrap:normal;word-break:normal}.hljs{box-sizing:border-box}.hljs.shcb-code-table{display:table;width:100%}.hljs.shcb-code-table>.shcb-loc{color:inherit;display:table-row;width:100%}.hljs.shcb-code-table .shcb-loc>span{display:table-cell}.wp-block-code code.hljs:not(.shcb-wrap-lines){white-space:pre}.wp-block-code code.hljs.shcb-wrap-lines{white-space:pre-wrap}.hljs.shcb-line-numbers{border-spacing:0;counter-reset:line}.hljs.shcb-line-numbers>.shcb-loc{counter-increment:line}.hljs.shcb-line-numbers .shcb-loc>span{padding-left:.75em}.hljs.shcb-line-numbers .shcb-loc::before{border-right:1px solid #ddd;content:counter(line);display:table-cell;padding:0 .75em;text-align:right;-webkit-user-select:none;-moz-user-select:none;-ms-user-select:none;user-select:none;white-space:nowrap;width:1%}```import java.util.stream.IntStream;
import org.apache.commons.lang3.StringUtils;

public class UpsideDown {

public int solve(int x, int y) {
return (int) IntStream.range(x, y)
.filter(i -> !StringUtils.containsAny(String.valueOf(i), "23457"))
.filter(i -> new StringBuilder(i + "").reverse().toString()
.replaceAll("6", "2")
.replaceAll("9", "6")
.replaceAll("2", "9")
.equals(String.valueOf(i)))
.count();
}
}
```Code language: Java (java)```

Option 2 (using `UnaryOperator`):

``````import java.util.function.UnaryOperator;
import static java.util.stream.IntStream.range;

public class UpsideDown {
public int solve(int x, int y) {
UnaryOperator<String> upside = s -> new StringBuilder(
s.replaceAll("[23457]", "0").replace('6', '_').replace('9', '6').replace('_', '9')).reverse()
.toString();
return (int) range(x, y).filter(i -> i == Integer.parseInt(upside.apply(i + ""))).count();
}
}
```Code language: Java (java)```

Option 3 (using `streams`):

``````import java.util.stream.*;
public class UpsideDown {

public int solve(int x, int y) {
return (int) IntStream.range(x, y).boxed().filter(z -> {
String s = String.valueOf(z);
if(s.matches(".*[23457].*")) return false;
int l = s.length();
if(l % 2 == 1) {
if(s.substring(l/2, l/2+1).matches(".*[69].*")) return false;
}
int[] d = s.chars().map(Character::getNumericValue).toArray();
for(int i = 0; i < l/2; i++) {
if(d[i] != d[l-1-i]) {
if(d[i] == 6 && d[l-1-i] == 9) continue;
if(d[i] == 9 && d[l-1-i] == 6) continue;
return false;
} else {
if(d[i] == 6 || d[i] == 9) return false;
}
}
return true;
}).count();
}
}
```Code language: Java (java)```

## Test cases to validate our solution

``````import org.junit.Test;
import static org.junit.Assert.assertEquals;
import org.junit.runners.JUnit4;

public class UpsideDownTest {
UpsideDown sol = new UpsideDown();

@Test
public void basicTests() {
assertEquals(3, sol.solve(0,10));
assertEquals(4, sol.solve(10,100));
assertEquals(12, sol.solve(100,1000));
assertEquals(20, sol.solve(1000,10000));
assertEquals(6, sol.solve(10000,15000));
assertEquals(9, sol.solve(15000,20000));
assertEquals(15, sol.solve(60000,70000));
assertEquals(55, sol.solve(60000,130000));
}
}
```Code language: Java (java)```
Tags:
Subscribe
Notify of