Hey, this is the first problem in a series of programming problems/puzzles I’ll be posting during my study abraod in Budapest, Hungary. If you like this puzzle and its solution, come back for more. If you don’t, tell me how I could improve the puzzle and/or solutions format via comments on this blog.
Before arriving Budapest, I tried to learn basic hungarian phrases used in day-to-day discussions. I learned to introduced myself: Jo napot, a nevem Daniel. I also learned to ask to use the toilet, ask for directions, and let my interlocutor know that I don’t speak Hungarian. But how much Hungarian have I used in my conversations thus far? None. Most young people in Budapest speak acceptable english. I’d rather speak in a language with which I’m very familiar with than converse in a very beautiful language that I have a strenuous grasp on. The puzzle below is a fictional attempt to sift out the young people who speak english very well.
Puzzle 1: Anyone Speak English Here?
I currently live around Astoria in Budapest. I want to go to the mall to purchase a video camera to use during the Hungarian drinking festival in the Buda Castle in the Buda side of Budapest. I have to use the subway. But I don’t know which metro line I should use. I’m determined to figure it out by asking someone, in English, around the metro station area to tell me when to get on and when to get out of the metro line when I arrive the Castle. To do that, I must first sift out the young people who speak English by using some algorithm (maybe?).
I’ll give my target young person an English word and ask him/her to give me another English word that’s closest to it. The distance metric I use to measure closesness of words uses costs on substitution, insertion, and deletion of characters in the word given. I prefer that fewer characters be deleted from or inserted into the source word. Substituting one character for another is, therefore, preferred over insertions or deletions of characters.
Example:
Source word: rain
Target words: pain, raining, in
pain is the preferred word here since the character ‘p’ is substituted for ‘r’ and no insertions or deletions take place. The other words (in, raining) are gotten through a rain of deletions and insertions (me don’t like this!).
Now, you’ve gotten my gist (or my dilemma), try to come up with an algorithm that’ll accept as input a source word and a list of target words and output what word I prefer the most. By knowing what target word is “closest” to the source word given, I can determine which youth speaks the better English and finally find my way to the mall!
The solution to the problem is here. But before you go there try to solve it on your own. A hint – this problem is very similar to another distance metric problem.