Vijay Krishna's Notes http://vijaykrishna.posterous.com Most of my notes as a student of computer software and everything around it. posterous.com Sat, 15 Jan 2011 18:21:30 -0800 Haiku http://vijaykrishna.posterous.com/haiku http://vijaykrishna.posterous.com/haiku No, this is not the operating system. Sorry to disappoint. This is actually an interesting question i came across a few days back. The problem is actually quite simple: code generate as many Haikus as the user wants. Now, why this got me interested is because i used to write a couple of Haikus myself. And i was decent at it as well (it was enough to get a couple of them published in my college magazine). So i sat about thinking what the possible methods of generating Japanese 3 line poetry with a 5-7-5 syllable rule via code be possible. Here is one idea that bounced my mind today and i would like to share it. Haiku, for those who do not know is as I mentioned earlier, Japanese 3 line poetry with a simple rule: the 1st and the 3rd lines are to have exactly 5 syllables (like that in English), and the 2nd line is to have 7 syllables. Simple!! But not so simple to implement it, or so i thought. I went around trying to break the problem down and realized that is was all about realizing, or making the code realize what a syllable is in a given word. Once you do that there is very little left to it. (Or so i think.) So to tackle that little issue i went about finding out the definition of what a syllable is. And here is what i found:
a combination or set of one or more units of sound in a language that must consist of a sonorous element (a sonant or vowel) and may or may not contain less sonorous elements (consonants or semivowels) flanking it on either or both sides: for example ``paper'' has two syllables. -http://www.thefreedictionary.com/syllables "Some consonants can be pronounced alone (mmm, zzz), and may or may not be regarded as syllables, but they normally accompany vowels, which tend to occupy the central position in a syllable (the syllabic position), as in pap, pep, pip, pop, pup. Consonants occupy the margins of the syllable, as with p in the examples just given. A vowel in the syllable margin is often referred to as a glide, as in ebb and baySyllabic consonants occur in the second syllables of words like middle or midden, replacing a sequence of schwa plus consonant . . .." - (Gerald Knowles and Tom McArthur, The Oxford Companion to the English Language, edited by Tom McArthur. Oxford Univ. Press, 1992). Reference http://grammar.about.com/od/rs/g/syllableterm.htm
So come to think of it, even this is not all that crazy as i thought it would be. If one can find enough rules to satisfy the basic criteria of finding the Number of Syllables i think the goal is achieved. Because the idea is not to break up the words into syllables. The idea is to count the number of syllables so that it meets the 5-7-5 criteria of the poem. What i mean is this: in the word combine there are two syllables. Now how does it matter if the two of them are com & bine and comb & ine. The final result is two in either case. So, now that this seems tangible, just pick up a set of words, work around a few combinations in code meet the 5-7-5 criteria and viola!!! You have a haiku. Who says that it has to make sense? ;) But there is a simple work around for that as well. Atleast the idea in my head can get more understandable haikus than the ones you would otherwise get with raw code driven combinations. The idea is to take a known, grammatically or even poetically correct source of text. Mix up the sentences if you like. If you are worried about plagiarism use the content of your blog or something. Just any trusted source of text which meet the above two criteria. Now, work up an algorithm to pick up fragments of sentences from this source which meets the 5-7-5 criteria. And, if your code is decent, you will get more than one human understandable haiku in one go. So there you have it. Go along now, make those words rhyme. ;) And when you do, give me a call, because i am too tired to try it out now. May be another day. Till then, good night.

Permalink | Leave a comment  »

]]>
http://files.posterous.com/user_profile_pics/1369599/pic.jpeg http://posterous.com/users/hcGXxsTkwP6SS Vijay Krishna Palepu vpalepu Vijay Krishna Palepu
Sat, 08 Jan 2011 18:01:21 -0800 Name and Address http://vijaykrishna.posterous.com/name-and-address http://vijaykrishna.posterous.com/name-and-address As a south indian, with my surname coming before my own name, unconventionally, i have always had all the issues in the world trying to fill forms right from my child hood days. My name would figure in ten different manners at ten different places. And quite frankly it is a matter of great importance to any sapient being, his identity i mean. Another such issue is that of ones address. Now, even here i have had a lot of explaining to do every time i had to change SIM cards for Mobile phones, or while getting official documents processed. So, i thought with the on set of the new age information technology, and everything having an eVersion, forms (especially) official ones will not be far behind and they might just resolve some of these issues. But, as luck (or poor efforts) would have it, it has not. Every now and again i still hear people complaining about the issue of mismatched or wrong addresses especially because there is a great diversity in the manner how a Human Being identifies himself. Now, let me clarify, while the instruments of identity are virtually the same across the globe, i.e his Name and the Place where he lives. The issue comes in the manner in which these two entities are conveyed. The diversity comes here. And this is where i feel that as responsible software developers we must ensure that the customer must be given the freedom and flexibility to express and represent his name and address the way he deems fit, not us. And do not tell me that it is not possible. The idea is to identify the atomic elements in both these things. To find the basic building blocks of a Name and an Address. Here are some that I have identified: Name: Initials, First Name, Middle Name, Last/Surname, Maiden Name, Family Name, Village Name. Address: C/o, Line1, Line2, Street, Village, Town, District, City, Province, State, Pin/Postal/Zip Code, Country, Geo Codes. Hopefully these elements cover most of the name and places in the world. Now, it might appear to you that i have stretched the limit beyond a point with the Village property in the two classes. But keep in mind that these are for the whole set of names and addresses one can think of in any context. Point is that while designing a system it is a good practice to keep such generic templates and classes of information and use the properties selectively. Like there are few properties such as First Name, Surname and Line1 or Postal Code (in Address), that you just need to have in any situation. Baring that, most other fields can be used selectively as the situation demands. For instance, if you know that your clients are in Urban areas then you can drop the Town and Village fields from your address. Similarly, if you know that your client is in say, in Italy, then you can drop the State field. So that is the point, keep you design in a manner that can fit into any situation and place. Parsers: This something that people are trying to break their heads with. Most people are trying to parse Names and Addresses into the above mentioned fields, which mind you is a very difficult task. Now I would not try and talk about the address parser, it happens to be my firm's fundamental hiring question. But then i am willing to talk about the semantics of the Name Parser, another similar problem, but more complex in nature due to the greater diversity of the sources and the way it can be mentioned. The first problem is to realize that, while most people will, not all people will write their names in the format given above, and you need to accept it. What you can do is to accept the input in a string and then parse it and show the results to the user. If anything is wrong then he will correct it and you can continue any way. Google does this in the Gmail contacts. Point is that you should always give the user the choice of representing things the way he wants to. It will be a good idea to create a feedback system here, where in the corrections can tell the system of the kind of surnames people have. That is another issue, there is no point in storing all the names in the world. Because the number of surnames are lesser in number than the number of First or middle names, it is a good idea to store them. The same can be done for the Initials, Family Name and Village name. The maiden name in most cases is a previous surname and can be recognized with the same DB. Another good idea is to identify a full stop in your string. If the full stop has come after a short burst of characters right after a space or at the beginning of the name then it is an initial. But beware, there is a pitfall here. People write their surnames in shorthand as well at times, eg: Kumar can be written as Kr. The examples are endless. One needs to identify such challenges for such problems. And in my opinion it is not all that a difficult thing to do once you have done it. I am currently working on one such parser and once the code is ready and presentable, maybe i will publish it here. Until then this is a good place to use your algorithmic skills.

Permalink | Leave a comment  »

]]>
http://files.posterous.com/user_profile_pics/1369599/pic.jpeg http://posterous.com/users/hcGXxsTkwP6SS Vijay Krishna Palepu vpalepu Vijay Krishna Palepu
Tue, 04 Jan 2011 16:31:05 -0800 My First TopCoder Problem http://vijaykrishna.posterous.com/my-first-topcoder-problem http://vijaykrishna.posterous.com/my-first-topcoder-problem Well, i was looking into some old and favorite codes and algorithms i developed while in college. That is when nostalgia hit me a little when i got reminded of my first Top Coder problem. It was a witty problem, which hardly required any coding skills, but expected you to be a real thinker. So here it is - used in the Single Round Match 449 Round 1 - Division II, Level One, The Mountain Road Problem:
Media_httpwwwtopcoder_aekkh
Problem Summary: This picture represents a probable path your friend took while he was vacationing in the mountains. Now the mountains are represented as right angled isosceles triangles, with their hypotenuse lying on the ground. The entire trail or the path taken by your friend is guaranteed to be continuously along the slopes of the mountains. Thus, e.g. in the given picture, your friend took the path shown in bold lines. Given the start and end points of all the mountains (the mountain co-ordinates, assuming the base as the x-axis starting from 0), what is the total distance traveled by your friend. For instance, the mountain co-ordinates in the given figure is: {{0,5} , {3,9} , {4,6}} I will leave the answers for you to figure it. Trust me it is simpler than rocket science. Hint: Try using high school math, you should get it.

Permalink | Leave a comment  »

]]>
http://files.posterous.com/user_profile_pics/1369599/pic.jpeg http://posterous.com/users/hcGXxsTkwP6SS Vijay Krishna Palepu vpalepu Vijay Krishna Palepu
Sun, 02 Jan 2011 17:11:44 -0800 Combinations http://vijaykrishna.posterous.com/combinations http://vijaykrishna.posterous.com/combinations I like things to be simple. And i like it better when i have simple alternatives to complicated solutions. One such problem was that of finding Combinations or make all possible selections from a given set of things. I had this as a part of my course in my second year of engineering. Most of us solved it using a weird recursive approach, which has haunted me till date. Never quite got the complete grasp of it. Well, that is because i found a simpler, sweeter and much more elegant way of finding C(n,n). Check this out: If you ever observe the bit patterns of binary numbers you will notice a pattern. You will realize that the position of the 1's in the bit form of a number sequence actually indicates which thing has been selected. Assume that there are 3 objects, of which all combinations have to be derived. Step 1: Write down all the numbers in sequence in the binary format, starting from 000 to 111 (since there are 3 objects, if 4 objects were to be selected write down the sequence from 0000 to 1111).

0 0 0

0 0 1

0 1 0

0 1 1

1 0 0

1 0 1

1 1 0

1 1 1

Step 2: Since there are 3 columns, mark each of them with on object. Eg: if the objects were A, B and C: the demarcation would look something like this:

A B C

0 0 0

0 0 1

0 1 0

0 1 1

1 0 0

1 0 1

1 1 0

1 1 1

Step 3: Next to each binary number, for every 1 (one) in the number, write the name of the column (the object A, B or C). For every zero do nothing.

A B C

0 0 0 - {}

0 0 1 - {C}

0 1 0 - {B}

0 1 1 - {B,C}

1 0 0 - {A}

1 0 1 - {A,C}

1 1 0 - {A,B}

1 1 1 - {A,B,C}

Step 4: The selections or the combinations (given the curly braces) according to the positions of the 1's in the bit pattern from 000 to 111, happen to be nothing but all the combinations for the objects A, B and C. The first selection is just the null set, i.e. nothing is selected.

{{} , {C}, {B}, {B,C}, {A}, {A,C}, {A,B}, {A,B,C}}

There you are! A simple and elegant method of finding combinations. Now after i found this method i never had the slightest interest to get into the intricacies of a complex recursive approach. And the best part is that all this could be implemented with relative ease using simple bit operations, if statements and for loops.

This was also the real reason, as to why my data structures and algorithms professor found it so difficult to teach me recursion, even she did not see the point of teaching it after i told her about this approach. :D

Permalink | Leave a comment  »

]]>
http://files.posterous.com/user_profile_pics/1369599/pic.jpeg http://posterous.com/users/hcGXxsTkwP6SS Vijay Krishna Palepu vpalepu Vijay Krishna Palepu