I've been googling away like a mad man but can't anything that is specific enough for me to get started. Please forgive me for my complete noobiness and possibly diabolical skim reading ability.
Essentially, I have a questionnaire with 9 questions, each with 2 possible answers. After doing the math (2^9), I know that there are 512 permutations.
I am looking to generate a list of all the permutations, without any repetition, to provide me with a list of possible answer combinations.
I would like my output to look similar to this:
112112111
Where the 1s mean that the person selected answer "a" for a question and the 2s mean the person has selected answer "b".
Any help would be appreciated, thank you.
You are just enumerating the numbers between 0 and 512 and you want to print the string in its binary representation with 0s and 1s replaced by 1s and 2s, appropriately padded, so the following code will work:
for (int i = 0; i < 512; i++) {
System.out.println(String.format("%9s", Integer.toBinaryString(i)).replace('1', '2').replace('0', '1').replace(' ', '1'));
}
See this related question for generating a padded binary string in Java: How to get 0-padded binary representation of an integer in java?
One possible approach would be to find all the numbers between 111111111 and 222222222 that only contain 1s and 2s.
Something like:
for (i=111111111 ; i<=222222222 ; i++)
if (number_has_only_one_or_twos(i))
print i
If I am reading your question correctly, you are just wanting the binary representations of the numbers between 0 and 511 (because 511 - 0 + 1 = 512). All you need to do is find the 9 bit binary representation of each number in that range. Take 0s to mean answer B and 1s to mean answer A.
You could easily convert the binary numbers to strings and replace the 0 with 2 and print out. That's the beauty of binary representations of numbers. It is one of the best ways to find all the permutations in a given range.
Imagine finding the permutations of all elements of an array. Range the numbers from zero to the size of your array, and take a one to mean you want to use that index and a zero to not use that index. It will greatly simplify that problem whenever you run into it.
Related
Hey I am trying to study DS&A and I have tried to understand this problem but I don't understand it. I have looked at other forms but most of them are in other languages. Can someone please explain this problem to me in easiest simple form of JAVA.
Thank you
You can use Sort algorithms and then binary search to count the number of 1.
For ref. https://www.geeksforgeeks.org/count-1s-sorted-binary-array/
For unsorted binary arrays, Given an unsorted binary array, count number of 1's, where allowed only to check if an entire subarray is all zeros
I have came across a subset sum problem recently. I was able to solve it for smaller arrays using Java earlier, but in this case I have really no idea what should I do. Brute force and recurrence is probably not an option, as I came across out of memory problem.
So, let's say we have an array of {2500, 3200, 3300}. We are looking for the sum closest to the desired number K = 135000. The main difference is that we can use numbers from the array multiple times.
Ok, if we can use them multiple times, then we can change it to more "traditional" way - just divide K by each of these numbers - that is 54, 42 and 40 - and create a new array, which has those numbers the number of times received from dividing. It would be {2500, 2500, 2500, ... , ... 3300, 3300} and the new array would have the length of 136. Now this is much more than 3.
So - how to solve the closest subset sum problem, where we can pick more than 2 numbers from the array of 136 elements or more using Java?
The thing I want to get is not only the closest sum, but also a list of elements which gave that sum.
I heard and was reading about dynamic programing, approximation algorithms and genetic algorithms, but unfortunately I have no idea about those. I did genetic algorithm for a different case some time ago, but I am not sure how to use it in this case.
Any ideas? I will be really glad for help.
I am not going to solve it for you. but I'll give you the key ideas in pseudocode (aka Python).
We start with a state that represents the following statement: "I don't know how to arrive to any numbers. The best thing I can generate is 0. I have not yet processed the fact that I could get to 0."
In data:
can_generate = set()
todo = [0]
best = 0
K = 135000
What we will do is, while anything is in todo, take off a value, see if it is new to us. If it is, we might update best, and possibly add new values to todo. Like this:
while len(todo):
value = todo.pop()
if value not in can_generate:
can_generate.add(value)
if abs(K-value) < abs(K-best):
best = value
if value < K:
for term in [2500, 3200, 3300]:
todo.append(value + term)
Now that we know the values in can_generate, we search backwards to find how to get there.
answer = []
while 0 < best:
for term in [3300, 3200, 2500]:
if best - term in can_generate:
answer.append(term)
best -= term
break
I looked through here and found questions similar but not solving for the equation. Here is what I'm looking to do. I need to be able to determine a collection of equations that potentially can be used to solve a set of input parameters and a result. I will always know the inputs and the results. I need to figure out a way to solve for the solution for lack of a better term.
For instance:
Input parameters: 5,1,1,2
Result: 8
I would like to input these numbers and result and get something like:
FirstNumber(5) * (SecondNumber(1) + ThirdNumber(1)) - FourthNumber(2) = 8
FirstNumber(5) * FourthNumber(2) - SecondNumber(1) - ThirdNumber(1) = 8
Obviously it can be complex and given more numbers could have many possible solutions. My general question is around feasibility.
It is actually quite difficult task to solve.
First - you have to be able to solve any equation with +-*/ and with (). How to do that? You have to create tree-like structure with these operators and be able to count the result.
If you visualize the tree it looks like this:
When you are finished with this task, you can start with generating all the possibilities that can happen. The Backtracking is actually quite useful, as it is automatically remove the paths that does not lead to anything and with proper implementation it finds all possible solutions.
To get a collection of all possible linear equations I would try permutation without repetition for the inputs except the result (ignore possible identical inputs) and permutation taken from each cobination of all operators taken G at a time where G is the number of gaps between input numbers to fill the gaps between numbers and then try every position and size more than one and less than N for the numbers inside of the "()" where N is the number of inputs excluding the result.
Check the answers in this link for how to get permutation in java
Generating all permutations of a given string
My teacher and I were discussing whether or not a recursive permutation function could be written without the use of substrings and/or arrays in Java.
Is there a way to do this?
The answer is yes, this can be done. I'm assuming that "without the use of substrings and/or arrays" refers to the info being passed to the recursion. You have to have some sort of container for the elements that are to be permuted.
In that case it can be done by pulling some hideous tricks with numerically encoding the indices of the elements as digits of a numeric argument. For instance, if there are 3 elements and I use 1 as a sentinel value in the left-most digit (so you can have 0 as the leading index sometimes), 1 means I haven't started, 10 means the first element has been selected, 102 means the first and third, and 1021 means I'm ready to print the permutation since I now have a 4 digit argument and there are 3 elements in the set. I can then deconstruct which elements to print using % 10 and / 10 arithmetic to pick them off.
I implemented this in Ruby rather than Java, and I'm not going to share the actual code because it's too horrible to contemplate. However, it works recursively with only the input array of elements and an integer as arguments, no partial solution substrings or arrays.
I’m working on a problem where its demanded to get the last 100 digits of 2^1001. The solution must be in java and without using BigInteger, only with int or Long. For the moment I think to create a class that handle 100 digits. So my question is, if there is another way the handle the overflow using int or long to get a number with 100 digits.
Thanks for all.
EDITED: I was off by a couple powers of 10 in my modulo operator (oops)
The last 100 digits of 2^1001 is the number 2^1001 (mod 10^100).
Note that 2^1001 (mod 10^100) = 2*(2^1000(mod 10^100)) (mod 10^100).
Look into properties of modulo: http://www.math.okstate.edu/~wrightd/crypt/lecnotes/node17.html
This is 99% math problem, 1% programming problem. :)
With this approach, though, you wouldn't be able to use just an integer, as 10^100 won't fit in an int or long.
However, you could use this to find, say, the last 10 digits of 2^1001, then use a separate routine to find the next 10 digits, etc, where each routine uses this feature...
The fastest way to do it (coding wise) is to create an array of 100 integers and then have a function which starts from the ones place and doubles every digit. Make sure to take the modulus of 10 and carry over 1s if needed. For the 100th digit, simply eliminate the carry over. Do it 1001 times and you're done.