mirror of
https://github.com/TheAlgorithms/Python.git
synced 2025-05-11 20:05:44 +00:00
Simplify code by dropping support for legacy Python (#1143)
* Simplify code by dropping support for legacy Python * sort() --> sorted()
This commit is contained in:
parent
32aa7ff081
commit
47a9ea2b0b
@ -84,7 +84,7 @@ We want your work to be readable by others; therefore, we encourage you to note
|
|||||||
```python
|
```python
|
||||||
input('Enter your input:')
|
input('Enter your input:')
|
||||||
# Or even worse...
|
# Or even worse...
|
||||||
input = eval(raw_input("Enter your input: "))
|
input = eval(input("Enter your input: "))
|
||||||
```
|
```
|
||||||
|
|
||||||
However, if your code uses __input()__ then we encourage you to gracefully deal with leading and trailing whitespace in user input by adding __.strip()__ to the end as in:
|
However, if your code uses __input()__ then we encourage you to gracefully deal with leading and trailing whitespace in user input by adding __.strip()__ to the end as in:
|
||||||
@ -99,11 +99,11 @@ We want your work to be readable by others; therefore, we encourage you to note
|
|||||||
def sumab(a, b):
|
def sumab(a, b):
|
||||||
return a + b
|
return a + b
|
||||||
# Write tests this way:
|
# Write tests this way:
|
||||||
print(sumab(1,2)) # 1+2 = 3
|
print(sumab(1, 2)) # 1+2 = 3
|
||||||
print(sumab(6,4)) # 6+4 = 10
|
print(sumab(6, 4)) # 6+4 = 10
|
||||||
# Or this way:
|
# Or this way:
|
||||||
print("1 + 2 = ", sumab(1,2)) # 1+2 = 3
|
print("1 + 2 = ", sumab(1, 2)) # 1+2 = 3
|
||||||
print("6 + 4 = ", sumab(6,4)) # 6+4 = 10
|
print("6 + 4 = ", sumab(6, 4)) # 6+4 = 10
|
||||||
```
|
```
|
||||||
|
|
||||||
Better yet, if you know how to write [__doctests__](https://docs.python.org/3/library/doctest.html), please consider adding them.
|
Better yet, if you know how to write [__doctests__](https://docs.python.org/3/library/doctest.html), please consider adding them.
|
||||||
|
@ -1,4 +1,3 @@
|
|||||||
from __future__ import print_function
|
|
||||||
import sys, random, cryptomath_module as cryptoMath
|
import sys, random, cryptomath_module as cryptoMath
|
||||||
|
|
||||||
SYMBOLS = r""" !"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~"""
|
SYMBOLS = r""" !"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~"""
|
||||||
|
@ -1,23 +1,15 @@
|
|||||||
try: # Python 2
|
def atbash():
|
||||||
raw_input
|
|
||||||
unichr
|
|
||||||
except NameError: # Python 3
|
|
||||||
raw_input = input
|
|
||||||
unichr = chr
|
|
||||||
|
|
||||||
|
|
||||||
def Atbash():
|
|
||||||
output=""
|
output=""
|
||||||
for i in raw_input("Enter the sentence to be encrypted ").strip():
|
for i in input("Enter the sentence to be encrypted ").strip():
|
||||||
extract = ord(i)
|
extract = ord(i)
|
||||||
if 65 <= extract <= 90:
|
if 65 <= extract <= 90:
|
||||||
output += unichr(155-extract)
|
output += chr(155-extract)
|
||||||
elif 97 <= extract <= 122:
|
elif 97 <= extract <= 122:
|
||||||
output += unichr(219-extract)
|
output += chr(219-extract)
|
||||||
else:
|
else:
|
||||||
output+=i
|
output += i
|
||||||
print(output)
|
print(output)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
Atbash()
|
atbash()
|
||||||
|
@ -1,4 +1,3 @@
|
|||||||
from __future__ import print_function
|
|
||||||
def decrypt(message):
|
def decrypt(message):
|
||||||
"""
|
"""
|
||||||
>>> decrypt('TMDETUX PMDVU')
|
>>> decrypt('TMDETUX PMDVU')
|
||||||
|
@ -1,5 +1,3 @@
|
|||||||
from __future__ import print_function
|
|
||||||
|
|
||||||
import random
|
import random
|
||||||
|
|
||||||
|
|
||||||
|
@ -1,4 +1,3 @@
|
|||||||
from __future__ import print_function
|
|
||||||
# Primality Testing with the Rabin-Miller Algorithm
|
# Primality Testing with the Rabin-Miller Algorithm
|
||||||
|
|
||||||
import random
|
import random
|
||||||
|
@ -1,4 +1,3 @@
|
|||||||
from __future__ import print_function
|
|
||||||
def dencrypt(s, n):
|
def dencrypt(s, n):
|
||||||
out = ''
|
out = ''
|
||||||
for c in s:
|
for c in s:
|
||||||
|
@ -1,4 +1,3 @@
|
|||||||
from __future__ import print_function
|
|
||||||
import sys, rsa_key_generator as rkg, os
|
import sys, rsa_key_generator as rkg, os
|
||||||
|
|
||||||
DEFAULT_BLOCK_SIZE = 128
|
DEFAULT_BLOCK_SIZE = 128
|
||||||
|
@ -1,4 +1,3 @@
|
|||||||
from __future__ import print_function
|
|
||||||
import random, sys, os
|
import random, sys, os
|
||||||
import rabin_miller as rabinMiller, cryptomath_module as cryptoMath
|
import rabin_miller as rabinMiller, cryptomath_module as cryptoMath
|
||||||
|
|
||||||
|
@ -1,4 +1,3 @@
|
|||||||
from __future__ import print_function
|
|
||||||
import sys, random
|
import sys, random
|
||||||
|
|
||||||
LETTERS = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
|
LETTERS = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
|
||||||
|
@ -1,4 +1,3 @@
|
|||||||
from __future__ import print_function
|
|
||||||
import math
|
import math
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
|
@ -1,4 +1,3 @@
|
|||||||
from __future__ import print_function
|
|
||||||
import time, os, sys
|
import time, os, sys
|
||||||
import transposition_cipher as transCipher
|
import transposition_cipher as transCipher
|
||||||
|
|
||||||
|
@ -1,4 +1,3 @@
|
|||||||
from __future__ import print_function
|
|
||||||
LETTERS = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
|
LETTERS = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
|
@ -1,7 +1,6 @@
|
|||||||
'''
|
'''
|
||||||
A binary search Tree
|
A binary search Tree
|
||||||
'''
|
'''
|
||||||
from __future__ import print_function
|
|
||||||
class Node:
|
class Node:
|
||||||
|
|
||||||
def __init__(self, label, parent):
|
def __init__(self, label, parent):
|
||||||
|
@ -1,4 +1,3 @@
|
|||||||
from __future__ import print_function
|
|
||||||
class FenwickTree:
|
class FenwickTree:
|
||||||
|
|
||||||
def __init__(self, SIZE): # create fenwick tree with size SIZE
|
def __init__(self, SIZE): # create fenwick tree with size SIZE
|
||||||
|
@ -1,4 +1,3 @@
|
|||||||
from __future__ import print_function
|
|
||||||
import math
|
import math
|
||||||
|
|
||||||
class SegmentTree:
|
class SegmentTree:
|
||||||
|
@ -1,4 +1,3 @@
|
|||||||
from __future__ import print_function
|
|
||||||
import math
|
import math
|
||||||
|
|
||||||
class SegmentTree:
|
class SegmentTree:
|
||||||
|
@ -1,15 +1,8 @@
|
|||||||
#!/usr/bin/python
|
#!/usr/bin/python
|
||||||
|
|
||||||
from __future__ import print_function, division
|
# This heap class start from here.
|
||||||
|
|
||||||
try:
|
|
||||||
raw_input # Python 2
|
|
||||||
except NameError:
|
|
||||||
raw_input = input # Python 3
|
|
||||||
|
|
||||||
#This heap class start from here.
|
|
||||||
class Heap:
|
class Heap:
|
||||||
def __init__(self): #Default constructor of heap class.
|
def __init__(self): # Default constructor of heap class.
|
||||||
self.h = []
|
self.h = []
|
||||||
self.currsize = 0
|
self.currsize = 0
|
||||||
|
|
||||||
@ -79,7 +72,7 @@ class Heap:
|
|||||||
print(self.h)
|
print(self.h)
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
l = list(map(int, raw_input().split()))
|
l = list(map(int, input().split()))
|
||||||
h = Heap()
|
h = Heap()
|
||||||
h.buildHeap(l)
|
h.buildHeap(l)
|
||||||
h.heapSort()
|
h.heapSort()
|
||||||
|
@ -4,7 +4,6 @@
|
|||||||
- Each link references the next link and the previous one.
|
- Each link references the next link and the previous one.
|
||||||
- A Doubly Linked List (DLL) contains an extra pointer, typically called previous pointer, together with next pointer and data which are there in singly linked list.
|
- A Doubly Linked List (DLL) contains an extra pointer, typically called previous pointer, together with next pointer and data which are there in singly linked list.
|
||||||
- Advantages over SLL - IT can be traversed in both forward and backward direction.,Delete operation is more efficent'''
|
- Advantages over SLL - IT can be traversed in both forward and backward direction.,Delete operation is more efficent'''
|
||||||
from __future__ import print_function
|
|
||||||
|
|
||||||
|
|
||||||
class LinkedList: #making main class named linked list
|
class LinkedList: #making main class named linked list
|
||||||
|
@ -1,6 +1,3 @@
|
|||||||
from __future__ import print_function
|
|
||||||
|
|
||||||
|
|
||||||
class Node: # create a Node
|
class Node: # create a Node
|
||||||
def __init__(self, data):
|
def __init__(self, data):
|
||||||
self.data = data # given data
|
self.data = data # given data
|
||||||
|
@ -1,4 +1,3 @@
|
|||||||
from __future__ import print_function
|
|
||||||
# Python code to demonstrate working of
|
# Python code to demonstrate working of
|
||||||
# extend(), extendleft(), rotate(), reverse()
|
# extend(), extendleft(), rotate(), reverse()
|
||||||
|
|
||||||
|
@ -1,6 +1,3 @@
|
|||||||
from __future__ import print_function
|
|
||||||
from __future__ import absolute_import
|
|
||||||
|
|
||||||
from .stack import Stack
|
from .stack import Stack
|
||||||
|
|
||||||
__author__ = 'Omkar Pathak'
|
__author__ = 'Omkar Pathak'
|
||||||
|
@ -1,5 +1,3 @@
|
|||||||
from __future__ import print_function
|
|
||||||
from __future__ import absolute_import
|
|
||||||
import string
|
import string
|
||||||
|
|
||||||
from .stack import Stack
|
from .stack import Stack
|
||||||
|
@ -1,4 +1,3 @@
|
|||||||
from __future__ import print_function
|
|
||||||
# Function to print element and NGE pair for all elements of list
|
# Function to print element and NGE pair for all elements of list
|
||||||
def printNGE(arr):
|
def printNGE(arr):
|
||||||
|
|
||||||
|
@ -1,4 +1,3 @@
|
|||||||
from __future__ import print_function
|
|
||||||
__author__ = 'Omkar Pathak'
|
__author__ = 'Omkar Pathak'
|
||||||
|
|
||||||
|
|
||||||
|
@ -6,7 +6,6 @@ The span Si of the stock's price on a given day i is defined as the maximum
|
|||||||
number of consecutive days just before the given day, for which the price of the stock
|
number of consecutive days just before the given day, for which the price of the stock
|
||||||
on the current day is less than or equal to its price on the given day.
|
on the current day is less than or equal to its price on the given day.
|
||||||
'''
|
'''
|
||||||
from __future__ import print_function
|
|
||||||
def calculateSpan(price, S):
|
def calculateSpan(price, S):
|
||||||
|
|
||||||
n = len(price)
|
n = len(price)
|
||||||
|
@ -1,5 +1,3 @@
|
|||||||
from __future__ import print_function, absolute_import, division
|
|
||||||
|
|
||||||
from numbers import Number
|
from numbers import Number
|
||||||
"""
|
"""
|
||||||
The convex hull problem is problem of finding all the vertices of convex polygon, P of
|
The convex hull problem is problem of finding all the vertices of convex polygon, P of
|
||||||
|
@ -1,5 +1,3 @@
|
|||||||
from __future__ import print_function, absolute_import, division
|
|
||||||
|
|
||||||
"""
|
"""
|
||||||
Given an array-like data structure A[1..n], how many pairs
|
Given an array-like data structure A[1..n], how many pairs
|
||||||
(i, j) for all 1 <= i < j <= n such that A[i] > A[j]? These pairs are
|
(i, j) for all 1 <= i < j <= n such that A[i] > A[j]? These pairs are
|
||||||
|
@ -9,7 +9,6 @@ Find the total no of ways in which the tasks can be distributed.
|
|||||||
|
|
||||||
|
|
||||||
"""
|
"""
|
||||||
from __future__ import print_function
|
|
||||||
from collections import defaultdict
|
from collections import defaultdict
|
||||||
|
|
||||||
|
|
||||||
|
@ -5,9 +5,6 @@ Can you determine number of ways of making change for n units using
|
|||||||
the given types of coins?
|
the given types of coins?
|
||||||
https://www.hackerrank.com/challenges/coin-change/problem
|
https://www.hackerrank.com/challenges/coin-change/problem
|
||||||
"""
|
"""
|
||||||
from __future__ import print_function
|
|
||||||
|
|
||||||
|
|
||||||
def dp_count(S, m, n):
|
def dp_count(S, m, n):
|
||||||
|
|
||||||
# table[i] represents the number of ways to get to amount i
|
# table[i] represents the number of ways to get to amount i
|
||||||
|
@ -7,7 +7,6 @@ This is a pure Python implementation of Dynamic Programming solution to the edit
|
|||||||
The problem is :
|
The problem is :
|
||||||
Given two strings A and B. Find the minimum number of operations to string B such that A = B. The permitted operations are removal, insertion, and substitution.
|
Given two strings A and B. Find the minimum number of operations to string B such that A = B. The permitted operations are removal, insertion, and substitution.
|
||||||
"""
|
"""
|
||||||
from __future__ import print_function
|
|
||||||
|
|
||||||
|
|
||||||
class EditDistance:
|
class EditDistance:
|
||||||
@ -82,21 +81,13 @@ def min_distance_bottom_up(word1: str, word2: str) -> int:
|
|||||||
return dp[m][n]
|
return dp[m][n]
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
try:
|
|
||||||
raw_input # Python 2
|
|
||||||
except NameError:
|
|
||||||
raw_input = input # Python 3
|
|
||||||
|
|
||||||
solver = EditDistance()
|
solver = EditDistance()
|
||||||
|
|
||||||
print("****************** Testing Edit Distance DP Algorithm ******************")
|
print("****************** Testing Edit Distance DP Algorithm ******************")
|
||||||
print()
|
print()
|
||||||
|
|
||||||
print("Enter the first string: ", end="")
|
S1 = input("Enter the first string: ").strip()
|
||||||
S1 = raw_input().strip()
|
S2 = input("Enter the second string: ").strip()
|
||||||
|
|
||||||
print("Enter the second string: ", end="")
|
|
||||||
S2 = raw_input().strip()
|
|
||||||
|
|
||||||
print()
|
print()
|
||||||
print("The minimum Edit Distance is: %d" % (solver.solve(S1, S2)))
|
print("The minimum Edit Distance is: %d" % (solver.solve(S1, S2)))
|
||||||
|
@ -5,7 +5,6 @@
|
|||||||
This program calculates the nth Fibonacci number in O(log(n)).
|
This program calculates the nth Fibonacci number in O(log(n)).
|
||||||
It's possible to calculate F(1000000) in less than a second.
|
It's possible to calculate F(1000000) in less than a second.
|
||||||
"""
|
"""
|
||||||
from __future__ import print_function
|
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
|
|
||||||
|
@ -1,7 +1,6 @@
|
|||||||
"""
|
"""
|
||||||
This is a pure Python implementation of Dynamic Programming solution to the fibonacci sequence problem.
|
This is a pure Python implementation of Dynamic Programming solution to the fibonacci sequence problem.
|
||||||
"""
|
"""
|
||||||
from __future__ import print_function
|
|
||||||
|
|
||||||
|
|
||||||
class Fibonacci:
|
class Fibonacci:
|
||||||
@ -29,21 +28,16 @@ class Fibonacci:
|
|||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
print("\n********* Fibonacci Series Using Dynamic Programming ************\n")
|
print("\n********* Fibonacci Series Using Dynamic Programming ************\n")
|
||||||
try:
|
|
||||||
raw_input # Python 2
|
|
||||||
except NameError:
|
|
||||||
raw_input = input # Python 3
|
|
||||||
|
|
||||||
print("\n Enter the upper limit for the fibonacci sequence: ", end="")
|
print("\n Enter the upper limit for the fibonacci sequence: ", end="")
|
||||||
try:
|
try:
|
||||||
N = eval(raw_input().strip())
|
N = int(input().strip())
|
||||||
fib = Fibonacci(N)
|
fib = Fibonacci(N)
|
||||||
print(
|
print(
|
||||||
"\n********* Enter different values to get the corresponding fibonacci sequence, enter any negative number to exit. ************\n")
|
"\n********* Enter different values to get the corresponding fibonacci "
|
||||||
|
"sequence, enter any negative number to exit. ************\n")
|
||||||
while True:
|
while True:
|
||||||
print("Enter value: ", end=" ")
|
|
||||||
try:
|
try:
|
||||||
i = eval(raw_input().strip())
|
i = int(input("Enter value: ").strip())
|
||||||
if i < 0:
|
if i < 0:
|
||||||
print("\n********* Good Bye!! ************\n")
|
print("\n********* Good Bye!! ************\n")
|
||||||
break
|
break
|
||||||
|
@ -1,27 +1,15 @@
|
|||||||
from __future__ import print_function
|
|
||||||
|
|
||||||
try:
|
|
||||||
xrange #Python 2
|
|
||||||
except NameError:
|
|
||||||
xrange = range #Python 3
|
|
||||||
|
|
||||||
try:
|
|
||||||
raw_input #Python 2
|
|
||||||
except NameError:
|
|
||||||
raw_input = input #Python 3
|
|
||||||
|
|
||||||
'''
|
'''
|
||||||
The number of partitions of a number n into at least k parts equals the number of partitions into exactly k parts
|
The number of partitions of a number n into at least k parts equals the number of partitions into exactly k parts
|
||||||
plus the number of partitions into at least k-1 parts. Subtracting 1 from each part of a partition of n into k parts
|
plus the number of partitions into at least k-1 parts. Subtracting 1 from each part of a partition of n into k parts
|
||||||
gives a partition of n-k into k parts. These two facts together are used for this algorithm.
|
gives a partition of n-k into k parts. These two facts together are used for this algorithm.
|
||||||
'''
|
'''
|
||||||
def partition(m):
|
def partition(m):
|
||||||
memo = [[0 for _ in xrange(m)] for _ in xrange(m+1)]
|
memo = [[0 for _ in range(m)] for _ in range(m+1)]
|
||||||
for i in xrange(m+1):
|
for i in range(m+1):
|
||||||
memo[i][0] = 1
|
memo[i][0] = 1
|
||||||
|
|
||||||
for n in xrange(m+1):
|
for n in range(m+1):
|
||||||
for k in xrange(1, m):
|
for k in range(1, m):
|
||||||
memo[n][k] += memo[n][k-1]
|
memo[n][k] += memo[n][k-1]
|
||||||
if n-k > 0:
|
if n-k > 0:
|
||||||
memo[n][k] += memo[n-k-1][k]
|
memo[n][k] += memo[n-k-1][k]
|
||||||
@ -33,7 +21,7 @@ if __name__ == '__main__':
|
|||||||
|
|
||||||
if len(sys.argv) == 1:
|
if len(sys.argv) == 1:
|
||||||
try:
|
try:
|
||||||
n = int(raw_input('Enter a number: '))
|
n = int(input('Enter a number: ').strip())
|
||||||
print(partition(n))
|
print(partition(n))
|
||||||
except ValueError:
|
except ValueError:
|
||||||
print('Please enter a number.')
|
print('Please enter a number.')
|
||||||
|
@ -3,7 +3,6 @@ LCS Problem Statement: Given two sequences, find the length of longest subsequen
|
|||||||
A subsequence is a sequence that appears in the same relative order, but not necessarily continuous.
|
A subsequence is a sequence that appears in the same relative order, but not necessarily continuous.
|
||||||
Example:"abc", "abg" are subsequences of "abcdefgh".
|
Example:"abc", "abg" are subsequences of "abcdefgh".
|
||||||
"""
|
"""
|
||||||
from __future__ import print_function
|
|
||||||
|
|
||||||
|
|
||||||
def longest_common_subsequence(x: str, y: str):
|
def longest_common_subsequence(x: str, y: str):
|
||||||
@ -80,4 +79,3 @@ if __name__ == '__main__':
|
|||||||
assert expected_ln == ln
|
assert expected_ln == ln
|
||||||
assert expected_subseq == subseq
|
assert expected_subseq == subseq
|
||||||
print("len =", ln, ", sub-sequence =", subseq)
|
print("len =", ln, ", sub-sequence =", subseq)
|
||||||
|
|
||||||
|
@ -7,8 +7,6 @@ The problem is :
|
|||||||
Given an ARRAY, to find the longest and increasing sub ARRAY in that given ARRAY and return it.
|
Given an ARRAY, to find the longest and increasing sub ARRAY in that given ARRAY and return it.
|
||||||
Example: [10, 22, 9, 33, 21, 50, 41, 60, 80] as input will return [10, 22, 33, 41, 60, 80] as output
|
Example: [10, 22, 9, 33, 21, 50, 41, 60, 80] as input will return [10, 22, 33, 41, 60, 80] as output
|
||||||
'''
|
'''
|
||||||
from __future__ import print_function
|
|
||||||
|
|
||||||
def longestSub(ARRAY): #This function is recursive
|
def longestSub(ARRAY): #This function is recursive
|
||||||
|
|
||||||
ARRAY_LENGTH = len(ARRAY)
|
ARRAY_LENGTH = len(ARRAY)
|
||||||
|
@ -1,4 +1,3 @@
|
|||||||
from __future__ import print_function
|
|
||||||
#############################
|
#############################
|
||||||
# Author: Aravind Kashyap
|
# Author: Aravind Kashyap
|
||||||
# File: lis.py
|
# File: lis.py
|
||||||
|
@ -6,7 +6,6 @@ This is a pure Python implementation of Dynamic Programming solution to the long
|
|||||||
The problem is :
|
The problem is :
|
||||||
Given an array, to find the longest and continuous sub array and get the max sum of the sub array in the given array.
|
Given an array, to find the longest and continuous sub array and get the max sum of the sub array in the given array.
|
||||||
'''
|
'''
|
||||||
from __future__ import print_function
|
|
||||||
|
|
||||||
|
|
||||||
class SubArray:
|
class SubArray:
|
||||||
|
@ -1,5 +1,3 @@
|
|||||||
from __future__ import print_function
|
|
||||||
|
|
||||||
import sys
|
import sys
|
||||||
'''
|
'''
|
||||||
Dynamic Programming
|
Dynamic Programming
|
||||||
|
@ -1,7 +1,6 @@
|
|||||||
"""
|
"""
|
||||||
author : Mayank Kumar Jha (mk9440)
|
author : Mayank Kumar Jha (mk9440)
|
||||||
"""
|
"""
|
||||||
from __future__ import print_function
|
|
||||||
from typing import List
|
from typing import List
|
||||||
import time
|
import time
|
||||||
import matplotlib.pyplot as plt
|
import matplotlib.pyplot as plt
|
||||||
|
@ -1,5 +1,3 @@
|
|||||||
from __future__ import print_function
|
|
||||||
|
|
||||||
grid = [[0, 1, 0, 0, 0, 0],
|
grid = [[0, 1, 0, 0, 0, 0],
|
||||||
[0, 1, 0, 0, 0, 0],#0 are free path whereas 1's are obstacles
|
[0, 1, 0, 0, 0, 0],#0 are free path whereas 1's are obstacles
|
||||||
[0, 1, 0, 0, 0, 0],
|
[0, 1, 0, 0, 0, 0],
|
||||||
|
@ -1,23 +1,10 @@
|
|||||||
from __future__ import print_function
|
|
||||||
|
|
||||||
try:
|
|
||||||
raw_input # Python 2
|
|
||||||
except NameError:
|
|
||||||
raw_input = input # Python 3
|
|
||||||
|
|
||||||
try:
|
|
||||||
xrange # Python 2
|
|
||||||
except NameError:
|
|
||||||
xrange = range # Python 3
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
# Accept No. of Nodes and edges
|
# Accept No. of Nodes and edges
|
||||||
n, m = map(int, raw_input().split(" "))
|
n, m = map(int, input().split(" "))
|
||||||
|
|
||||||
# Initialising Dictionary of edges
|
# Initialising Dictionary of edges
|
||||||
g = {}
|
g = {}
|
||||||
for i in xrange(n):
|
for i in range(n):
|
||||||
g[i + 1] = []
|
g[i + 1] = []
|
||||||
|
|
||||||
"""
|
"""
|
||||||
@ -25,8 +12,8 @@ if __name__ == "__main__":
|
|||||||
Accepting edges of Unweighted Directed Graphs
|
Accepting edges of Unweighted Directed Graphs
|
||||||
----------------------------------------------------------------------------
|
----------------------------------------------------------------------------
|
||||||
"""
|
"""
|
||||||
for _ in xrange(m):
|
for _ in range(m):
|
||||||
x, y = map(int, raw_input().strip().split(" "))
|
x, y = map(int, input().strip().split(" "))
|
||||||
g[x].append(y)
|
g[x].append(y)
|
||||||
|
|
||||||
"""
|
"""
|
||||||
@ -34,8 +21,8 @@ if __name__ == "__main__":
|
|||||||
Accepting edges of Unweighted Undirected Graphs
|
Accepting edges of Unweighted Undirected Graphs
|
||||||
----------------------------------------------------------------------------
|
----------------------------------------------------------------------------
|
||||||
"""
|
"""
|
||||||
for _ in xrange(m):
|
for _ in range(m):
|
||||||
x, y = map(int, raw_input().strip().split(" "))
|
x, y = map(int, input().strip().split(" "))
|
||||||
g[x].append(y)
|
g[x].append(y)
|
||||||
g[y].append(x)
|
g[y].append(x)
|
||||||
|
|
||||||
@ -44,8 +31,8 @@ if __name__ == "__main__":
|
|||||||
Accepting edges of Weighted Undirected Graphs
|
Accepting edges of Weighted Undirected Graphs
|
||||||
----------------------------------------------------------------------------
|
----------------------------------------------------------------------------
|
||||||
"""
|
"""
|
||||||
for _ in xrange(m):
|
for _ in range(m):
|
||||||
x, y, r = map(int, raw_input().strip().split(" "))
|
x, y, r = map(int, input().strip().split(" "))
|
||||||
g[x].append([y, r])
|
g[x].append([y, r])
|
||||||
g[y].append([x, r])
|
g[y].append([x, r])
|
||||||
|
|
||||||
@ -170,10 +157,10 @@ def topo(G, ind=None, Q=[1]):
|
|||||||
|
|
||||||
|
|
||||||
def adjm():
|
def adjm():
|
||||||
n = raw_input().strip()
|
n = input().strip()
|
||||||
a = []
|
a = []
|
||||||
for i in xrange(n):
|
for i in range(n):
|
||||||
a.append(map(int, raw_input().strip().split()))
|
a.append(map(int, input().strip().split()))
|
||||||
return a, n
|
return a, n
|
||||||
|
|
||||||
|
|
||||||
@ -193,10 +180,10 @@ def adjm():
|
|||||||
def floy(A_and_n):
|
def floy(A_and_n):
|
||||||
(A, n) = A_and_n
|
(A, n) = A_and_n
|
||||||
dist = list(A)
|
dist = list(A)
|
||||||
path = [[0] * n for i in xrange(n)]
|
path = [[0] * n for i in range(n)]
|
||||||
for k in xrange(n):
|
for k in range(n):
|
||||||
for i in xrange(n):
|
for i in range(n):
|
||||||
for j in xrange(n):
|
for j in range(n):
|
||||||
if dist[i][j] > dist[i][k] + dist[k][j]:
|
if dist[i][j] > dist[i][k] + dist[k][j]:
|
||||||
dist[i][j] = dist[i][k] + dist[k][j]
|
dist[i][j] = dist[i][k] + dist[k][j]
|
||||||
path[i][k] = k
|
path[i][k] = k
|
||||||
@ -245,10 +232,10 @@ def prim(G, s):
|
|||||||
|
|
||||||
|
|
||||||
def edglist():
|
def edglist():
|
||||||
n, m = map(int, raw_input().split(" "))
|
n, m = map(int, input().split(" "))
|
||||||
l = []
|
l = []
|
||||||
for i in xrange(m):
|
for i in range(m):
|
||||||
l.append(map(int, raw_input().split(' ')))
|
l.append(map(int, input().split(' ')))
|
||||||
return l, n
|
return l, n
|
||||||
|
|
||||||
|
|
||||||
@ -272,10 +259,10 @@ def krusk(E_and_n):
|
|||||||
break
|
break
|
||||||
print(s)
|
print(s)
|
||||||
x = E.pop()
|
x = E.pop()
|
||||||
for i in xrange(len(s)):
|
for i in range(len(s)):
|
||||||
if x[0] in s[i]:
|
if x[0] in s[i]:
|
||||||
break
|
break
|
||||||
for j in xrange(len(s)):
|
for j in range(len(s)):
|
||||||
if x[1] in s[j]:
|
if x[1] in s[j]:
|
||||||
if i == j:
|
if i == j:
|
||||||
break
|
break
|
||||||
|
@ -1,5 +1,3 @@
|
|||||||
from __future__ import print_function
|
|
||||||
|
|
||||||
def printDist(dist, V):
|
def printDist(dist, V):
|
||||||
print("\nVertex Distance")
|
print("\nVertex Distance")
|
||||||
for i in range(V):
|
for i in range(V):
|
||||||
|
@ -3,8 +3,6 @@
|
|||||||
|
|
||||||
""" Author: OMKAR PATHAK """
|
""" Author: OMKAR PATHAK """
|
||||||
|
|
||||||
from __future__ import print_function
|
|
||||||
|
|
||||||
|
|
||||||
class Graph():
|
class Graph():
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
|
@ -2,7 +2,6 @@
|
|||||||
# encoding=utf8
|
# encoding=utf8
|
||||||
|
|
||||||
""" Author: OMKAR PATHAK """
|
""" Author: OMKAR PATHAK """
|
||||||
from __future__ import print_function
|
|
||||||
|
|
||||||
|
|
||||||
class Graph():
|
class Graph():
|
||||||
|
@ -1,5 +1,3 @@
|
|||||||
from __future__ import print_function
|
|
||||||
|
|
||||||
def printDist(dist, V):
|
def printDist(dist, V):
|
||||||
print("\nVertex Distance")
|
print("\nVertex Distance")
|
||||||
for i in range(V):
|
for i in range(V):
|
||||||
|
@ -2,7 +2,6 @@
|
|||||||
# Author: Shubham Malik
|
# Author: Shubham Malik
|
||||||
# References: https://en.wikipedia.org/wiki/Dijkstra%27s_algorithm
|
# References: https://en.wikipedia.org/wiki/Dijkstra%27s_algorithm
|
||||||
|
|
||||||
from __future__ import print_function
|
|
||||||
import math
|
import math
|
||||||
import sys
|
import sys
|
||||||
# For storing the vertex set to retreive node with the lowest distance
|
# For storing the vertex set to retreive node with the lowest distance
|
||||||
|
@ -12,7 +12,6 @@ Constraints
|
|||||||
Note: The tree input will be such that it can always be decomposed into
|
Note: The tree input will be such that it can always be decomposed into
|
||||||
components containing an even number of nodes.
|
components containing an even number of nodes.
|
||||||
"""
|
"""
|
||||||
from __future__ import print_function
|
|
||||||
# pylint: disable=invalid-name
|
# pylint: disable=invalid-name
|
||||||
from collections import defaultdict
|
from collections import defaultdict
|
||||||
|
|
||||||
|
@ -1,7 +1,6 @@
|
|||||||
#!/usr/bin/python
|
#!/usr/bin/python
|
||||||
# encoding=utf8
|
# encoding=utf8
|
||||||
|
|
||||||
from __future__ import print_function
|
|
||||||
# Author: OMKAR PATHAK
|
# Author: OMKAR PATHAK
|
||||||
|
|
||||||
# We can use Python's dictionary for constructing the graph.
|
# We can use Python's dictionary for constructing the graph.
|
||||||
|
@ -1,6 +1,3 @@
|
|||||||
from __future__ import print_function
|
|
||||||
|
|
||||||
|
|
||||||
class Graph:
|
class Graph:
|
||||||
|
|
||||||
def __init__(self, vertex):
|
def __init__(self, vertex):
|
||||||
|
@ -4,8 +4,6 @@
|
|||||||
have negative edge weights.
|
have negative edge weights.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
from __future__ import print_function
|
|
||||||
|
|
||||||
|
|
||||||
def _print_dist(dist, v):
|
def _print_dist(dist, v):
|
||||||
print("\nThe shortest path matrix using Floyd Warshall algorithm\n")
|
print("\nThe shortest path matrix using Floyd Warshall algorithm\n")
|
||||||
|
@ -1,5 +1,3 @@
|
|||||||
from __future__ import print_function
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
num_nodes, num_edges = list(map(int, input().strip().split()))
|
num_nodes, num_edges = list(map(int, input().strip().split()))
|
||||||
|
|
||||||
|
@ -1,12 +1,6 @@
|
|||||||
from __future__ import print_function
|
|
||||||
import heapq
|
import heapq
|
||||||
import numpy as np
|
import numpy as np
|
||||||
|
|
||||||
try:
|
|
||||||
xrange # Python 2
|
|
||||||
except NameError:
|
|
||||||
xrange = range # Python 3
|
|
||||||
|
|
||||||
|
|
||||||
class PriorityQueue:
|
class PriorityQueue:
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
@ -96,7 +90,7 @@ def do_something(back_pointer, goal, start):
|
|||||||
grid[(n-1)][0] = "-"
|
grid[(n-1)][0] = "-"
|
||||||
|
|
||||||
|
|
||||||
for i in xrange(n):
|
for i in range(n):
|
||||||
for j in range(n):
|
for j in range(n):
|
||||||
if (i, j) == (0, n-1):
|
if (i, j) == (0, n-1):
|
||||||
print(grid[i][j], end=' ')
|
print(grid[i][j], end=' ')
|
||||||
|
@ -1,6 +1,3 @@
|
|||||||
from __future__ import print_function
|
|
||||||
|
|
||||||
|
|
||||||
def dfs(u):
|
def dfs(u):
|
||||||
global g, r, scc, component, visit, stack
|
global g, r, scc, component, visit, stack
|
||||||
if visit[u]: return
|
if visit[u]: return
|
||||||
|
@ -1,10 +1,4 @@
|
|||||||
"""example of simple chaos machine"""
|
"""example of simple chaos machine"""
|
||||||
from __future__ import print_function
|
|
||||||
|
|
||||||
try:
|
|
||||||
input = raw_input # Python 2
|
|
||||||
except NameError:
|
|
||||||
pass # Python 3
|
|
||||||
|
|
||||||
# Chaos Machine (K, t, m)
|
# Chaos Machine (K, t, m)
|
||||||
K = [0.33, 0.44, 0.55, 0.44, 0.33]; t = 3; m = 5
|
K = [0.33, 0.44, 0.55, 0.44, 0.33]; t = 3; m = 5
|
||||||
|
@ -1,5 +1,3 @@
|
|||||||
from __future__ import print_function
|
|
||||||
|
|
||||||
alphabets = [chr(i) for i in range(32, 126)]
|
alphabets = [chr(i) for i in range(32, 126)]
|
||||||
gear_one = [i for i in range(len(alphabets))]
|
gear_one = [i for i in range(len(alphabets))]
|
||||||
gear_two = [i for i in range(len(alphabets))]
|
gear_two = [i for i in range(len(alphabets))]
|
||||||
|
@ -1,4 +1,3 @@
|
|||||||
from __future__ import print_function
|
|
||||||
import math
|
import math
|
||||||
|
|
||||||
|
|
||||||
|
@ -3,8 +3,6 @@ Implementation of a basic regression decision tree.
|
|||||||
Input data set: The input data set must be 1-dimensional with continuous labels.
|
Input data set: The input data set must be 1-dimensional with continuous labels.
|
||||||
Output: The decision tree maps a real number input to a real number output.
|
Output: The decision tree maps a real number input to a real number output.
|
||||||
"""
|
"""
|
||||||
from __future__ import print_function
|
|
||||||
|
|
||||||
import numpy as np
|
import numpy as np
|
||||||
|
|
||||||
class Decision_Tree:
|
class Decision_Tree:
|
||||||
|
@ -1,7 +1,6 @@
|
|||||||
"""
|
"""
|
||||||
Implementation of gradient descent algorithm for minimizing cost of a linear hypothesis function.
|
Implementation of gradient descent algorithm for minimizing cost of a linear hypothesis function.
|
||||||
"""
|
"""
|
||||||
from __future__ import print_function, division
|
|
||||||
import numpy
|
import numpy
|
||||||
|
|
||||||
# List of input, output pairs
|
# List of input, output pairs
|
||||||
|
@ -46,7 +46,6 @@ Usage:
|
|||||||
5. Have fun..
|
5. Have fun..
|
||||||
|
|
||||||
'''
|
'''
|
||||||
from __future__ import print_function
|
|
||||||
from sklearn.metrics import pairwise_distances
|
from sklearn.metrics import pairwise_distances
|
||||||
import numpy as np
|
import numpy as np
|
||||||
|
|
||||||
|
@ -7,8 +7,6 @@ We try to set these Feature weights, over many iterations, so that they best
|
|||||||
fits our dataset. In this particular code, i had used a CSGO dataset (ADR vs
|
fits our dataset. In this particular code, i had used a CSGO dataset (ADR vs
|
||||||
Rating). We try to best fit a line through dataset and estimate the parameters.
|
Rating). We try to best fit a line through dataset and estimate the parameters.
|
||||||
"""
|
"""
|
||||||
from __future__ import print_function
|
|
||||||
|
|
||||||
import requests
|
import requests
|
||||||
import numpy as np
|
import numpy as np
|
||||||
|
|
||||||
|
@ -8,9 +8,6 @@ method 2:
|
|||||||
"Simpson Rule"
|
"Simpson Rule"
|
||||||
|
|
||||||
"""
|
"""
|
||||||
from __future__ import print_function
|
|
||||||
|
|
||||||
|
|
||||||
def method_2(boundary, steps):
|
def method_2(boundary, steps):
|
||||||
# "Simpson Rule"
|
# "Simpson Rule"
|
||||||
# int(f) = delta_x/2 * (b-a)/3*(f1 + 4f2 + 2f_3 + ... + fn)
|
# int(f) = delta_x/2 * (b-a)/3*(f1 + 4f2 + 2f_3 + ... + fn)
|
||||||
|
@ -7,8 +7,6 @@ method 1:
|
|||||||
"extended trapezoidal rule"
|
"extended trapezoidal rule"
|
||||||
|
|
||||||
"""
|
"""
|
||||||
from __future__ import print_function
|
|
||||||
|
|
||||||
def method_1(boundary, steps):
|
def method_1(boundary, steps):
|
||||||
# "extended trapezoidal rule"
|
# "extended trapezoidal rule"
|
||||||
# int(f) = dx/2 * (f1 + 2f2 + ... + fn)
|
# int(f) = dx/2 * (f1 + 2f2 + ... + fn)
|
||||||
|
@ -1,4 +1,3 @@
|
|||||||
from __future__ import annotations
|
|
||||||
import datetime
|
import datetime
|
||||||
import argparse
|
import argparse
|
||||||
|
|
||||||
|
@ -13,9 +13,6 @@ Converting to matrix,
|
|||||||
So we just need the n times multiplication of the matrix [1,1],[1,0]].
|
So we just need the n times multiplication of the matrix [1,1],[1,0]].
|
||||||
We can decrease the n times multiplication by following the divide and conquer approach.
|
We can decrease the n times multiplication by following the divide and conquer approach.
|
||||||
"""
|
"""
|
||||||
from __future__ import print_function
|
|
||||||
|
|
||||||
|
|
||||||
def multiply(matrix_a, matrix_b):
|
def multiply(matrix_a, matrix_b):
|
||||||
matrix_c = []
|
matrix_c = []
|
||||||
n = len(matrix_a)
|
n = len(matrix_a)
|
||||||
|
@ -15,8 +15,6 @@
|
|||||||
Date: 2017.9.20
|
Date: 2017.9.20
|
||||||
- - - - - -- - - - - - - - - - - - - - - - - - - - - - -
|
- - - - - -- - - - - - - - - - - - - - - - - - - - - - -
|
||||||
'''
|
'''
|
||||||
from __future__ import print_function
|
|
||||||
|
|
||||||
import pickle
|
import pickle
|
||||||
import numpy as np
|
import numpy as np
|
||||||
import matplotlib.pyplot as plt
|
import matplotlib.pyplot as plt
|
||||||
|
@ -9,8 +9,6 @@
|
|||||||
p2 = 1
|
p2 = 1
|
||||||
|
|
||||||
'''
|
'''
|
||||||
from __future__ import print_function
|
|
||||||
|
|
||||||
import random
|
import random
|
||||||
|
|
||||||
|
|
||||||
|
@ -1,4 +1,3 @@
|
|||||||
from __future__ import print_function
|
|
||||||
import collections, pprint, time, os
|
import collections, pprint, time, os
|
||||||
|
|
||||||
start_time = time.time()
|
start_time = time.time()
|
||||||
|
@ -1,4 +1,3 @@
|
|||||||
from __future__ import print_function
|
|
||||||
# https://en.wikipedia.org/wiki/Euclidean_algorithm
|
# https://en.wikipedia.org/wiki/Euclidean_algorithm
|
||||||
|
|
||||||
def euclidean_gcd(a, b):
|
def euclidean_gcd(a, b):
|
||||||
|
@ -1,4 +1,3 @@
|
|||||||
from __future__ import print_function
|
|
||||||
__author__ = "Tobias Carryer"
|
__author__ = "Tobias Carryer"
|
||||||
|
|
||||||
from time import time
|
from time import time
|
||||||
|
@ -13,9 +13,6 @@ The function called is_balanced takes as input a string S which is a sequence of
|
|||||||
returns true if S is nested and false otherwise.
|
returns true if S is nested and false otherwise.
|
||||||
|
|
||||||
'''
|
'''
|
||||||
from __future__ import print_function
|
|
||||||
|
|
||||||
|
|
||||||
def is_balanced(S):
|
def is_balanced(S):
|
||||||
|
|
||||||
stack = []
|
stack = []
|
||||||
|
@ -1,5 +1,4 @@
|
|||||||
"""Password generator allows you to generate a random password of length N."""
|
"""Password generator allows you to generate a random password of length N."""
|
||||||
from __future__ import print_function
|
|
||||||
from random import choice
|
from random import choice
|
||||||
from string import ascii_letters, digits, punctuation
|
from string import ascii_letters, digits, punctuation
|
||||||
|
|
||||||
|
@ -1,4 +1,3 @@
|
|||||||
from __future__ import print_function
|
|
||||||
def moveTower(height, fromPole, toPole, withPole):
|
def moveTower(height, fromPole, toPole, withPole):
|
||||||
'''
|
'''
|
||||||
>>> moveTower(3, 'A', 'B', 'C')
|
>>> moveTower(3, 'A', 'B', 'C')
|
||||||
|
@ -9,8 +9,6 @@ Given nums = [2, 7, 11, 15], target = 9,
|
|||||||
Because nums[0] + nums[1] = 2 + 7 = 9,
|
Because nums[0] + nums[1] = 2 + 7 = 9,
|
||||||
return [0, 1].
|
return [0, 1].
|
||||||
"""
|
"""
|
||||||
from __future__ import print_function
|
|
||||||
|
|
||||||
def twoSum(nums, target):
|
def twoSum(nums, target):
|
||||||
"""
|
"""
|
||||||
:type nums: List[int]
|
:type nums: List[int]
|
||||||
|
@ -1,4 +1,3 @@
|
|||||||
from __future__ import print_function
|
|
||||||
import pprint, time
|
import pprint, time
|
||||||
|
|
||||||
def getWordPattern(word):
|
def getWordPattern(word):
|
||||||
|
@ -4,14 +4,6 @@ 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.
|
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.
|
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):
|
def solution(n):
|
||||||
"""Returns the sum of all the multiples of 3 or 5 below n.
|
"""Returns the sum of all the multiples of 3 or 5 below n.
|
||||||
|
|
||||||
@ -31,4 +23,4 @@ def solution(n):
|
|||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
print(solution(int(raw_input().strip())))
|
print(solution(int(input().strip())))
|
||||||
|
@ -4,12 +4,6 @@ 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.
|
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.
|
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):
|
def solution(n):
|
||||||
@ -36,4 +30,4 @@ def solution(n):
|
|||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
print(solution(int(raw_input().strip())))
|
print(solution(int(input().strip())))
|
||||||
|
@ -4,14 +4,6 @@ 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.
|
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.
|
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):
|
def solution(n):
|
||||||
"""
|
"""
|
||||||
This solution is based on the pattern that the successive numbers in the
|
This solution is based on the pattern that the successive numbers in the
|
||||||
@ -63,4 +55,4 @@ def solution(n):
|
|||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
print(solution(int(raw_input().strip())))
|
print(solution(int(input().strip())))
|
||||||
|
@ -4,14 +4,6 @@ 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.
|
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.
|
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):
|
def solution(n):
|
||||||
"""Returns the sum of all the multiples of 3 or 5 below n.
|
"""Returns the sum of all the multiples of 3 or 5 below n.
|
||||||
|
|
||||||
@ -50,4 +42,4 @@ def solution(n):
|
|||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
print(solution(int(raw_input().strip())))
|
print(solution(int(input().strip())))
|
||||||
|
@ -4,12 +4,6 @@ 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.
|
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.
|
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
|
|
||||||
|
|
||||||
"""A straightforward pythonic solution using list comprehension"""
|
"""A straightforward pythonic solution using list comprehension"""
|
||||||
|
|
||||||
@ -31,4 +25,4 @@ def solution(n):
|
|||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
print(solution(int(raw_input().strip())))
|
print(solution(int(input().strip())))
|
||||||
|
@ -4,14 +4,6 @@ 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.
|
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.
|
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):
|
def solution(n):
|
||||||
"""Returns the sum of all the multiples of 3 or 5 below n.
|
"""Returns the sum of all the multiples of 3 or 5 below n.
|
||||||
|
|
||||||
@ -37,4 +29,4 @@ def solution(n):
|
|||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
print(solution(int(raw_input().strip())))
|
print(solution(int(input().strip())))
|
||||||
|
@ -9,14 +9,6 @@ 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
|
n, find the sum of the even-valued terms. e.g. for n=10, we have {2,8}, sum is
|
||||||
10.
|
10.
|
||||||
"""
|
"""
|
||||||
from __future__ import print_function
|
|
||||||
|
|
||||||
try:
|
|
||||||
raw_input # Python 2
|
|
||||||
except NameError:
|
|
||||||
raw_input = input # Python 3
|
|
||||||
|
|
||||||
|
|
||||||
def solution(n):
|
def solution(n):
|
||||||
"""Returns the sum of all fibonacci sequence even elements that are lower
|
"""Returns the sum of all fibonacci sequence even elements that are lower
|
||||||
or equals to n.
|
or equals to n.
|
||||||
@ -44,4 +36,4 @@ def solution(n):
|
|||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
print(solution(int(raw_input().strip())))
|
print(solution(int(input().strip())))
|
||||||
|
@ -9,14 +9,6 @@ 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
|
n, find the sum of the even-valued terms. e.g. for n=10, we have {2,8}, sum is
|
||||||
10.
|
10.
|
||||||
"""
|
"""
|
||||||
from __future__ import print_function
|
|
||||||
|
|
||||||
try:
|
|
||||||
raw_input # Python 2
|
|
||||||
except NameError:
|
|
||||||
raw_input = input # Python 3
|
|
||||||
|
|
||||||
|
|
||||||
def solution(n):
|
def solution(n):
|
||||||
"""Returns the sum of all fibonacci sequence even elements that are lower
|
"""Returns the sum of all fibonacci sequence even elements that are lower
|
||||||
or equals to n.
|
or equals to n.
|
||||||
@ -42,4 +34,4 @@ def solution(n):
|
|||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
print(solution(int(raw_input().strip())))
|
print(solution(int(input().strip())))
|
||||||
|
@ -9,14 +9,6 @@ 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
|
n, find the sum of the even-valued terms. e.g. for n=10, we have {2,8}, sum is
|
||||||
10.
|
10.
|
||||||
"""
|
"""
|
||||||
from __future__ import print_function
|
|
||||||
|
|
||||||
try:
|
|
||||||
raw_input # Python 2
|
|
||||||
except NameError:
|
|
||||||
raw_input = input # Python 3
|
|
||||||
|
|
||||||
|
|
||||||
def solution(n):
|
def solution(n):
|
||||||
"""Returns the sum of all fibonacci sequence even elements that are lower
|
"""Returns the sum of all fibonacci sequence even elements that are lower
|
||||||
or equals to n.
|
or equals to n.
|
||||||
@ -44,4 +36,4 @@ def solution(n):
|
|||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
print(solution(int(raw_input().strip())))
|
print(solution(int(input().strip())))
|
||||||
|
@ -9,15 +9,9 @@ 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
|
n, find the sum of the even-valued terms. e.g. for n=10, we have {2,8}, sum is
|
||||||
10.
|
10.
|
||||||
"""
|
"""
|
||||||
from __future__ import print_function
|
|
||||||
import math
|
import math
|
||||||
from decimal import Decimal, getcontext
|
from decimal import Decimal, getcontext
|
||||||
|
|
||||||
try:
|
|
||||||
raw_input # Python 2
|
|
||||||
except NameError:
|
|
||||||
raw_input = input # Python 3
|
|
||||||
|
|
||||||
|
|
||||||
def solution(n):
|
def solution(n):
|
||||||
"""Returns the sum of all fibonacci sequence even elements that are lower
|
"""Returns the sum of all fibonacci sequence even elements that are lower
|
||||||
@ -68,4 +62,4 @@ def solution(n):
|
|||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
print(solution(int(raw_input().strip())))
|
print(solution(int(input().strip())))
|
||||||
|
@ -5,14 +5,8 @@ of a given number N?
|
|||||||
|
|
||||||
e.g. for 10, largest prime factor = 5. For 17, largest prime factor = 17.
|
e.g. for 10, largest prime factor = 5. For 17, largest prime factor = 17.
|
||||||
"""
|
"""
|
||||||
from __future__ import print_function, division
|
|
||||||
import math
|
import math
|
||||||
|
|
||||||
try:
|
|
||||||
raw_input # Python 2
|
|
||||||
except NameError:
|
|
||||||
raw_input = input # Python 3
|
|
||||||
|
|
||||||
|
|
||||||
def isprime(no):
|
def isprime(no):
|
||||||
if no == 2:
|
if no == 2:
|
||||||
@ -81,4 +75,4 @@ def solution(n):
|
|||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
print(solution(int(raw_input().strip())))
|
print(solution(int(input().strip())))
|
||||||
|
@ -5,12 +5,6 @@ of a given number N?
|
|||||||
|
|
||||||
e.g. for 10, largest prime factor = 5. For 17, largest prime factor = 17.
|
e.g. for 10, largest prime factor = 5. For 17, largest prime factor = 17.
|
||||||
"""
|
"""
|
||||||
from __future__ import print_function, division
|
|
||||||
|
|
||||||
try:
|
|
||||||
raw_input # Python 2
|
|
||||||
except NameError:
|
|
||||||
raw_input = input # Python 3
|
|
||||||
|
|
||||||
|
|
||||||
def solution(n):
|
def solution(n):
|
||||||
@ -60,4 +54,4 @@ def solution(n):
|
|||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
print(solution(int(raw_input().strip())))
|
print(solution(int(input().strip())))
|
||||||
|
@ -6,14 +6,6 @@ the product of two 2-digit numbers is 9009 = 91 x 99.
|
|||||||
Find the largest palindrome made from the product of two 3-digit numbers which
|
Find the largest palindrome made from the product of two 3-digit numbers which
|
||||||
is less than N.
|
is less than N.
|
||||||
"""
|
"""
|
||||||
from __future__ import print_function
|
|
||||||
|
|
||||||
try:
|
|
||||||
raw_input # Python 2
|
|
||||||
except NameError:
|
|
||||||
raw_input = input # Python 3
|
|
||||||
|
|
||||||
|
|
||||||
def solution(n):
|
def solution(n):
|
||||||
"""Returns the largest palindrome made from the product of two 3-digit
|
"""Returns the largest palindrome made from the product of two 3-digit
|
||||||
numbers which is less than n.
|
numbers which is less than n.
|
||||||
@ -47,4 +39,4 @@ def solution(n):
|
|||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
print(solution(int(raw_input().strip())))
|
print(solution(int(input().strip())))
|
||||||
|
@ -6,14 +6,6 @@ the product of two 2-digit numbers is 9009 = 91 x 99.
|
|||||||
Find the largest palindrome made from the product of two 3-digit numbers which
|
Find the largest palindrome made from the product of two 3-digit numbers which
|
||||||
is less than N.
|
is less than N.
|
||||||
"""
|
"""
|
||||||
from __future__ import print_function
|
|
||||||
|
|
||||||
try:
|
|
||||||
raw_input # Python 2
|
|
||||||
except NameError:
|
|
||||||
raw_input = input # Python 3
|
|
||||||
|
|
||||||
|
|
||||||
def solution(n):
|
def solution(n):
|
||||||
"""Returns the largest palindrome made from the product of two 3-digit
|
"""Returns the largest palindrome made from the product of two 3-digit
|
||||||
numbers which is less than n.
|
numbers which is less than n.
|
||||||
@ -35,4 +27,4 @@ def solution(n):
|
|||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
print(solution(int(raw_input().strip())))
|
print(solution(int(input().strip())))
|
||||||
|
@ -6,14 +6,6 @@ to 10 without any remainder.
|
|||||||
What is the smallest positive number that is evenly divisible(divisible with no
|
What is the smallest positive number that is evenly divisible(divisible with no
|
||||||
remainder) by all of the numbers from 1 to N?
|
remainder) by all of the numbers from 1 to N?
|
||||||
"""
|
"""
|
||||||
from __future__ import print_function
|
|
||||||
|
|
||||||
try:
|
|
||||||
raw_input # Python 2
|
|
||||||
except NameError:
|
|
||||||
raw_input = input # Python 3
|
|
||||||
|
|
||||||
|
|
||||||
def solution(n):
|
def solution(n):
|
||||||
"""Returns the smallest positive number that is evenly divisible(divisible
|
"""Returns the smallest positive number that is evenly divisible(divisible
|
||||||
with no remainder) by all of the numbers from 1 to n.
|
with no remainder) by all of the numbers from 1 to n.
|
||||||
@ -66,4 +58,4 @@ def solution(n):
|
|||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
print(solution(int(raw_input().strip())))
|
print(solution(int(input().strip())))
|
||||||
|
@ -6,13 +6,6 @@ to 10 without any remainder.
|
|||||||
What is the smallest positive number that is evenly divisible(divisible with no
|
What is the smallest positive number that is evenly divisible(divisible with no
|
||||||
remainder) by all of the numbers from 1 to N?
|
remainder) by all of the numbers from 1 to N?
|
||||||
"""
|
"""
|
||||||
from __future__ import print_function
|
|
||||||
|
|
||||||
try:
|
|
||||||
raw_input # Python 2
|
|
||||||
except NameError:
|
|
||||||
raw_input = input # Python 3
|
|
||||||
|
|
||||||
""" Euclidean GCD Algorithm """
|
""" Euclidean GCD Algorithm """
|
||||||
|
|
||||||
|
|
||||||
@ -47,4 +40,4 @@ def solution(n):
|
|||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
print(solution(int(raw_input().strip())))
|
print(solution(int(input().strip())))
|
||||||
|
@ -14,14 +14,6 @@ numbers and the square of the sum is 3025 − 385 = 2640.
|
|||||||
Find the difference between the sum of the squares of the first N natural
|
Find the difference between the sum of the squares of the first N natural
|
||||||
numbers and the square of the sum.
|
numbers and the square of the sum.
|
||||||
"""
|
"""
|
||||||
from __future__ import print_function
|
|
||||||
|
|
||||||
try:
|
|
||||||
raw_input # Python 2
|
|
||||||
except NameError:
|
|
||||||
raw_input = input # Python 3
|
|
||||||
|
|
||||||
|
|
||||||
def solution(n):
|
def solution(n):
|
||||||
"""Returns the difference between the sum of the squares of the first n
|
"""Returns the difference between the sum of the squares of the first n
|
||||||
natural numbers and the square of the sum.
|
natural numbers and the square of the sum.
|
||||||
@ -45,4 +37,4 @@ def solution(n):
|
|||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
print(solution(int(raw_input().strip())))
|
print(solution(int(input().strip())))
|
||||||
|
@ -14,14 +14,6 @@ numbers and the square of the sum is 3025 − 385 = 2640.
|
|||||||
Find the difference between the sum of the squares of the first N natural
|
Find the difference between the sum of the squares of the first N natural
|
||||||
numbers and the square of the sum.
|
numbers and the square of the sum.
|
||||||
"""
|
"""
|
||||||
from __future__ import print_function
|
|
||||||
|
|
||||||
try:
|
|
||||||
raw_input # Python 2
|
|
||||||
except NameError:
|
|
||||||
raw_input = input # Python 3
|
|
||||||
|
|
||||||
|
|
||||||
def solution(n):
|
def solution(n):
|
||||||
"""Returns the difference between the sum of the squares of the first n
|
"""Returns the difference between the sum of the squares of the first n
|
||||||
natural numbers and the square of the sum.
|
natural numbers and the square of the sum.
|
||||||
@ -42,4 +34,4 @@ def solution(n):
|
|||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
print(solution(int(raw_input().strip())))
|
print(solution(int(input().strip())))
|
||||||
|
@ -14,14 +14,8 @@ numbers and the square of the sum is 3025 − 385 = 2640.
|
|||||||
Find the difference between the sum of the squares of the first N natural
|
Find the difference between the sum of the squares of the first N natural
|
||||||
numbers and the square of the sum.
|
numbers and the square of the sum.
|
||||||
"""
|
"""
|
||||||
from __future__ import print_function
|
|
||||||
import math
|
import math
|
||||||
|
|
||||||
try:
|
|
||||||
raw_input # Python 2
|
|
||||||
except NameError:
|
|
||||||
raw_input = input # Python 3
|
|
||||||
|
|
||||||
|
|
||||||
def solution(n):
|
def solution(n):
|
||||||
"""Returns the difference between the sum of the squares of the first n
|
"""Returns the difference between the sum of the squares of the first n
|
||||||
@ -42,4 +36,4 @@ def solution(n):
|
|||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
print(solution(int(raw_input().strip())))
|
print(solution(int(input().strip())))
|
||||||
|
@ -6,14 +6,8 @@ By listing the first six prime numbers:
|
|||||||
|
|
||||||
We can see that the 6th prime is 13. What is the Nth prime number?
|
We can see that the 6th prime is 13. What is the Nth prime number?
|
||||||
"""
|
"""
|
||||||
from __future__ import print_function
|
|
||||||
from math import sqrt
|
from math import sqrt
|
||||||
|
|
||||||
try:
|
|
||||||
raw_input # Python 2
|
|
||||||
except NameError:
|
|
||||||
raw_input = input # Python 3
|
|
||||||
|
|
||||||
|
|
||||||
def isprime(n):
|
def isprime(n):
|
||||||
if n == 2:
|
if n == 2:
|
||||||
@ -58,4 +52,4 @@ def solution(n):
|
|||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
print(solution(int(raw_input().strip())))
|
print(solution(int(input().strip())))
|
||||||
|
@ -6,14 +6,6 @@ By listing the first six prime numbers:
|
|||||||
|
|
||||||
We can see that the 6th prime is 13. What is the Nth prime number?
|
We can see that the 6th prime is 13. What is the Nth prime number?
|
||||||
"""
|
"""
|
||||||
from __future__ import print_function
|
|
||||||
|
|
||||||
try:
|
|
||||||
raw_input # Python 2
|
|
||||||
except NameError:
|
|
||||||
raw_input = input # Python 3
|
|
||||||
|
|
||||||
|
|
||||||
def isprime(number):
|
def isprime(number):
|
||||||
for i in range(2, int(number ** 0.5) + 1):
|
for i in range(2, int(number ** 0.5) + 1):
|
||||||
if number % i == 0:
|
if number % i == 0:
|
||||||
@ -73,4 +65,4 @@ def solution(n):
|
|||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
print(solution(int(raw_input().strip())))
|
print(solution(int(input().strip())))
|
||||||
|
@ -6,15 +6,9 @@ By listing the first six prime numbers:
|
|||||||
|
|
||||||
We can see that the 6th prime is 13. What is the Nth prime number?
|
We can see that the 6th prime is 13. What is the Nth prime number?
|
||||||
"""
|
"""
|
||||||
from __future__ import print_function
|
|
||||||
import math
|
import math
|
||||||
import itertools
|
import itertools
|
||||||
|
|
||||||
try:
|
|
||||||
raw_input # Python 2
|
|
||||||
except NameError:
|
|
||||||
raw_input = input # Python 3
|
|
||||||
|
|
||||||
|
|
||||||
def primeCheck(number):
|
def primeCheck(number):
|
||||||
if number % 2 == 0 and number > 2:
|
if number % 2 == 0 and number > 2:
|
||||||
@ -50,4 +44,4 @@ def solution(n):
|
|||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
print(solution(int(raw_input().strip())))
|
print(solution(int(input().strip())))
|
||||||
|
@ -7,7 +7,6 @@ For example, 3^2 + 4^2 = 9 + 16 = 25 = 5^2.
|
|||||||
There exists exactly one Pythagorean triplet for which a + b + c = 1000.
|
There exists exactly one Pythagorean triplet for which a + b + c = 1000.
|
||||||
Find the product abc.
|
Find the product abc.
|
||||||
"""
|
"""
|
||||||
from __future__ import print_function
|
|
||||||
|
|
||||||
|
|
||||||
def solution():
|
def solution():
|
||||||
|
@ -7,14 +7,6 @@ For example, 3^2 + 4^2 = 9 + 16 = 25 = 5^2.
|
|||||||
There exists exactly one Pythagorean triplet for which a + b + c = 1000.
|
There exists exactly one Pythagorean triplet for which a + b + c = 1000.
|
||||||
Find the product abc.
|
Find the product abc.
|
||||||
"""
|
"""
|
||||||
from __future__ import print_function
|
|
||||||
|
|
||||||
try:
|
|
||||||
raw_input # Python 2
|
|
||||||
except NameError:
|
|
||||||
raw_input = input # Python 3
|
|
||||||
|
|
||||||
|
|
||||||
def solution(n):
|
def solution(n):
|
||||||
"""
|
"""
|
||||||
Return the product of a,b,c which are Pythagorean Triplet that satisfies
|
Return the product of a,b,c which are Pythagorean Triplet that satisfies
|
||||||
@ -41,4 +33,4 @@ def solution(n):
|
|||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
print(solution(int(raw_input().strip())))
|
print(solution(int(input().strip())))
|
||||||
|
@ -10,9 +10,6 @@ For example, 3^2 + 4^2 = 9 + 16 = 25 = 5^2.
|
|||||||
There exists exactly one Pythagorean triplet for which a + b + c = 1000.
|
There exists exactly one Pythagorean triplet for which a + b + c = 1000.
|
||||||
Find the product abc.
|
Find the product abc.
|
||||||
"""
|
"""
|
||||||
from __future__ import print_function
|
|
||||||
|
|
||||||
|
|
||||||
def solution():
|
def solution():
|
||||||
"""
|
"""
|
||||||
Returns the product of a,b,c which are Pythagorean Triplet that satisfies
|
Returns the product of a,b,c which are Pythagorean Triplet that satisfies
|
||||||
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
x
Reference in New Issue
Block a user