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: