Skip to content

Added doctest and more explanation about Dijkstra execution. #1014

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 13 commits into from
Jul 16, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
20 changes: 18 additions & 2 deletions project_euler/problem_01/sol1.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,21 @@
raw_input # Python 2
except NameError:
raw_input = input # Python 3
n = int(raw_input().strip())
print(sum([e for e in range(3, n) if e % 3 == 0 or e % 5 == 0]))

def solution(n):
"""Returns the sum of all the multiples of 3 or 5 below n.

>>> solution(3)
Copy link
Member

Choose a reason for hiding this comment

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

Please consider adding solution(-7) just to be sure.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I will make it tomorrow.

0
>>> solution(4)
3
>>> solution(10)
23
>>> solution(600)
83700
"""

return sum([e for e in range(3, n) if e % 3 == 0 or e % 5 == 0])

if __name__ == "__main__":
print(solution(int(raw_input().strip())))
34 changes: 25 additions & 9 deletions project_euler/problem_01/sol2.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,28 @@
raw_input # Python 2
except NameError:
raw_input = input # Python 3
n = int(raw_input().strip())
sum = 0
terms = (n-1)//3
sum+= ((terms)*(6+(terms-1)*3))//2 #sum of an A.P.
terms = (n-1)//5
sum+= ((terms)*(10+(terms-1)*5))//2
terms = (n-1)//15
sum-= ((terms)*(30+(terms-1)*15))//2
print(sum)

def solution(n):
"""Returns the sum of all the multiples of 3 or 5 below n.

>>> solution(3)
0
>>> solution(4)
3
>>> solution(10)
23
>>> solution(600)
83700
"""

sum = 0
terms = (n-1)//3
sum+= ((terms)*(6+(terms-1)*3))//2 #sum of an A.P.
terms = (n-1)//5
sum+= ((terms)*(10+(terms-1)*5))//2
terms = (n-1)//15
sum-= ((terms)*(30+(terms-1)*15))//2
return sum

if __name__ == "__main__":
print(solution(int(raw_input().strip())))
89 changes: 50 additions & 39 deletions project_euler/problem_01/sol3.py
Original file line number Diff line number Diff line change
@@ -1,50 +1,61 @@
from __future__ import print_function

'''
Problem Statement:
If we list all the natural numbers below 10 that are multiples of 3 or 5,
we get 3,5,6 and 9. The sum of these multiples is 23.
Find the sum of all the multiples of 3 or 5 below N.
'''
'''
This solution is based on the pattern that the successive numbers in the series follow: 0+3,+2,+1,+3,+1,+2,+3.
'''

from __future__ import print_function
try:
raw_input # Python 2
except NameError:
raw_input = input # Python 3
n = int(raw_input().strip())
sum=0
num=0
while(1):
num+=3
if(num>=n):
break
sum+=num
num+=2
if(num>=n):
break
sum+=num
num+=1
if(num>=n):
break
sum+=num
num+=3
if(num>=n):
break
sum+=num
num+=1
if(num>=n):
break
sum+=num
num+=2
if(num>=n):
break
sum+=num
num+=3
if(num>=n):
break
sum+=num

print(sum);
"""This solution is based on the pattern that the successive numbers in the series follow: 0+3,+2,+1,+3,+1,+2,+3."""
def solution(n):
"""Returns the sum of all the multiples of 3 or 5 below n.

>>> solution(3)
0
>>> solution(4)
3
>>> solution(10)
23
>>> solution(600)
83700
"""

sum=0
num=0
while(1):
num+=3
if(num>=n):
break
sum+=num
num+=2
if(num>=n):
break
sum+=num
num+=1
if(num>=n):
break
sum+=num
num+=3
if(num>=n):
break
sum+=num
num+=1
if(num>=n):
break
sum+=num
num+=2
if(num>=n):
break
sum+=num
num+=3
if(num>=n):
break
sum+=num
return sum

if __name__ == "__main__":
print(solution(int(raw_input().strip())))
40 changes: 30 additions & 10 deletions project_euler/problem_01/sol4.py
Original file line number Diff line number Diff line change
@@ -1,30 +1,50 @@
def mulitples(limit):
'''
Problem Statement:
If we list all the natural numbers below 10 that are multiples of 3 or 5,
we get 3,5,6 and 9. The sum of these multiples is 23.
Find the sum of all the multiples of 3 or 5 below N.
'''
from __future__ import print_function
try:
raw_input # Python 2
except NameError:
raw_input = input # Python 3

def solution(n):
"""Returns the sum of all the multiples of 3 or 5 below n.

>>> solution(3)
0
>>> solution(4)
3
>>> solution(10)
23
>>> solution(600)
83700
"""

xmulti = []
zmulti = []
z = 3
x = 5
temp = 1
while True:
result = z * temp
if (result < limit):
if (result < n):
zmulti.append(result)
temp += 1
else:
temp = 1
break
while True:
result = x * temp
if (result < limit):
if (result < n):
xmulti.append(result)
temp += 1
else:
break
collection = list(set(xmulti+zmulti))
return (sum(collection))






print (mulitples(1000))

if __name__ == "__main__":
print(solution(int(raw_input().strip())))
22 changes: 18 additions & 4 deletions project_euler/problem_01/sol5.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,25 @@
'''
from __future__ import print_function
try:
input = raw_input #python3
raw_input # Python 2
except NameError:
pass #python 2
raw_input = input # Python 3
Copy link
Member

Choose a reason for hiding this comment

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

The previous way works as well and has the advantage that it moves the usage to the Python 3 function name but the disadvantages are that it overrides a Python 2 builtin and is not consistent with other files in this repo.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I see, thanks for letting me know.


"""A straightforward pythonic solution using list comprehension"""
n = int(input().strip())
print(sum([i for i in range(n) if i%3==0 or i%5==0]))
def solution(n):
"""Returns the sum of all the multiples of 3 or 5 below n.

>>> solution(3)
0
>>> solution(4)
3
>>> solution(10)
23
>>> solution(600)
83700
"""

return sum([i for i in range(n) if i%3==0 or i%5==0])

if __name__ == "__main__":
print(solution(int(raw_input().strip())))
46 changes: 37 additions & 9 deletions project_euler/problem_01/sol6.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,37 @@
a = 3
result = 0
while a < 1000:
if(a % 3 == 0 or a % 5 == 0):
result += a
elif(a % 15 == 0):
result -= a
a += 1
print(result)
'''
Problem Statement:
If we list all the natural numbers below 10 that are multiples of 3 or 5,
we get 3,5,6 and 9. The sum of these multiples is 23.
Find the sum of all the multiples of 3 or 5 below N.
'''
from __future__ import print_function
try:
raw_input # Python 2
except NameError:
raw_input = input # Python 3

def solution(n):
"""Returns the sum of all the multiples of 3 or 5 below n.

>>> solution(3)
0
>>> solution(4)
3
>>> solution(10)
23
>>> solution(600)
83700
"""

a = 3
result = 0
while a < n:
if(a % 3 == 0 or a % 5 == 0):
result += a
elif(a % 15 == 0):
result -= a
a += 1
return result

if __name__ == "__main__":
print(solution(int(raw_input().strip())))
35 changes: 26 additions & 9 deletions project_euler/problem_02/sol1.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,29 @@
except NameError:
raw_input = input # Python 3

n = int(raw_input().strip())
i=1
j=2
sum=0
while(j<=n):
if j%2 == 0:
sum+=j
i , j = j, i+j
print(sum)
def solution(n):
"""Returns the sum of all fibonacci sequence even elements that are lower or equals to n.

>>> solution(10)
10
>>> solution(15)
10
>>> solution(2)
2
>>> solution(1)
0
>>> solution(34)
44
"""
i=1
j=2
sum=0
while(j<=n):
if j%2 == 0:
sum+=j
i , j = j, i+j

return sum

if __name__ == "__main__":
print(solution(int(raw_input().strip())))
38 changes: 31 additions & 7 deletions project_euler/problem_02/sol2.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,39 @@
def fib(n):
"""
Returns a list of all the even terms in the Fibonacci sequence that are less than n.
'''
Problem:
Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting with 1 and 2,
the first 10 terms will be:
1,2,3,5,8,13,21,34,55,89,..
By considering the terms in the Fibonacci sequence whose values do not exceed n, find the sum of the even-valued terms.
e.g. for n=10, we have {2,8}, sum is 10.
'''
from __future__ import print_function

try:
raw_input # Python 2
except NameError:
raw_input = input # Python 3

def solution(n):
"""Returns the sum of all fibonacci sequence even elements that are lower or equals to n.

>>> solution(10)
[2, 8]
>>> solution(15)
[2, 8]
>>> solution(2)
[2]
>>> solution(1)
[]
>>> solution(34)
[2, 8, 34]
"""
ls = []
a, b = 0, 1
while b < n:
while b <= n:
if b % 2 == 0:
ls.append(b)
a, b = b, a+b
return ls

if __name__ == '__main__':
n = int(input("Enter max number: ").strip())
print(sum(fib(n)))
if __name__ == "__main__":
print(solution(int(raw_input().strip())))
Loading