Tuesday, May 5, 2009

Python: extract URLs in web pages

Given a URL of a web page, extract all the URLs from that web page. Do this in one line. Assume that URLs in the web page are of the form <a href="...">...</a>. Bonus Question: also return the text in the <a>text</a> tag for the given URL.

Python: formatting big integers

Write a one line Python function to format a big integer into fixed sized byte representation such that 0x0102030405 gets converted to '\x00\x00\x00\x01\x02\x03\x04\x05'. You cannot use struct module as it is limited to 32 bit number. Such functions are useful when dealing with SHA1/MD5 or crypto/RSA routines.

Python: parsing big integers

Write a one line Python function to parse a integer from it's representation, e.g., '\x01\x02\x03\x04\x05' should result in an integer representing 0x0102030405.

Python: parsing of header:value

Give a one line statement in python to parse a HTTP or SIP header value of the form "Header1: Value1\r\nHeader2: Value2:\r\n" by returning a tuple (('Header1', 'Value1'), ('Header2', 'Value2'). Bonus question: what are the problem in returning a dict instead?

Python: convert a list to a dictionary

Give a one line Python code to convert a list such as L=['A','B','C'] to a dictionary indexed by the numeric index 0, 1, 2, e.g., {0: 'A', 1: 'B', 2: 'C'}