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

@@ -39,15 +39,11 @@ def bogosort(collection):
return collection
if __name__ == '__main__':
import sys
try:
raw_input # Python 2
except NameError:
raw_input = input # Python 3
# 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 a comma:\n')
user_input = raw_input('Enter numbers separated by a comma:\n').stript()
unsorted = [int(item) for item in user_input.split(',')]
print(bogosort(unsorted))

View File

@@ -40,14 +40,11 @@ def bubble_sort(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
try:
raw_input # Python 2
except NameError:
raw_input = input # Python 3
user_input = input_function('Enter numbers separated by a comma:\n')
user_input = raw_input('Enter numbers separated by a comma:\n').strip()
unsorted = [int(item) for item in user_input.split(',')]
print(bubble_sort(unsorted))

View File

@@ -13,6 +13,7 @@
# Time Complexity of Solution:
# Best Case O(n); Average Case O(n); Worst Case O(n)
from __future__ import print_function
from P26_InsertionSort import insertionSort
import math

View File

@@ -21,16 +21,12 @@ def cocktail_shaker_sort(unsorted):
return unsorted
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
try:
raw_input # Python 2
except NameError:
raw_input = input # Python 3
user_input = input_function('Enter numbers separated by a comma:\n')
user_input = raw_input('Enter numbers separated by a comma:\n').strip()
unsorted = [int(item) for item in user_input.split(',')]
cocktail_shaker_sort(unsorted)
print(unsorted)
print(unsorted)

View File

@@ -59,14 +59,11 @@ def counting_sort(collection):
if __name__ == '__main__':
import sys
# 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
try:
raw_input # Python 2
except NameError:
raw_input = input # Python 3
user_input = input_function('Enter numbers separated by a comma:\n')
user_input = raw_input('Enter numbers separated by a comma:\n').strip()
unsorted = [int(item) for item in user_input.split(',')]
print(counting_sort(unsorted))

View File

@@ -1,3 +1,4 @@
from __future__ import print_function
# Python program for counting sort
# This is the main function that sort the given string arr[] in

View File

@@ -158,4 +158,4 @@ def main():
if __name__ == '__main__':
main()
main()

View File

@@ -19,16 +19,12 @@ def gnome_sort(unsorted):
i = 1
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
try:
raw_input # Python 2
except NameError:
raw_input = input # Python 3
user_input = input_function('Enter numbers separated by a comma:\n')
user_input = raw_input('Enter numbers separated by a comma:\n').strip()
unsorted = [int(item) for item in user_input.split(',')]
gnome_sort(unsorted)
print(unsorted)
print(unsorted)

View File

@@ -54,12 +54,11 @@ def heap_sort(unsorted):
return unsorted
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
user_input = input_function('Enter numbers separated by a comma:\n')
user_input = raw_input('Enter numbers separated by a comma:\n').strip()
unsorted = [int(item) for item in user_input.split(',')]
print(heap_sort(unsorted))

View File

@@ -39,15 +39,11 @@ def insertion_sort(collection):
if __name__ == '__main__':
import sys
try:
raw_input # Python 2
except NameError:
raw_input = input # Python 3
# 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 a comma:\n')
user_input = raw_input('Enter numbers separated by a comma:\n').strip()
unsorted = [int(item) for item in user_input.split(',')]
print(insertion_sort(unsorted))

View File

@@ -62,15 +62,11 @@ def merge_sort(collection):
if __name__ == '__main__':
import sys
try:
raw_input # Python 2
except NameError:
raw_input = input # Python 3
# 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 a comma:\n')
user_input = raw_input('Enter numbers separated by a comma:\n').strip()
unsorted = [int(item) for item in user_input.split(',')]
print(merge_sort(unsorted))

View File

@@ -40,15 +40,11 @@ def quick_sort(ARRAY):
if __name__ == '__main__':
import sys
try:
raw_input # Python 2
except NameError:
raw_input = input # Python 3
# 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 a comma:\n')
user_input = raw_input('Enter numbers separated by a comma:\n').strip()
unsorted = [ int(item) for item in user_input.split(',') ]
print( quick_sort(unsorted) )

View File

@@ -1,3 +1,4 @@
from __future__ import print_function
from random import randint
from tempfile import TemporaryFile
import numpy as np

View File

@@ -43,14 +43,11 @@ def selection_sort(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
try:
raw_input # Python 2
except NameError:
raw_input = input # Python 3
user_input = input_function('Enter numbers separated by a comma:\n')
user_input = raw_input('Enter numbers separated by a comma:\n').strip()
unsorted = [int(item) for item in user_input.split(',')]
print(selection_sort(unsorted))

View File

@@ -44,14 +44,11 @@ def shell_sort(collection):
return 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
try:
raw_input # Python 2
except NameError:
raw_input = input # Python 3
user_input = input_function('Enter numbers separated by a comma:\n')
user_input = raw_input('Enter numbers separated by a comma:\n').strip()
unsorted = [int(item) for item in user_input.split(',')]
print(shell_sort(unsorted))

View File

@@ -1,3 +1,4 @@
from __future__ import print_function
def binary_search(lst, item, start, end):
if start == end:
if lst[start] > item:

View File

@@ -1,3 +1,4 @@
from __future__ import print_function
# a
# / \
# b c