Interesting code snippets

(C) Fibonacci One-liner

#include <stdio.h>
 
int main(void)
{
    long x = 1, y = 1;
    do { printf("%d\\n%d\\n", x, y); } while ( (x += y) && (y += x) );
}
#include <stdio.h>
 
int main(void)
{
    for ( unsigned int x = 1, y = 1 ; (x += y) && (y < x) && (y += x) && (x < y) ; printf("%d\\n%d\\n", x, y) ) continue;
}

(Python) String Generator

def string_generator(chars, length):
    positions = [0] * (length + 1)
    while positions[0] == 0:
        yield ''.join(chars[x] for x in positions[1:])
 
        pos = length
        positions[pos] = (positions[pos] + 1) % len(chars)
        while positions[pos] == 0 and pos > 0:
            pos -= 1
            positions[pos] = (positions[pos] + 1) % len(chars)
interesting_code_snippets/root.txt · Last modified: 2009/09/20 20:15 by alan
Recent changes RSS feed Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki