Skip to content

improved prime numbers implementation #1606

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 16 commits into from
Dec 7, 2019
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion data_structures/data_structures/heap/heap_generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ def _swap(self, i, j):
"""Performs changes required for swapping two elements in the heap"""
# First update the indexes of the items in index map.
self.pos_map[self.arr[i][0]], self.pos_map[self.arr[j][0]] = (
self.pos_map[self.arr[j][0]], self.pos_map[self.arr[i][0]]
self.pos_map[self.arr[j][0]],
self.pos_map[self.arr[i][0]],
)
# Then swap the items in the list.
self.arr[i], self.arr[j] = self.arr[j], self.arr[i]
Expand Down
24 changes: 12 additions & 12 deletions maths/prime_numbers.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from typing import List
from typing import Generator, List


def primes(max: int) -> List[int]:
def primes(max: int) -> Generator[int, None, None]:
"""
Return a list of all primes numbers up to max.
>>> primes(10)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please change this to list(primes(10)) so this doctest passes.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

Expand All @@ -13,16 +13,16 @@ def primes(max: int) -> List[int]:
>>> primes(1_000_000)[-1]
Copy link
Member

@cclauss cclauss Dec 2, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please change this to list(primes(1_000_000))[-1] so this doctest passes.

Please add doctests for list(primes(0)), list(primes(-1)), list(primes(-10))

You can try these on your own computer with:
python3 -m doctest -v maths/prime_numbers.py

999983
"""
max += 1
numbers = [False] * max
ret = []
for i in range(2, max):
if not numbers[i]:
for j in range(i, max, i):
numbers[j] = True
ret.append(i)
return ret
numbers: Generator = (i for i in range(1, (max + 1)))
for i in filter(lambda x: x > 1, numbers):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for i in [x for x in numbers if x > 1]:

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

filter returns an iterator, better then a full list, i guess

Copy link
Member

@cclauss cclauss Dec 3, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for i in (x for x in numbers if x > 1):

for j in range(2, i):
if (i % j) == 0:
break
else:
yield i


if __name__ == "__main__":
print(primes(int(input("Calculate primes up to:\n>> "))))
number = int(input("Calculate primes up to:\n>> "))
Copy link
Member

@cclauss cclauss Dec 2, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As discussed in CONTRIBUTING.md, use str.strip() to gracefully deal with leading or trailing whitespace in user input.
number = int(input("Calculate primes up to:\n>> ").strip())

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

for ret in primes(number):
print(ret)