Oregon is home of two notable calderas, Crater Lake and Newberry Caldera at Newberry National Volcanic Monument, both within a 80 miles of each other. Crater Lake, famous for its size at 5 miles accross and 1,943 feet deep, formed 7,700 years ago when Mount Mazama erupted. Newberry is known for its “Big Obsidian Flow” which formed 1,300 years ago, from Oregon’s youngest lava flow. The panaroma above shows Crater Lake with Wizard Island taken with iPhone 6. Click image for higher resolution. Newberry Caldera contains two lakes, Paulina Lake and East Lake as seen from Paulina Peak (elevation 7,984 feet) in the image below.
Validating and searching for a phone number is a common task for user entry or content searching. A person may enter their phone number in a number of formats making the task slightly more difficult, but the important part concerns recognizing a number with correct number of digits with allowed prefixes. For this task we’ll create a regular expression that can be used to validate a US phone number an entry or search for US phone numbers within a body of text.
In the US, a phone number must be 10 digits (area code followed by 7 digit-number) and may be prefixed by the international or long-distance code (+1 or 1). Breaking up a US phone number into parts, we have area code of 3 digits followed by central office code (CO code, also known as exchange code) of 3 digits followed by four digits. Obviously beginning an area code or CO code with the international prefix of ‘1’ would be confusing, so these cannot begin with 1. Zero is for the operator, so that’s out as the first digit of area or CO codes. Certain prefixes are reserved in North America such as 911 and 411. What else is reserved? According this Wikipedia entry, all n11 combinations are reserved such that if the second digit is a ‘1’ then third digit cannot be a ‘1’ which makes things a bit easier for matching area and CO codes.
What about fictional characters with “555” CO code? It turns out only a range is set aside for fiction (555-0100 to 555-0199), so “555” is acceptable.
Phone numbers commonly appear with separators other than hyphen such as spaces, periods, or none at all such as (907) 555-0123, 1-907-555-0123, 907.555.0123, 907 555 0123, or 9075550123.
Since we had tested anagrams, it seems fair we give the palindrome check problem a go. How to check if a string is a palindrome? My solution is shown at the bottom in C# and Objective-C.
By definition a palindrome is word or phrase that is the same reversed ignoring lettercase, spaces, and punctuation. Other symbols and numbers count. Some examples:
- Madam, I’m Adam.
- Never odd or even.
- Rise to vote, sir!
An edited repost of my 2009 #FridayFlash for the final collector.
“What’s the matter with you?”
Fred winced at the familiar query. Crouched, he held the paintbrush tight. He knew what came next. It never failed. Dipping the brush into the can, he sloshed white paint onto the fence.
Leaning on her walker, Mother Dove stood on the porch glaring out over the yard. “Have a hole in your head?
Paint slapped on wood turning mottled gray white. Bristles splattered paint on Fred’s face. Frowning, he continued on pretending the old woman was dead.
“After Labor Day,” said Mother Dove. “The yard can’t wear white.”
In the previous problem, we looked at anagrams for fun and practice. Today we search for the sum-zero triplet. Given an array of integers, find three integers with a sum of zero. I encountered this problem during a phone interview and asked to write the code live in collabedit. My first thought was to do some sort of mapping, but then I thought I could sort the array in order to break from a loop early. If you’re practicing for an interview, try writing your solution in a text editor rather than an IDE. At the bottom you’ll find two variations to my solution written in C# and a sample written in Java.
The array must have at least three integers since the problem specifically asks for three, not three or fewer. Three zeros would do it, otherwise there needs to be at least one negative. Sorting an array allows us to use BinarySearch, direct our search for a third integer based on a starting pair, or break early realizing the lowest value is positive.