I am writing a program in Scheme (Dr. Racket) to verify Canadian postal-codes. The user inputs a postal code and gets a response whether it is valid or not. I got the boolean logic down but I am stumped as to how to actually tell it what the correct format is.
ex. (valid-postal-code? N2L 3G1) => true
How do I do this?
Thanks
If you want to know if a string has the format of a valid postal code, you can use a regular expression. Canadian postal codes consist of six characters, alternating letters and digits beginning with a letter, with a space embedded between the third and fourth characters. A suitable regular expression is ^[A-Z][0-9][A-Z] [0-9][A-Z][0-9]$.
if you want to know if a string with a valid format is on the list of postal codes, the easiest solution is a bloom filter. I provide a bloom filter, written in Scheme, at my blog.
I don't know how Canadian postal codes work, but I think what you are asking is that you probably just have a long list of valid codes and need to tell the program that they are ok, and no other codes are.
Using a mutable hash map would be ideal for your purpose: http://docs.racket-lang.org/guide/hash-tables.html
Related
I am new to RegEx and need some guidance. Right now I have the following validation for phone numbers:
(\d{3}) ?\d{3}( |-)?\d{4}|^\d{3}( |-)?\d{3}( |-)?\d{4}
Unfortunately, the system I am importing the results into does not think favorably of the numbers being separated solely by spaces or not separated at all. What would the formula look like that requires either dashes or parentheses and accepts only the following formats: XXX-XXX-XXXX or (XXX) XXX-XXXX?
Thank you for your assistance.
Start simple:
\d{3}-\d{3}-\d{4} works beautifully for numbers like 212-867-5309.
As for others, I'd say you and your users would be better off if you kept it simple. No switching, no choices. Pick a standard. Simple is good.
If you must persist, look at this web site for help. You aren't the first.
For my internship I'm working on a App that uses GPS data! That's already implemented and I wrote a class which converts the double-value the mapView sends into an user-picked format for Geo Coordinates (Degrees, Degrees-Minutes or Degrees-Minutes-Seconds)! Now there are also text fields the user should enter some coordinates in for adding waypoints!
What's the best technique here to get a the seperate numbers out of a string in a format similar to this 57° 14' 03" N!
Since it's a user input, the format won't be this, it's only similar! So is it better to parse these out the string or maybe limit the users input method from a textfield to something more strict which only allows one format (separate textfields for each number f.ex.)!
Actually a question to UX rather than a how-to-do!
Acting as the delegate of the text field and not allowing invalid content / format is a good first step.
For parsing the string, NSScanner is the appropriate class to use to split out the parts. If you tie the format down though, you could use componentsSeparatedByString: to separate each number by the space between them.
First, a comment. Ending all your sentences with exclamation points is silly!
As to your question. Yes, you should enforce a strict input format on your users. If you look in the software developers's dictionary, user is a synonym for idiot.
I would suggest separate UITextFields for each numeric value of each lat/long, with the symbols drawn in place with labels. The user would enter degrees, and the input would jump to minutes. The user enters minutes, and the input jumps to seconds. The user enters seconds, and the input jumps to degrees on the longitude.
Validate each input as a well-formed number.
If you want to use free-form input of strings like "57° 14' 03" N!", you might want to create a regular expression to validate it, plus range-checking on the numeric parts. If you Google it you should find online docs on regular expression. I don't use them often enough to be able to write a regular expression off the top of my head.
I am facing one issue in one of my Rails project.
My users database contain names with special character and i want them to be shown in search result while searching it with simple characters.
Example: Lets suppose i have a user whose name is "Noël Nocciolo" (please notice soft sign on e) and i want that to be searched if i pass "Noel Nocciolo" as a parameter.
Can anyone tell me how to handle with these cases because no one knows how to provide input of "e with two dots".
And i am using postgres as my databse.
Regards,
Karan
You can create separate field "indexed_name" for search and fill it only with ASCII characters.
Then you have to preprocess query string with .gsub('ë', 'e') (or any other non ASCII characters to its ASCII analog) and search with this processed query
and i believe there is more elegant way to convert any string to ascii analog i just gave you direction )
.parameterize or ActiveSupport::Inflector.transliterate will probably be acceptable for your use case.
"àáâãäå".parameterize
=> "aaaaaa"
However, it won't handle ligatures such as ffi, so for that you'll need:
"àáâãÀffi".mb_chars.normalize(:kd).gsub(/[^\x00-\x7F]/,'').to_s
=> "aaaaAffi"
I've seen a number of QR codes that contain a URL but also have extra some text after it. Something like:
http://www.example.com Thanks for scanning this QR code!
I've experimented with using a number of different delimiting methods (several spaces, a question mark, two dashes, one or two returns) and all work to varying degrees on various scanning programs.
Some respect the space character, others respect the return. Some think the URL isn't a URL at all when I use a return. Long story short, it's all over the map how the various scanning programs (NeoReader, iNigma, Qrafter, Beetag, OptiScan, etc.) treat characters after a URL.
Is there any consensus on weather (a)this is even a good idea or not and (b)if so, what is the 'correct' (best practice) way to do it? (I know I should go read the RFC for the exact definition of a URL but since the reader programs are all over the map, I suspect they didn't read it either.)
You can make it work by converting the text message into valid URL, while trying to keep readability.
In your case it can be:
http://www.example.com?Thanks_for_scanning_this_QR_code
It's not perfect, but it can help on web analytics side to distinguish all QR code users.
Spaces are definitely not part of a URL, so, in that sense a space definitely should delimit the end of a URL.
The entire string is not a URL, taken as a whole of course. So yes it's asking for trouble.
As you've found, the empirical answer is that not every reader does what you want. Barcode Scanner for instance understands the split here, but does not prompt the user to launch the browser since the payload isn't a URL per se.
So: it's a bad idea.
For north american phone numbers, (999) 999-9999 works pretty well for an input mask.
However, I can't find a good example that will handle non-north american numbers. I know that the number of digits can vary, so other than restricting it to digits only, is there a good example anywhere?
There is no generic mask, really: There are too many combinations.
The only thing that is fixed is the international country code, usually prefixed by +.
According to the Wikipedia Article on telephone numbering plans, most countries conform with the E.164 numbering plan.
If I read E.164 correctly, you can safely make the following assumptions:
Country code: 1-3 digits
Network / Area code and Number: Up to 19 digits
I would ask for the country code, and have the "area code + number" field as a 19-digit input.
You can deduce the country code with a simple RegEx such as:
^(?:(?:0(?:0|11)\s?)|+)([17]|2([07]|[1-689]\d)|3([0-469]|[578]\d)|4([013-9]|2\d)|5([1-8]|[09]\d)|6([0-6]|[789]\d)|8([12469]|[03578]\d)|9([0-58]|[679]\d))
Followed by
(([\s\(\).-]{0,2}\d){4,13})$
to extract the national number.
For validating the national number length and validity, you'd need libphonenumber or similar.
The long RegEx above allows +, 00 or 011 before the country code and a selection of punctuation in the number which will also have to be stripped.
You don't mention your application but this is certainly possible using regular expressions. You might want to take a look here.
Not easily. Take a look at this page for an example why: if you only look at the German phone numbers, you'll note that there are different formats depending on where you're calling the number from. Which one do you pick? And that's just for German phone numbers; they differ from continent to continent, and from country to country.
Going with "numbers-only" is probably your safest bet.
I would allow for spaces, dashes, slashes and all that, but actually only care for numbers and the optional leading + sign. Everything else, such as assuming certain blocks of a certain length is just asking for trouble.
May be it is bad to answer an old question. But libphonenumber seems like a good solution to your question.