2015-02-11 43 views
-4

當我運行這個時,什麼也沒有顯示,甚至沒有錯誤信息。所以,我想知道什麼是錯的這個問題以及如何正確地調用這些功能。(我猜的東西錯了,當我把這個功能)如何在python中調用這個函數

class BinHeap: 
    def makeheap(self): 
      self.heapList=[0]; 
      self.currentSize=0; 

    def perUp(self,i): 
      while i // 2 > 0: 
        if self.heapList[i]<self.heapList[i//2]: 
          tmp=self.heapList[i//2] 
          self.heapList[i//2]=self.heapList[i] 
          self.heapList[i]=tmp 
        i = i // 2 

    def insert(self,k): 
      self.heapList.append(k) 
      self.currentSize = self.currentSize+1 
      self.perUp(self.currentSize) 

    def percDown(self,i): 
      while (i * 2) <= self.currentSize: 
        mc = self.minChild(i) 
      if self.heapList[i] > self.heapList[mc]: 
        tmp = self.heapList[i] 
      self.heapList[i] = self.heapList[mc] 
      self.heapList[mc] = tmp 
      i = mc 

    def minChild(self,i): 
      if i * 2 + 1 > self.currentSize: 
        return i * 2 
      else: 
         if self.heapList[i*2] < self.heapList[i*2+1]: 
          return i * 2 
        else: 
          return i * 2 + 1 
    def delMin(self): 
      retval = self.heapList[1] 
      self.heapList[1] = self.heapList[self.currentSize] 
      self.currentSize = self.currentSize - 1 
      self.heapList.pop() 
      self.percDown(1) 
      return retval 

    def buildHeap(self,alist): 
      i = len(alist) // 2 
      self.currentSize = len(alist) 
      self.heapList = [0] + alist[:] 
      while (i > 0): 
        self.percDown(i) 
        i = i - 1 

bh=BinHeap() 
bh.buildHeap([9,5,6,2,3]) 
print("BinHeap") 
print(bh.delMin()) 
print(bh.delMin()) 
print(bh.delMin()) 
print(bh.delMin()) 
print(bh.delMin()) 
+3

Python代碼塊可用於縮進;如果這是正確的縮進,那麼您創建類爲'bh = BinHeap()'和後綴的對象的代碼將縮進爲類的一部分。移出來,你應該開始看到一些執行。 – 2015-02-11 04:57:46

回答

0

Python是非常講究的間距。你弄亂了空格/格式。編輯你的代碼,讓最後幾行在它前面沒有空格/標籤,至少應該得到一兩個錯誤。

# This is a subroutine to be executed when called 
def buildHeap(self, alist): 
    i = len(alist) // 2 
    self.currentSize = len(alist) 
    self.heapList = [0] + alist[:] 
    while (i > 0): 
     self.percDown(i) 
     i = i - 1  

# these are commands to be executed now 
bh = BinHeap() 
bh.buildHeap([9,5,6,2,3]) 
print(bh.delMin()) 
print(bh.delMin()) 
print(bh.delMin()) 
print(bh.delMin()) 
print(bh.delMin()) 
+0

我知道格式應該如何,因爲StackOverflow一直要求我根據需要調整格式,或者我無法提交此問題。 – christ 2015-02-11 05:19:28

+0

然後我認爲我們不能幫助你。您呈現的代碼無論格式如何,都會顯示至少一些錯誤。回到'print(「hello world」)'並繼續前進。 – 2015-02-11 05:55:36

0

編輯:在您的代碼中沒有巨大的縮進問題。 「什麼也沒有顯示,甚至沒有錯誤信息」..這是肯定的!你的代碼在buildHeap函數中的while循環中被阻塞了!它將出現在percDown功能這樣做:

while (i * 2) <= self.currentSize: 
     mc = self.minChild(i) 

,然後讓術語,「環回」到:

def minChild(self,i): 
     if i * 2 + 1 > self.currentSize: 

       return i * 2 

     else: 
        if self.heapList[i*2] < self.heapList[i*2+1]: 

         return i * 2 
        else: 
         return i * 2 + 1 

它開始這一次又一次的沒有做,告訴你在什麼貝殼。 很顯然我會給你一些建議'回合它:

1)在你的代碼行之間使用註釋來澄清你在做什麼。

a = b + C#It's stupid but "a" is equal to "b" plus "c" 

2)打印功能是你的朋友!它可以幫助你當貝殼「不說話」。 我爲您提供打印語句代碼的一些作品,只是看你如何使用它:

def buildHeap(self,alist): 
     i = len(alist) // 2 
     self.currentSize = len(alist) 
     self.heapList = [0] + alist[:] 
     while (i > 0): 
       print 'something wrong ..' 
       self.percDown(i) 
       print 'ok' 
       i = i - 1 
def percDown(self,i): 
     while (i * 2) <= self.currentSize: 
       print 'we re in the perc_Down' 
       mc = self.minChild(i) 
       print 'end of min child min child' 
     if self.heapList[i] > self.heapList[mc]: 
       tmp = self.heapList[i] 
     self.heapList[i] = self.heapList[mc] 
     self.heapList[mc] = tmp 
     i = mc 

def minChild(self,i): 
     if i * 2 + 1 > self.currentSize: 
       print 'if my min child' 
       return i * 2 
       print 'maybe stuck here ..' 
     else: 
        if self.heapList[i*2] < self.heapList[i*2+1]: 
         print '*' 
         return i * 2 
        else: 
         return i * 2 + 1 
         print '*' 

3)請不要做「如果」像你這樣的語句在minChild功能做: 只要嘗試將聲明包含在第一個if!像:

#this is your organization 
if ... :    #1) if 
    #doing stuff 
else:     #1) else 
    if ... :       #2) if 
     #stuff again 
    else:       #2) else 
     .... 
#try this one 
if ... and ... :  #1) if .. and .. 2) if 

需要再次有所不同嗎?那麼你可以做elif聲明:

if x < y: 
    print .... 
elif x > y: 
    print .... 
else: 
    print .... 

用這種方法解決方案更加優雅和可讀。 還有一件事: 在Python中執行「函數」類時,構建您的「init」。每個對象都將具有構造函數中描述的屬性,如:

class BinHeap: 
    def __init__(self): 
     self.currentSize = 0 
     self.heapList = [] 
相關問題