Sunday, September 26, 2004
FTP* stuff
When i was preparing for aptitude tests, as a fresher (I even got thru Infy puzzle round as a fresher. Didnt get thru the next round. But now i thank heavens i'm not in Infy) i realized, i enjoyed those brainteasers. Few days later i gave my IQ test on one of those IQ test sites. After that i take IQ tests regularly just to check if my IQ has improved. I usually do miserably in these tests. I'v got scores as low as 80 and as high as 150 - its never consistent. but my IQ i believe is somewhere between 124-126.
Advantages of taking IQ test:
1. Improves reasoning/analytical skills.
2. Improves lateral thinking.
3. IQ can increase by, upto 10, by regularly taking IQ tests (but that never happened to me).
4. Improves problem solving skills and helps in coming up with innovative solutions to problems in life/work.
5. It is good time pass and can be even addictive.
6. Good results in IQ tests makes you more confident.
Anyways, a lot of IQ test can be found here including this. The latter has a link to sample test from the famous Mensa society,which has Sharon Stone** as its Member.
* FTP stands for Full Time Pass (Full to be pronounced in kannda accent)
** Yes, its the same Basic Instinct babe. She's got more brains than most of her male fans, including me!. I was shocked when i heard,with an IQ of 154 she's among the top 2% in the world. She is a Nobel prize material........ and PlayBoy material, Oscar nominee. She could have been anything she wanted to be a - rocket scientist, a techie anything. I wonder why she was gifted with such a genius mind and exquisite beauty - she cant make use of both at a time can she?. But then life is not always fair.
Advantages of taking IQ test:
1. Improves reasoning/analytical skills.
2. Improves lateral thinking.
3. IQ can increase by, upto 10, by regularly taking IQ tests (but that never happened to me).
4. Improves problem solving skills and helps in coming up with innovative solutions to problems in life/work.
5. It is good time pass and can be even addictive.
6. Good results in IQ tests makes you more confident.
Anyways, a lot of IQ test can be found here including this. The latter has a link to sample test from the famous Mensa society,which has Sharon Stone** as its Member.
* FTP stands for Full Time Pass (Full to be pronounced in kannda accent)
** Yes, its the same Basic Instinct babe. She's got more brains than most of her male fans, including me!. I was shocked when i heard,with an IQ of 154 she's among the top 2% in the world. She is a Nobel prize material........ and PlayBoy material, Oscar nominee. She could have been anything she wanted to be a - rocket scientist, a techie anything. I wonder why she was gifted with such a genius mind and exquisite beauty - she cant make use of both at a time can she?. But then life is not always fair.
Friday, September 17, 2004
Regular expression adventure
I never thought, something as trivial as date validation (fool proof) with regexp would turn out to be challenging (and fun-filled) task. here's how i went about it:
Aim: to validate date in mm/dd/yyyy format. leap years and 30/31 days months have to be validated with a single regular expresion.
1. \d{2}/\d{2}/\d{4} - matched 2 digits + / + 2 digits + / + 4 digits
No good.
2.
0?[1-9]|1[012] - matches 1,2, .... 9 or 01,02... 09 or 10,11,12 - mm lets call it A
0?[1-9]|[012][1-9]|3[01] - matches 01 or 1,02 or 2............. 31 - dd - B
\d{4} - matches yyyy - C
This was the my first regex: A/B/C
i.e: (0?[1-9]|1[012])/(0?[1-9]|[12]\d|3[01])/d[4]
Not good enough doesnt validate 30 days month,feb,lep year feb and a bug too....
B doesnt match the dates 10,20 this 0?[1-9]|[12]\d|3[01] is the correct exprn for dd
3. validating leap year? almost impossible!. i could give 30 days months a try....
0?[13578]|1[02] - matches 31 days month jan,mar.... dec - A
0?[469]|11 - matches 30 days month - B
0?2 - matches feb - C
0?[1-9]|[12]\d|3[01] - D, 0?[1-9]|[12]\d|30 - E, 0?[1-9]|[12]\d - F matches 1 (or 01) .... 31 ,1 (or 01) .... 30 and 1 (or 01) .... 29 respectively
combing all the pieces:
((A/D)|(B/E)|(C/F))/d{4}
i.e,
(((0?[13578]|1[02])/(0?[1-9]|[12]\d|3[01]))|((0?[469]|11)/(0?[1-9]|[12]\d|30))|(0?2/(0?[1-9]|[12]\d)))/\d{4}
Looks almost complete except it accepts 29th of feb in any given year.
4. Now the leap year matching doesnt look impossible. does it?
Logic: 1000's,100's are mutiples of 4 so ina 4 digit number if the last 2 digits are mutiple of 4 then
the number is a multiple of 4. between 00-99 there are 25 numbers that are multiple of 4 i.e
00,04,08,12,16,20...... 96
\d{2}(00|04|08|12|16|20.... 96) - matches leap years. now can you beat that?
i did - since i was in a pattern matching thinking pattern
Alternate logic: consider the last 2 digits of leap year
00,04,08
12,16
20,24,28
32,36
40,44,48 .....
see the pattern? every alternate row looks similar
[02468][048] matches even rows - 00,04,02,20,24,28,40,44,48,..... call it A
odd rows - [13579][26] - 12,26,32,36,52,56, ..... call it B
\d{2}(A|B)
i.e,
\d{2}([02468][048]|[13579][26]) - is regexp to match leap years
To match feb of any given year:
0?2/(0?[1-9]|1\d|2[0-8])/\d{4} - 0-28 of feb in any year
0?2/29/\d{2}([02468][048]|[13579][26]) - 29th of feb in leap years
5. 4 main pieces of regex:
1. 0-30 - in 30 days month, in any year
A: 0?[13578]|1[02]
B: 0?[1-9]|[12]\d|3[01]
2. 0-31 - in 31 days month, in any year
C: 0?[469]|11
D: 0?[1-9]|[12]\d|30
3.0-28 - of feb in any year
E: 0?2/(0?[1-9]|1\d|2[0-8])
4. 29th - of feb in leap year
F: 0?2/29/\d{2}([02468][048]|[13579][26])
combining these gives the complete regex,
((((A/B)|(C/D)|(E))/\d{4})|(F))
(((((0?[13578]|1[02])/(0?[1-9]|[12]\d|3[01]))|((0?[469]|11)/(0?[1-9]|[12]\d|30))|(0?2/(0?[1-9]|1\d|2[0-8])))/\d{4})|(0?2/29/\d{2}([02468][048]|[13579][26])))
Performance: There are arround 10 (i suspect much) sub expressions that will be capured. we need to use ?: to indicate that we are only interested in matching and not capturing
^(?:(?:(?:(?:(?:0?[13578]|1[02])/(?:0?[1-9]|[12]\d|3[01]))|(?:(?:0?[469]|11)/(?:0?[1-9]|[12]\d|30))|(?:0?2/(?:0?[1-9]|1\d|2[0-8])))/\d{4})|0?2/29/\d{2}(?:[02468][048]|[13579][26]))$
note:
1. ^ and $ in the beginning and end of inout stirng respectively. It is used to indicate the that date as a whole is the input for the pattern matching. it should not be part of another string or in should not have anything extra (not even spaces just mm/dd/yyyy)
2. Sub expression in anthing that cimes inside round brackets ["(" and ")"]. subexperssion are captured and retunred in a array along with the string that matches the complete regex.
3. yyyy/mm/dd is th emost regex friendly date format. Now, that a useless observation.
4. i wrote all this down because a week later i may not remember what that cryptic regex does and what those magic numbers are.
Aim: to validate date in mm/dd/yyyy format. leap years and 30/31 days months have to be validated with a single regular expresion.
1. \d{2}/\d{2}/\d{4} - matched 2 digits + / + 2 digits + / + 4 digits
No good.
2.
0?[1-9]|1[012] - matches 1,2, .... 9 or 01,02... 09 or 10,11,12 - mm lets call it A
0?[1-9]|[012][1-9]|3[01] - matches 01 or 1,02 or 2............. 31 - dd - B
\d{4} - matches yyyy - C
This was the my first regex: A/B/C
i.e: (0?[1-9]|1[012])/(0?[1-9]|[12]\d|3[01])/d[4]
Not good enough doesnt validate 30 days month,feb,lep year feb and a bug too....
B doesnt match the dates 10,20 this 0?[1-9]|[12]\d|3[01] is the correct exprn for dd
3. validating leap year? almost impossible!. i could give 30 days months a try....
0?[13578]|1[02] - matches 31 days month jan,mar.... dec - A
0?[469]|11 - matches 30 days month - B
0?2 - matches feb - C
0?[1-9]|[12]\d|3[01] - D, 0?[1-9]|[12]\d|30 - E, 0?[1-9]|[12]\d - F matches 1 (or 01) .... 31 ,1 (or 01) .... 30 and 1 (or 01) .... 29 respectively
combing all the pieces:
((A/D)|(B/E)|(C/F))/d{4}
i.e,
(((0?[13578]|1[02])/(0?[1-9]|[12]\d|3[01]))|((0?[469]|11)/(0?[1-9]|[12]\d|30))|(0?2/(0?[1-9]|[12]\d)))/\d{4}
Looks almost complete except it accepts 29th of feb in any given year.
4. Now the leap year matching doesnt look impossible. does it?
Logic: 1000's,100's are mutiples of 4 so ina 4 digit number if the last 2 digits are mutiple of 4 then
the number is a multiple of 4. between 00-99 there are 25 numbers that are multiple of 4 i.e
00,04,08,12,16,20...... 96
\d{2}(00|04|08|12|16|20.... 96) - matches leap years. now can you beat that?
i did - since i was in a pattern matching thinking pattern
Alternate logic: consider the last 2 digits of leap year
00,04,08
12,16
20,24,28
32,36
40,44,48 .....
see the pattern? every alternate row looks similar
[02468][048] matches even rows - 00,04,02,20,24,28,40,44,48,..... call it A
odd rows - [13579][26] - 12,26,32,36,52,56, ..... call it B
\d{2}(A|B)
i.e,
\d{2}([02468][048]|[13579][26]) - is regexp to match leap years
To match feb of any given year:
0?2/(0?[1-9]|1\d|2[0-8])/\d{4} - 0-28 of feb in any year
0?2/29/\d{2}([02468][048]|[13579][26]) - 29th of feb in leap years
5. 4 main pieces of regex:
1. 0-30 - in 30 days month, in any year
A: 0?[13578]|1[02]
B: 0?[1-9]|[12]\d|3[01]
2. 0-31 - in 31 days month, in any year
C: 0?[469]|11
D: 0?[1-9]|[12]\d|30
3.0-28 - of feb in any year
E: 0?2/(0?[1-9]|1\d|2[0-8])
4. 29th - of feb in leap year
F: 0?2/29/\d{2}([02468][048]|[13579][26])
combining these gives the complete regex,
((((A/B)|(C/D)|(E))/\d{4})|(F))
(((((0?[13578]|1[02])/(0?[1-9]|[12]\d|3[01]))|((0?[469]|11)/(0?[1-9]|[12]\d|30))|(0?2/(0?[1-9]|1\d|2[0-8])))/\d{4})|(0?2/29/\d{2}([02468][048]|[13579][26])))
Performance: There are arround 10 (i suspect much) sub expressions that will be capured. we need to use ?: to indicate that we are only interested in matching and not capturing
^(?:(?:(?:(?:(?:0?[13578]|1[02])/(?:0?[1-9]|[12]\d|3[01]))|(?:(?:0?[469]|11)/(?:0?[1-9]|[12]\d|30))|(?:0?2/(?:0?[1-9]|1\d|2[0-8])))/\d{4})|0?2/29/\d{2}(?:[02468][048]|[13579][26]))$
note:
1. ^ and $ in the beginning and end of inout stirng respectively. It is used to indicate the that date as a whole is the input for the pattern matching. it should not be part of another string or in should not have anything extra (not even spaces just mm/dd/yyyy)
2. Sub expression in anthing that cimes inside round brackets ["(" and ")"]. subexperssion are captured and retunred in a array along with the string that matches the complete regex.
3. yyyy/mm/dd is th emost regex friendly date format. Now, that a useless observation.
4. i wrote all this down because a week later i may not remember what that cryptic regex does and what those magic numbers are.


del.icio.us