I have a function that generate an integer random number between two given values. This function in my app will be called only two times however, I want to avoid that it generates two same numbers or two number next to each other. How could I fix the function below to achieve that?
Example Result:
1, 1 = wrong
1, 2 = wrong
8, 7 = wrong
8, 12 = correct
1, 3 = correct
3, 0 = correct
Function:
func randomNumber(minX:UInt32, maxX:UInt32) -> Int {
let result = (arc4random() % (maxX - minX + 1)) + minX
return Int(result)
}
EDIT:
How can it be a duplicate???
I am not asking to shuffle an array but I want to generate a single Int from two given numbers
You said "I want to avoid ... two numbers next to each other" so I don't understand how 8 ,7 is wrong but 8, 9 is correct.
Anyhow, my answer answer is based on your example. Just send the previous value and loop until you get a satisfactory asnwer:
func randomNumber(minX:UInt32, maxX:UInt32, previousNumber: Int? = nil) -> Int {
var result: Int
repeat {
result = Int((arc4random() % (maxX - minX + 1)) + minX)
} while previousNumber == result || previousNumber == result + 1
return result
}
let r1 = randomNumber(1, maxX: 3)
let r2 = randomNumber(1, maxX: 3, previousNumber: r1)
This will lead to an infinite loop when there's no number that can satisfy your conditions, for example:
randonNumber(1, maxX: 2, previousNumber: 2)
You should add a guard statement by working out the possibility mathematically and create a counter so that the repeat loop ends after, say 100 iterations.
Make global/static variable, where you will remember last valid generated number. Then add if statement, which will recursively call randomNumber, when you generate same(/next to each other) number as in that variable. If there hasn't been generated any number, set number out of interval.
Related
How can I check whether 5 given numbers and mathematical operations (+, -, *)could be arranged to get the result of 23?
For instance:
1 1 1 1 1 –– Impossible
1 2 3 4 5 –– Possible
Specifications:
All the operations have the same priority and performed from left to right (not in mathematically correct order).
While you can use brute force to try out every possible combination, i would suggest a slightly more elegant solution:
The last digit as well as multiplication is the key. If the result (23) isn't divisible by the last digit, then the last operator can't be "*". Then you can try out the same for the result +- the last digit, as it is either added or subtracted. Iterating backwards this way should save quite a few iterations.
Pseudo-Code Example:
var digits = [1, 2, 3, 4, 5];
var expected = 23;
var combinatoric = function(digits, expected) {
var result = false;
var digit = digits[digits.length -1];
var nDigits = digits.removeLast();
// If we are at the last digit...
if(nDigits.isEmpty() && Math.abs(digit) == Math.abs(expected)) {
//Last digit must be added or substracted, as its the starting digit.
result = true;
} else if(!nDigits.isEmpty()) {
//Only if divisible is "*" an option.
if(expected % digit == 0) {
if(combinatoric(nDigits, expected / digit) {
result = true;
}
}
// "+" and "-" are always options.
if(combinatoric(nDigits, expected - digit) {
result = true;
}
if(combinatoric(nDigits, expected + digit) {
result = true;
}
}
return result;
}
This approach saves at least a few iterations, as you dont try to multiply if it won't resolve to a natural number anyway. And by going backwards you can make this calculation recursive, as the modified expected result is passed on to every iteration.
I actually tried to search this, I'm sure this basic algorithm is everywhere on internet, CS textbooks etc, but I cannot find the right words to search it.
What I want from this algorithm to do is write "A" and "B" with the limit always increasing by 2. Like I want it to write A 3 times, then B 5 times, then A 7 times, then B 9 times and so on. And I plan to have 100 elements in total.
Like: AAABBBBBAAAAAAABBBBBBBBB...
I only want to use a single "for loop" for the entire 100 elements starting from 1 to 100. And just direct/sort "A" and "B" through "if/else if/ else".
I'm just asking for the basic mathematical algorithm behind it, showing it through any programming language would be better or redirecting me to such topic would also be fine.
You can do something like this:
There might be shorter answers, but I find this one easy to understand.
Basically, you keep a bool variable that will tell you if it's A's turn or Bs. Then we keep a variable switch that will tell us when we should switch between them. times is being updated with the repeated times we need to print the next character.
A_B = true
times = 3 // 3,5,7,9,...
switch = 3 // 3,8,15,24,...
for (i from 1 to 100)
if (A_B)
print 'A'
else
print 'B'
if (i == switch)
times += 2
switch += times
A_B = !A_B
Python:
for n in range(1, 101):
print "BA"[(int(sqrt(n)) % 2)],
The parity of the square roots of the integers follows that pattern. (Think that (n+1)²-n² = 2n+1.)
If you prefer to avoid the square root, it suffices to use an extra variable that represents the integer square root and keep it updated
r= 1
for n in range(1, 101):
if r * r <= n:
r+= 1
print "AB"[r % 2],
Here is the snippet you can test on this page. It is an example for about 500 letters totally, sure you can modify it for 100 letters. It is quite flexible that you can change the constants to produce lot of different strings in the same manner.
var toRepeat = ['A', 'B'];
var result='', j, i=3;
var sum=i;
var counter = 0;
while (sum < 500) {
j = counter % 2;
result = result + toRepeat[j].repeat(i);
sum = sum + i;
i = i + 2;
counter++;
}
document.getElementById('hLetters').innerHTML=result;
console.log(result);
<div id="hLetters"></div>
If you want it to be exactly 500 / 100 letters, just use a substring function to trim off the extra letters from the end.
To get 100 groups of A and B with increasing length of 3, 5, 7 and so on, you can run this Python code:
''.join(('B' if i % 2 else 'A') * (2 * i + 3) for i in range(100))
The output is a string of 10200 characters.
If you want the output to have only 100 characters, you can use:
import math
''.join(('B' if math.ceil(math.sqrt(i)) % 2 else 'A') for i in range(2, 102))
In js you can start with somethink like this :
$res ="";
count2 = 0;
for (i=2;i<100; i = i+2) {
count = 0;
alert(i);
while (count < i ) {
$res = $res.concat(String.fromCharCode(65+count2));
count++;
}
count2++;
}
alert ($res);
I have the following code which implements a recursive solution for this problem, instead of using the reference variable 'x' to store overall max, How can I or can I return the result from recursion so I don't have to use the 'x' which would help memoization?
// Test Cases:
// Input: {1, 101, 2, 3, 100, 4, 5} Output: 106
// Input: {3, 4, 5, 10} Output: 22
int sum(vector<int> seq)
{
int x = INT32_MIN;
helper(seq, seq.size(), x);
return x;
}
int helper(vector<int>& seq, int n, int& x)
{
if (n == 1) return seq[0];
int maxTillNow = seq[0];
int res = INT32_MIN;
for (int i = 1; i < n; ++i)
{
res = helper(seq, i, x);
if (seq[i - 1] < seq[n - 1] && res + seq[n - 1] > maxTillNow) maxTillNow = res + seq[n - 1];
}
x = max(x, maxTillNow);
return maxTillNow;
}
First, I don't think this implementation is correct. For this input {5, 1, 2, 3, 4} it gives 14 while the correct result is 10.
For writing a recursive solution for this problem, you don't need to pass x as a parameter, as x is the result you expect to get from the function itself. Instead, you can construct a state as the following:
Current index: this is the index you're processing at the current step.
Last taken number: This is the value of the last number you included in your result subsequence so far. This is to make sure that you pick larger numbers in the following steps to keep the result subsequence increasing.
So your function definition is something like sum(current_index, last_taken_number) = the maximum increasing sum from current_index until the end, given that you have to pick elements greater than last_taken_number to keep it an increasing subsequence, where the answer that you desire is sum(0, a small value) since it calculates the result for the whole sequence. by a small value I mean smaller than any other value in the whole sequence.
sum(current_index, last_taken_number) could be calculated recursively using smaller substates. First assume the simple cases:
N = 0, result is 0 since you don't have a sequence at all.
N = 1, the sequence contains only one number, the result is either that number or 0 in case the number is negative (I'm considering an empty subsequence as a valid subsequence, so not taking any number is a valid answer).
Now to the tricky part, when N >= 2.
Assume that N = 2. In this case you have two options:
Either ignore the first number, then the problem can be reduced to the N=1 version where that number is the last one in the sequence. In this case the result is the same as sum(1,MIN_VAL), where current_index=1 since we already processed index=0 and decided to ignore it, and MIN_VAL is the small value we mentioned above
Take the first number. Assume the its value is X. Then the result is X + sum(1, X). That means the solution includes X since you decided to include it in the sequence, plus whatever the result is from sum(1,X). Note that we're calling sum with MIN_VAL=X since we decided to take X, so the following values that we pick have to be greater than X.
Both decisions are valid. The result is whatever the maximum of these two. So we can deduce the general recurrence as the following:
sum(current_index, MIN_VAL) = max(
sum(current_index + 1, MIN_VAL) // ignore,
seq[current_index] + sum(current_index + 1, seq[current_index]) // take
).
The second decision is not always valid, so you have to make sure that the current element > MIN_VAL in order to be valid to take it.
This is a pseudo code for the idea:
sum(current_index, MIN_VAL){
if(current_index == END_OF_SEQUENCE) return 0
if( state[current_index,MIN_VAL] was calculated before ) return the perviously calculated result
decision_1 = sum(current_index + 1, MIN_VAL) // ignore case
if(sequence[current_index] > MIN_VAL) // decision_2 is valid
decision_2 = sequence[current_index] + sum(current_index + 1, sequence[current_index]) // take case
else
decision_2 = INT_MIN
result = max(decision_1, decision_2)
memorize result for the state[current_index, MIN_VAL]
return result
}
http://codeforces.com/contest/520/problem/B
Vasya has found a strange device. On the front panel of a device there are: a red button, a blue button and a display showing some positive integer. After clicking the red button, device multiplies the displayed number by two. After clicking the blue button, device subtracts one from the number on the display. If at some point the number stops being positive, the device breaks down. The display can show arbitrarily large numbers. Initially, the display shows number n.
Bob wants to get number m on the display. What minimum number of clicks he has to make in order to achieve this result?
Input
The first and the only line of the input contains two distinct integers n and m (1 ≤ n, m ≤ 10^4), separated by a space .
Output
Print a single number — the minimum number of times one needs to push the button required to get the number m out of number n.
I developed the following recursive solution. I know it will time out, but I will memoise it, and that will get my solution accepted. But as of now, I am getting a wrong answer in one of the inputs.
My code is:
int func (int n, int m);
int main (void)
{
int n,m;
cin>>n>>m;
int count = func(n,m);
cout<<count<<"\n";
return 0;
}
int func (int n, int m)
{
if (n == 0)
return INT_MAX; // this should be because we can never go to some
// other digit if we are at 0
if (n == m)
return 0;
else if (2*n == m || n == m+1)
return 1;
else if (n > m)
return func(n-1,m)+1;
else
return min(func(n-1,m),func(n*2,m))+1;
}
Now, when I enter the input as (1,3), my code shows Segmentation fault. I tried to debug it, and I found out that it sorts of go in an infinite loop because of which I get the Seg fault. However, I want to know, then how should I make the logic for this? What will be the recursive function for this? Thanks!
The SEG fault is due to calculating doing INT_MAX+1.
Actually I think this problem is better solved working this way.
For all cases n>m, the shortest count is n-m.
if (n<m)
return n-m;
For all cases n==m, the shortest count is 0.
else if (n==m)
return 0;
For all cases n < m, the shortest count can be calculated as:
let sequence Y= [(m/(2^1), m/(2^2), ... 1] // use the ceiling values
find X is the next number in the Y where n >= X.
return func(X*2) + n-X + 1;
For n = 57, m = 201, then Y = [101, 51, 26, 13, 7, 4, 2, 1], X would be 51.
So the answer can be calculated as
(57-51)+1 = 7 steps, result now 51*2 = 102
(102-101)+1 = 2 steps, result now 101*2 = 202
(202-201) = 1 steps
=====> Total steps 10
For n = 4, m = 6, then Y = [3, 2, 1], X would be 3.
So the answer can be calculated as
(4-3)+1 = 2 steps, result now 3*2=6
=====> Total steps 2
For n = 1, m = 3, then Y = [2, 1], X would be 1.
So the answer can be calculated as
already in Y= 1 steps, result now 1*2 = 2
already in Y= 1 steps, result now 2*2 = 4
(4-3) = 1 step
=====> Total steps 3
Notice you can precalcuate Y before entering your function and pass it in so you don't have to recompute each time.
Actually, this question can be generalized as below:
Find all possible combinations from a given set of elements, which meets
a certain criteria.
So, any good algorithms?
There are only 16 possibilities (and one of those is to add together "none of them", which ain't gonna give you 24), so the old-fashioned "brute force" algorithm looks pretty good to me:
for (unsigned int choice = 1; choice < 16; ++choice) {
int sum = 0;
if (choice & 1) sum += elements[0];
if (choice & 2) sum += elements[1];
if (choice & 4) sum += elements[2];
if (choice & 8) sum += elements[3];
if (sum == 24) {
// we have a winner
}
}
In the completely general form of your problem, the only way to tell whether a combination meets "certain criteria" is to evaluate those criteria for every single combination. Given more information about the criteria, maybe you could work out some ways to avoid testing every combination and build an algorithm accordingly, but not without those details. So again, brute force is king.
There are two interesting explanations about the sum problem, both in Wikipedia and MathWorld.
In the case of the first question you asked, the first answer is good for a limited number of elements. You should realize that the reason Mr. Jessop used 16 as the boundary for his loop is because this is 2^4, where 4 is the number of elements in your set. If you had 100 elements, the loop limit would become 2^100 and your algorithm would literally take forever to finish.
In the case of a bounded sum, you should consider a depth first search, because when the sum of elements exceeds the sum you are looking for, you can prune your branch and backtrack.
In the case of the generic question, finding the subset of elements that satisfy certain criteria, this is known as the Knapsack problem, which is known to be NP-Complete. Given that, there is no algorithm that will solve it in less than exponential time.
Nevertheless, there are several heuristics that bring good results to the table, including (but not limited to) genetic algorithms (one I personally like, for I wrote a book on them) and dynamic programming. A simple search in Google will show many scientific papers that describe different solutions for this problem.
Find all possible combinations from a given set of elements, which
meets a certain criteria
If i understood you right, this code will helpful for you:
>>> from itertools import combinations as combi
>>> combi.__doc__
'combinations(iterable, r) --> combinations object\n\nReturn successive r-length
combinations of elements in the iterable.\n\ncombinations(range(4), 3) --> (0,1
,2), (0,1,3), (0,2,3), (1,2,3)'
>>> set = range(4)
>>> set
[0, 1, 2, 3]
>>> criteria = range(3)
>>> criteria
[0, 1, 2]
>>> for tuple in list(combi(set, len(criteria))):
... if cmp(list(tuple), criteria) == 0:
... print 'criteria exists in tuple: ', tuple
...
criteria exists in tuple: (0, 1, 2)
>>> list(combi(set, len(criteria)))
[(0, 1, 2), (0, 1, 3), (0, 2, 3), (1, 2, 3)]
Generally for a problem as this you have to try all posebilities, the thing you should do have the code abort the building of combiantion if you know it will not satesfie the criteria (if you criteria is that you do not have more then two blue balls, then you have to abort calculation that has more then two). Backtracing
def perm(set,permutation):
if lenght(set) == lenght(permutation):
print permutation
else:
for element in set:
if permutation.add(element) == criteria:
perm(sett,permutation)
else:
permutation.pop() //remove the element added in the if
The set of input numbers matters, as you can tell as soon as you allow e.g. negative numbers, imaginary numbers, rational numbers etc in your start set. You could also restrict to e.g. all even numbers, all odd number inputs etc.
That means that it's hard to build something deductive. You need brute force, a.k.a. try every combination etc.
In this particular problem you could build an algoritm that recurses - e.g. find every combination of 3 Int ( 1,22) that add up to 23, then add 1, every combination that add to 22 and add 2 etc. Which can again be broken into every combination of 2 that add up to 21 etc. You need to decide if you can count same number twice.
Once you have that you have a recursive function to call -
combinations( 24 , 4 ) = combinations( 23, 3 ) + combinations( 22, 3 ) + ... combinations( 4, 3 );
combinations( 23 , 3 ) = combinations( 22, 2 ) + ... combinations( 3, 2 );
etc
This works well except you have to be careful around repeating numbers in the recursion.
private int[][] work()
{
const int target = 24;
List<int[]> combos = new List<int[]>();
for(int i = 0; i < 9; i++)
for(int x = 0; x < 9; x++)
for(int y = 0; y < 9; y++)
for (int z = 0; z < 9; z++)
{
int res = x + y + z + i;
if (res == target)
{
combos.Add(new int[] { x, y, z, i });
}
}
return combos.ToArray();
}
It works instantly, but there probably are better methods rather than 'guess and check'. All I am doing is looping through every possibility, adding them all together, and seeing if it comes out to the target value.
If i understand your question correctly, what you are asking for is called "Permutations" or the number (N) of possible ways to arrange (X) numbers taken from a set of (Y) numbers.
N = Y! / (Y - X)!
I don't know if this will help, but this is a solution I came up with for an assignment on permutations.
You have an input of : 123 (string) using the substr functions
1) put each number of the input into an array
array[N1,N2,N3,...]
2)Create a swap function
function swap(Number A, Number B)
{
temp = Number B
Number B = Number A
Number A = temp
}
3)This algorithm uses the swap function to move the numbers around until all permutations are done.
original_string= '123'
temp_string=''
While( temp_string != original_string)
{
swap(array element[i], array element[i+1])
if (i == 1)
i == 0
temp_string = array.toString
i++
}
Hopefully you can follow my pseudo code, but this works at least for 3 digit permutations
(n X n )
built up a square matrix of nxn
and print all together its corresponding crossed values
e.g.
1 2 3 4
1 11 12 13 14
2 .. .. .. ..
3 ..
4 .. ..