Modernize Python 2 code to get ready for Python 3

This commit is contained in:
cclauss
2017-11-25 10:23:50 +01:00
parent a03b2eafc0
commit 4e06949072
95 changed files with 580 additions and 521 deletions

View File

@@ -12,6 +12,11 @@ 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
@@ -137,23 +142,14 @@ def __assert_sorted(collection):
if __name__ == '__main__':
import sys
# 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
user_input = input_function('Enter numbers separated by comma:\n')
user_input = raw_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 sorted to apply binary search')
target_input = input_function(
'Enter a single number to be found in the list:\n'
)
target_input = raw_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

@@ -4,6 +4,11 @@ This is pure python implementation of interpolation search algorithm
from __future__ import print_function
import bisect
try:
raw_input # Python 2
except NameError:
raw_input = input # Python 3
def interpolation_search(sorted_collection, item):
"""Pure implementation of interpolation search algorithm in Python
@@ -77,26 +82,18 @@ def __assert_sorted(collection):
if __name__ == '__main__':
import sys
# 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
user_input = input_function('Enter numbers separated by comma:\n')
user_input = raw_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 sorted to apply interpolation search')
target_input = input_function(
'Enter a single number to be found in the list:\n'
)
target_input = raw_input('Enter a single number to be found in the list:\n')
target = int(target_input)
result = interpolation_search(collection, target)
if result is not None:
print('{} found at positions: {}'.format(target, result))
else:
print('Not found')
print('Not found')

View File

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

View File

@@ -11,6 +11,10 @@ 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
@@ -39,21 +43,10 @@ def linear_search(sequence, target):
if __name__ == '__main__':
import sys
# 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
user_input = input_function('Enter numbers separated by coma:\n')
user_input = raw_input('Enter numbers separated by coma:\n').strip()
sequence = [int(item) for item in user_input.split(',')]
target_input = input_function(
'Enter a single number to be found in the list:\n'
)
target_input = raw_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

@@ -6,9 +6,15 @@ This is a type of divide and conquer algorithm which divides the search space in
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
@@ -81,16 +87,7 @@ def __assert_sorted(collection):
if __name__ == '__main__':
# For python 2.x and 3.x compatibility: 3.x has not 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
user_input = input_function('Enter numbers separated by coma:\n')
user_input = raw_input('Enter numbers separated by coma:\n').strip()
collection = [int(item) for item in user_input.split(',')]
try:
@@ -98,9 +95,7 @@ if __name__ == '__main__':
except ValueError:
sys.exit('Sequence must be sorted to apply the ternary search')
target_input = input_function(
'Enter a single number to be found in the list:\n'
)
target_input = raw_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)