Exemplo n.º 1
0
 def __init__(self, capacity=10):
     self.data = Array(capacity)
Exemplo n.º 2
0
    def __init__(self, numRows, numCols):
        self.theRows = Array(numRows)

        for i in range(numRows):
            self.theRows[i] = Array(numCols)
Exemplo n.º 3
0
 def __init__(self, seq=None, capacity=10):
     self._array = Array(seq, capacity)
Exemplo n.º 4
0
class MaxHeap():
    def __init__(self, capacity=10):
        self.data = Array(capacity)

    def getSize(self):
        return self.data.getSize()

    def isEmpty(self):
        return self.data.isEmpty()

    def __parent(self, index):
        return (index - 1) // 2

    def __leftChild(self, index):
        return index * 2 + 1

    def __rightCild(self, index):
        return index * 2 + 2

    def add(self, e):
        self.data.addLast(e)
        self.__SlipUp(self.getSize() - 1, e)

    def __SlipUp(self, k, e):
        while k > 0 and e > self.data.get(self.__parent(k)):
            self.data.swap(k, self.__parent(k))
            k = self.__parent(k)

    def findMax(self):
        assert self.getSize() != 0, "The maxHeap must not be empty"
        return self.data.get(0)

    def extractMax(self):
        re = self.data.get(0)
        # self.data.set(0, self.data.get(self.getSize()-1))
        self.data.swap(0, self.getSize() - 1)
        self.data.removeLast()
        self.__siftDown(0)

        return re

    def __siftDown(self, k):

        while (self.__leftChild(k) < self.getSize()):
            j = self.__leftChild(k)
            if j + 1 < self.getSize() and self.data.get(j +
                                                        1) > self.data.get(j):
                j += 1
            # j 为k 左右孩子中数值较大的孩子
            if self.data.get(j) > self.data.get(k):
                self.data.swap(k, j)
                k = j
            else:
                break

    # 取出堆中最大的元素,并替换成元素e
    def replace(self, e):
        maxValue = self.findMax()
        self.data.set(0, e)
        self.__siftDown(0)
        return maxValue

    # 把一个数组 构造成最大堆
    def heapify(self, arr):
        self.data = Array(arr=arr)
        # 从最后一个不是叶子节点的节点开始下沉,即最后一个叶子节点的父节点
        k = self.__parent(self.getSize() - 1)
        while k >= 0:
            self.__siftDown(k)
            k -= 1

    def __str__(self):
        res = '['
        for i in range(self.getSize()):
            res += str(self.data.get(i))
            if i != self.getSize() - 1:
                res += ', '
        res += ']'
        return res
 def __init__(self, num_rows, num_cols):
     self._num_rows = num_rows
     self._num_cols = num_cols
     self._list_of_array = Array(num_rows)
Exemplo n.º 6
0
from myarray import Array

#===============Test Array==============

array = Array(5)

print("test getitem and set item")
for i in range(len(array)):
    array[i] = i

print("test iteration")
for i in array:
    print(i)
Exemplo n.º 7
0
class MaxHeap():
    def __init__(self, capacity=10):
        self.data = Array(capacity)

    def getSize(self):
        return self.data.getSize()

    def isEmpty(self):
        return self.data.isEmpty()

    # 假设 index 是从0开始的
    def __parent(self, index):
        return (index - 1) // 2

    def __leftChild(self, index):
        return index * 2 + 1

    def __rightCild(self, index):
        return index * 2 + 2

    def add(self, e):
        self.data.add(self.getSize(), e)
        child = self.getSize() - 1
        parent = self.__parent(self.getSize() - 1)
        if parent > 0:
            while e > self.data.get(parent):
                temp = self.data.get(parent)
                self.data.set(parent, e)
                self.data.set(child, temp)
                child = parent
                parent = self.__parent(parent)

    def __str__(self):
        res = '['
        for i in range(self.getSize()):
            res += str(self.data.get(i))
            if i != self.getSize() - 1:
                res += ', '
        res += ']'
        return res
Exemplo n.º 8
0
 def __init__(self, capacity=10):
     self.__myqueue = Array(capacity)
Exemplo n.º 9
0
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from myarray import Array
class Student():
    def __init__(self, name, score):
        self.name = name
        self.score = score

    def __repr__(self):
        return "Student(name: %s, score: %d)" % (self.name, self.score)


student = Student("Alice", 100)
arr = Array(10)
arr.addLast(student)
arr.addLast(student)
print(student.name)
print(arr)
from myarray import Array

theCounters = Array(127)
theCounters.clear(0)

theFile = open('atextfile.txt', 'r')
for line in theFile:
    for letter in line:
        code = ord(letter)
        theCounters[code] += 1

theFile.close()

#Print the results. The uppercase letters have ASCII values in the
# range 65..90 and the lowercase letters are in the range 97..122.
for i in range(26):
    print("%c - %4d     %c - %4d" %
          (chr(65 + i), theCounters[65 + i], chr(97 + i), theCounters[97 + i]))
Exemplo n.º 11
0
 def __init__(self, capacity=10):
     self.__array = Array(capacity)