Simplify code by dropping support for legacy Python (#1143)

* Simplify code by dropping support for legacy Python

* sort() --> sorted()
This commit is contained in:
Christian Clauss
2019-08-19 15:37:49 +02:00
committed by GitHub
parent 32aa7ff081
commit 47a9ea2b0b
145 changed files with 367 additions and 976 deletions

View File

@@ -9,14 +9,8 @@ python3 -m doctest -v binary_search.py
For manual testing run:
python binary_search.py
"""
from __future__ import print_function
import bisect
try:
raw_input # Python 2
except NameError:
raw_input = input # Python 3
def binary_search(sorted_collection, item):
"""Pure implementation of binary search algorithm in Python
@@ -112,7 +106,7 @@ def binary_search_by_recursion(sorted_collection, item, left, right):
"""
if (right < left):
return None
midpoint = left + (right - left) // 2
if sorted_collection[midpoint] == item:
@@ -121,7 +115,7 @@ def binary_search_by_recursion(sorted_collection, item, left, right):
return binary_search_by_recursion(sorted_collection, item, left, midpoint-1)
else:
return binary_search_by_recursion(sorted_collection, item, midpoint+1, right)
def __assert_sorted(collection):
"""Check if collection is ascending sorted, if not - raises :py:class:`ValueError`
@@ -145,14 +139,14 @@ def __assert_sorted(collection):
if __name__ == '__main__':
import sys
user_input = raw_input('Enter numbers separated by comma:\n').strip()
user_input = input('Enter numbers separated by comma:\n').strip()
collection = [int(item) for item in user_input.split(',')]
try:
__assert_sorted(collection)
except ValueError:
sys.exit('Sequence must be ascending sorted to apply binary search')
target_input = raw_input('Enter a single number to be found in the list:\n')
target_input = input('Enter a single number to be found in the list:\n')
target = int(target_input)
result = binary_search(collection, target)
if result is not None:

View File

@@ -1,12 +1,6 @@
"""
This is pure python implementation of interpolation search algorithm
"""
from __future__ import print_function
try:
raw_input # Python 2
except NameError:
raw_input = input # Python 3
def interpolation_search(sorted_collection, item):
@@ -29,7 +23,7 @@ def interpolation_search(sorted_collection, item):
return None
point = left + ((item - sorted_collection[left]) * (right - left)) // (sorted_collection[right] - sorted_collection[left])
#out of range check
if point<0 or point>=len(sorted_collection):
return None
@@ -42,9 +36,9 @@ def interpolation_search(sorted_collection, item):
right = left
left = point
elif point>right:
left = right
left = right
right = point
else:
else:
if item < current_item:
right = point - 1
else:
@@ -70,7 +64,7 @@ def interpolation_search_by_recursion(sorted_collection, item, left, right):
return None
point = left + ((item - sorted_collection[left]) * (right - left)) // (sorted_collection[right] - sorted_collection[left])
#out of range check
if point<0 or point>=len(sorted_collection):
return None
@@ -86,7 +80,7 @@ def interpolation_search_by_recursion(sorted_collection, item, left, right):
return interpolation_search_by_recursion(sorted_collection, item, left, point-1)
else:
return interpolation_search_by_recursion(sorted_collection, item, point+1, right)
def __assert_sorted(collection):
"""Check if collection is ascending sorted, if not - raises :py:class:`ValueError`
:param collection: collection
@@ -107,16 +101,16 @@ def __assert_sorted(collection):
if __name__ == '__main__':
import sys
"""
user_input = raw_input('Enter numbers separated by comma:\n').strip()
user_input = input('Enter numbers separated by comma:\n').strip()
collection = [int(item) for item in user_input.split(',')]
try:
__assert_sorted(collection)
except ValueError:
sys.exit('Sequence must be ascending sorted to apply interpolation search')
target_input = raw_input('Enter a single number to be found in the list:\n')
target_input = input('Enter a single number to be found in the list:\n')
target = int(target_input)
"""
@@ -128,7 +122,7 @@ if __name__ == '__main__':
except ValueError:
sys.exit('Sequence must be ascending sorted to apply interpolation search')
target = 67
result = interpolation_search(collection, target)
if result is not None:
print('{} found at positions: {}'.format(target, result))

View File

@@ -1,4 +1,3 @@
from __future__ import print_function
import math
def jump_search(arr, x):
n = len(arr)

View File

@@ -9,12 +9,7 @@ python3 -m doctest -v linear_search.py
For manual testing run:
python linear_search.py
"""
from __future__ import print_function
try:
raw_input # Python 2
except NameError:
raw_input = input # Python 3
def linear_search(sequence, target):
"""Pure implementation of linear search algorithm in Python
@@ -43,10 +38,10 @@ def linear_search(sequence, target):
if __name__ == '__main__':
user_input = raw_input('Enter numbers separated by comma:\n').strip()
user_input = input('Enter numbers separated by comma:\n').strip()
sequence = [int(item) for item in user_input.split(',')]
target_input = raw_input('Enter a single number to be found in the list:\n')
target_input = input('Enter a single number to be found in the list:\n')
target = int(target_input)
result = linear_search(sequence, target)
if result is not None:

View File

@@ -45,15 +45,10 @@ def sentinel_linear_search(sequence, target):
if __name__ == '__main__':
try:
raw_input # Python 2
except NameError:
raw_input = input # Python 3
user_input = raw_input('Enter numbers separated by comma:\n').strip()
user_input = input('Enter numbers separated by comma:\n').strip()
sequence = [int(item) for item in user_input.split(',')]
target_input = raw_input('Enter a single number to be found in the list:\n')
target_input = input('Enter a single number to be found in the list:\n')
target = int(target_input)
result = sentinel_linear_search(sequence, target)
if result is not None:

View File

@@ -1,20 +1,13 @@
'''
This is a type of divide and conquer algorithm which divides the search space into
3 parts and finds the target value based on the property of the array or list
3 parts and finds the target value based on the property of the array or list
(usually monotonic property).
Time Complexity : O(log3 N)
Space Complexity : O(1)
'''
from __future__ import print_function
import sys
try:
raw_input # Python 2
except NameError:
raw_input = input # Python 3
# This is the precision for this function which can be altered.
# It is recommended for users to keep this number greater than or equal to 10.
precision = 10
@@ -31,23 +24,23 @@ def ite_ternary_search(A, target):
right = len(A) - 1;
while(True):
if(left<right):
if(right-left < precision):
return lin_search(left,right,A,target)
oneThird = (left+right)/3+1;
twoThird = 2*(left+right)/3+1;
if(A[oneThird] == target):
return oneThird
elif(A[twoThird] == target):
return twoThird
elif(target < A[oneThird]):
right = oneThird-1
elif(A[twoThird] < target):
left = twoThird+1
else:
left = oneThird+1
right = twoThird-1
@@ -57,7 +50,7 @@ def ite_ternary_search(A, target):
# This is the recursive method of the ternary search algorithm.
def rec_ternary_search(left, right, A, target):
if(left<right):
if(right-left < precision):
return lin_search(left,right,A,target)
@@ -68,12 +61,12 @@ def rec_ternary_search(left, right, A, target):
return oneThird
elif(A[twoThird] == target):
return twoThird
elif(target < A[oneThird]):
return rec_ternary_search(left, oneThird-1, A, target)
elif(A[twoThird] < target):
return rec_ternary_search(twoThird+1, right, A, target)
else:
return rec_ternary_search(oneThird+1, twoThird-1, A, target)
else:
@@ -87,7 +80,7 @@ def __assert_sorted(collection):
if __name__ == '__main__':
user_input = raw_input('Enter numbers separated by coma:\n').strip()
user_input = input('Enter numbers separated by coma:\n').strip()
collection = [int(item) for item in user_input.split(',')]
try:
@@ -95,11 +88,11 @@ if __name__ == '__main__':
except ValueError:
sys.exit('Sequence must be sorted to apply the ternary search')
target_input = raw_input('Enter a single number to be found in the list:\n')
target_input = input('Enter a single number to be found in the list:\n')
target = int(target_input)
result1 = ite_ternary_search(collection, target)
result2 = rec_ternary_search(0, len(collection)-1, collection, target)
if result2 is not None:
print('Iterative search: {} found at positions: {}'.format(target, result1))
print('Recursive search: {} found at positions: {}'.format(target, result2))