jeudi 5 mars 2015

__init__ not initializing array


Vote count:

0




So I'm trying to initialize an array inside the constructor, however it doesn't seem to actually be working as when the program is run, nothing is output.





class Merge():
def __init__(self):
self.result = []

def merge(self, left, right):
left = left
right = right
i, j = 0, 0

while i < len(left) and j < len(right):
if left[i] <= right[j]:
self.result.append(left[i])
i += 1
else:
self.result.append(right[j])
j += 1

if left:
self.result.extend(left[i:])
if right:
self.result.extend(right[j:])

return self.result

def mergesort(self, a):
if len(a) < 2:
return a

middle = len(a) / 2
left = self.mergesort(a[:middle])
right = self.mergesort(a[middle:])

return self.merge(left, right)

m = Merge()
msort = [8,5,3,1,9,6,0,7,4,2,5]
print m.mergesort(msort)




asked 1 min ago







__init__ not initializing array

Aucun commentaire:

Enregistrer un commentaire