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