Draco Torre

David G Shrock

Programming Problem: Palindromes

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.

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.”

Programming Problem: Sum-Zero Triplet

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.

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.

Programming Problem: Determine if Two Strings Are Anagrams

This problem may come up during an interview, and it’s a bit of fun. How do we determine if two strings are anagrams?

If you’re practicing for an interview, write your solution in a text editor rather than an IDE. For syntax coloring, Visual Studio Code without IntelliSense works well with support for many languages. I’ve provided my solution in C# (csharp) and Objective-C (ObjC) at the bottom. A Java solution would appear similar to C# and I’ve noted differences.

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.

Computer Perfection and the User Experience

Like building a movie set, the virtual world created by software designers and artists begins clean and perfect. Computer models are pure, created at origin in perfect orientation. For a better experience, movie set artists add dirt and signs of aging. In a race simulator game, the track needs dirt, weathering, scuff marks, and trees require some randomness so they don’t appear too much alike. More signs of realism improves immersion and reduces distractions.

The two screenshots below reveal parked cars within racing games. Since these show off the car, likely before a racing event, clean cars are expected. The first screenshot is from Asseto Corsa by Kunos Simulazioni. The second screenshot comes from Forza Motorsport 4 by Turn 10 Studios. What’s unnatural about the parked cars in the two images?