Beispiel #1
0
    def __init__(self, chars):
        '''
        Initializes the KanaSet object.
        :param chars: list of KanaChar objects
        '''
        self.kana = ArrayExpanded(len(chars))

        for i in range(len(chars)):
            self.kana.insert(i, chars[i])
Beispiel #2
0
    def __init__(self, kanjis=[]):
        '''
        (object, list) -> None
        Initialization.
        '''
        self.theme = None
        self.byRadical = None
        self.size = len(kanjis)

        self._kanjis = ArrayExpanded(len(kanjis))
        for i in range(len(kanjis)):
            self._kanjis[i] = kanjis[i]
Beispiel #3
0
 def __init__(self, sourceCollection=None):
     """Sets the initial state of self, which includes the
     contents of sourceCollection, if it's present."""
     self._items = ArrayExpanded(ArrayStack.DEFAULT_CAPACITY)
     AbstractStack.__init__(self, sourceCollection)
Beispiel #4
0
class ArrayStack(AbstractStack):
    """An array-based stack implementation."""

    # Class variable
    DEFAULT_CAPACITY = 10

    # Constructor
    def __init__(self, sourceCollection=None):
        """Sets the initial state of self, which includes the
        contents of sourceCollection, if it's present."""
        #self._items = Array(ArrayStack.DEFAULT_CAPACITY)
        self._items = ArrayExpanded(ArrayStack.DEFAULT_CAPACITY)
        AbstractStack.__init__(self, sourceCollection)

    # Accessor methods
    def __iter__(self):
        """Supports iteration over a view of self.
        Visits items from bottom to top of stack."""
        cursor = 0
        while cursor < len(self):
            yield self._items[cursor]
            cursor += 1

    def peek(self):
        """Returns the item at the top of the stack.
        Precondition: the stack is not empty.
        Raises: KeyError if stack is empty."""
        if self.isEmpty():
            raise KeyError("The stack is empty")
        return self._items[len(self) - 1]

    # Mutator methods
    def clear(self):
        """Makes self become empty."""
        self._size = 0
        self._items = ArrayExpanded(ArrayStack.DEFAULT_CAPACITY)

    def push(self, item):
        """Inserts item at top of the stack."""
        # Resize array here if necessary
        #print(type(self), self._size)
        if len(self) == len(self._items):
            #print("grow")
            self._items.grow()
            #temp = Array(2 * len(self))
            #for i in range(len(self)):
            #    temp[i] = self._items[i]
            #self._items = temp
        self._items[len(self)] = item
        self._size += 1

    def pop(self):
        """Removes and returns the item at the top of the stack.
        Precondition: the stack is not empty.
        Raises: KeyError if stack is empty.
        Postcondition: the top item is removed from the stack."""
        if self.isEmpty():
            raise KeyError("The stack is empty")
        oldItem = self._items[len(self) - 1]
        self._size -= 1
        # Resize the array here if necessary
        if len(self) <= len(self._items) // 4 and \
           len(self._items) >= 2 * ArrayStack.DEFAULT_CAPACITY:
            print("shrink")
            #temp = Array(len(self._items) // 2)
            #for i in range(len(self)):
            #    temp [i] = self._items[i]
            #self._items = temp
            self._items.shrink()
        return oldItem
Beispiel #5
0
 def clear(self):
     """Makes self become empty."""
     self._size = 0
     self._items = ArrayExpanded(ArrayStack.DEFAULT_CAPACITY)
Beispiel #6
0
class KanaSet:
    '''Data structure based on ArrayExpanded data structure.
    Is used for work with KanaChar objects and their storage'''
    def __init__(self, chars):
        '''
        Initializes the KanaSet object.
        :param chars: list of KanaChar objects
        '''
        self.kana = ArrayExpanded(len(chars))

        for i in range(len(chars)):
            self.kana.insert(i, chars[i])


    def sameConsonant(self, cons):
        '''
        Returns all KanaChars with corresponding consonant.
        :param cons: str
        :return: list
        '''
        result = list()

        for i in range(len(self)):
            excon = self.kana[i].consonant()
            if self.kana[i].sound == 'ji':
                excon = 'd'
            elif self.kana[i].sound == 'chi':
                excon = 't'
            elif self.kana[i].sound == 'tsu':
                excon = 't'
            elif self.kana[i].sound == 'fu':
                excon = 'h'
            elif self.kana[i].sound == 'shi':
                excon = 's'
            if excon == cons:
                result.append(str(self.kana[i]))

        return result

    def sameVowel(self, vow):
        '''
        Returns all KanaChars with corresponding vowel.
        :param vow: str
        :return: list
        '''
        result = list()

        for i in range(len(self)):
            if self.kana[i].vowel() == vow:
                result.append(str(self.kana[i]))

        return result

    def charBySound(self, snd):
        '''
        Returns a KanaChar with corresponding sound value.
        :param sound: str
        :return:KanaChar
        '''
        for i in range(self.itemCount()):
            if self.kana[i].sound == snd:
                return self.kana[i]

    def soundByChar(self, chr):
        '''
        Returns a sound value of KanaChar with corresponding character value.
        :param chr: str
        :return: str
        '''
        for i in range(self.itemCount()):
            if self.kana[i].char == chr:
                return self.kana[i].sound

    def getRandom(self):
        '''
        Returns a random character from the KanaSet.
        :return: KanaChar
        '''
        indx = random.randint(0, self.itemCount()-1)
        return self.kana[indx]


    def itemCount(self):
        '''
        Returns the amount of items, not the actual length.
        :return: int
        '''
        return self.kana.size()

    def __len__(self):
        '''
        Magic method for len() operation.
        :return: int
        '''
        return len(self.kana)

    def __getitem__(self, index):
        '''
        Subscript operator for access at index.
        Precondition: 0 <= index < size()
        '''
        return self.kana[index]

    def __str__(self):
        '''
        Represents object as str.
        :return: str
        '''
        res = '<<'

        for i in range(self.itemCount()):
            res += "'" + str(self.kana[i]) + "', "

        res = res[:-2]
        res += '>>'
        return res

    def __add__(self, other):
        '''
        Magic method for '+' operator.

        Adds two KanaSet objects by creating
        a new KanaSet with the elements
        of the first KanaSet but extended
        with the elements of the second KanaSet.

        :param other: KanaSet object
        :return:
        '''
        initresult = []

        for i in range(self.itemCount()):
            initresult.append(self.kana[i])

        for j in range(other.itemCount()):
            initresult.append(other.kana[j])

        result = KanaSet(initresult)
        return result