mirror of
https://github.com/TheAlgorithms/Python.git
synced 2026-03-13 09:50:19 +08:00
Modernize Python 2 code to get ready for Python 3
This commit is contained in:
@@ -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))
|
||||
|
||||
@@ -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))
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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))
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -158,4 +158,4 @@ def main():
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
main()
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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))
|
||||
|
||||
@@ -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))
|
||||
|
||||
@@ -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))
|
||||
|
||||
@@ -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) )
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
from __future__ import print_function
|
||||
from random import randint
|
||||
from tempfile import TemporaryFile
|
||||
import numpy as np
|
||||
|
||||
@@ -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))
|
||||
|
||||
@@ -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))
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
from __future__ import print_function
|
||||
def binary_search(lst, item, start, end):
|
||||
if start == end:
|
||||
if lst[start] > item:
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
from __future__ import print_function
|
||||
# a
|
||||
# / \
|
||||
# b c
|
||||
|
||||
Reference in New Issue
Block a user