Wednesday, May 31, 2023
HomeSoftware EngineeringThe wrong way up numbers in Java

The wrong way up numbers in Java


The problem

Think about the numbers 6969 and 9116. While you rotate them 180 levels (the other way up), these numbers stay the identical. To make clear, if we write them down on a paper and switch the paper the other way up, the numbers would be the similar. Strive it and see! Some numbers corresponding to 2 or 5 don’t yield numbers when rotated.

Given a variety, return the rely of the other way up numbers inside that vary. For instance, remedy(0,10) = 3, as a result of there are solely 3 the other way up numbers >= 0 and < 10. They’re 0, 1, 8.

Extra examples within the take a look at circumstances.

The answer in Java code

Choice 1 (utilizing IntStream):

import java.util.stream.IntStream;
import org.apache.commons.lang3.StringUtils;

public class UpsideDown {

    public int remedy(int x, int y) {
        return (int) IntStream.vary(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)))
            .rely();
    }
}

Choice 2 (utilizing UnaryOperator):

import java.util.perform.UnaryOperator;
import static java.util.stream.IntStream.vary;

public class UpsideDown {
    public int remedy(int x, int y) {
    UnaryOperator<String> upside = s -> new StringBuilder(
        s.replaceAll("[23457]", "0").change('6', '_').change('9', '6').change('_', '9')).reverse()
            .toString();
    return (int) vary(x, y).filter(i -> i == Integer.parseInt(upside.apply(i + ""))).rely();
    }
}

Choice 3 (utilizing streams):

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

    public int remedy(int x, int y) {
        return (int) IntStream.vary(x, y).boxed().filter(z -> {
          String s = String.valueOf(z);
          if(s.matches(".*[23457].*")) return false;
          int l = s.size();
          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) proceed;
              if(d[i] == 9 && d[l-1-i] == 6) proceed;
              return false;
            } else 
          }
          return true;
        }).rely();
    }
}

Take a look at circumstances to validate our answer

import org.junit.Take a look at;
import static org.junit.Assert.assertEquals;
import org.junit.runners.JUnit4;

public class UpsideDownTest {
    UpsideDown sol = new UpsideDown();
    
    @Take a look at
    public void basicTests() {
        assertEquals(3, sol.remedy(0,10));
        assertEquals(4, sol.remedy(10,100));
        assertEquals(12, sol.remedy(100,1000));
        assertEquals(20, sol.remedy(1000,10000));
        assertEquals(6, sol.remedy(10000,15000));
        assertEquals(9, sol.remedy(15000,20000));
        assertEquals(15, sol.remedy(60000,70000));
        assertEquals(55, sol.remedy(60000,130000));
    }
}
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments