From the CS olympiads:
binary in mult times 7 binary out ->
pythhon:(python code: int(raw_input(),base=2)*7

Recommended Answers

All 6 Replies

In my programs I usually only encounter 3 types of epic one liners, they all use the ternary conditional operator extensively.
(all in c++)

Type 1: Conditional arithmetic - When I want to change how an equation behaves based on conditions. Example taken directly from the add function of my integer library: (i<s?v[i]:0)+(i<o.s?o.v[i]:0)+(carry?1:0).

Type 2: Inline switch statements - Any switch statement can be made inline with the ternary operator.

switch (x)
{
    case A:doA();break;
    case B:doB();break;
    case C:doC();break;
    default:doD();break;
}

becomes:

(x==A?doA():(x==B?doB():(x==C?doC():doD())));

Type 3: Complicated bitshifty stuff, typically to create inline hash functions. I can't think of a good example off hand, but they end up looking really cool.

However, I believe epic one liners are generally considered bad practice. That is why I try to keep a comment above each one that describes what the one-liner does.

I normally like to keep my code spaced, clear, and easy to read. So I don't normally have any epic one liners in any programming languages.

On the web design side, I have very messy CSS with some epic one liners, just some very long stylings on one line.

The only useful one-liner I know is in Pascal/Delphi:

asm int 3; end;

Python baby ...

''' factorial_oneliner.py
get the factorial of integer n
'''

fact = lambda n:[1,0][n>1] or fact(n-1)*n

# test it ...
print(fact(53))

''' result ...
4274883284060025564298013753389399649690343788366813724672000000000000
'''

C ...
#define isleapyear(year) ((! (year % 4) && (year % 100)) || (! (year % 400) && (year % 1000)))

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.