A Java string with hashCode equal to Integer.MIN_VALUE - java

Is there a known Java String with hashCode exactly equal to Integer.MIN_VALUE ? It would be helpful for writing a test for a hash table to help avoid a common mistake of running Math.Abs on the hashcode before performing the remainder operation.
Ideally the string would include only ASCII characters, but I'm not sure if it woul dbe feasible.

Based on the formula for hash code (from StringLatin1):
public static int hashCode(byte[] value) {
int h = 0;
for (byte v : value) {
h = 31 * h + (v & 0xff);
}
return h;
}
it depends linearly on the characters, the longer the string and greater the characters, the greater the hash code will be, until it overflows. Also note that the first characters have a greater impact on the resulting hash code (more often multiplied by 31).
The basic idea of the two first algorithm is to increment the characters until the hash code turns negative, starting with the left-most character since it has a greater weight. The searched string must have the character previous to the one that caused it to overflow on each position but the last one.
The code starts testing the strings "A", "AA", "AAA", ... until one starts returning a negative value - the previous string is used as starting value.
Now it starts incrementing the first character up to Z or until a string with a negative hash is found. The same is done for every next character.
Since the hash code of such strings was not yet reaching Integer.MIN_VALUE, an additional pass is done, to also test lowercase characters. This should have been integrated in the previous loop...
Now the last character is adjusted to exactly get to Integer.MIN_VALUE - simple since the last character is just added, without multiplication to calculate the hash code.
Here the code:
var string = "A";
while ((string+"A").hashCode() > 0) {
string += "A";
}
var array = string.toCharArray();
var i = 0;
while (i < array.length) {
array[i] += 1;
if (array[i] > 'z' || new String(array).hashCode() < 0) {
array[i] -= 1;
i += 1;
continue;
}
}
i = 1;
while (i < array.length) {
if (array[i] == 'Z') {
array[i] = 'a';
}else {
array[i] += 1;
}
if (array[i] > 'Z' || new String(array).hashCode() < 0) {
if (array[i] == 'a')
array[i] = 'Z';
else
array[i] -= 1;
i += 1;
continue;
}
}
int hash = new String(array).hashCode();
if (hash > 0) {
array[array.length-1] += Integer.MAX_VALUE - hash + 1;
}
System.out.printf("%s = %d%n", new String(array), new String(array).hashCode());
This results in:
HZcxf_ = -2147483648
Merging the two incrementing loops of previous code, we have:
var string = "A";
while ((string+"A").hashCode() > 0) {
string += "A";
}
var array = string.toCharArray();
var i = 0;
while (i < array.length) {
var prev = array[i];
if (prev == 'Z') {
array[i] = 'a';
} else {
array[i] += 1;
}
if (array[i] > 'z' || new String(array).hashCode() < 0) {
array[i] = prev;
i += 1;
continue;
}
}
int hash = new String(array).hashCode();
if (hash > 0) {
array[array.length-1] += Integer.MAX_VALUE - hash + 1;
}
System.out.printf("%s = %d%n", new String(array), new String(array).hashCode());
Resulting in (slightly different than previous):
HZdZG_ = -2147483648
Another method would be more strongly based on the hash calculation, basically undoing it.
Since I did not want to work with negative number, it starts with Integer.MAX_VALUE, which is one less than Integer.MIN_VALUE (considering over/underflow).
First it finds out how often it must be divided by 31 until the result is less than 128 (ASCII), kind of determining the string length.
Next it loops and finds out each character with some special handling to avoid characters less than ' '.
At the end, the last character is incremented by one to move the hash code from MAX_VALUE to MIN_VALUE by overflowing.
var string = "";
var remain = Integer.MAX_VALUE;
var i = 0;
var multiplier = 1;
while (remain > 127) {
remain /= 31;
multiplier *= 31;
i += 1;
}
remain = Integer.MAX_VALUE;
while (i >= 0) {
var ch = (char)(remain / multiplier);
remain -= ch * multiplier;
multiplier /= 31;
if (i > 0) {
// correct if next ch will be less than ' '
var correct = (' ' - (remain / multiplier) + 30) / 31; // old fashion rounding
if (correct > 0) {
ch -= correct;
remain += correct * 31 * multiplier;
}
} else {
ch += 1;
}
string += ch;
i -= 1;
}
System.out.printf("%s = %d%n", string, string.hashCode());
And its results:
I='<*! = -2147483648
Note: the last code will definitively fail if the hash code algorithm of String is changed! The previous two may fail, depending on how the hash calculation is changed.

String#hashCode() is defined as:
Returns a hash code for this string. The hash code for a String object is computed as
s[0]*31^(n-1) + s[1]*31^(n-2) + ... + s[n-1]
using int arithmetic, where s[i] is the ith character of the string, n is the length of the string, and ^ indicates exponentiation. (The hash value of the empty string is zero.)
Now you just need to solve for -2147483648 (perhaps with the restriction of only printable ASCII chars: 32–127) :)
Or you brute-force (this will take a while):
public class HashFinder {
private static final int SIZE = 7;
private static long hashesCalculated = 0L;
public static void main(String[] args) {
hashesCalculated = 0L;
final long start = System.nanoTime();
findHash(SIZE);
final long duration = System.nanoTime() - start;
System.err.println("Checked strings of size " + SIZE);
System.err.println(hashesCalculated + " hashes in " + TimeUnit.NANOSECONDS.toSeconds(duration) + "s");
}
public static void findHash(final int size) {
findHash("", size);
}
public static void findHash(final String prefix, final int size) {
if (size <= 0) {
return;
}
final StringBuilder sb = new StringBuilder(prefix).append(' ');
for (char c = ' '; c < '~'; ++c) {
sb.setCharAt(prefix.length(), c);
final String s = sb.toString();
++hashesCalculated;
if (s.hashCode() == Integer.MIN_VALUE) {
System.out.printf("Found string with min hashCode! '%s'%n", s);
}
findHash(s, size - 1);
}
}
}
But allocating all those strings and string builders is expensive. Brute-forcing becomes feasible when we calculate the hash code manually from a char array:
public class HashFinderBytes {
public static void main(String[] args) {
final char start = ' ', end = '~';
for (int size = 1; size <= 9; size++) {
char[] chars = new char[size];
Arrays.fill(chars, start);
final long startNano = System.nanoTime();
final long combinations = BigInteger.valueOf(end - start).pow(size).longValue();
System.err.println("Checking " + combinations + " strings of size " + size);
for (long i = 0; i < combinations; ++i) {
if (hashCode(chars) == Integer.MIN_VALUE) {
System.out.printf("Found string with min hashCode! \"%s\"%n", new String(chars));
System.out.println("Sanity check: " + (new String(chars).hashCode() == Integer.MIN_VALUE));
}
for (int j = 0; j < chars.length; ++j) {
++chars[j];
if (chars[j] <= end) {
break;
}
chars[j] = (byte) start;
}
}
final long duration = System.nanoTime() - startNano;
final long millis = TimeUnit.NANOSECONDS.toMillis(duration);
System.err.println("in " + millis + "ms (" + (combinations / millis) + " ops/ms)");
}
}
public static int hashCode(char[] value) {
int h = 0;
for (char v : value) {
h = 31 * h + (v & 0xff);
}
return h;
}
}
In fact, there are lots of strings with a hash code identical to Integer.MIN_VALUE.
Length 6:
I='<*!
H\'<*!
G{'<*!
I<F<*!
H[F<*!
GzF<*!
I;e<*!
HZe<*!
Gye<*!
I=&[*!
H\&[*!
G{&[*!
I<E[*!
H[E[*!
GzE[*!
I;d[*!
HZd[*!
Gyd[*!
I=%z*!
H\%z*!
G{%z*!
I<Dz*!
H[Dz*!
GzDz*!
I;cz*!
HZcz*!
Gycz*!
I=';I!
H\';I!
G{';I!
I<F;I!
H[F;I!
GzF;I!
I;e;I!
HZe;I!
Gye;I!
I=&ZI!
H\&ZI!
G{&ZI!
I<EZI!
H[EZI!
GzEZI!
I;dZI!
HZdZI!
GydZI!
I=%yI!
H\%yI!
G{%yI!
I<DyI!
H[DyI!
GzDyI!
I;cyI!
HZcyI!
GycyI!
I=':h!
H\':h!
G{':h!
I<F:h!
H[F:h!
GzF:h!
I;e:h!
HZe:h!
Gye:h!
I=&Yh!
H\&Yh!
G{&Yh!
I<EYh!
H[EYh!
GzEYh!
I;dYh!
HZdYh!
GydYh!
I=%xh!
H\%xh!
G{%xh!
I<Dxh!
H[Dxh!
GzDxh!
I;cxh!
HZcxh!
Gycxh!
Length 7 (all of the strings below end with a space character); not all shown:
p4*|{e
oS*|{e
nr*|{e
p3I|{e
oRI|{e
nqI|{e
p2h|{e
oQh|{e
nph|{e

Related

Calculate the similarity of string 1 with string 2?

I have problem with my algorithm, I tried with my sample data noted below. And I don't understand why the result is not what I thought at all. For example when i = 3(n) and j = 2(n). The q should have output 2 but it's equal to 1. I guess the cause could just be that the while loop working was having problems. But I don't know the reason why. Please someone help me! Thank you very much
Running method
public static void compareStringToString(String str1, String str2) {
ArrayList<Integer> numSames = new ArrayList<Integer>();
int p = 0, n; // p - Maximum number of similar characters, n - the number of characters of the longer string
// str1 = "slcnsicnn"
// str2 = "ahnscnn"
int i = 0, j = 0;
if (str1.length() >= str2.length()) {
n = str1.length();
for (i = 0; i < n; i++) {
for (j = 0; j < str2.length(); j++) {
if (str1.charAt(i) == str2.charAt(j)) {
p = 0;
while (str1.charAt(i + p) == str2.charAt(j + p)) {
p++;
if ((i + p < n) || (j + p < str2.length())) {
break;
}
}
numSames.add(p);
}
}
}
System.out.println("The similarity of the two strings is: " + (((float)
Collections.max(numSames)) / n) * 100 + "%");
else {
n = str2.length();
// TODO Same things
}
Running Screen
**
I did as you said. I created a new method to split the work. Indeed, when I did that, my mind became clearer. And more specifically, I was able to run it smoothly without errors. Or maybe there are some more minor bugs that I haven't noticed yet. But it's really great like a little bit of a burden. Once again thank you very much for your kind advice.
**
// Get the number of similar characters after a pair of similar characters of 2 strings
public static int getNumSimilarAfterChar(String str1, String str2, int indexStr1, int indexStr2) {
int numChar = 0; // It's both a count and an index
// Substrings to work with algorithm
String subStr1;
String subStr2;
if (str1.charAt(indexStr1) == str2.charAt(indexStr2)) {
// Cut substring from highlighted part
subStr1 = str1.substring(indexStr1);
subStr2 = str2.substring(indexStr2);
while (subStr1.charAt(numChar) == subStr2.charAt(numChar)) {
numChar++;
if ((numChar >= (subStr1.length() - 1)) || (numChar >= (subStr2.length() - 1))) {
break;
}
}
return numChar + 1;
} else {
return numChar;
}
}
// Given any two strings of characters, compare the match of these two strings
/**
* Compare the match of string 1 vs string 2
*
* #param str1
* #param str2
*/
public static void compareStringToString(String str1, String str2) {
ArrayList<Integer> numSames = new ArrayList<Integer>();
// maxSimilar - maximum number of similar characters, totalLength - the number of characters of longer string
int numSimilar = 0, totalLength;
// slcnsicnn
// ahnscnn
int i = 0, j = 0;
// If string 1 is longer than string 2
if (str1.length() >= str2.length()) {
totalLength = str1.length();
for (i = 0; i < totalLength; i++) {
for (j = 0; j < str2.length(); j++) {
// Browse until you come across two similar characters
if (str1.charAt(i) == str2.charAt(j)) {
numSimilar = MyStrings.getNumSimilarAfterChar(str1, str2, i, j);
numSames.add(numSimilar);
System.out.println(numSames.toString());
}
}
}
// Get the max value in a list of how many identical characters are generated
System.out.println("The similarity of the two strings is: " + (((float) Collections.max(numSames)) / totalLength) * 100 + "%");
} else {
totalLength = str2.length();
// TODO Same things
}
}

find the correct algorithme to find all the possible binary combination

I'm trying to write a non-recursive Java method called showStar, which takes a string, and generates ALL possible combinations of that string without the mask “*” characters.
receiving this as an input "1011*00*10",
the method `showStar` will display output like:
1011000010
1011000110
1011100010
1011100110
I tried this way, however, as soon as the number of possible cases is more than the String length, the output is not exact.
Here is my code.
public static void showStar(String s){
String save;
int count = 0;
int poss;
save = s.replace('*','0');
StringBuilder myString = new StringBuilder(save);
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) == '*' && myString.charAt(i) == '0') {
myString.setCharAt(i, '1');
System.out.println(myString);
}
}
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) == '*' && myString.charAt(i) == '1') {
myString.setCharAt(i, '0');
System.out.println(myString);
}
}
}
Say there are k *s. Then there are 2^k solutions. You can generate these by copying the bits from the integers 0 - 2^k-1 in order. (adding sufficient leading zeroes)
E.g. 1**1:
0 = 00 => 1001
1 = 01 => 1011
2 = 10 => 1101
3 = 11 => 1111
Here a recursive algoritm works just perfectly:
You check if an input string contains an asterisk '*' by using an x = str.indexOf('*');
If no asterisk is present (x == -1), you just print the string and return
Otherwise, you replace the asterisk at the position to '0' and '1' and call showStar() recursively for both replacements
public static void showStar(String str) {
int x = str.indexOf('*');
if(x == -1) {
System.out.println(str);
return;
}
String prefix = str.substring(0, x);
String suffix = str.substring(x + 1);
for (char i = '0'; i <= '1'; i++) {
showStar(prefix + i + suffix);
}
}
Update
In non-recursive implementation we need to collect the asterisk positions, then prepare a binary representation and set appropriate bits at the known positions:
public static void showStar(String str) {
int[] xs = IntStream.range(0, str.length())
.filter(i -> str.charAt(i) == '*')
.toArray();
int num = (int) Math.pow(2, xs.length); // 2^n variants for n asterisks
String format = xs.length > 0 ? "%" + xs.length + "s" : "%s"; // fix if no '*'
for (int i = 0; i < num; i++) {
String bin = String.format(format, Integer.toBinaryString(i))
.replace(' ', '0'); // pad leading zeros
StringBuilder sb = new StringBuilder(str);
// set 0 or 1 in place of asterisk(s)
for (int j = 0; j < xs.length; j++) {
sb.setCharAt(xs[j], bin.charAt(j));
}
System.out.println(sb);
}
}

The smallest Palindrome number is greater N

For a given positive integer N of not more than 1000000 digits, write the value of the smallest palindrome larger than N to output.
Here is my code:
public class Palin {
public static String reverseString(String s) {
String newS = "";
for(int i = s.length() - 1; i >= 0; i--)
newS += s.charAt(i);
return newS;
}
public static String getPalin(String s) {
int lth = s.length();
String left = "", mid = "", right = "", newS = "";
if(lth % 2 != 0) {
left = s.substring(0, lth / 2);
mid = s.substring(lth / 2, lth / 2 + 1);
right = reverseString(left);
newS = left + mid + right;
if(s.compareTo(newS) < 0) return newS;
else {
int temp = Integer.parseInt(mid);
temp++;
mid = Integer.toString(temp);
newS = left + mid + right;
return newS;
}
}
else {
left = s.substring(0, lth / 2 - 1);
mid = s.substring(lth / 2 - 1, lth / 2);
right = reverseString(left);
newS = left + mid + mid + right;
if(s.compareTo(newS) < 0) return newS;
else {
int temp = Integer.parseInt(mid);
temp++;
mid = Integer.toString(temp);
newS = left + mid + mid + right;
return newS;
}
}
}
public static void main(String[] args) throws java.lang.Exception {
Scanner input = new Scanner(System.in);
//Scanner input = new Scanner(System.in);
int k = input.nextInt();
String[] s = new String[k];
for(int i = 0; i < k; i++) {
s[i] = input.next();
}
for(int i = 0; i < k; i++) {
System.out.println(getPalin(s[i]));
}
}
}
My idea is use a String represent for a number. I divide this String into 2 part, coppy first part and reverse it for second part. I think my solve is correct but it not fast enough. I need a more efficient algorithm.
Thanks
EDITED
Since you said that:
For a given positive integer N of not more than 1000000 digits
My previous solution won't work since I have converted them to int and an int can't accommodate 1000000 digits. Thus I have made a new approach, an approach that doesn't need any String to int conversion.
Refer to the code and comment below for details.
CODE:
package main;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
// Scanner input = new Scanner(System.in);
int k = Integer.parseInt(input.nextLine());
String[] s = new String[k];
for (int i = 0; i < k; i++) {
s[i] = input.nextLine();
}
for (int i = 0; i < k; i++) {
System.out.println(getPalin(s[i]));
}
input.close();
}
public static String getPalin(String s) {
// initialize the result to "" since if input is 1 digit, nothing is printed
String result = "";
// if input is greater than 1000000 digits
if (s.length() >= 1000000) {
// return the highest palindrome less than 1000000
result = "999999";
} else if (s.length() > 1) {
// get the middle index of the string
int mid = s.length() % 2 == 0 ? s.length() / 2 : (s.length() / 2) + 1;
// get the left part of the string
String leftPart = getPalindrome(s.substring(0, mid));
if (s.length() % 2 == 0) {
// attach the left part and the reverse left part
result = leftPart + new StringBuilder(leftPart).reverse().toString();
} else {
// attach the left part and the reverse left part excluding the middle digit
result = leftPart
+ new StringBuilder(leftPart.substring(0, leftPart.length() - 1)).reverse().toString();
}
// check if the new result greater than 1000000 digits
if (result.length() >= 1000000) {
// return the highest palindrome less than 1000000
result = "999999";
}
}
return result;
}
public static String getPalindrome(String param) {
String result = "";
// iterate through the string from last index until index 0
for (int i = param.length() - 1; i >= 0; i--) {
// get the char at index i
char c = param.charAt(i);
/*
* increment char since the next palindrome is the current digit + 1. Example:
* User input is 121, then param will be 12 so the next is 13
*/
c++;
/*
* check if the current character is greater than '9', which means it is not a
* digit after incrementing
*/
if (c > '9') {
// set the current char to 0
c = '0';
// check if index is at index 0
if (i - 1 < 0) {
// if at index 0 then add '1' at start
result = '1' + result;
} else {
// if not then append c at result
result = result + c;
}
} else {
// check if index is at index 0
if (i - 1 < 0) {
// if not then prepend c at result
result = c + result;
} else {
// if not then get the rest of param then append c and result
result = param.substring(0, i) + c + result;
}
break;
}
}
return result;
}
}

Java - number in expanded form

I have given number and want it to return as a String in expanded form. For example
expandedForm(12); # Should return "10 + 2"
expandedForm(42); # Should return "40 + 2"
expandedForm(70304); # Should return "70000 + 300 + 4"
My function works for first and second case, but with 70304 it gives this:
70 + 00 + 300 + 000 + 4
Here's my code
import java.util.Arrays;
public static String expandedForm(int num)
{
String[] str = Integer.toString(num).split("");
String result = "";
for(int i = 0; i < str.length-1; i++) {
if(Integer.valueOf(str[i]) > 0) {
for(int j = i; j < str.length-1; j++) {
str[j] += '0';
}
}
}
result = Arrays.toString(str);
result = result.substring(1, result.length()-1).replace(",", " +");
System.out.println(result);
return result;
}
I think there's a problem with the second loop, but can't figure out why.
You should be adding '0's to str[i], not str[j]:
for(int i = 0; i < str.length-1; i++) {
if(Integer.valueOf(str[i]) > 0) {
for(int j = i; j < str.length-1; j++) {
str[i] += '0';
}
}
}
This will result in:
70000 + 0 + 300 + 0 + 4
You still have to get rid of the 0 digits.
One possible way to get rid of them:
result = result.substring(1, result.length()-1).replace(", 0","").replace(",", " +");
Now the output is
70000 + 300 + 4
Pseudocode uses integer arithmetics to extract decimal digits one-by-one (from the right one):
mul = 1 //will contain power of 10
while (num > 0):
dig = num % 10 //integer modulo retrieves the last digit
if (dig > 0): //filter out zero summands
add (dig * mul) to output //like 3 * 100 = 300
num = num / 10 //integer division removes the last decimal digit 6519 => 651
mul = mul * 10 //updates power of 10 for the next digit
You could do the same with pure math, using modulo % and integer division /, e.g. using Stream API:
int n = 70304;
String res = IntStream
.iterate(1, k -> n / k > 0, k -> k * 10) // divisors
.map(k -> (n % (k*10) / k ) * k) // get 1s, 10s, 100s, etc.
.filter(x -> x > 0) // throw out zeros
.mapToObj(Integer::toString) // convert to string
.collect(Collectors.joining(" + ")); // join with '+'
System.out.println(res); // 4 + 300 + 70000
There are many variations possible. If the usage of a list is allowed:
public static String expandedForm(int num){
String[] str = Integer.toString(num).split("");
String result;
List<String> l = new ArrayList<String>();
for(int i = 0; i < str.length; i++){
if(Integer.valueOf(str[i]) > 0){
String s = str[i];
for(int j = i; j < str.length - 1; j++){
s += '0';
}
l.add(s);
}
}
result = l.toString();
result = result.substring(1, result.length() - 1).replace(",", " +");
System.out.println(result);
return result;
}
One could also work directly on result:
public static String expandedForm2(int num){
String[] str = Integer.toString(num).split("");
String result = "";
for(int i = 0; i < str.length; i++){
if(Integer.valueOf(str[i]) > 0){
result += str[i];
for(int j = i; j < str.length - 1; j++){
result += '0';
}
result += " + ";
}
}
result = result.substring(0, result.length() - 3);
System.out.println(result);
return result;
}
This is also possible to do recursively. Here an example implementation:
String g(int n, int depth){ // Recursive method with 2 int parameters & String return-type
int remainder = n % depth; // The current recursive remainder
if(depth < n){ // If we aren't done with the number yet:
int nextDepth = depth * 10; // Go to the next depth (of the power of 10)
int nextN = n - remainder; // Remove the remainder from the input `n`
// Do a recursive call with these next `n` and `depth`
String resultRecursiveCall = g(nextN, nextDepth);
if(remainder != 0){ // If the remainder was not 0:
// Append a " + " and this remainder to the result
resultRecursiveCall += " + " + remainder;
}
return resultRecursiveCall; // And return the result
} else{ // Else:
return Integer.toString(n); // Simply return input `n` as result
}
}
String f(int n){ // Second method so we can accept just integer `n`
return g(n, 1); // Which will call the recursive call with parameters `n` and 1
}
The second method is so we can call the method with just a single input n. For example:
String result = f(70304);
Which will result in the String 70000 + 300 + 4.
Try it online.
To explain a bit more in depth of what this recursive method does, let's just do a step-by-step for the input 70304:
In the first recursive iteration: n=70304, depth=1, remainder=70304%1 = 0.
Since depth < n is truthy, it will do a recursive call with 70304-0 and 1*10
And since remainder is 0, it will append nothing more to the result
In the second recursive iteration: n=70304, depth=10, remainder=70304%10 = 4.
Since depth < n is still truthy, it will do a recursive call with 70304-4 and 10*10
And since remainder is 4, it will append a " + " and this 4 to the result
In the third recursive iteration: n=70300, depth=100, remainder=70300%100 = 0.
Since depth < n is still truthy, it will do a recursive call with 70300-0 and 100*10
And since remainder is 0, it will append nothing more to the result
In the fourth recursive iteration: n=70300, depth=1000, remainder=70300%1000 = 300.
Since depth < n is still truthy, it will do a recursive call with 70300-300 and 1000*10
And since remainder is 300, it will append a " + " and this 300 to the result
In the fifth recursive iteration: n=70000, depth=10000, remainder=70000%10000 = 0.
Since depth < n is still truthy, it will do a recursive call with 70000-0 and 10000*10
And since remainder is 0, it will append nothing more to the result
In the sixth recursive iteration: n=70000, depth=100000, remainder=70000%100000 = 70000.
Since now depth < n is falsey, it won't do any more recursive calls, but instead return the current n (which is 70000).
And since these were all recursive calls, we should actually look at it backwards for the result, so it will result in 70000 + 300 + 4.
So in general:
The depth < n if-check is to see when we are done with the recursive calls.
The g(n-remainder, depth*10) will remove the digits we've already output in a previous recursive iteration, and goes to the next 10k power in the next recursive iteration
The remainder != 0 if-check determines if the number we want to append was not a 0
public class Kata
{
public static String expandedForm(int num)
{
String outs = "";
for (int i = 10; i < num; i *= 10) {
int rem = num % i;
outs = (rem > 0) ? " + " + rem + outs : outs;
num -= rem;
}
outs = num + outs;
return outs;
}
}
package backup;
import java.util.Arrays;
public class FileOutput {
public static void main(String[] args){
String expForm = expandedForm(70304);
//System.out.println(expForm);
}
public static String expandedForm(int num)
{
String[] str = Integer.toString(num).split("");
String result = "";
for(int i = 0; i < str.length-1; i++) {
if(Integer.valueOf(str[i]) > 0) {
for(int j = i; j < str.length-1; j++) {
str[i] += '0';
}
}
}
result = Arrays.toString(str);
result = result.substring(1, result.length()-1).replace(",", " +");
System.out.println(result);
return result;
}
}
Output : 70000 + 0 + 300 + 0 + 4
Solution in most inner loop you need to add '0' to str[i] : str[i] += '0';
Then you need to replace "+ 0" from the resulted output.
for(int i = 0; i < str.length; i++) {
if(Integer.valueOf(str[i]) > 0) {
for(int j = 0; j < str.length - i - 1; j++) {
str[i] += '0';
}
}
}
I think the challenge of this problem is omitting 0(zero) and extra +(plus) while iterating throughout the number. String concat function can be used with condition as bellow:
public static String expandedForm(int num) {
String st = String.valueOf(num);
String finalResult = "";
for (int i = 0; i < st.length(); i++) {
String s = String.valueOf(st.charAt(i));
if (Integer.valueOf(s) > 0) {
for (int j = i; j < st.length() - 1; j++) {
s = s.concat("0");
}
if (i == st.length() - 1) {
finalResult = finalResult.concat(s);
} else {
finalResult = finalResult.concat(s + " + ");
}
}
}
return finalResult;
}
public static String expandedForm(int num)
{
String[] str = Integer.toString(num).split("");
String result = "";
String st="";
for(int i = 0; i < str.length-1; i++) {
if(Integer.valueOf(str[i]) > 0) {
for(int j = i; j < str.length-1; j++) {
str[i] += '0';
}
}
}
for(String s:str) {
st += s+" ";
}
result=st;
result = result.substring(0, result.length()-1).replace(" 0","").replace(" ", " + ");
System.out.println(result);
return result;
}
public static String expandedForm(int num)
{
int numberOfDigits =(int) Math.floor(Math.log10(num)+1);
String result="";
while(numberOfDigits-->0){
int divisor = (int)Math.pow(10,numberOfDigits);
int quotient = num/divisor;
num%=divisor;
int value = quotient * divisor;
if(value!=0)
result+=value+(numberOfDigits != 0 ?"+":"");
}
return result;
}
Try with this.
Find number of digits and iterate for every single digit.
Make a divisor of 10 power (number of digits -1) and divide the number to get quotient.
Take reminder for next iteration.
Multiply quotient and divisor and store in result if it value is not zero.
Simple Javascript Expanded Form of a number
Asked in interviews for beginners/Juniors
function expandedForm(n){
let temp = n;
let count=0;
while(temp>0){
console.log(((temp%10)*(10**count)));
count++;
temp = Math.floor(temp/10);
}
}
output will be like this:-
7
80
400
5000
10000

How to convert a Binary String to a base 10 integer in Java

I have an array of Strings that represent Binary numbers (without leading zeroes) that I want to convert to their corresponding base 10 numbers. Consider:
binary 1011 becomes integer 11
binary 1001 becomes integer 9
binary 11 becomes integer 3 etc.
What's the best way to proceed? I've been exploring java.lang.number.* without finding a direct conversion method. Integer.parseInt(b) yields an integer EQUAL to the String...e.g., 1001 becomes 1,001 instead of 9...and does not seem to include a parameter for an output base. toBinaryString does the conversion the wrong direction. I suspect I'll need to do a multistep conversion, but can't seem to find the right combination of methods or subclasses. I'm also not sure the extent to which leading zeros or lack thereof will be an issue. Anyone have any good directions to point me?
You need to specify the radix. There's an overload of Integer#parseInt() which allows you to.
int foo = Integer.parseInt("1001", 2);
This might work:
public int binaryToInteger(String binary) {
char[] numbers = binary.toCharArray();
int result = 0;
for(int i=numbers.length - 1; i>=0; i--)
if(numbers[i]=='1')
result += Math.pow(2, (numbers.length-i - 1));
return result;
}
int foo = Integer.parseInt("1001", 2);
works just fine if you are dealing with positive numbers but if you need to deal with signed numbers you may need to sign extend your string then convert to an Int
public class bit_fun {
public static void main(String[] args) {
int x= (int)Long.parseLong("FFFFFFFF", 16);
System.out.println("x =" +x);
System.out.println(signExtend("1"));
x= (int)Long.parseLong(signExtend("1"), 2);
System.out.println("x =" +x);
System.out.println(signExtend("0"));
x= (int)Long.parseLong(signExtend("0"), 2);
System.out.println("x =" +x);
System.out.println(signExtend("1000"));
x= (int)Long.parseLong(signExtend("1000"), 2);
System.out.println("x =" +x);
System.out.println(signExtend("01000"));
x= (int)Long.parseLong(signExtend("01000"), 2);
System.out.println("x =" +x);
}
private static String signExtend(String str){
//TODO add bounds checking
int n=32-str.length();
char[] sign_ext = new char[n];
Arrays.fill(sign_ext, str.charAt(0));
return new String(sign_ext)+str;
}
}
output:
x =-1
11111111111111111111111111111111
x =-1
00000000000000000000000000000000
x =0
11111111111111111111111111111000
x =-8
00000000000000000000000000001000
x =8
I hope that helps!
static int binaryToInt (String binary){
char []cA = binary.toCharArray();
int result = 0;
for (int i = cA.length-1;i>=0;i--){
//111 , length = 3, i = 2, 2^(3-3) + 2^(3-2)
// 0 1
if(cA[i]=='1') result+=Math.pow(2, cA.length-i-1);
}
return result;
}
public Integer binaryToInteger(String binary){
char[] numbers = binary.toCharArray();
Integer result = 0;
int count = 0;
for(int i=numbers.length-1;i>=0;i--){
if(numbers[i]=='1')result+=(int)Math.pow(2, count);
count++;
}
return result;
}
I guess I'm even more bored! Modified Hassan's answer to function correctly.
For me I got NumberFormatException when trying to deal with the negative numbers. I used the following for the negative and positive numbers.
System.out.println(Integer.parseUnsignedInt("11111111111111111111111111110111", 2));
Output : -9
Using bitshift is more elegant and faster than Math.pow. Simply bitshift the digits (0 or 1) into position with val <<= 1
// parse an unsigned binary string, valid up to 31 bits
static int binaryToBase10(String binaryString) {
int val = 0;
for (char c : binaryString.toCharArray()) {
val <<= 1;
val += c-'0';
}
return val;
}
Example use
int val = binaryToBase10("1011");
System.out.println(val);
prints 11
Fixed version of java's Integer.parseInt(text) to work with negative numbers:
public static int parseInt(String binary) {
if (binary.length() < Integer.SIZE) return Integer.parseInt(binary, 2);
int result = 0;
byte[] bytes = binary.getBytes();
for (int i = 0; i < bytes.length; i++) {
if (bytes[i] == 49) {
result = result | (1 << (bytes.length - 1 - i));
}
}
return result;
}
I love loops! Yay!
String myString = "1001001"; //73
While loop with accumulator, left to right (l doesn't change):
int n = 0,
j = -1,
l = myString.length();
while (++j < l) n = (n << 1) + (myString.charAt(j) == '0' ? 0 : 1);
return n;
Right to left with 2 loop vars, inspired by Convert boolean to int in Java (absolutely horrible):
int n = 0,
j = myString.length,
i = 1;
while (j-- != 0) n -= (i = i << 1) * new Boolean(myString.charAt(j) == '0').compareTo(true);
return n >> 1;
A somewhat more reasonable implementation:
int n = 0,
j = myString.length(),
i = 1;
while (j-- != 0) n += (i = i << 1) * (myString.charAt(j) == '0' ? 0 : 1);
return n >> 1;
A readable version :p
int n = 0;
for (int j = 0; j < myString.length(); j++) {
n *= 2;
n += myString.charAt(j) == '0' ? 0 : 1;
}
return n;
Now you want to do from binary string to Decimal but Afterword, You might be needed contrary method. It's down below.
public static String decimalToBinaryString(int value) {
String str = "";
while(value > 0) {
if(value % 2 == 1) {
str = "1"+str;
} else {
str = "0"+str;
}
value /= 2;
}
return str;
}
You can also use this method to convert the binary to decimal integer if you are given with string.(Java Language)
static int binaryTodecimal(String s){
int i= -1;
char[] str = s.toCharArray();
int dec_val= 0;
for (int j=str.length-1; j>=0 ;j-- ){
int k= Integer.valueOf(str[j]) - '0';
i = i+1;
dec_val += k*(Math.pow(2, i));
}
System.out.println(dec_val);
}
If you're worried about performance, Integer.parseInt() and Math.pow() are too expensive. You can use bit manipulation to do the same thing twice as fast (based on my experience):
final int num = 87;
String biStr = Integer.toBinaryString(num);
System.out.println(" Input Number: " + num + " toBinary "+ biStr);
int dec = binaryStringToDecimal(biStr);
System.out.println("Output Number: " + dec + " toBinary "+Integer.toBinaryString(dec));
Where
int binaryStringToDecimal(String biString){
int n = biString.length();
int decimal = 0;
for (int d = 0; d < n; d++){
// append a bit=0 (i.e. shift left)
decimal = decimal << 1;
// if biStr[d] is 1, flip last added bit=0 to 1
if (biString.charAt(d) == '1'){
decimal = decimal | 1; // e.g. dec = 110 | (00)1 = 111
}
}
return decimal;
}
Output:
Input Number: 87 toBinary 1010111
Output Number: 87 toBinary 1010111

Categories