Monday, September 12, 2011

Eight queen problem in one line of Python

Write a one line python code (excluding any imports) that prints all the solutions of the 8-queen problem.

Friday, January 15, 2010

AS3: Remove fractional part in Number

Give a one line expression to remove the fraction part from a Number variable in AS3. Hint: should work for positive as well as negative numbers, and more than 32-bit numbers.

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'}

Friday, September 12, 2008

Typed container in AS3

In ActionScript3, how can you define and enforce type for an
Array. Something like the following:
var a:MyArray = new MyArray(NetConnection, 2);
a[0] = new NetConnection(); // should work
a[1] = "some string"; // should fail with exception.

Variable constructor arguments in AS3

The new Flex SDK (for Flash Player 10) allows two arguments in
the constructor of NetStream although second is optional,
but the old SDK (for Flash Player 9) allows only one. The following
piece of code tries to allocate NetStream, but cannot be compiled
in old SDK. How would you change it so that it can compile and work
in both old and new SDK?

if (flashMajorVersion >= 10)
ns = new NetStream(connection, id);
else
ns = new NetStream(connection);

Arithmetic in AS3

What is wrong in the following ActionScript3 code. It tries to
keep a (bit) vector for high-order and low-order bits of a two-byte
number. Assume somearray is an array of two-byte numbers.

var a:Array = new Array(256), b:Array = new Array(256);
for each (var n:uint in somearray) {
a[n/256] = 1;
b[n%256] = 1;
}

a++ for int vs short in C

Using a particular C compiler, the expression 'printf("%d\n", (a++ -
--a));' printed '0' if 'a' was int, and '1' if 'a' was short. Why?

Binary representation in Python

Give a one line expression to convert a number to its binary
representation in Python. For example, 258 to "100000010".

C++ vs Java

Give a numeric expression that is valid in both C++ and Java, but
evaluates to different values.

Parse dotted IP string in AS3

Give a one line expression in ActionScript 3 to convert a dotted IPv4
address (String) to its numeric (uint) representation. For example,
"10.0.0.1" to 0xa000001.

C vs Python

Give a code snippet that is valid in both C and Python, but produces
different results.