Objective-c random point which lies in given cgrect [closed] - objective-c

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 11 years ago.
my requirement is generating a random point in a given area, i.e i have an CGRect of some space and i need to generate a rendom point in this rect ..
how can i proceed in this scenario ??

You should generate 2 random values that lie in range of your rect.
Eg. a rect like this (100,50,300,200) will require you to get an x-value between 0 and 300 (your width) and then add 100 to it (your origin). Same will be required for y-value, get a random value between 0 and 200 (height) and add 50 (origin) to it.

Related

Algorithm for finding a sum within an array? [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
Say you have some array with elements that you know are all positive, and smaller than a number N.
Can someone give me a general, high-level description of an algorithm to find out whether there is some subset within the array in which all the elements add up exactly to N?
It doesn't need to be particularly efficient; the set I'm working with is very small.
If efficiency is not important, an algorithm at a high-level is:
input: array A[1..K] of positive numbers, positive N
for each subset S of { 1..K }
sum = 0
for each i in S
sum = sum + A[i]
end
if (sum equals N) return true
end
return false
Pseudocode. Extremely inefficient.
if empty(numbers) return false
return hasSumSubset(numbers, N)
boolean hasSumSubset(numbers[], N):
p = pop(numbers)
if empty(numbers) return N == p
return hasSumSubset(numbers, N-p) or hasSumSubset(numbers, N)
If you actually implement this make sure numbers is copied (not passed in by ref) for the recursive calls; otherwise it won't work correctly.

Sieve Of Sundaram [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I'm need someone to walk me through the Sieve of Sundaram (wiki). Lets say I have a list of Integers, (41,43,47,49,51,53,59) in an array.
Now the above mentioned wikipedia explanation mentions: *
An odd integer is excluded from the final list if and only if it is of
the form 2(i+j+2ij)+1
*. I am trying to understand how to achieve this in a program (preferably VBA). Such that the output will be (41,43,47,53,59).
Explanations sincerely appreciated.
Regards,
I discuss the Sieve of Sundaram at my blog. The algorithm is given by the following pseudocode:
function sundaram(n)
m := n // 2
sieve := makeArray(m+1, True)
for i from 1 to m // 4
for j from i to (m-i) // (2*i+1)
sieve[i+j+2*i*j] := False
ps := [2]
for i from 1 to m
if sieve[i]
append 2*i+1 to ps
return ps
As per the WP article, after removing some numbers from a range k=1...n, we get the results as m=2k+1. That means that all ms are odd.
Now, an odd composite must be a product of two odd numbers: 2k+1 = (2i+1)(2j+1) = 4ij+2i+2j+1 (if one of them were even, the product would be even too). But that means that k = 2ij + i + j. Hence the sieve's operation.
As nicely explained here.

How to test my solution (contest)? [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I made a code for contest, I am getting 'WA' but I am sure that my code is correct . So before I complain admin, how could I be sure that my code is actually correct and not that problem test data is incorrect? Here's the question and this is my solution in c++ , so can anyone suggest methods of testing one's own solution or can anybody supply test input where my solution produces wrong answer?
Oh , god you should check properly , i found another input which gives wrong output ( i told you usually test inputs are correct and we are usually wrong :D) . For input "1 2 1 3 4 2" your code outputs "1 4 2" which is certainly wrong , it should be "2 4 2" .
You have several examples inside the question explanation you can use to test your code:
For instance, the alternating depth of (([]))[[[()]]] is 2, the
maximum number of symbols between a matched pair () is 6 and the
maximum number of symbols between a matched pair [] is 8.
And
Sample Input
14
1 1 3 4 2 2 3 3 3 1 2 4 4 4
Sample Output
2 6 8
You can also invent some yourself, count the depth and maximum number of symbols and use that as input.
Moreover, it is said in the question that
The time limit for this task is 1 second. The memory limit is 32MB.
Are you sure your code didn't take more than a second on one of the very big inputs, with N is close to 10^5?
If the tests don't pass, its highly probable that the problem is in your code and not in the automated checker.

I need to check the X & Y points of a CGPoint for an image, then show an alert [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I'm making a game where images fall from the top of the screen, and I need to detect when they reach/go past a certain x & y value. I tried doing:
if (asteroid.center.y == 296 && asteroid.center.x == 50) {
// etc...displayed alert
}
But for some reason, it won't work. Thanks in advance!
It depends on what type of object "asteroid" is. If it is an instance of UIView, you should be able to get the position from the UIView's frame property:
if (asteroid.frame.origin.y >= 296 && asteroid.frame.origin.x >= 50) {
// etc...displayed alert
}
frame is a CGRect reference. You can learn more about it here: http://developer.apple.com/library/ios/#documentation/GraphicsImaging/Reference/CGGeometry/Reference/reference.html#//apple_ref/doc/c_ref/CGRect
EDIT:
If you wanted to check the center of the asteroid as oppose to the top/left, it would look like this:
if (asteroid.frame.origin.y + asteroid.frame.size.height / 2 >= 296 && asteroid.frame.origin.x + asteroid.frame.size.width / 2 >= 50) {
// etc...displayed alert
}

How to remove all lines that are already contained in another line [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
Say I have a large file "done.txt"
Then I have another large file "post.txt"
I want to get rid all occurrence in post.txt that is already located in done.txt
I do not want to load all content of done.txt on the memory. How would I do so?
100% accuracy is not important.
Since 100% accuracy is not a requirement, you can hash all the lines in done.txt and keep in-memory a collection (array, list, whatever) of those hashes.
Then, process every line in post.txt. If the hash of that line matches one you already have, throw it away.
There'll be false positives (lines thrown away even though they're not in done.txt) but no false negatives.
Something like:
hash = []
for each line in done.txt:
hashVal = makeHash (line)
hash[hashVal] = true
for each line in post.txt:
hashVal = makeHash (line)
if not defined hash[hashVal]:
print line
Or, if you want 100% accuracy with minimal in-memory storage, keep the hashes along with an collection of file offsets per hash.
If the line in post.txt doesn't match any hash, there's no possibility it's a duplicate, so you keep it.
If it does match a hash, then there's a possibility it's a duplicate. You then use the one or more file offsets for that hash entry to do a binary compare of the line being tested against the lines in done.txt (by reading in the actual lines). If a match is found there, it's a dupe so you toss away the line, otherwise you keep it.
That reduces in-memory storage (other than the lines from post.txt of course, but they're needed no matter what) to the hash-with-line-offsets collections and, at most, one line from done.txt, at the cost of some potential extra I/O.
But, since I'm not a big fan of "sub-100% accuracy", that's the way I'd probably go.
That would go something like:
hash = []
fileOffset = 0
for each line in done.txt:
hashVal = makeHash (line)
if not defined hash[hashVal]:
hash[hashVal] = new list ()
hash[hashVal].append (fileOffset)
fileOffset = fileOffset + line.length ()
for each line in post.txt:
hashVal = makeHash (line)
printIt = true
if defined hash[hashVal]:
for each offset in hash[hashVal]:
read chkLine from done.txt starting at offset
if line == chkLine:
printIt = false
if printIt:
print line