-
-
Notifications
You must be signed in to change notification settings - Fork 46.8k
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
Changes from 1 commit
401ed00
d13eb2c
0e4e788
8c4c820
9fa20b3
8b17220
6c11811
cf4a9cf
b69db30
efa4bad
abfee10
304c09c
aa87466
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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()))) |
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()))) |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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()))) |
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()))) |
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()))) |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.