NSString built in method to compare number of similar characters - iphone

Is there a built in method (I can't find it by searching the documentation) to see the number of similar letters in two strings? The order of the letters are not relevant so comparing "abc" to "cad" would have a 66% match for the characters 'c' and 'd'. The number of occurences is also relevant. 'a' should match the first time around, but not on the second since there is only one common 'a' between the two strings. Is there a built in way to do this currently by using some bitwise operation or do I have to loop and manually compare?

You will have to build this yourself, but here is a shortcut for doing it. There is a built-in collection class called NSCountedSet. This object keeps each unique object and a count of how many of each were added.
You can take the two strings and load their characters into two different NSCountedSet collections. Then just check the items in the resulting collections. For example, grab an object from the first NSCountedSet. Check to see if it exists in the second NSCountedSet. The smaller of the 2 counts for that particular letter is how many of those letters that the 2 strings have in common. To shorten the number of iterations, start with the collection with fewer objects and then enumerate through those objects.
Here is Apple's Documentation for NSCountedSet.
https://developer.apple.com/library/ios/#documentation/Cocoa/Reference/Foundation/Classes/NSCountedSet_Class/Reference/Reference.html

I am hesitant to say but, there is probably no method out there that fills your requirements. I'd do this:
Create a category on NSString. Lets call it -(float)percentageOfSimilarCharactersForString:(NSString*)targetString
Here's a rough pseudocode that goes into this category:
Make a copy of self called selfCopy and trimselfCopy` to contain only unique characters.
Similarly trim targetString to unique characters. For trimming to unique characters, you could utilize NSSet or a subclass thereof. Looping over each character and adding to a set would help.
Now sort both sets by ASCII values.
Loop through each character of targetString-related NSSet and check for it's presence in selfCopy-related NSSet. For this you could use another category called containsString. You can find that here. Every time containsString returns true, increment a pre-defined counter.
Your return value would be (counter_value/length_of_selfCopy)*100.

Related

Is there a way to split a String/Sequence using a sub-string/collection delimiter in Swift?

I've seen various queries here on splitting a Swift string. But they always deal with using either a single character or one of a set of individual characters to split the string. How do I split one string with another? For example, what if I want to split on CR-LF? The analogous Sequence/Collection interfaces have the same problem, I can split on a single Element value, or one of a set of Element values, but not on a specific sub-sequence (actually, sub-collection since the value needs to be repeatedly visitable).
Is there some methods I'm missing? Or do I have to manually search by looping through the receiver Sequence finding the first element of the matching Collection, then return only if the rest of the collection is found at that sequence point?

A type for sorted arrays in Swift

Not sure if this is science fiction, but would it be possible to create a type that represents an Array that matches a certain condition, such as being always sorted?
Or a 2-tuple where the first element is always bigger than the second?
What you're describing is called a dependent type (https://en.wikipedia.org/wiki/Dependent_type). Swift does not have these, and I'm not aware of any mainstream (non-research) language that does. You can of course create a special kind of collection that is indexed like an array and sorts itself whenever it is modified, and you can crate a struct with greater and lessor properties that always reorders itself. But these criteria cannot be attached to the existing Array or tuple types.

Is there any way for Access 2016 to sort the numbers that are part of a "text" data type formatted field as though they are numeric values?

I am working on a database that (hopefully) will end up using a primary key with both numbers and letters in the values to track lots of agricultural product. Due to the way in which the weighing of product takes place at more than one facility, I have no other option but to maintain the same base number but use letters in addition to this base number to denote split portions of each lot of product. The problem is, after I create record number 99, the number 100 suddenly floats up and underneath 10. This makes it difficult to maintain consistency and forces me to replace this alphanumeric lot ID with a strictly numeric value in order to keep it sorted (which I use "autonumber" as the data type). Either way, I need the alphanumeric lot ID, and so having 2 ID's for the same lot can be confusing for anyone inputting values into the form. Is there a way around this that I am just not seeing?
If you're using query as a data source then you may try to sort it by string converted to number, something like
SELECT id, field1, field2, ..
ORDER BY CLng(YourAlphaNumericField)
Edit: you may also try Val function instead of CLng - it should not fail on non-numeric input
Why not properly format your key before saving ? e.g: "0000099". You will avoid a costly conversion later.
Alternatively, you could use 2 fields as the composite PK. One with the Number (as Long) and one with the Location (as String).

Indexing into cells using identifiers

I have a cell array in MATLAB that is reasonably large with very mixed data called sales. One column is a store identifier and that store identifier is a mix of letters and numbers (i.e. AF7-24M). I want to grab all the rows in sales where the store identifier is equal to a particular store identifier. I tried doing some logical indexing but I'm having trouble getting it to work...
I also would rather not just loop over all the rows because I need to do this multiple times and it's quite a slow process
you can use strcmp... for example:
strcmp(sales,'AF7-24M')
For case insensitive string comparison, use strcmpi instead of strcmp.

How to specify minimum word length in PostgreSQL full text search?

Is there a way to prevent words shorter than a specified value end up in tsvector? MySQL has the ft_min_word_len option, is there something similar for PostgreSQL?
The short answer would be no.
The tsearch2 uses dictionaries to normalize the text:
12.6. Dictionaries
Dictionaries are used to eliminate words that should not be considered
in a search (stop words), and to normalize words so that different
derived forms of the same word will match. A successfully normalized
word is called a lexeme.
and how the dictionaries are used Parsing and Lexing