mirror of
https://github.com/TheAlgorithms/Python.git
synced 2025-07-06 10:31:29 +08:00
Modernize Python 2 code to get ready for Python 3
This commit is contained in:
@ -5,6 +5,7 @@ Can you determine number of ways of making change for n units using
|
||||
the given types of coins?
|
||||
https://www.hackerrank.com/challenges/coin-change/problem
|
||||
"""
|
||||
from __future__ import print_function
|
||||
def dp_count(S, m, n):
|
||||
table = [0] * (n + 1)
|
||||
|
||||
@ -21,5 +22,5 @@ def dp_count(S, m, n):
|
||||
return table[n]
|
||||
|
||||
if __name__ == '__main__':
|
||||
print dp_count([1, 2, 3], 3, 4) # answer 4
|
||||
print dp_count([2, 5, 3, 6], 4, 10) # answer 5
|
||||
print(dp_count([1, 2, 3], 3, 4)) # answer 4
|
||||
print(dp_count([2, 5, 3, 6], 4, 10)) # answer 5
|
||||
|
@ -51,11 +51,10 @@ class EditDistance:
|
||||
return self.__solveDP(len(A)-1, len(B)-1)
|
||||
|
||||
if __name__ == '__main__':
|
||||
import sys
|
||||
if sys.version_info.major < 3:
|
||||
input_function = raw_input
|
||||
else:
|
||||
input_function = input
|
||||
try:
|
||||
raw_input # Python 2
|
||||
except NameError:
|
||||
raw_input = input # Python 3
|
||||
|
||||
solver = EditDistance()
|
||||
|
||||
@ -63,10 +62,10 @@ if __name__ == '__main__':
|
||||
print()
|
||||
|
||||
print("Enter the first string: ", end="")
|
||||
S1 = input_function()
|
||||
S1 = raw_input().strip()
|
||||
|
||||
print("Enter the second string: ", end="")
|
||||
S2 = input_function()
|
||||
S2 = raw_input().strip()
|
||||
|
||||
print()
|
||||
print("The minimum Edit Distance is: %d" % (solver.solve(S1, S2)))
|
||||
|
@ -2,6 +2,7 @@
|
||||
This program calculates the nth Fibonacci number in O(log(n)).
|
||||
It's possible to calculate F(1000000) in less than a second.
|
||||
"""
|
||||
from __future__ import print_function
|
||||
import sys
|
||||
|
||||
|
||||
|
@ -27,26 +27,22 @@ class Fibonacci:
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
import sys
|
||||
|
||||
print("\n********* Fibonacci Series Using Dynamic Programming ************\n")
|
||||
# For python 2.x and 3.x compatibility: 3.x has no raw_input builtin
|
||||
# otherwise 2.x's input builtin function is too "smart"
|
||||
if sys.version_info.major < 3:
|
||||
input_function = raw_input
|
||||
else:
|
||||
input_function = input
|
||||
try:
|
||||
raw_input # Python 2
|
||||
except NameError:
|
||||
raw_input = input # Python 3
|
||||
|
||||
print("\n Enter the upper limit for the fibonacci sequence: ", end="")
|
||||
try:
|
||||
N = eval(input())
|
||||
N = eval(raw_input().strip())
|
||||
fib = Fibonacci(N)
|
||||
print(
|
||||
"\n********* Enter different values to get the corresponding fibonacci sequence, enter any negative number to exit. ************\n")
|
||||
while True:
|
||||
print("Enter value: ", end=" ")
|
||||
try:
|
||||
i = eval(input())
|
||||
i = eval(raw_input().strip())
|
||||
if i < 0:
|
||||
print("\n********* Good Bye!! ************\n")
|
||||
break
|
||||
|
@ -3,6 +3,13 @@ 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 continious.
|
||||
Example:"abc", "abg" are subsequences of "abcdefgh".
|
||||
"""
|
||||
from __future__ import print_function
|
||||
|
||||
try:
|
||||
xrange # Python 2
|
||||
except NameError:
|
||||
xrange = range # Python 3
|
||||
|
||||
def lcs_dp(x, y):
|
||||
# find the length of strings
|
||||
m = len(x)
|
||||
@ -27,4 +34,4 @@ def lcs_dp(x, y):
|
||||
if __name__=='__main__':
|
||||
x = 'AGGTAB'
|
||||
y = 'GXTXAYB'
|
||||
print lcs_dp(x, y)
|
||||
print(lcs_dp(x, y))
|
||||
|
@ -7,6 +7,7 @@ The problem is :
|
||||
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
|
||||
'''
|
||||
from __future__ import print_function
|
||||
|
||||
def longestSub(ARRAY): #This function is recursive
|
||||
|
||||
|
@ -1,3 +1,4 @@
|
||||
from __future__ import print_function
|
||||
#############################
|
||||
# Author: Aravind Kashyap
|
||||
# File: lis.py
|
||||
@ -37,4 +38,4 @@ def LongestIncreasingSubsequenceLength(v):
|
||||
|
||||
|
||||
v = [2, 5, 3, 7, 11, 8, 10, 13, 6]
|
||||
print LongestIncreasingSubsequenceLength(v)
|
||||
print(LongestIncreasingSubsequenceLength(v))
|
||||
|
@ -6,6 +6,7 @@ This is a pure Python implementation of Dynamic Programming solution to the long
|
||||
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.
|
||||
'''
|
||||
from __future__ import print_function
|
||||
|
||||
|
||||
class SubArray:
|
||||
@ -13,7 +14,7 @@ class SubArray:
|
||||
def __init__(self, arr):
|
||||
# we need a list not a string, so do something to change the type
|
||||
self.array = arr.split(',')
|
||||
print("the input array is:", self.array)
|
||||
print(("the input array is:", self.array))
|
||||
|
||||
def solve_sub_array(self):
|
||||
rear = [int(self.array[0])]*len(self.array)
|
||||
@ -28,5 +29,5 @@ if __name__ == '__main__':
|
||||
whole_array = input("please input some numbers:")
|
||||
array = SubArray(whole_array)
|
||||
re = array.solve_sub_array()
|
||||
print("the results is:", re)
|
||||
print(("the results is:", re))
|
||||
|
||||
|
@ -1,59 +1,60 @@
|
||||
"""
|
||||
author : Mayank Kumar Jha (mk9440)
|
||||
"""
|
||||
|
||||
import time
|
||||
import matplotlib.pyplot as plt
|
||||
from random import randint
|
||||
def find_max_sub_array(A,low,high):
|
||||
if low==high:
|
||||
return low,high,A[low]
|
||||
else :
|
||||
mid=(low+high)//2
|
||||
left_low,left_high,left_sum=find_max_sub_array(A,low,mid)
|
||||
right_low,right_high,right_sum=find_max_sub_array(A,mid+1,high)
|
||||
cross_left,cross_right,cross_sum=find_max_cross_sum(A,low,mid,high)
|
||||
if left_sum>=right_sum and left_sum>=cross_sum:
|
||||
return left_low,left_high,left_sum
|
||||
elif right_sum>=left_sum and right_sum>=cross_sum :
|
||||
return right_low,right_high,right_sum
|
||||
else:
|
||||
return cross_left,cross_right,cross_sum
|
||||
|
||||
def find_max_cross_sum(A,low,mid,high):
|
||||
left_sum,max_left=-999999999,-1
|
||||
right_sum,max_right=-999999999,-1
|
||||
summ=0
|
||||
for i in range(mid,low-1,-1):
|
||||
summ+=A[i]
|
||||
if summ > left_sum:
|
||||
left_sum=summ
|
||||
max_left=i
|
||||
summ=0
|
||||
for i in range(mid+1,high+1):
|
||||
summ+=A[i]
|
||||
if summ > right_sum:
|
||||
right_sum=summ
|
||||
max_right=i
|
||||
return max_left,max_right,(left_sum+right_sum)
|
||||
|
||||
|
||||
if __name__=='__main__':
|
||||
inputs=[10,100,1000,10000,50000,100000,200000,300000,400000,500000]
|
||||
tim=[]
|
||||
for i in inputs:
|
||||
li=[randint(1,i) for j in range(i)]
|
||||
strt=time.time()
|
||||
(find_max_sub_array(li,0,len(li)-1))
|
||||
end=time.time()
|
||||
tim.append(end-strt)
|
||||
print("No of Inputs Time Taken")
|
||||
for i in range(len(inputs)):
|
||||
print(inputs[i],'\t\t',tim[i])
|
||||
plt.plot(inputs,tim)
|
||||
plt.xlabel("Number of Inputs");plt.ylabel("Time taken in seconds ")
|
||||
plt.show()
|
||||
|
||||
|
||||
|
||||
|
||||
"""
|
||||
author : Mayank Kumar Jha (mk9440)
|
||||
"""
|
||||
from __future__ import print_function
|
||||
|
||||
import time
|
||||
import matplotlib.pyplot as plt
|
||||
from random import randint
|
||||
def find_max_sub_array(A,low,high):
|
||||
if low==high:
|
||||
return low,high,A[low]
|
||||
else :
|
||||
mid=(low+high)//2
|
||||
left_low,left_high,left_sum=find_max_sub_array(A,low,mid)
|
||||
right_low,right_high,right_sum=find_max_sub_array(A,mid+1,high)
|
||||
cross_left,cross_right,cross_sum=find_max_cross_sum(A,low,mid,high)
|
||||
if left_sum>=right_sum and left_sum>=cross_sum:
|
||||
return left_low,left_high,left_sum
|
||||
elif right_sum>=left_sum and right_sum>=cross_sum :
|
||||
return right_low,right_high,right_sum
|
||||
else:
|
||||
return cross_left,cross_right,cross_sum
|
||||
|
||||
def find_max_cross_sum(A,low,mid,high):
|
||||
left_sum,max_left=-999999999,-1
|
||||
right_sum,max_right=-999999999,-1
|
||||
summ=0
|
||||
for i in range(mid,low-1,-1):
|
||||
summ+=A[i]
|
||||
if summ > left_sum:
|
||||
left_sum=summ
|
||||
max_left=i
|
||||
summ=0
|
||||
for i in range(mid+1,high+1):
|
||||
summ+=A[i]
|
||||
if summ > right_sum:
|
||||
right_sum=summ
|
||||
max_right=i
|
||||
return max_left,max_right,(left_sum+right_sum)
|
||||
|
||||
|
||||
if __name__=='__main__':
|
||||
inputs=[10,100,1000,10000,50000,100000,200000,300000,400000,500000]
|
||||
tim=[]
|
||||
for i in inputs:
|
||||
li=[randint(1,i) for j in range(i)]
|
||||
strt=time.time()
|
||||
(find_max_sub_array(li,0,len(li)-1))
|
||||
end=time.time()
|
||||
tim.append(end-strt)
|
||||
print("No of Inputs Time Taken")
|
||||
for i in range(len(inputs)):
|
||||
print((inputs[i],'\t\t',tim[i]))
|
||||
plt.plot(inputs,tim)
|
||||
plt.xlabel("Number of Inputs");plt.ylabel("Time taken in seconds ")
|
||||
plt.show()
|
||||
|
||||
|
||||
|
||||
|
||||
|
Reference in New Issue
Block a user