mirror of
https://github.com/TheAlgorithms/Python.git
synced 2024-11-23 21:11:08 +00:00
Travis CI: Add a flake8 test for unused imports (#1038)
This commit is contained in:
parent
46bcee0978
commit
3c8e9314b6
|
@ -6,7 +6,7 @@ before_install: pip install --upgrade pip setuptools
|
||||||
install: pip install -r requirements.txt
|
install: pip install -r requirements.txt
|
||||||
before_script:
|
before_script:
|
||||||
- black --check . || true
|
- black --check . || true
|
||||||
- flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics
|
- flake8 . --count --select=E9,F401,F63,F7,F82 --show-source --statistics
|
||||||
script:
|
script:
|
||||||
- mypy --ignore-missing-imports .
|
- mypy --ignore-missing-imports .
|
||||||
- pytest . --doctest-modules
|
- pytest . --doctest-modules
|
||||||
|
|
|
@ -16,10 +16,19 @@ while Q is non-empty:
|
||||||
|
|
||||||
"""
|
"""
|
||||||
|
|
||||||
import collections
|
G = {'A': ['B', 'C'],
|
||||||
|
'B': ['A', 'D', 'E'],
|
||||||
|
'C': ['A', 'F'],
|
||||||
|
'D': ['B'],
|
||||||
|
'E': ['B', 'F'],
|
||||||
|
'F': ['C', 'E']}
|
||||||
|
|
||||||
|
|
||||||
def bfs(graph, start):
|
def bfs(graph, start):
|
||||||
|
"""
|
||||||
|
>>> ''.join(sorted(bfs(G, 'A')))
|
||||||
|
'ABCDEF'
|
||||||
|
"""
|
||||||
explored, queue = set(), [start] # collections.deque([start])
|
explored, queue = set(), [start] # collections.deque([start])
|
||||||
explored.add(start)
|
explored.add(start)
|
||||||
while queue:
|
while queue:
|
||||||
|
@ -31,11 +40,5 @@ def bfs(graph, start):
|
||||||
return explored
|
return explored
|
||||||
|
|
||||||
|
|
||||||
G = {'A': ['B', 'C'],
|
if __name__ == '__main__':
|
||||||
'B': ['A', 'D', 'E'],
|
print(bfs(G, 'A'))
|
||||||
'C': ['A', 'F'],
|
|
||||||
'D': ['B'],
|
|
||||||
'E': ['B', 'F'],
|
|
||||||
'F': ['C', 'E']}
|
|
||||||
|
|
||||||
print(bfs(G, 'A'))
|
|
||||||
|
|
|
@ -6,8 +6,6 @@ Wikipedia reference: https://en.wikipedia.org/wiki/Volume
|
||||||
|
|
||||||
from math import pi
|
from math import pi
|
||||||
|
|
||||||
PI = pi
|
|
||||||
|
|
||||||
|
|
||||||
def vol_cube(side_length):
|
def vol_cube(side_length):
|
||||||
"""Calculate the Volume of a Cube."""
|
"""Calculate the Volume of a Cube."""
|
||||||
|
@ -39,9 +37,7 @@ def vol_right_circ_cone(radius, height):
|
||||||
volume = (1/3) * pi * radius^2 * height
|
volume = (1/3) * pi * radius^2 * height
|
||||||
"""
|
"""
|
||||||
|
|
||||||
import math
|
return (float(1) / 3) * pi * (radius ** 2) * height
|
||||||
|
|
||||||
return (float(1) / 3) * PI * (radius ** 2) * height
|
|
||||||
|
|
||||||
|
|
||||||
def vol_prism(area_of_base, height):
|
def vol_prism(area_of_base, height):
|
||||||
|
@ -71,7 +67,7 @@ def vol_sphere(radius):
|
||||||
V = (4/3) * pi * r^3
|
V = (4/3) * pi * r^3
|
||||||
Wikipedia reference: https://en.wikipedia.org/wiki/Sphere
|
Wikipedia reference: https://en.wikipedia.org/wiki/Sphere
|
||||||
"""
|
"""
|
||||||
return (float(4) / 3) * PI * radius ** 3
|
return (float(4) / 3) * pi * radius ** 3
|
||||||
|
|
||||||
|
|
||||||
def vol_circular_cylinder(radius, height):
|
def vol_circular_cylinder(radius, height):
|
||||||
|
@ -80,7 +76,7 @@ def vol_circular_cylinder(radius, height):
|
||||||
Wikipedia reference: https://en.wikipedia.org/wiki/Cylinder
|
Wikipedia reference: https://en.wikipedia.org/wiki/Cylinder
|
||||||
volume = pi * radius^2 * height
|
volume = pi * radius^2 * height
|
||||||
"""
|
"""
|
||||||
return PI * radius ** 2 * height
|
return pi * radius ** 2 * height
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
|
|
|
@ -16,7 +16,7 @@ primeFactorization(number)
|
||||||
greatestPrimeFactor(number)
|
greatestPrimeFactor(number)
|
||||||
smallestPrimeFactor(number)
|
smallestPrimeFactor(number)
|
||||||
getPrime(n)
|
getPrime(n)
|
||||||
getPrimesBetween(pNumber1, pNumber2)
|
getPrimesBetween(pNumber1, pNumber2)
|
||||||
|
|
||||||
----
|
----
|
||||||
|
|
||||||
|
@ -39,34 +39,36 @@ goldbach(number) // Goldbach's assumption
|
||||||
|
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
from math import sqrt
|
||||||
|
|
||||||
|
|
||||||
def isPrime(number):
|
def isPrime(number):
|
||||||
"""
|
"""
|
||||||
input: positive integer 'number'
|
input: positive integer 'number'
|
||||||
returns true if 'number' is prime otherwise false.
|
returns true if 'number' is prime otherwise false.
|
||||||
"""
|
"""
|
||||||
import math # for function sqrt
|
|
||||||
|
|
||||||
# precondition
|
# precondition
|
||||||
assert isinstance(number,int) and (number >= 0) , \
|
assert isinstance(number,int) and (number >= 0) , \
|
||||||
"'number' must been an int and positive"
|
"'number' must been an int and positive"
|
||||||
|
|
||||||
status = True
|
status = True
|
||||||
|
|
||||||
# 0 and 1 are none primes.
|
# 0 and 1 are none primes.
|
||||||
if number <= 1:
|
if number <= 1:
|
||||||
status = False
|
status = False
|
||||||
|
|
||||||
for divisor in range(2,int(round(math.sqrt(number)))+1):
|
for divisor in range(2,int(round(sqrt(number)))+1):
|
||||||
|
|
||||||
# if 'number' divisible by 'divisor' then sets 'status'
|
# if 'number' divisible by 'divisor' then sets 'status'
|
||||||
# of false and break up the loop.
|
# of false and break up the loop.
|
||||||
if number % divisor == 0:
|
if number % divisor == 0:
|
||||||
status = False
|
status = False
|
||||||
break
|
break
|
||||||
|
|
||||||
# precondition
|
# precondition
|
||||||
assert isinstance(status,bool), "'status' must been from type bool"
|
assert isinstance(status,bool), "'status' must been from type bool"
|
||||||
|
|
||||||
return status
|
return status
|
||||||
|
|
||||||
# ------------------------------------------
|
# ------------------------------------------
|
||||||
|
@ -75,37 +77,37 @@ def sieveEr(N):
|
||||||
"""
|
"""
|
||||||
input: positive integer 'N' > 2
|
input: positive integer 'N' > 2
|
||||||
returns a list of prime numbers from 2 up to N.
|
returns a list of prime numbers from 2 up to N.
|
||||||
|
|
||||||
This function implements the algorithm called
|
This function implements the algorithm called
|
||||||
sieve of erathostenes.
|
sieve of erathostenes.
|
||||||
|
|
||||||
"""
|
"""
|
||||||
|
|
||||||
# precondition
|
# precondition
|
||||||
assert isinstance(N,int) and (N > 2), "'N' must been an int and > 2"
|
assert isinstance(N,int) and (N > 2), "'N' must been an int and > 2"
|
||||||
|
|
||||||
# beginList: conatins all natural numbers from 2 upt to N
|
# beginList: conatins all natural numbers from 2 upt to N
|
||||||
beginList = [x for x in range(2,N+1)]
|
beginList = [x for x in range(2,N+1)]
|
||||||
|
|
||||||
ans = [] # this list will be returns.
|
ans = [] # this list will be returns.
|
||||||
|
|
||||||
# actual sieve of erathostenes
|
# actual sieve of erathostenes
|
||||||
for i in range(len(beginList)):
|
for i in range(len(beginList)):
|
||||||
|
|
||||||
for j in range(i+1,len(beginList)):
|
for j in range(i+1,len(beginList)):
|
||||||
|
|
||||||
if (beginList[i] != 0) and \
|
if (beginList[i] != 0) and \
|
||||||
(beginList[j] % beginList[i] == 0):
|
(beginList[j] % beginList[i] == 0):
|
||||||
beginList[j] = 0
|
beginList[j] = 0
|
||||||
|
|
||||||
# filters actual prime numbers.
|
# filters actual prime numbers.
|
||||||
ans = [x for x in beginList if x != 0]
|
ans = [x for x in beginList if x != 0]
|
||||||
|
|
||||||
# precondition
|
# precondition
|
||||||
assert isinstance(ans,list), "'ans' must been from type list"
|
assert isinstance(ans,list), "'ans' must been from type list"
|
||||||
|
|
||||||
return ans
|
return ans
|
||||||
|
|
||||||
|
|
||||||
# --------------------------------
|
# --------------------------------
|
||||||
|
|
||||||
|
@ -114,203 +116,201 @@ def getPrimeNumbers(N):
|
||||||
input: positive integer 'N' > 2
|
input: positive integer 'N' > 2
|
||||||
returns a list of prime numbers from 2 up to N (inclusive)
|
returns a list of prime numbers from 2 up to N (inclusive)
|
||||||
This function is more efficient as function 'sieveEr(...)'
|
This function is more efficient as function 'sieveEr(...)'
|
||||||
"""
|
"""
|
||||||
|
|
||||||
# precondition
|
# precondition
|
||||||
assert isinstance(N,int) and (N > 2), "'N' must been an int and > 2"
|
assert isinstance(N,int) and (N > 2), "'N' must been an int and > 2"
|
||||||
|
|
||||||
ans = []
|
ans = []
|
||||||
|
|
||||||
# iterates over all numbers between 2 up to N+1
|
# iterates over all numbers between 2 up to N+1
|
||||||
# if a number is prime then appends to list 'ans'
|
# if a number is prime then appends to list 'ans'
|
||||||
for number in range(2,N+1):
|
for number in range(2,N+1):
|
||||||
|
|
||||||
if isPrime(number):
|
if isPrime(number):
|
||||||
|
|
||||||
ans.append(number)
|
ans.append(number)
|
||||||
|
|
||||||
# precondition
|
# precondition
|
||||||
assert isinstance(ans,list), "'ans' must been from type list"
|
assert isinstance(ans,list), "'ans' must been from type list"
|
||||||
|
|
||||||
return ans
|
return ans
|
||||||
|
|
||||||
|
|
||||||
# -----------------------------------------
|
# -----------------------------------------
|
||||||
|
|
||||||
def primeFactorization(number):
|
def primeFactorization(number):
|
||||||
"""
|
"""
|
||||||
input: positive integer 'number'
|
input: positive integer 'number'
|
||||||
returns a list of the prime number factors of 'number'
|
returns a list of the prime number factors of 'number'
|
||||||
"""
|
"""
|
||||||
|
|
||||||
import math # for function sqrt
|
|
||||||
|
|
||||||
# precondition
|
# precondition
|
||||||
assert isinstance(number,int) and number >= 0, \
|
assert isinstance(number,int) and number >= 0, \
|
||||||
"'number' must been an int and >= 0"
|
"'number' must been an int and >= 0"
|
||||||
|
|
||||||
ans = [] # this list will be returns of the function.
|
ans = [] # this list will be returns of the function.
|
||||||
|
|
||||||
# potential prime number factors.
|
# potential prime number factors.
|
||||||
|
|
||||||
factor = 2
|
factor = 2
|
||||||
|
|
||||||
quotient = number
|
quotient = number
|
||||||
|
|
||||||
|
|
||||||
if number == 0 or number == 1:
|
if number == 0 or number == 1:
|
||||||
|
|
||||||
ans.append(number)
|
ans.append(number)
|
||||||
|
|
||||||
# if 'number' not prime then builds the prime factorization of 'number'
|
# if 'number' not prime then builds the prime factorization of 'number'
|
||||||
elif not isPrime(number):
|
elif not isPrime(number):
|
||||||
|
|
||||||
while (quotient != 1):
|
while (quotient != 1):
|
||||||
|
|
||||||
if isPrime(factor) and (quotient % factor == 0):
|
if isPrime(factor) and (quotient % factor == 0):
|
||||||
ans.append(factor)
|
ans.append(factor)
|
||||||
quotient /= factor
|
quotient /= factor
|
||||||
else:
|
else:
|
||||||
factor += 1
|
factor += 1
|
||||||
|
|
||||||
else:
|
else:
|
||||||
ans.append(number)
|
ans.append(number)
|
||||||
|
|
||||||
# precondition
|
# precondition
|
||||||
assert isinstance(ans,list), "'ans' must been from type list"
|
assert isinstance(ans,list), "'ans' must been from type list"
|
||||||
|
|
||||||
return ans
|
return ans
|
||||||
|
|
||||||
|
|
||||||
# -----------------------------------------
|
# -----------------------------------------
|
||||||
|
|
||||||
def greatestPrimeFactor(number):
|
def greatestPrimeFactor(number):
|
||||||
"""
|
"""
|
||||||
input: positive integer 'number' >= 0
|
input: positive integer 'number' >= 0
|
||||||
returns the greatest prime number factor of 'number'
|
returns the greatest prime number factor of 'number'
|
||||||
"""
|
"""
|
||||||
|
|
||||||
# precondition
|
# precondition
|
||||||
assert isinstance(number,int) and (number >= 0), \
|
assert isinstance(number,int) and (number >= 0), \
|
||||||
"'number' bust been an int and >= 0"
|
"'number' bust been an int and >= 0"
|
||||||
|
|
||||||
ans = 0
|
ans = 0
|
||||||
|
|
||||||
# prime factorization of 'number'
|
# prime factorization of 'number'
|
||||||
primeFactors = primeFactorization(number)
|
primeFactors = primeFactorization(number)
|
||||||
|
|
||||||
ans = max(primeFactors)
|
ans = max(primeFactors)
|
||||||
|
|
||||||
# precondition
|
# precondition
|
||||||
assert isinstance(ans,int), "'ans' must been from type int"
|
assert isinstance(ans,int), "'ans' must been from type int"
|
||||||
|
|
||||||
return ans
|
return ans
|
||||||
|
|
||||||
|
|
||||||
# ----------------------------------------------
|
# ----------------------------------------------
|
||||||
|
|
||||||
|
|
||||||
def smallestPrimeFactor(number):
|
def smallestPrimeFactor(number):
|
||||||
"""
|
"""
|
||||||
input: integer 'number' >= 0
|
input: integer 'number' >= 0
|
||||||
returns the smallest prime number factor of 'number'
|
returns the smallest prime number factor of 'number'
|
||||||
"""
|
"""
|
||||||
|
|
||||||
# precondition
|
# precondition
|
||||||
assert isinstance(number,int) and (number >= 0), \
|
assert isinstance(number,int) and (number >= 0), \
|
||||||
"'number' bust been an int and >= 0"
|
"'number' bust been an int and >= 0"
|
||||||
|
|
||||||
ans = 0
|
ans = 0
|
||||||
|
|
||||||
# prime factorization of 'number'
|
# prime factorization of 'number'
|
||||||
primeFactors = primeFactorization(number)
|
primeFactors = primeFactorization(number)
|
||||||
|
|
||||||
ans = min(primeFactors)
|
ans = min(primeFactors)
|
||||||
|
|
||||||
# precondition
|
# precondition
|
||||||
assert isinstance(ans,int), "'ans' must been from type int"
|
assert isinstance(ans,int), "'ans' must been from type int"
|
||||||
|
|
||||||
return ans
|
return ans
|
||||||
|
|
||||||
|
|
||||||
# ----------------------
|
# ----------------------
|
||||||
|
|
||||||
def isEven(number):
|
def isEven(number):
|
||||||
"""
|
"""
|
||||||
input: integer 'number'
|
input: integer 'number'
|
||||||
returns true if 'number' is even, otherwise false.
|
returns true if 'number' is even, otherwise false.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
# precondition
|
# precondition
|
||||||
assert isinstance(number, int), "'number' must been an int"
|
assert isinstance(number, int), "'number' must been an int"
|
||||||
assert isinstance(number % 2 == 0, bool), "compare bust been from type bool"
|
assert isinstance(number % 2 == 0, bool), "compare bust been from type bool"
|
||||||
|
|
||||||
return number % 2 == 0
|
return number % 2 == 0
|
||||||
|
|
||||||
# ------------------------
|
# ------------------------
|
||||||
|
|
||||||
def isOdd(number):
|
def isOdd(number):
|
||||||
"""
|
"""
|
||||||
input: integer 'number'
|
input: integer 'number'
|
||||||
returns true if 'number' is odd, otherwise false.
|
returns true if 'number' is odd, otherwise false.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
# precondition
|
# precondition
|
||||||
assert isinstance(number, int), "'number' must been an int"
|
assert isinstance(number, int), "'number' must been an int"
|
||||||
assert isinstance(number % 2 != 0, bool), "compare bust been from type bool"
|
assert isinstance(number % 2 != 0, bool), "compare bust been from type bool"
|
||||||
|
|
||||||
return number % 2 != 0
|
return number % 2 != 0
|
||||||
|
|
||||||
# ------------------------
|
# ------------------------
|
||||||
|
|
||||||
|
|
||||||
def goldbach(number):
|
def goldbach(number):
|
||||||
"""
|
"""
|
||||||
Goldbach's assumption
|
Goldbach's assumption
|
||||||
input: a even positive integer 'number' > 2
|
input: a even positive integer 'number' > 2
|
||||||
returns a list of two prime numbers whose sum is equal to 'number'
|
returns a list of two prime numbers whose sum is equal to 'number'
|
||||||
"""
|
"""
|
||||||
|
|
||||||
# precondition
|
# precondition
|
||||||
assert isinstance(number,int) and (number > 2) and isEven(number), \
|
assert isinstance(number,int) and (number > 2) and isEven(number), \
|
||||||
"'number' must been an int, even and > 2"
|
"'number' must been an int, even and > 2"
|
||||||
|
|
||||||
ans = [] # this list will returned
|
ans = [] # this list will returned
|
||||||
|
|
||||||
# creates a list of prime numbers between 2 up to 'number'
|
# creates a list of prime numbers between 2 up to 'number'
|
||||||
primeNumbers = getPrimeNumbers(number)
|
primeNumbers = getPrimeNumbers(number)
|
||||||
lenPN = len(primeNumbers)
|
lenPN = len(primeNumbers)
|
||||||
|
|
||||||
# run variable for while-loops.
|
# run variable for while-loops.
|
||||||
i = 0
|
i = 0
|
||||||
j = None
|
j = None
|
||||||
|
|
||||||
# exit variable. for break up the loops
|
# exit variable. for break up the loops
|
||||||
loop = True
|
loop = True
|
||||||
|
|
||||||
while (i < lenPN and loop):
|
while (i < lenPN and loop):
|
||||||
|
|
||||||
j = i+1
|
j = i+1
|
||||||
|
|
||||||
|
|
||||||
while (j < lenPN and loop):
|
while (j < lenPN and loop):
|
||||||
|
|
||||||
if primeNumbers[i] + primeNumbers[j] == number:
|
if primeNumbers[i] + primeNumbers[j] == number:
|
||||||
loop = False
|
loop = False
|
||||||
ans.append(primeNumbers[i])
|
ans.append(primeNumbers[i])
|
||||||
ans.append(primeNumbers[j])
|
ans.append(primeNumbers[j])
|
||||||
|
|
||||||
j += 1
|
j += 1
|
||||||
|
|
||||||
i += 1
|
i += 1
|
||||||
|
|
||||||
# precondition
|
# precondition
|
||||||
assert isinstance(ans,list) and (len(ans) == 2) and \
|
assert isinstance(ans,list) and (len(ans) == 2) and \
|
||||||
(ans[0] + ans[1] == number) and isPrime(ans[0]) and isPrime(ans[1]), \
|
(ans[0] + ans[1] == number) and isPrime(ans[0]) and isPrime(ans[1]), \
|
||||||
"'ans' must contains two primes. And sum of elements must been eq 'number'"
|
"'ans' must contains two primes. And sum of elements must been eq 'number'"
|
||||||
|
|
||||||
return ans
|
return ans
|
||||||
|
|
||||||
# ----------------------------------------------
|
# ----------------------------------------------
|
||||||
|
|
||||||
def gcd(number1,number2):
|
def gcd(number1,number2):
|
||||||
|
@ -319,173 +319,173 @@ def gcd(number1,number2):
|
||||||
input: two positive integer 'number1' and 'number2'
|
input: two positive integer 'number1' and 'number2'
|
||||||
returns the greatest common divisor of 'number1' and 'number2'
|
returns the greatest common divisor of 'number1' and 'number2'
|
||||||
"""
|
"""
|
||||||
|
|
||||||
# precondition
|
# precondition
|
||||||
assert isinstance(number1,int) and isinstance(number2,int) \
|
assert isinstance(number1,int) and isinstance(number2,int) \
|
||||||
and (number1 >= 0) and (number2 >= 0), \
|
and (number1 >= 0) and (number2 >= 0), \
|
||||||
"'number1' and 'number2' must been positive integer."
|
"'number1' and 'number2' must been positive integer."
|
||||||
|
|
||||||
rest = 0
|
rest = 0
|
||||||
|
|
||||||
while number2 != 0:
|
while number2 != 0:
|
||||||
|
|
||||||
rest = number1 % number2
|
rest = number1 % number2
|
||||||
number1 = number2
|
number1 = number2
|
||||||
number2 = rest
|
number2 = rest
|
||||||
|
|
||||||
# precondition
|
# precondition
|
||||||
assert isinstance(number1,int) and (number1 >= 0), \
|
assert isinstance(number1,int) and (number1 >= 0), \
|
||||||
"'number' must been from type int and positive"
|
"'number' must been from type int and positive"
|
||||||
|
|
||||||
return number1
|
return number1
|
||||||
|
|
||||||
# ----------------------------------------------------
|
# ----------------------------------------------------
|
||||||
|
|
||||||
def kgV(number1, number2):
|
def kgV(number1, number2):
|
||||||
"""
|
"""
|
||||||
Least common multiple
|
Least common multiple
|
||||||
input: two positive integer 'number1' and 'number2'
|
input: two positive integer 'number1' and 'number2'
|
||||||
returns the least common multiple of 'number1' and 'number2'
|
returns the least common multiple of 'number1' and 'number2'
|
||||||
"""
|
"""
|
||||||
|
|
||||||
# precondition
|
# precondition
|
||||||
assert isinstance(number1,int) and isinstance(number2,int) \
|
assert isinstance(number1,int) and isinstance(number2,int) \
|
||||||
and (number1 >= 1) and (number2 >= 1), \
|
and (number1 >= 1) and (number2 >= 1), \
|
||||||
"'number1' and 'number2' must been positive integer."
|
"'number1' and 'number2' must been positive integer."
|
||||||
|
|
||||||
ans = 1 # actual answer that will be return.
|
ans = 1 # actual answer that will be return.
|
||||||
|
|
||||||
# for kgV (x,1)
|
# for kgV (x,1)
|
||||||
if number1 > 1 and number2 > 1:
|
if number1 > 1 and number2 > 1:
|
||||||
|
|
||||||
# builds the prime factorization of 'number1' and 'number2'
|
# builds the prime factorization of 'number1' and 'number2'
|
||||||
primeFac1 = primeFactorization(number1)
|
primeFac1 = primeFactorization(number1)
|
||||||
primeFac2 = primeFactorization(number2)
|
primeFac2 = primeFactorization(number2)
|
||||||
|
|
||||||
elif number1 == 1 or number2 == 1:
|
elif number1 == 1 or number2 == 1:
|
||||||
|
|
||||||
primeFac1 = []
|
primeFac1 = []
|
||||||
primeFac2 = []
|
primeFac2 = []
|
||||||
ans = max(number1,number2)
|
ans = max(number1,number2)
|
||||||
|
|
||||||
count1 = 0
|
count1 = 0
|
||||||
count2 = 0
|
count2 = 0
|
||||||
|
|
||||||
done = [] # captured numbers int both 'primeFac1' and 'primeFac2'
|
done = [] # captured numbers int both 'primeFac1' and 'primeFac2'
|
||||||
|
|
||||||
# iterates through primeFac1
|
# iterates through primeFac1
|
||||||
for n in primeFac1:
|
for n in primeFac1:
|
||||||
|
|
||||||
if n not in done:
|
if n not in done:
|
||||||
|
|
||||||
if n in primeFac2:
|
if n in primeFac2:
|
||||||
|
|
||||||
count1 = primeFac1.count(n)
|
count1 = primeFac1.count(n)
|
||||||
count2 = primeFac2.count(n)
|
count2 = primeFac2.count(n)
|
||||||
|
|
||||||
for i in range(max(count1,count2)):
|
for i in range(max(count1,count2)):
|
||||||
ans *= n
|
ans *= n
|
||||||
|
|
||||||
else:
|
else:
|
||||||
|
|
||||||
count1 = primeFac1.count(n)
|
count1 = primeFac1.count(n)
|
||||||
|
|
||||||
for i in range(count1):
|
for i in range(count1):
|
||||||
ans *= n
|
ans *= n
|
||||||
|
|
||||||
done.append(n)
|
done.append(n)
|
||||||
|
|
||||||
# iterates through primeFac2
|
# iterates through primeFac2
|
||||||
for n in primeFac2:
|
for n in primeFac2:
|
||||||
|
|
||||||
if n not in done:
|
if n not in done:
|
||||||
|
|
||||||
count2 = primeFac2.count(n)
|
count2 = primeFac2.count(n)
|
||||||
|
|
||||||
for i in range(count2):
|
for i in range(count2):
|
||||||
ans *= n
|
ans *= n
|
||||||
|
|
||||||
done.append(n)
|
done.append(n)
|
||||||
|
|
||||||
# precondition
|
# precondition
|
||||||
assert isinstance(ans,int) and (ans >= 0), \
|
assert isinstance(ans,int) and (ans >= 0), \
|
||||||
"'ans' must been from type int and positive"
|
"'ans' must been from type int and positive"
|
||||||
|
|
||||||
return ans
|
return ans
|
||||||
|
|
||||||
# ----------------------------------
|
# ----------------------------------
|
||||||
|
|
||||||
def getPrime(n):
|
def getPrime(n):
|
||||||
"""
|
"""
|
||||||
Gets the n-th prime number.
|
Gets the n-th prime number.
|
||||||
input: positive integer 'n' >= 0
|
input: positive integer 'n' >= 0
|
||||||
returns the n-th prime number, beginning at index 0
|
returns the n-th prime number, beginning at index 0
|
||||||
"""
|
"""
|
||||||
|
|
||||||
# precondition
|
# precondition
|
||||||
assert isinstance(n,int) and (n >= 0), "'number' must been a positive int"
|
assert isinstance(n,int) and (n >= 0), "'number' must been a positive int"
|
||||||
|
|
||||||
index = 0
|
index = 0
|
||||||
ans = 2 # this variable holds the answer
|
ans = 2 # this variable holds the answer
|
||||||
|
|
||||||
while index < n:
|
while index < n:
|
||||||
|
|
||||||
index += 1
|
index += 1
|
||||||
|
|
||||||
ans += 1 # counts to the next number
|
ans += 1 # counts to the next number
|
||||||
|
|
||||||
# if ans not prime then
|
# if ans not prime then
|
||||||
# runs to the next prime number.
|
# runs to the next prime number.
|
||||||
while not isPrime(ans):
|
while not isPrime(ans):
|
||||||
ans += 1
|
ans += 1
|
||||||
|
|
||||||
# precondition
|
# precondition
|
||||||
assert isinstance(ans,int) and isPrime(ans), \
|
assert isinstance(ans,int) and isPrime(ans), \
|
||||||
"'ans' must been a prime number and from type int"
|
"'ans' must been a prime number and from type int"
|
||||||
|
|
||||||
return ans
|
return ans
|
||||||
|
|
||||||
# ---------------------------------------------------
|
# ---------------------------------------------------
|
||||||
|
|
||||||
def getPrimesBetween(pNumber1, pNumber2):
|
def getPrimesBetween(pNumber1, pNumber2):
|
||||||
"""
|
"""
|
||||||
input: prime numbers 'pNumber1' and 'pNumber2'
|
input: prime numbers 'pNumber1' and 'pNumber2'
|
||||||
pNumber1 < pNumber2
|
pNumber1 < pNumber2
|
||||||
returns a list of all prime numbers between 'pNumber1' (exclusiv)
|
returns a list of all prime numbers between 'pNumber1' (exclusiv)
|
||||||
and 'pNumber2' (exclusiv)
|
and 'pNumber2' (exclusiv)
|
||||||
"""
|
"""
|
||||||
|
|
||||||
# precondition
|
# precondition
|
||||||
assert isPrime(pNumber1) and isPrime(pNumber2) and (pNumber1 < pNumber2), \
|
assert isPrime(pNumber1) and isPrime(pNumber2) and (pNumber1 < pNumber2), \
|
||||||
"The arguments must been prime numbers and 'pNumber1' < 'pNumber2'"
|
"The arguments must been prime numbers and 'pNumber1' < 'pNumber2'"
|
||||||
|
|
||||||
number = pNumber1 + 1 # jump to the next number
|
number = pNumber1 + 1 # jump to the next number
|
||||||
|
|
||||||
ans = [] # this list will be returns.
|
ans = [] # this list will be returns.
|
||||||
|
|
||||||
# if number is not prime then
|
# if number is not prime then
|
||||||
# fetch the next prime number.
|
# fetch the next prime number.
|
||||||
while not isPrime(number):
|
while not isPrime(number):
|
||||||
number += 1
|
number += 1
|
||||||
|
|
||||||
while number < pNumber2:
|
while number < pNumber2:
|
||||||
|
|
||||||
ans.append(number)
|
ans.append(number)
|
||||||
|
|
||||||
number += 1
|
number += 1
|
||||||
|
|
||||||
# fetch the next prime number.
|
# fetch the next prime number.
|
||||||
while not isPrime(number):
|
while not isPrime(number):
|
||||||
number += 1
|
number += 1
|
||||||
|
|
||||||
# precondition
|
# precondition
|
||||||
assert isinstance(ans,list) and ans[0] != pNumber1 \
|
assert isinstance(ans,list) and ans[0] != pNumber1 \
|
||||||
and ans[len(ans)-1] != pNumber2, \
|
and ans[len(ans)-1] != pNumber2, \
|
||||||
"'ans' must been a list without the arguments"
|
"'ans' must been a list without the arguments"
|
||||||
|
|
||||||
# 'ans' contains not 'pNumber1' and 'pNumber2' !
|
# 'ans' contains not 'pNumber1' and 'pNumber2' !
|
||||||
return ans
|
return ans
|
||||||
|
|
||||||
# ----------------------------------------------------
|
# ----------------------------------------------------
|
||||||
|
|
||||||
def getDivisors(n):
|
def getDivisors(n):
|
||||||
|
@ -493,25 +493,23 @@ def getDivisors(n):
|
||||||
input: positive integer 'n' >= 1
|
input: positive integer 'n' >= 1
|
||||||
returns all divisors of n (inclusive 1 and 'n')
|
returns all divisors of n (inclusive 1 and 'n')
|
||||||
"""
|
"""
|
||||||
|
|
||||||
# precondition
|
# precondition
|
||||||
assert isinstance(n,int) and (n >= 1), "'n' must been int and >= 1"
|
assert isinstance(n,int) and (n >= 1), "'n' must been int and >= 1"
|
||||||
|
|
||||||
from math import sqrt
|
|
||||||
|
|
||||||
ans = [] # will be returned.
|
ans = [] # will be returned.
|
||||||
|
|
||||||
for divisor in range(1,n+1):
|
for divisor in range(1,n+1):
|
||||||
|
|
||||||
if n % divisor == 0:
|
if n % divisor == 0:
|
||||||
ans.append(divisor)
|
ans.append(divisor)
|
||||||
|
|
||||||
|
|
||||||
#precondition
|
#precondition
|
||||||
assert ans[0] == 1 and ans[len(ans)-1] == n, \
|
assert ans[0] == 1 and ans[len(ans)-1] == n, \
|
||||||
"Error in function getDivisiors(...)"
|
"Error in function getDivisiors(...)"
|
||||||
|
|
||||||
|
|
||||||
return ans
|
return ans
|
||||||
|
|
||||||
|
|
||||||
|
@ -523,18 +521,18 @@ def isPerfectNumber(number):
|
||||||
input: positive integer 'number' > 1
|
input: positive integer 'number' > 1
|
||||||
returns true if 'number' is a perfect number otherwise false.
|
returns true if 'number' is a perfect number otherwise false.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
# precondition
|
# precondition
|
||||||
assert isinstance(number,int) and (number > 1), \
|
assert isinstance(number,int) and (number > 1), \
|
||||||
"'number' must been an int and >= 1"
|
"'number' must been an int and >= 1"
|
||||||
|
|
||||||
divisors = getDivisors(number)
|
divisors = getDivisors(number)
|
||||||
|
|
||||||
# precondition
|
# precondition
|
||||||
assert isinstance(divisors,list) and(divisors[0] == 1) and \
|
assert isinstance(divisors,list) and(divisors[0] == 1) and \
|
||||||
(divisors[len(divisors)-1] == number), \
|
(divisors[len(divisors)-1] == number), \
|
||||||
"Error in help-function getDivisiors(...)"
|
"Error in help-function getDivisiors(...)"
|
||||||
|
|
||||||
# summed all divisors up to 'number' (exclusive), hence [:-1]
|
# summed all divisors up to 'number' (exclusive), hence [:-1]
|
||||||
return sum(divisors[:-1]) == number
|
return sum(divisors[:-1]) == number
|
||||||
|
|
||||||
|
@ -545,13 +543,13 @@ def simplifyFraction(numerator, denominator):
|
||||||
input: two integer 'numerator' and 'denominator'
|
input: two integer 'numerator' and 'denominator'
|
||||||
assumes: 'denominator' != 0
|
assumes: 'denominator' != 0
|
||||||
returns: a tuple with simplify numerator and denominator.
|
returns: a tuple with simplify numerator and denominator.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
# precondition
|
# precondition
|
||||||
assert isinstance(numerator, int) and isinstance(denominator,int) \
|
assert isinstance(numerator, int) and isinstance(denominator,int) \
|
||||||
and (denominator != 0), \
|
and (denominator != 0), \
|
||||||
"The arguments must been from type int and 'denominator' != 0"
|
"The arguments must been from type int and 'denominator' != 0"
|
||||||
|
|
||||||
# build the greatest common divisor of numerator and denominator.
|
# build the greatest common divisor of numerator and denominator.
|
||||||
gcdOfFraction = gcd(abs(numerator), abs(denominator))
|
gcdOfFraction = gcd(abs(numerator), abs(denominator))
|
||||||
|
|
||||||
|
@ -559,46 +557,46 @@ def simplifyFraction(numerator, denominator):
|
||||||
assert isinstance(gcdOfFraction, int) and (numerator % gcdOfFraction == 0) \
|
assert isinstance(gcdOfFraction, int) and (numerator % gcdOfFraction == 0) \
|
||||||
and (denominator % gcdOfFraction == 0), \
|
and (denominator % gcdOfFraction == 0), \
|
||||||
"Error in function gcd(...,...)"
|
"Error in function gcd(...,...)"
|
||||||
|
|
||||||
return (numerator // gcdOfFraction, denominator // gcdOfFraction)
|
return (numerator // gcdOfFraction, denominator // gcdOfFraction)
|
||||||
|
|
||||||
# -----------------------------------------------------------------
|
# -----------------------------------------------------------------
|
||||||
|
|
||||||
def factorial(n):
|
def factorial(n):
|
||||||
"""
|
"""
|
||||||
input: positive integer 'n'
|
input: positive integer 'n'
|
||||||
returns the factorial of 'n' (n!)
|
returns the factorial of 'n' (n!)
|
||||||
"""
|
"""
|
||||||
|
|
||||||
# precondition
|
# precondition
|
||||||
assert isinstance(n,int) and (n >= 0), "'n' must been a int and >= 0"
|
assert isinstance(n,int) and (n >= 0), "'n' must been a int and >= 0"
|
||||||
|
|
||||||
ans = 1 # this will be return.
|
ans = 1 # this will be return.
|
||||||
|
|
||||||
for factor in range(1,n+1):
|
for factor in range(1,n+1):
|
||||||
ans *= factor
|
ans *= factor
|
||||||
|
|
||||||
return ans
|
return ans
|
||||||
|
|
||||||
# -------------------------------------------------------------------
|
# -------------------------------------------------------------------
|
||||||
|
|
||||||
def fib(n):
|
def fib(n):
|
||||||
"""
|
"""
|
||||||
input: positive integer 'n'
|
input: positive integer 'n'
|
||||||
returns the n-th fibonacci term , indexing by 0
|
returns the n-th fibonacci term , indexing by 0
|
||||||
"""
|
"""
|
||||||
|
|
||||||
# precondition
|
# precondition
|
||||||
assert isinstance(n, int) and (n >= 0), "'n' must been an int and >= 0"
|
assert isinstance(n, int) and (n >= 0), "'n' must been an int and >= 0"
|
||||||
|
|
||||||
tmp = 0
|
tmp = 0
|
||||||
fib1 = 1
|
fib1 = 1
|
||||||
ans = 1 # this will be return
|
ans = 1 # this will be return
|
||||||
|
|
||||||
for i in range(n-1):
|
for i in range(n-1):
|
||||||
|
|
||||||
tmp = ans
|
tmp = ans
|
||||||
ans += fib1
|
ans += fib1
|
||||||
fib1 = tmp
|
fib1 = tmp
|
||||||
|
|
||||||
return ans
|
return ans
|
||||||
|
|
|
@ -3,18 +3,12 @@ Problem Statement:
|
||||||
Work out the first ten digits of the sum of the following one-hundred 50-digit
|
Work out the first ten digits of the sum of the following one-hundred 50-digit
|
||||||
numbers.
|
numbers.
|
||||||
"""
|
"""
|
||||||
from __future__ import print_function
|
|
||||||
import os
|
|
||||||
|
|
||||||
try:
|
|
||||||
raw_input # Python 2
|
|
||||||
except NameError:
|
|
||||||
raw_input = input # Python 3
|
|
||||||
|
|
||||||
|
|
||||||
def solution(array):
|
def solution(array):
|
||||||
"""Returns the first ten digits of the sum of the array elements.
|
"""Returns the first ten digits of the sum of the array elements.
|
||||||
|
|
||||||
|
>>> import os
|
||||||
>>> sum = 0
|
>>> sum = 0
|
||||||
>>> array = []
|
>>> array = []
|
||||||
>>> with open(os.path.dirname(__file__) + "/num.txt","r") as f:
|
>>> with open(os.path.dirname(__file__) + "/num.txt","r") as f:
|
||||||
|
|
Loading…
Reference in New Issue
Block a user