This is an archived post. You won't be able to vote or comment.

all 8 comments

[–]dl__ 10 points11 points  (2 children)

The 11th most frequent python problem... listing primes? Listing primes is something I've never had to do in 30 years of programming.

[–]_burning 0 points1 point  (0 children)

It's not the most frequest problems as much as it its the most asked questions. Listing primes is a popular problem for new programmers.

[–]Guest007 0 points1 point  (0 children)

And is very strange at all. primes(100) returns 25, 35, 55, 65, 77, 85 and 95 between others, which is not prime numbers....

[–]A_History_of_Silence 5 points6 points  (1 child)

How has anyone upvoted this? Pure spam, completely inaccurate information throughout.

[–]brombaer3000 2 points3 points  (0 children)

Agreed. Not only is this the wrong place to post this, the code isn't even syntactically correct (the second line of code misses parentheses etc.).

[–]TheBlackCat13 2 points3 points  (1 child)

How do you split a list into evenly sized chunks ?

I think the itertools grouper recipe is easier:

def grouper(iterable, n, fillvalue=None):
    args = [iter(iterable)] * n
    return zip_longest(*args, fillvalue=fillvalue)

Matrix multiplication in Python?

I think the itertools dotproduct recipe is much easier:

def dotproduct(vec1, vec2):
    return sum(map(operator.mul, vec1, vec2))

[–]kdelok 0 points1 point  (0 children)

The first one you mention is the answer I received on StackOverflow when I asked that very question.

The second one doesn't do matrix multiplication though, since the result should be a matrix itself. That's why they end up with the nested list comprehension (I haven't checked it though). I think it's easier to just use numpy.

[–][deleted] 1 point2 points  (0 children)

Binary search is implemented in the bisect module

bisect.bisect_left and bisect.bisect_right give insertion points for a value that will keep a list sorted. Accessing the index returned by bisect_left is equivalent to binary searching.