As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 10 years ago.
I think that to add an object we need: create new array with a bigger size + make a copy of older array + add an element. So final complexity is O(N), where N - final number of elements.
Removing - O(N) also.
Am I wrong?
Thx.
I think that to add an object we need: create new array with a bigger size + make a copy of older array + add an element.
NOOOoooo....
To add an object, no new arrays are created and done all those stuffs.
If you remember cocoa has all pointers. And if you see C/C++ with pointers, just take it as a linked list. To add a new element only its address is saved in the list, and head/tail is adjusted if required.
Same case is here with MutableArrays.
Complexity should be O(1).
Related
As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 9 years ago.
I'm using AFNetworking to get JSON from the server, but response contains a little bit more then just JSON, so I cannot parse it:
array(11) {
["something"]=>
string(4) "none"
(...)
}
[[],{"JSONDataNow":
(...)
}]
Is this possible to remove other data then JSON from string?
The server is not returning JSON. It's returning a JSON-like string. Either fix the server to return JSON or write your own custom parser based on the custom rules used by the server.
Attempting to write a pre-processor that turns non-JSON string into JSON is not a good idea. You will have all the difficulties of writing a custom parser with none of the benefits of being the master of your own syntax.
As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 10 years ago.
I want to develop a logical expression evaluator to compute applicability of certain logical expression against a particular expression. For example,
An expression could be of the form
(A AND B) NOT C
this expression should then be evaluated with another expression like
(B AND C) OR D
The result of the evaluation in the above case is FALSE as the second expression doesn't full fill the first.
The expression can be more complex also, like it can have numerical ranges R(1-100) which means the applicability of the expression is valid through the ranges, like [A-Za-z0-9] in regular expression.
So the expression can be complex like
(A AND B) OR C AND R(1-100) NOT R(80-100)
and this has to be then evaluated by an expression like
(C OR D) AND B NOT R(1-7) AND R(25-100)
There are clear rules on when an expression satisfies another expression. So, if one has to write an expression evaluator, what is the best way to go. Since, I haven't done something before, I would like to have an head start. Any relevant pointers, or similar implementations could be vast help.
You can evaluate boolean expressions fairly easily using a stack.
basically as you see "values" you push them on the stack, as you see operators you apply them. Google "boolean expression stack" will give you plenty of hits.
As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 10 years ago.
I have a template of a letter and many of it's variations (see below) which i acquire from a digital pen :
Template
Test 1
Test 2
These letters are scaled to be in the same bounding box .
I want to detect the mistakes in the letter , like the mistake in Test 1 is that that there is a extra line , and mistake in Test 2 is that there is a missing segment . Similarly there can be a mistake in which there is a curve instead of a line segment . I want to find the parts which need to be corrected . How should i go about doing it ?
One ambiguity is that whether you only want to know the difference between your template and test image or you want to detect letter A by using your template.
As mentioned by you, the difference between your template and test image is of that extra line but i think there are more differences e.g template A is not made of straight lines rather including some curves as well but the test 1 image is approximately made up of straight lines.
These two are different problems in image processing and must be entertained differently. First you have to think what you want to do?
However, one solution is that You can divide the template and test image in sub blocks and try to find the correlation between them, if that gives matching upto a predefined threshold( you should define it intelligently) than there is no difference otherwise mark that block as difference between the template and test image.
you can use xcorr2 function in MATLAB and MATLAB help is sufficient to understand the working of this function.
As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 11 years ago.
I understand well how postfix and prefix increments/decrements work. But my question is, in a for loop, which is more efficient or faster, and which is more commonly used and why?
Prefix?
for(i = 0; i < 3; ++i) {...}
Or postfix?
for(i = 0; i < 3; i++) {...}
For ints in this context there is no difference -- the compiler will emit the same code under most optimization levels (I'd venture to say even in the case of no optimization).
In other contexts, like with C++ class instances, there is some difference.
In this particular case, none is actually more efficient than the other. I would expect ++i to be more commonly used, because that's what would be more efficient for other kinds of iterations, like iterator objects.
In my opinion, choosing prefix or postfix in a for loop depends on the language itself. In c++ prefix is more efficient and consistent. Because in the prefix type, compiler does not need to copy of unincremented value. Besides your value must not be an integer, if your value is an object than this prefix type is more powerful.
Either works, and one is not more efficient or faster than the other in this case. It's common for people to use ++1, maybe because that is what was used in K&R and other influential books.
As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 10 years ago.
This question asking for a phone number format API in Java got me wondering why such an API doesn't seem to exist. Given the number of validation classes available under Apache Commons, I would have thought such an API would be included.
My question is, does anyone know of a phone number formatting/validation library available in any language that could be used as a model for a small Java open source project? Also, other than the large number of different phone number formats used throughout the world, does anyone know of any reason that such a project would be infeasible?
Google recently released libphonenumber for parsing, formatting, storing and validating international phone numbers.
Such an API in any language: Number::Phone (example submodule: Number::Phone::US).
Data: the ITU's National Numbering Plans index.
At http://www.geonames.org/ they have a database you can download, theres regEx rules for phone numbers included in the country info dataset.
http://download.geonames.org/export/dump/
Look for the 'countryInfo.txt' file.
I suggest you build your own validator based upon that.
Like it was said in the answers to the other question, no one's seen any for Java, and like you said, it would be nearly impossible due to the difference of phone number formats across the world.
And if you know the different formats for the regions that you're targeting, then it's usually pretty easy to implement it in regular expressions.
You can check out Parsify Format here - https://www.mashape.com/parsify/format#endpoint-Phone-Number
It will return your number in several formats e.g.
{
"e164": "+1234567890",
"national": "234567890",
"international": "+1 234567890",
"valid": false,
"type": "UNKNOWN"
}
You can get a more extensive response if it's a real number
Phone Input field :
<input class="left" id="field" name="field" />
jquery Script:
jQuery.validator.addMethod("phoneUS", function(phone_number, element) {
phone_number = phone_number.replace(/\s+/g, "");
return this.optional(element) || phone_number.length > 9 &&
phone_number.match(/^(1-?)?(([2-9]\d{2})|[2-9]\d{2})-?[2-9]\d{2}-?\d{4}$/);
}, "Please specify a valid phone number");