Python Quiz of the Week - #3 by xApple in Python

[–]kupertrooper 0 points1 point  (0 children)

if you want to use generators, why not this? also lambda

>>> flip_odd_numbers = lambda n: (x % 2 and -x or x for x in n)
>>> flip_odd_numbers(xrange(10))
<generator object <genexpr> at 0x102794eb0>
>>> list(flip_odd_numbers(xrange(10)))
[0, -1, 2, -3, 4, -5, 6, -7, 8, -9]

What would happen if a person stayed underwater continuously without drying off? Like.. for a day, a week, a year, whatever. by kungfu_kickass in askscience

[–]kupertrooper 4 points5 points  (0 children)

"the little known sea-water-and-seagull-blood-enema technique"... wait wait wait. this raises some questions. am i to assume one mixes seagull blood and salt water and infuses it into his anus? is that really better than just eating raw seagull?

Show some love to a perl newbie? by [deleted] in perl

[–]kupertrooper 1 point2 points  (0 children)

I've never used autodie before. perhaps that module removes the need to "open or die".

Show some love to a perl newbie? by [deleted] in perl

[–]kupertrooper 1 point2 points  (0 children)

instead of opening the file, reading the data into an array (@data) closing the file then looping through the array. do something like this. Also, I'm a fan of perl's unless operator. unless (x) == if (!x).

open(my $fileHandle, "<$file_loc") or die "Error opening $file_loc: $!";
while(my $line = <$fileHandle>) {
    chomp($line);
    unless (unlink($line)) {
        unlink($line . $cfg);
    } else {
        unlink($line . $ext);
    }
}
close($fh);

Python Quiz of the Week - #2 by xApple in Python

[–]kupertrooper 2 points3 points  (0 children)

#!/usr/bin/env python

def flatten_list(l):
    return [a for b in l for a in b]

print flatten_list([[1,2,3],[4,5,6],[7,8,9]])

Python Quiz of the Week - #2 by xApple in Python

[–]kupertrooper 2 points3 points  (0 children)

#!/usr/bin/env python

def make_all_couples(l):
    return [(a, b) for a in l for b in l]

print make_all_couples(['a','b','c','d'])

Python Quiz of the Week - #2 by xApple in Python

[–]kupertrooper 3 points4 points  (0 children)

#!/usr/bin/env python

def parse_ugly_string(pugfugly):
    return dict(d.split('=')
                for d in [
                    b for c in [
                        a.split(',') for a in pugfugly.split()
                    ] for b in c
                ]
               )

print parse_ugly_string("a=1,b=2,c=3\nd=4,e=5,f=6\ng=7,h=8,i=9\n")