mirror of
https://github.com/TheAlgorithms/Python.git
synced 2025-07-19 19:03:02 +08:00
Added doctest and more explanation about Dijkstra execution. (#1014)
* Added doctest and more explanation about Dijkstra execution. * tests were not passing with python2 due to missing __init__.py file at number_theory folder * Removed the dot at the beginning of the imported modules names because 'python3 -m doctest -v data_structures/hashing/*.py' and 'python3 -m doctest -v data_structures/stacks/*.py' were failing not finding hash_table.py and stack.py modules. * Moved global code to main scope and added doctest for project euler problems 1 to 14. * Added test case for negative input. * Changed N variable to do not use end of line scape because in case there is a space after it the script will break making it much more error prone. * Added problems description and doctests to the ones that were missing. Limited line length to 79 and executed python black over all scripts. * Changed the way files are loaded to support pytest call. * Added __init__.py to problems to make them modules and allow pytest execution. * Added project_euler folder to test units execution * Changed 'os.path.split(os.path.realpath(__file__))' to 'os.path.dirname()'
This commit is contained in:

committed by
cclauss

parent
2fb3beeaf1
commit
267b5eff40
0
project_euler/problem_07/__init__.py
Normal file
0
project_euler/problem_07/__init__.py
Normal file
@ -1,30 +1,61 @@
|
||||
'''
|
||||
# -*- coding: utf-8 -*-
|
||||
"""
|
||||
By listing the first six prime numbers:
|
||||
2, 3, 5, 7, 11, and 13, we can see that the 6th prime is 13.
|
||||
What is the Nth prime number?
|
||||
'''
|
||||
|
||||
2, 3, 5, 7, 11, and 13
|
||||
|
||||
We can see that the 6th prime is 13. What is the Nth prime number?
|
||||
"""
|
||||
from __future__ import print_function
|
||||
from math import sqrt
|
||||
|
||||
try:
|
||||
raw_input # Python 2
|
||||
except NameError:
|
||||
raw_input = input # Python 3
|
||||
|
||||
|
||||
def isprime(n):
|
||||
if (n==2):
|
||||
if n == 2:
|
||||
return True
|
||||
elif (n%2==0):
|
||||
elif n % 2 == 0:
|
||||
return False
|
||||
else:
|
||||
sq = int(sqrt(n))+1
|
||||
for i in range(3,sq,2):
|
||||
if(n%i==0):
|
||||
sq = int(sqrt(n)) + 1
|
||||
for i in range(3, sq, 2):
|
||||
if n % i == 0:
|
||||
return False
|
||||
return True
|
||||
n = int(input())
|
||||
i=0
|
||||
j=1
|
||||
while(i!=n and j<3):
|
||||
j+=1
|
||||
if (isprime(j)):
|
||||
i+=1
|
||||
while(i!=n):
|
||||
j+=2
|
||||
if(isprime(j)):
|
||||
i+=1
|
||||
print(j)
|
||||
|
||||
|
||||
def solution(n):
|
||||
"""Returns the n-th prime number.
|
||||
|
||||
>>> solution(6)
|
||||
13
|
||||
>>> solution(1)
|
||||
2
|
||||
>>> solution(3)
|
||||
5
|
||||
>>> solution(20)
|
||||
71
|
||||
>>> solution(50)
|
||||
229
|
||||
>>> solution(100)
|
||||
541
|
||||
"""
|
||||
i = 0
|
||||
j = 1
|
||||
while i != n and j < 3:
|
||||
j += 1
|
||||
if isprime(j):
|
||||
i += 1
|
||||
while i != n:
|
||||
j += 2
|
||||
if isprime(j):
|
||||
i += 1
|
||||
return j
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
print(solution(int(raw_input().strip())))
|
||||
|
@ -1,16 +1,53 @@
|
||||
# By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, we can see that the 6th prime is 13. What is the Nth prime number?
|
||||
# -*- coding: utf-8 -*-
|
||||
"""
|
||||
By listing the first six prime numbers:
|
||||
|
||||
2, 3, 5, 7, 11, and 13
|
||||
|
||||
We can see that the 6th prime is 13. What is the Nth prime number?
|
||||
"""
|
||||
from __future__ import print_function
|
||||
from math import sqrt
|
||||
|
||||
try:
|
||||
raw_input # Python 2
|
||||
except NameError:
|
||||
raw_input = input # Python 3
|
||||
|
||||
|
||||
def isprime(number):
|
||||
for i in range(2,int(number**0.5)+1):
|
||||
if number%i==0:
|
||||
return False
|
||||
return True
|
||||
n = int(input('Enter The N\'th Prime Number You Want To Get: ')) # Ask For The N'th Prime Number Wanted
|
||||
primes = []
|
||||
num = 2
|
||||
while len(primes) < n:
|
||||
if isprime(num):
|
||||
primes.append(num)
|
||||
num += 1
|
||||
else:
|
||||
num += 1
|
||||
print(primes[len(primes) - 1])
|
||||
for i in range(2, int(number ** 0.5) + 1):
|
||||
if number % i == 0:
|
||||
return False
|
||||
return True
|
||||
|
||||
|
||||
def solution(n):
|
||||
"""Returns the n-th prime number.
|
||||
|
||||
>>> solution(6)
|
||||
13
|
||||
>>> solution(1)
|
||||
2
|
||||
>>> solution(3)
|
||||
5
|
||||
>>> solution(20)
|
||||
71
|
||||
>>> solution(50)
|
||||
229
|
||||
>>> solution(100)
|
||||
541
|
||||
"""
|
||||
primes = []
|
||||
num = 2
|
||||
while len(primes) < n:
|
||||
if isprime(num):
|
||||
primes.append(num)
|
||||
num += 1
|
||||
else:
|
||||
num += 1
|
||||
return primes[len(primes) - 1]
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
print(solution(int(raw_input().strip())))
|
||||
|
@ -1,28 +1,53 @@
|
||||
'''
|
||||
# -*- coding: utf-8 -*-
|
||||
"""
|
||||
By listing the first six prime numbers:
|
||||
2, 3, 5, 7, 11, and 13, we can see that the 6th prime is 13.
|
||||
What is the Nth prime number?
|
||||
'''
|
||||
|
||||
2, 3, 5, 7, 11, and 13
|
||||
|
||||
We can see that the 6th prime is 13. What is the Nth prime number?
|
||||
"""
|
||||
from __future__ import print_function
|
||||
# from Python.Math import PrimeCheck
|
||||
import math
|
||||
import itertools
|
||||
|
||||
try:
|
||||
raw_input # Python 2
|
||||
except NameError:
|
||||
raw_input = input # Python 3
|
||||
|
||||
|
||||
def primeCheck(number):
|
||||
if number % 2 == 0 and number > 2:
|
||||
return False
|
||||
return all(number % i for i in range(3, int(math.sqrt(number)) + 1, 2))
|
||||
|
||||
|
||||
def prime_generator():
|
||||
num = 2
|
||||
while True:
|
||||
if primeCheck(num):
|
||||
yield num
|
||||
num+=1
|
||||
|
||||
def main():
|
||||
n = int(input('Enter The N\'th Prime Number You Want To Get: ')) # Ask For The N'th Prime Number Wanted
|
||||
print(next(itertools.islice(prime_generator(),n-1,n)))
|
||||
num += 1
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
def solution(n):
|
||||
"""Returns the n-th prime number.
|
||||
|
||||
>>> solution(6)
|
||||
13
|
||||
>>> solution(1)
|
||||
2
|
||||
>>> solution(3)
|
||||
5
|
||||
>>> solution(20)
|
||||
71
|
||||
>>> solution(50)
|
||||
229
|
||||
>>> solution(100)
|
||||
541
|
||||
"""
|
||||
return next(itertools.islice(prime_generator(), n - 1, n))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
print(solution(int(raw_input().strip())))
|
||||
|
Reference in New Issue
Block a user