1
vote
1answer
33 views

Any way to optimize or improve this python combinations/subsets functions?

I believe this is essentially the same method as the itertools.combinations function, but is there any way to make this code more more perfect in terms of speed, code size and readability : def ...
0
votes
1answer
33 views

Generalize subsets function for any order subset, concisest way possible? [closed]

Implementing a toy Apriori algorithm for a small-data association rule mine, I have a need for a function to return all subsets. The length of the subsets is given by parameter i. I need to ...
2
votes
2answers
102 views

How to make this python function and its inverse more beautiful and symmetric?

I like the radix function and found a really neat way to compute it's inverse in python with a lambda and reduce (so I really feel like I'm learning my cool functional programming stuff) : def ...
1
vote
3answers
70 views

How to improve this functional python fast exponentiation routine?

I have the following python functions for exponentiation by squaring : def rep_square(b,exp): return reduce(lambda sq,i: sq + [sq[-1]*sq[-1]],xrange(len(radix(exp,2))),[b]) def ...
1
vote
1answer
32 views

How to improve this functional python trial division routine?

The following functional program factors an integer by trial division. I am not interested in improving the efficiency (but not in decreasing it either), I am interested how it can be made better or ...
1
vote
2answers
115 views

Flatten dictionary in Python (functional style)

I'm trying to learn how to write functional code with Python and have found some tutorials online. Please note that I know Python is not a promoter for functional programming. I just want to try it ...
1
vote
1answer
55 views

Refactor Python code with for-loops and continue

I wrote a code that queries a data structure that contains various triples - 3-item tuples in the form subject-predicate-object. I wrote this code based on an exercise from Programming the Semantic ...
4
votes
2answers
79 views

Refactor when complex expression in multiple parts of list comprehension

In Python a have a nested for-loop, that populates list with elements: a = [] for i in range(1, limit+1): for j in range(1, limit+1): p = i + j + (i**2 + j**2)**0.5 if p <= ...
0
votes
2answers
236 views

Functional prime factor generator

I have this prime factor generator for some number n. It returns list of all prime factors. In other words, product of the list equals n. def prime_factors_generate(n): a = [] prime_list = ...
1
vote
2answers
106 views

Safe composition in python

def json_response(response): assert response.code == 200, 'bad http response' return json.loads(response.body) def custom_json_response(response): response = json_response(response) ...
10
votes
2answers
421 views

Replacing Python Classes with Modules

I try to avoid using classes in Python as much as possible; if I don't plan on building on it, I don't build it in the first place. It helps me avoid Java-like classes like FileDownloader(), when I ...
4
votes
1answer
161 views

How to dynamically create Python unit tests in a readable way

I have django unittests that are pretty much the following format: class Tests(unittest.TestCase): def check(self, i, j): self.assertNotEquals(0, i-j) for i in xrange(1, 4): for j in ...
15
votes
4answers
652 views

Functional Python

Python newbie here. Trying to port this little F# snippet while staying pythonic: ["something"; "something else"; "blah"; "a string"] |> List.map (fun p -> p, p.Length) |> List.sortBy snd ...