我正在努力编写一个程序,从一长串输入值中提取最大的K值

2024-09-30 01:36:14 发布

您现在位置:Python中文网/ 问答频道 /正文

救命啊!我正在努力编写一个程序,从一长串输入值中提取最大的K值。单个参数是一个整数,指定从输入数据中提取多少值。 调用这个值K。然后你的程序将从标准中读取任意数量的输入数据项 输入ubtil直到文件结束。一旦到达文件末尾,程序将打印出 它从输入数据元素捕获的最大K值。 请记住,您的程序可能会获得非常大量的数据,因此它将不实用 使您的程序尝试保留所有输入数据,直到到达文件结尾。你知道吗

class MinHeap:

def _init_(self):
    self.list = [ ]

def swap(self, i, j):
    temp = self.list[i]
    self.list[i] = self.list[j]
    self.list[j] = temp

def siftUp(self, pos):
    i = pos;
    while i > 0:
        parent = (i - 1) // 2
        if self.list[parent] > self.list[i]:
            self.swap(parent, i)
        else:
            break
            i = parent
    return self

def siftDown(self, lo, hi):
    i = lo
    child = 2*i + 1
    while child <= hi:
        rchild = child + 1
        if rchild <= hi and self.list[child] > self.list[rchild]:
            child = rchild
        if self.list[i] > self.list[child]:
            self.swap(i, child)
        else:
            break
            i = child
            child = 2*i +1
        return self

def getMin(self):
    if len(self.list) <= 0:
        return None
    else:
        return self.list[0]

def removeMin(self):
    if len(self.list) <= 0:
        return None
    else:
        self.swap(0, -1)

Tags: 文件数据self程序childreturnifdef

热门问题