I'm doing the merge sort in python but I have a problem. When I try to insert the list from the console (one number per line which return a list of string) I cannot convert it in integers. Can you help me understanding the problem.
import sys
def mergeSort(lista):
res = []
for i in lista[0].split():
res.append(int(i))
if len(res)>1:
mid = len(res)//2
lefthalf = res[:mid]
righthalf = res[mid:]
mergeSort(lefthalf)
mergeSort(righthalf)
i=0
j=0
k=0
while i<len(lefthalf) and j<len(righthalf):
if lefthalf[i]<righthalf[j]:
res[k]=lefthalf[i]
i=i+1
else:
res[k]=righthalf[j]
j=j+1
k=k+1
while i<len(lefthalf):
res[k]=lefthalf[i]
i=i+1
k=k+1
while j<len(righthalf):
res[k]=righthalf[j]
j=j+1
k=k+1
print(res)
alist = []
for l in sys.stdin:
alist.append(l.strip())
mergeSort(alist)
The code error says: AttributeError: 'int' object has no attribute 'split' The input is a file (given from the shell with the command: "python3 merge.py < data.txt") with the list of numbers one per line. Example: 2 3 0 12 11 7 4 Should return 0 2 3 4 7 11 12 Of course I don't have an output because of the error

listais anintobject, not a string. Somehow your code is is appending an integer to the list you're passing in. So this can't be a complete code example...str.split()is on an element of a list that ostensibly contains strings. You're probably not looking at the same code you're running, or you didn't post the same code you're running here.mergeSortrecursively you're passing in a list of integers rather than a list of strings. You should handle the conversion from strings to integers outside themergeSortroutine.