Posts
Problem: Validate a Phone Number
Article #4 in a 9-part series.
- 1 - Programming Problem: Determine if Two Strings Are Anagrams
- 2 - Programming Problem: Sum-Zero Triplet
- 3 - Programming Problem: Palindromes
- 4 - this article
- 5 - Programming Problem: Single-Edit Difference
- 6 - Prime Factors Problem 1: LCM
- 7 - Prime Factors Problem 2: Largest Prime Factor
- 8 - How-To: Substrings in Swift
- 9 - Programming Problem: Pangram
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.
observations
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.
Continue reading...Programming Problem: Palindromes
Article #3 in a 9-part series.
- 1 - Programming Problem: Determine if Two Strings Are Anagrams
- 2 - Programming Problem: Sum-Zero Triplet
- 3 - this article
- 4 - Problem: Validate a Phone Number
- 5 - Programming Problem: Single-Edit Difference
- 6 - Prime Factors Problem 1: LCM
- 7 - Prime Factors Problem 2: Largest Prime Factor
- 8 - How-To: Substrings in Swift
- 9 - Programming Problem: Pangram
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#, Java, Objective-C, Swift (updated for 5), and C++.
observations
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!
- Salas
- 191
Mother Dove Redux
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.”
Continue reading...Programming Problem: Sum-Zero Triplet
Article #2 in a 9-part series.
- 1 - Programming Problem: Determine if Two Strings Are Anagrams
- 2 - this article
- 3 - Programming Problem: Palindromes
- 4 - Problem: Validate a Phone Number
- 5 - Programming Problem: Single-Edit Difference
- 6 - Prime Factors Problem 1: LCM
- 7 - Prime Factors Problem 2: Largest Prime Factor
- 8 - How-To: Substrings in Swift
- 9 - Programming Problem: Pangram
In the previous problem, we looked at anagrams for fun and practice. In this post 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 later I realized 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.
observations
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.
Continue reading...Programming Problem: Determine if Two Strings Are Anagrams
Article #1 in a 9-part series.
- 1 - this article
- 2 - Programming Problem: Sum-Zero Triplet
- 3 - Programming Problem: Palindromes
- 4 - Problem: Validate a Phone Number
- 5 - Programming Problem: Single-Edit Difference
- 6 - Prime Factors Problem 1: LCM
- 7 - Prime Factors Problem 2: Largest Prime Factor
- 8 - How-To: Substrings in Swift
- 9 - Programming Problem: Pangram
How do we determine if two strings are anagrams?
For syntax coloring, Visual Studio Code without IntelliSense works well with support for many languages. I’ve provided my solution in C# (csharp) and Java followed by Objective-C (ObjC) and Swift at the bottom (updated for Swift 5).
observations
By definition, an anagram is a word or phrase formed from another word or phrase by rearranging the letters. Spaces and punctuation don’t count, just the letters ignoring case. Here are a few samples:
- “Tom Marvolo Riddle” <-> “I am Lord Voldemort!”
- “Dave Barry” <-> “Ray Adverb”
- “debit card” <-> “bad credit”
- “astronomer” <-> “Moon starer”
- “School master” <-> “the classroom”
Since we must use all of the letters we can conclude that ignoring spaces and punctuation, the two strings must be the same length. Also, the same phrase is not an anagram of itself.
Notice that if we sort the letters of a phrase and its anagram we get identical strings.
Continue reading...