コード例 #1
0
ファイル: testhashtable.py プロジェクト: andrewdildy/basics
 def test_contains_simple(self):
     t = HashTable()
     t.put('a', 1)
     self.assertEqual(t.contains('a'), True)
     self.assertEqual(t.contains('b'), False)
     t.put('b', 1)
     self.assertEqual(t.contains('b'), True)
コード例 #2
0
ファイル: testhashtable.py プロジェクト: andrewdildy/basics
 def test_remove_simple(self):
     t = HashTable()
     t.put('a', 1)
     self.assertEqual(t.contains('a'), True)
     t.remove('a')
     self.assertEqual(t.contains('a'), False)
     self.assertEqual(t.remove('b'), 'key not found')
コード例 #3
0
ファイル: hashtable_test.py プロジェクト: imthaghost/tweetGen
def test_boolean_contains(self):
    ht = HashTable()
    ht.set('A', 1)
    ht.set('B', 5)
    ht.set('C', 10)
    assert ht.contains('A') is True
    assert ht.contains('B') is True
    assert ht.contains('C') is True
    assert ht.contains('G') is False
コード例 #4
0
 def test_contains(self):
     ht = HashTable()
     ht.set('I', 1)
     ht.set('V', 5)
     ht.set('X', 10)
     assert ht.contains('I') is True
     assert ht.contains('V') is True
     assert ht.contains('X') is True
     assert ht.contains('A') is False
コード例 #5
0
    def test_my_contains(self):
        ht = HashTable()
        ht.set('G', 4)
        ht.set('D', 4)
        ht.set('A', 4)

        assert ht.contains('G') is True
        assert ht.contains('D') is True
        assert ht.contains('A') is True
        assert ht.contains('AA') is False
コード例 #6
0
ファイル: hash_set.py プロジェクト: 2bithack/Data-Structures
class HashSet(object):

    def __init__(self, elements=None):
        self.hashtable = HashTable()
        self.size = 0

    def contains(self, element):
        return self.hashtable.contains(element)

    def add(self, element):
        if not self.contains(element):
            self.hashtable.set(element, None)
            self.size += 1

    def remove(self, element):
        if self.contains(element):
            self.hashtable.delete(element)
            self.size -= 1

    def union(self, other_set):
        new_set = HashSet()
        for key in other_set.hashtable.keys():
            new_set.add(key)
        for key in self.hashtable.keys():
            new_set.add(key)
        return new_items

    def intersection(self, other_set):
        shared = HashSet()
        for key in other_set.hashtable.keys():
            if self.hashtable.contains(key) == True:
                shared.add(key)
        return shared

    def difference(self, other_set):
        different_items = HashSet()
        for key in other_set.hashtable.keys():
            if self.hashtable.contains(key) == False:
                different_items.add(key)
        for key in self.hashtable.keys():
            if other_set.hashtable.contains(key) == False:
                different_items.add(key)
        return different_items
        # different_items = []
        # for key in other_set.hashtable.keys():
        #     if self.contains(key) == False:
        #         new_items.append(key)
        # return new_items

    def is_subset(self, other_set):
        for key in other_set.hashtable.keys():
            if self.hashtable.contains(key) == False:
                return False
        return True
コード例 #7
0
ファイル: testhashtable.py プロジェクト: andrewdildy/basics
 def test_collisions(self):
     t = HashTable()
     t.put('rwlqyilqlt', 1)
     t.put('eulvcjibif', 2)
     self.assertEqual(t.contains('rwlqyilqlt'), True)
     self.assertEqual(t.contains('eulvcjibif'), True)
     self.assertEqual(t.get('rwlqyilqlt'), 1)
     self.assertEqual(t.get('eulvcjibif'), 2)
     t.remove('rwlqyilqlt')
     self.assertEqual(t.contains('rwlqyilqlt'), False)
     self.assertEqual(t.contains('eulvcjibif'), True)
     t.remove('eulvcjibif')
     self.assertEqual(t.contains('eulvcjibif'), False)
コード例 #8
0
def repeated_word(text_string):
    ht = HashTable()
    x = text_string.split()
    count = 0

    for word in x:
        if word[-1] in string.ascii_letters:
            if ht.contains(word.lower()):
                return word
            ht.add(word.lower(), count)
        else:
            if ht.contains(word[:-1].lower()):
                return word[-1]
            ht.add(word[:-1], count)
        count += 1
コード例 #9
0
class HashSet(AbstractSet):
    def __init__(self, elements=None):
        """Initialize this hash table with the given data."""
        self.table = HashTable()
        if elements is not None:
            for element in elements:
                self.add(element)

    @property
    def size(self):
        """ Makes size an attribute """
        return self.tree.size

    def __repr__(self):
        """Return a string representation of this binary tree node."""
        return 'Set({!r})'.format(self.table)

    def contains(self, item):
        return self.table.contains(item)

    def add(self, item):
        if not self.contains(item):
            self.table.set(item)

    def remove(self, item):
        self.table.delete(item)

    def items(self):
        return self.table.items()
コード例 #10
0
class Jumble(object):
    @time_it
    def __init__(self, words):
        """Initialize a new Hash Table and then clean up each line to get individual
        words. Add the dictionary words that equal the length of the scrambled words.
        """
        self.words = words
        self.hash = HashTable()
        lengths = []
        #Determining the lengths of the scrambled words, increases speed greatly.
        for scrambled_word in words:
            if len(scrambled_word) not in lengths:
                lengths.append(len(scrambled_word))

        with open("/usr/share/dict/words", 'r') as f:
            for word in f:
                #Remove endline characters
                word = word.strip().lower()
                #Adds the word back into a hash table from the dictionary
                #Also only sets the word if it equals the length of the scrambled words
                if len(word) in lengths:
                    self.hash.set(word, word)

    def get_permutations(self, scrambled_word):
        """Get every single permutation of the scrambled word."""
        permutations = []

        if len(scrambled_word) == 1:
            return scrambled_word
        else:
            for char in scrambled_word:
                #Replacing parts of the scrambled word until we get down to 1 letter,
                #then continue to re-add strings to the character in each call
                for string in self.get_permutations(
                        scrambled_word.replace(char, "", 1)):
                    #Checking to not repeat permutations
                    if (char + string) not in permutations:
                        permutations.append(char + string)

        return permutations

    def find_word(self, permutations):
        """Take each of the permutation of the scrambled word and look for it in the hash
        table."""
        #Look up the word in the hash table
        for perm in permutations:
            if self.hash.contains(perm):
                return self.hash.get(perm)

        return "Word not Found"

    @time_it
    def unscramble(self):
        """Unscramble each word in our list of scrambled words."""
        unscrambled = []
        for word in self.words:
            unscrambled.append(self.find_word(self.get_permutations(word)))

        return unscrambled
コード例 #11
0
def test_contains_pass_with_collsion():
    hashtable = HashTable()
    hashtable.add('Chuck', 40)
    hashtable.add('kcuhC', 33)
    hashtable.add('Ckcuh', 11)
    expected = hashtable.contains('kcuhC')
    actual = True
    assert actual == expected
コード例 #12
0
class HashSet(object):
    def __init__(self, elements=None):
        self.ht = HashTable()
        if elements is not None:
            for element in elements:
                self.add(element)

    def __len__(self):
        return self.size

    @property
    def size(self):
        return self.ht.size

    def get(self, element):
        """Get item from the Set"""
        return self.ht.get(element)

    def contains(self, element):
        """return a boolean indicating whether element is in this set"""
        return self.ht.contains(element)

    def add(self, element):
        """add element to this set, if not present already"""
        if not self.contains(element):
            self.ht.set(element, element)

    def remove(self, element):
        """remove element from this set, if present, or else raise KeyError"""
        self.ht.delete(element)

    def items(self):
        return self.ht.keys()

    def union(self, other_set):
        """return a new set that is the union of this set and other_set"""
        return HashSet(self.items() + other_set.items())

    def intersection(self, other_set):
        """return a new set that is the intersection of this set and other_set"""
        return HashSet([
            element for element in self.items() if other_set.contains(element)
        ])

    def difference(self, other_set):
        """return a new set that is the difference of this set and other_set"""
        return HashSet([
            element for element in self.items()
            if not other_set.contains(element)
        ])

    def is_subset(self, other_set):
        """return a boolean indicating whether other_set is a subset of this set"""
        for item in other_set.items():
            if not self.contains(item):
                # An item in our set is not in the given set
                return False
        return True
def test_get_null():
    '''Successfully returns null for a key that does not exist in the hashtable'''
    hash = HashTable()
    hash.add('dog', 'bark')
    hash.add('Freddie', 'Nightmare')
    hash.add('Jason', 'Lake')
    actual = hash.contains('Frankie')
    expected = False
    assert expected == actual
def test_get_collision():
    '''Successfully handle a collision within the hashtable'''
    hash = HashTable()
    hash.add('dog', 'bark')
    hash.add('god', 'karb')
    actual = hash.contains('dog')
    actual = hash.contains('god')
    expected = True
    assert expected == actual
コード例 #15
0
class Set(object):
    def __init__(self, items=None):
        """Initialize this hash table with the given initial size."""
        self.ht = HashTable()

        if items is not None:
            for item in items:
                self.add(item)

    def __repr__(self):
        """Return a string representation of this hash table."""
        return 'HashTable({!r})'.format(self.ht.keys())

    def __iter__(self):
        for item in self.ht.keys():
            yield item

    def contains(self, item):
        return self.ht.contains(item)

    def size(self):
        return self.ht.size

    def add(self, item):
        return self.ht.set(item)

    def remove(self, item):
        return self.ht.delete(item)

    def union(self, set2):
        union_set = Set(
            set2.ht.keys())  # Initialize union set to have contain set 2
        for item in self:
            union_set.add(item)  # add item to set
        return union_set

    def intersect(self, set2):
        intersect_set = Set()  # Initialize an empty set
        for item in self:  # Iterate through items in self
            if set2.contains(item):  # Check if item is contained in set 2
                intersect_set.add(item)  # Add item to intersect set
        return intersect_set

    def difference(self, set2):
        diff_set = Set()
        for item in self:
            if not set2.contains(item):
                diff_set.add(item)
        return diff_set

    def subset(self, set2):
        if set2.size() > self.size():
            return False
        for item in set2:
            if not self.contains(item):
                return False
        return True
コード例 #16
0
class Set(object):
    def __init__(self, elements=None):
        """initialize a new empty set structure, and add each element if a sequence is given"""
        self.size = 0
        # map is just a dictionary with Key Value Pairs
        self.map = HashTable()
        if elements is not None:
            for element in elements:
                self.add(element)

    def add(self, element):
        """add element to this set, if not present already"""
        if not self.contains(element):
            self.size += 1
            self.map.set(element, None)

    def remove(self, element):
        """remove element from this set, if present, or else raise KeyError"""
        self.map.delete(element)
        self.size -= 1

    def contains(self, element):
        """return a boolean indicating whether element is in this set"""
        return self.map.contains(element)

    def union(self, other_set):
        """return a new set that is the union of this set and other_set"""
        new_set = Set()
        for element in self.map.keys():
            new_set.add(element)

        for element in other_set.map.keys():
            if not new_set.contains(element):
                new_set.add(element)

        return new_set

    def intersection(self, other_set):
        new_set = Set()
        for element in other_set.map.keys():
            if self.contains(element):
                new_set.add(element)
        return new_set

    def difference(self, other_set):
        new_set = Set()
        for element in self.map.keys():
            if not other_set.contains(element):
                new_set.add(element)
        return new_set

    def is_subset(self, other_set):
        for element in other_set.map.keys():
            if not self.contains(element):
                return False
        return True
コード例 #17
0
def redact(array1, array2):
    result = []
    hash_table = HashTable()
    for word in array2:
        hash_table.set(word, word)

    for word in array1:
        if hash_table.contains(word) is False:
            result.append(word)
    return result
コード例 #18
0
def repeated_word(phrase):
    start = 0
    word_table = HashTable()
    words = phrase.split(' ')
    for word in words:
        if not word_table.contains(word):
            word_table.add(word, word_table.hash(word))
        else:
            return word
    return 'No matching words.'
コード例 #19
0
class Set(object):
    def __init__(self, elems=[]):
        self.size = 0
        self.table = HashTable()
        for i in elems:
            self.table.set(i, i)

    def __str__(self):
        return str(self.table.keys())

    def contains(self, item):
        return self.table.contains(item)

    def add(self, item):
        if self.contains(item):
            raise ValueError("item already in set")
        self.size += 1
        self.table.set(item, item)

    def remove(self, item):
        if not self.contains(item):
            raise ValueError("item not in set")
        self.size -= 1
        self.table.delete(item)

    def union(self, set2):
        new = Set()
        for i in self.table.keys():
            new.add(i)
        for val in set2.table.keys():
            if not new.contains(val):
                new.add(val)
        return new

    def difference(self, set2):
        new = Set()
        for i in self.table.keys():
            if not set2.contains(i):
                new.add(i)
        return new

    def intersection(self, set2):
        new = Set()
        for val in self.table.keys():
            if set2.contains(val):
                new.add(val)
        return new

    def is_subset(self, set2):
        for val in set2.table.keys():
            if not self.contains(val):
                return False
        return True
コード例 #20
0
class Set:
    def __init__(self, elements=None):
        self.hashtable = HashTable()
        self.size = self.hashtable.size

    def contains(self, element):
        return self.hashtable.contains(element)

    def add(self, element):
        if self.contains(element):
            raise ValueError
        else:
            return self.hashtable.set(element, 3)

    def remove(self, element):
        if self.contains(element):
            self.hashtable.delete(element)
        else:
            return KeyError

    def union(self, other_set):
        new_set = Set()
        for key in self.hashtable.keys():
            new_set.add(key)
        for key in other_set.hashtable.keys():
            new_set.add(key)
        return new_set

    def difference(self, other_set):
        new_set = Set()
        for key in other_set.hashtable.keys():
            if not self.hashtable.contains(key):
                new_set.add(key)
        return new_set

    def is_subset(self, other_set):
        for key in other_set.hashtable.keys():
            if not self.hashtable.contains(key):
                return False
        return True
コード例 #21
0
def splitIntoHash(path):
    with open(path) as file:
        lines = file.read().splitlines()
    theHash = HashTable()
    for line in lines:
        mylist = line.split(',')
        if (theHash.contains(mylist[0])):
            oldVal = theHash.get(mylist[0])
            if (oldVal > mylist[1]):
                theHash.set(mylist[0], mylist[1])
        else:
            theHash.set(mylist[0], mylist[1])
    return theHash
コード例 #22
0
ファイル: wordsJumble.py プロジェクト: Keyology/cs-1-3-new
class Jumble(object):
    def __init__(self, words):
        '''Create  a hash table and gets individual words. 
        adds words that are
        the same length of scramble'''
        self.words = words
        self.hash = HashTable()
        length = []
        for scrambled_word in words:
            if len(scrambled_word) not in length:
                length.append(len(scrambled_word))
        
        with open("/usr/share/dict/words", 'r') as file:
            for word in file:
                word = word.strip().lower() 
                if len(word) in length:
                    self.hash.set(word, word)
    
    def find_permutation(self, scrambled_word):
        '''get all possible permutation'''
        permutation = []

        if len(scrambled_word) == 1:
            return scrambled_word
        else:
            for char in scrambled_word:
                # Replcaes scramvbled letters until down to a single letter
                for string in self.find_permutation(scrambled_word.replace(char, "", 1)):
                    if (char + string) not in permutation:
                        permutation.append(char + string)
        return permutation


    def find_word(self, permutations):
        for perm in permutations:
            if self.hash.contains(perm):
                return self.hash.get(perm)
        return "word not Found"


    def unscramble(self):
        unscrabled = []
        for word in self.words:
            unscrabled.append(self.find_word(self.find_permutation(word)))
        
        return unscrabled
コード例 #23
0
 def __convert_file_into_hashtable(self, file_path):
     """
         Read route costs file into a dictionary and return the result.
         Runtime: Θ(n) Space: Θ(m)
         n = number of lines in file
         m = total entries in the hash table
     """
     hash_lookup = HashTable()
     with open('data/' + file_path, "r") as file:
         for line in file:
             line = line[:-1]
             route, cost = line.split(",")
             if hash_lookup.contains(route):
                 original_cost = hash_lookup.get(route)
                 if cost < original_cost:
                     hash_lookup.set(route, cost)
             else:
                 hash_lookup.set(route, cost)
     return hash_lookup
コード例 #24
0
def table_generator(corpus_text, order):
    """Make the actual markov table."""
    """It's a hashtable with tuples, list => tuples, dictionary."""
    # Corpus in linkedlist form
    corpus_ll = LinkedList(corpus_text)
    # Window and the table
    window_queue = LinkedList()
    current_table = HashTable()

    # Current window for iterating through corpus; order changes size
    for i in range(order):
        window_queue.append(corpus_text[i])

    # Add above to hashtable + the word that comes after
    current_table.set((window_queue.items()), [corpus_text[order + 1]])

    # For the rest
    for i in range(corpus_ll.length() - (order + 1)):
        # Dequeue window, add next to window;
        window_queue.move()
        window_queue.append(corpus_text[i + order])
        # Word after window
        next_word = corpus_text[i + order + 1]
        # Check if window exists in hash table already
        # Add tuple + new word to list, or tuple and list w/ new word
        if current_table.contains((window_queue.items())):
            currentvalues = current_table.get(window_queue.items())
            currentvalues.append(next_word)
            new_value = currentvalues
            current_table.set((window_queue.items()), new_value)
        else:
            current_table.set((window_queue.items()), [next_word])

    # Turn the second element (list) into a dictionary
    for key, value in current_table.items():
        current_table.set(key, Dictogram(value))

    return current_table
コード例 #25
0
class Set(object):
    def __init__(self, items=None):
        self.items = HashTable()  # setup Hasttable

        # Insert everything
        if items is not None:
            for item in items:
                self.add(item)

    @property
    def size(self):
        """ Makes size an attribute """
        return self.items.length()

    def add(self, item):
        """ Insert one item to the set if it doesn't already exist 
    Time complexity: O(1) since we are using hash table
    """
        if not self.items.contains(item):  # check if item doesn't exist
            self.items.set(item, None)  # add the item

    def length(self):
        """ Gets the length of the set
    Time complexity: O(1) since we store the length in a property"""
        return self.size

    def is_empty(self):
        """ Checks if the set is empty
    Time complexity: O(1) since we store the length in a property"""
        return self.size == 0

    def contains(self, item):
        """ Checks if item is in the set
    Time Complexity: O(1) since Hash table access is constant time"""
        return self.items.contains(item)

    def remove(self, item):
        """ Check if item exists and remove it or raise Keyerror
    Time complexity: O(1) since accessing from Hashtable is constant time"""
        self.items.delete(
            item)  # remove the item or the inner hashtable will raise error

    def union(self, other_set):
        """ Makes a union with the other set and returns a new set 
    Time complexity: O(n) since getting the keys take linear time"""
        smaller = self.items
        larger = other_set.items
        if smaller.size > larger.size:
            smaller, larger = larger, smaller
        new_set = Set(larger.keys())
        for item in smaller.keys():
            new_set.add(item)
        return new_set

    def intersection(self, other_set):
        """ Makes an intersection between self and other set
    Time complexity: O(min(m,n)) since we iterate over the smaller set"""
        smaller = self.items
        larger = other_set.items
        if smaller.size > larger.size:
            smaller, larger = larger, smaller
        new_set = Set()
        for item in smaller.keys():
            if larger.contains(item):
                new_set.add(item)
        return new_set

    def difference(self, other_set):
        """ Gets the difference between two sets and returns it.
    Time complexity: O(n) since the keys() method takes linear time"""
        return Set(x for x in self.items.keys() if not other_set.contains(x))

    def is_subset(self, other_set):
        """ Checks if all the items in other_set are in self
    Time complexity: O(n) since we are calling the keys() method that takes linear time"""
        if other_set.size > self.size:
            return False
        return len(other_set.difference(self).items.keys()) == 0
コード例 #26
0
class Set(object):
    """Set Data Structure w/."""
    def __init__(self, init_size=8):
        """Initialize this hash table with the given initial size."""
        self.ht = HashTable(init_size)

    @property
    def size(self):
        """Return size of ht."""
        return self.ht.size

    def contains(self, element):
        """Check if the element is store in set (self)."""
        return self.ht.contains(element)

    def add(self, element):
        """Add new element to set, if unique."""
        self.ht.set(element, None)

    def remove(self, element):
        """Remove element from set if present. Else raise keyerror."""
        self.ht.delete(element)  # Key error is raised in function

    def union(self, other_set):
        """Return a new set that is the union of self and other_set."""
        union_set = deepcopy(self)

        for item in other_set.ht.items():
            union_set.add(item)
        return union_set

    def intersection(self, other_set):
        """Return a new set that is the intersection of self and other_set."""
        intersection_set = Set()

        # Use smaller set in order to iterate less items and improve runtime
        if self.size < other_set.size:
            for item in self.ht.items():
                if other_set.contains(item):
                    intersection_set.add(item)
        else:  # If other set is smaller than self
            for item in other_set.items():
                if self.contains(item):
                    intersection_set.add(item)

        return intersection_set

    def difference(self, other_set):
        """Return a new set that is the difference of self and other_set."""
        difference_set = Set()

        for item in self.ht.items():
            if not other_set.contains(item):
                difference_set.add(item)
        return difference_set

    def is_subset(self, other_set):
        """Return a boolean indicating whether other_set is a subset of this set."""
        for item in other_set.ht.items():
            if not self.contains(item):
                return False
        return True
コード例 #27
0
class Set(object):
    def __init__(self, elements=None):
        self.hashtable = HashTable()
        self.size = 0
        if elements is not None:
            for element in elements:
                self.add(element)

    def contains(self, element):
        """Returns a boolean indicating whether element is in this set
        Time Complexity: O(1) >> hashtable lookup is a const operation on average case
        Space Complexity: O(n) >> n is a bucket size.
        """
        if not self.hashtable:
            raise ValueError('Empty Hashtable')
        else:
            return self.hashtable.contains(element)

    def add(self, element):
        """Adds element to this set, if not present already
        Time Complexity: O(1) >> set method of hastable is const time
        Space Complexity: O(1) >> one new space is created for new element
        """
        if self.contains(element) is True:
            return
        else:
            self.hashtable.set(element, None)
            self.size += 1

    def remove(self, element):
        """Removes element from this set, if present, or else raise KeyError
        Time Complexity: O(n) >> delete method of hastable is const time
        Space Complexity: O(n) >> 
        """
        if self.contains(element) is False:
            raise KeyError('Element does not exist')
        else:
            self.hashtable.delete(element)
            self.size -= 1

    def union(self, other_set):
        """Returns a new set that is the union of this set and other_set
        Time Complexity: O(n+m) >> two loops >> two different hashtables and each has own length 
        Space Complexity: O(n+m) >> creating space for new each element one by one. n + m  
        """
        united_set = Set()
        # adding elements of self to united_set
        for element in self.hashtable.keys():
            united_set.add(element)
        # adding elements of other_set to united_set
        for element in other_set.hashtable.keys():
            united_set.add(element)

        return united_set

    def intersection(self, other_set):
        """Returns a new set that is the intersection of this set and other_set
        Time Complexity: O(n) >> travaersing through the hashtable to collect and compare the elements 
        Space Complexity: O(n) >> creating space for new each element one by one
        """

        inter_set = Set()
        # smaller set has to be iterated and its elements should be compared to larger set
        if self.size > other_set.size:
            big_set = self
            small_set = other_set
        else:
            big_set = other_set
            small_set = self

        for element in small_set.hashtable.keys():
            if big_set.hashtable.contains(element) is True:
                inter_set.add(element)

        return inter_set

    def difference(self, other_set):
        """Returns a new set that is the difference of this set and other_set
        Time Complexity: O(n) >> travaersing through the hashtable to collect and compare the elements 
        Space Complexity: O(n) >> creating space for new each element one by one
        """
        differ_set = Set()
        for element in self.hashtable.keys():
            if not other_set.hashtable.contains(element):
                differ_set.add(element)

        for element in other_set.hashtable.keys():
            if not self.hashtable.contains(element):
                differ_set.add(element)

        return differ_set

    def is_subset(self, other_set):
        """Returns a boolean indicating whether other_set is a subset of this set
        Time Complexity: O(n) >> travaersing through the hashtable to collect and compare the elements 
        Space Complexity: O(n) >> creating space for new each element one by one
        """
        # smaller set has to be iterated and its elements should be compared to larger set
        if self.size > other_set.size:
            big_set = self
            small_set = other_set
        else:
            big_set = other_set
            small_set = self

        for element in small_set.hashtable.keys():
            if big_set.hashtable.contains(element) is True:
                return True
            else:
                return False
コード例 #28
0
class HashSet(object):
    def __init__(self, elements=None):
        """Initialize this hash set; add the given items, if any"""
        self.ht = HashTable()
        self.size = 0
        if elements:
            for element in elements:
                self.add(element)

    def elements(self):
        """Return all of the elements in this set"""
        return self.ht.keys()

    def contains(self, element):
        """Return True if the given element is in this set,
        returns False otherwise.
        Best case running time: Omega(?) ?
        Worst case running time: O(?) ?"""
        if self.size > 0 and self.ht.contains(element):
            return True
        return False

    def add(self, element):
        """Add the given element to this set"""
        if self.ht.contains(element):
            raise ValueError('Element already exists')
        self.ht.set(element, True)
        self.size += 1

    def remove(self, element):
        """Remove the given element from this set, or raise ValueError"""
        if not self.contains(element):
            raise ValueError('Element not found: {}'.format(element))
        self.ht.delete(element)
        self.size -= 1

    def union(self, other_set):
        """Returns the union of other_set and this set"""
        union_set = HashSet(self.elements())
        for item in other_set.elements():
            union_set.add(item)
        return union_set

    def intersection(self, other_set):
        """Returns the intersection of other_set and this set, if any elements are shared."""
        intersection_set = HashSet()
        for item in self.elements():
            if other_set.contains(item):
                intersection_set.add(item)
        return intersection_set

    def difference(self, other_set):
        """Returns the differemce of other_set and this set, if any elements are not shared."""
        difference_set = HashSet()
        for item in self.elements():
            if not other_set.contains(item):
                difference_set.add(item)

        return difference_set

    def is_subset(self, other_set):
        """Returns True if other_set is a subset of this set, returns False otherwise."""
        for item in other_set.elements():
            if not self.contains(item):
                return False
        return True
コード例 #29
0
def setup():
    table = HashTable(len(words) + 1)
    for i in range(0, len(words)):
        # Our initial implementation of Hashtable is so limited (no resize/load)
        # That I might as well use a LinkedList for the innerTable, Because
        # Our HashTable with an initial size of 1, is essentially a LinkedList
        # The only way to improve this is recreating the HashTable with size + 1
        # with each new word for a word

        # I could use the table.get surrounded by a try-except instead of contains
        begin = False
        end = False
        word = words[i]

        # If the word starts a sentence
        if "BEGINSENT" in word:
            begin = True
            word = word[(len("BEGINSENT")):]

        # If the word ends a sentence
        if "ENDSENT" in word:
            end = True
            word = word[:-(len("ENDSENT"))]

        # We have to remove any beginning or ending flags for the next word (word2)
        # in order to add the next word (word2) to the current word's innertable
        word2 = ""

        # We have to check that we're not at the end of the for-loop
        if (i + 1 != len(words)):
            word2 = words[i + 1]
            word2.replace("\n", "")
            # If the next word starts a sentence,
            if "BEGINSENT" in word2:
                word2 = word2[(len("BEGINSENT")):]

            # If the next word ends a sentence
            if "ENDSENT" in word2:
                word2 = word2[:-(len("ENDSENT"))]

        # I could also do a check to see if the word is an ending word, then
        # proceed to add only the ending token. However, I decided to add
        # both the next word and possibly the ending token because lyrics are
        # are really short.
        # If the table contains the word already
        if table.contains(word):
            lis = table.get(word)
            # Increment the total number of tokens that can come after this word
            lis[0] += 1
            # This variable represent the total number of times the word that comes
            # after this word in the for-loop appears
            tup = 0

            # if the the current word isn't the end of the for-loop, meaning
            # that it has a word coming after the current one
            # (The word at the end of the loop is automatically an ending word)
            if i + 1 != len(words):
                # Try to find the word, if the word isnt in there, then the
                # get method throws an error so catch it
                # We want to set the "tup" variable to how many times the word
                # has already appeared
                try:
                    tup = lis[1].get(word2)
                except:
                    tup = 0
                # Then we reset the next word but increment the number of times
                # the next word appears after the current word
                lis[1].set(word2, tup + 1)

        # If the word is new to the table
        else:
            lis = [None, None]
            lis[0] = 1
            lis[1] = HashTable(1)
            lis[1].set(word2, 1)
            table.set(word, lis)

        # Technically we could've used the same character for both beginning
        # and ending since they wont live in the same space (table vs innertable)

        # If the word is an end of a sentence (or if it's at the end of the
        # for-loop then it's automatically the end of the sentence)
        # then we need to account for how likely is it to end the sentence
        # It's stored inside the innertable
        if end:
            lis = table.get(word)
            lis[0] += 1
            tup = 0
            try:
                tup = lis[1].get("]")
            except:
                # Word hasn't ended a sentence before
                tup = 0
            lis[1].set("]", tup + 1)

        # If the word is an beginning of a sentence
        # then we need to account for how likely is it to start a sentence
        # We use '[' as the character to signify starting a new sentence
        # It's stored in a table of it's own (not an innertable)
        if begin:
            # Not the first sentence starter
            if table.contains("["):
                lis = table.get("[")
                lis[0] += 1
                tup = 0
                try:
                    tup = lis[1].get(word)
                except:
                    # Word hasn't started a sentence before
                    tup = 0
                lis[1].set(word, tup + 1)

            # This is the first sentence starter (Happens at beginning of for-loop)
            else:
                lis = [None, None]
                lis[0] = 1
                lis[1] = HashTable(1)
                lis[1].set(word, 1)
                table.set("[", lis)

    return table
コード例 #30
0
ファイル: set.py プロジェクト: AwesomeZaidi/Data-Structures
class Set(object):

    def __init__(self, elements=None):
        """Initialize this new empty set structure with the given initial size."""
        self.map = HashTable()
        self.size = 0 # property that tracks the number of elements in constant time
        if elements is not None:
            for element in elements:
                self.size += 1 
                self.map.set(element, True)
    
    def contains(self, element):
        """return a boolean indicating whether element is in this set."""
        # return element in self.elements -> return self.elements.___contains__(element)
        return self.map.contains(element)

    def add(self, element):
        """add element to this set, if not present already"""
        # check if its unique by 
        # if not self.contains(element):
        if self.map.contains(element) == False:
            self.map.set(element, None)
        self.size += 1
        return self.size

    def remove(self, element):
        """remove element from this set, if present, or else raise KeyError"""
        # if element in self.elements:
        # if self.elements.___contains__(element):
        # if self.contains(element):
        self.map.delete(element)
        self.size -= 1
        return self.size
        # else:
        #     raise KeyError('Element not found: {}'.format(element))
    
    def union(self, other_set):
        """return a new set that is the union of this set and other_set"""
        new_set = Set()
        # for element in self.map:
        # for element in self.elements.__iter__():
        for element in self.map.keys():
            new_set.add(element) # or new_set.add(element.data[0]) ?

        for element in other_set.map.keys():
            if not new_set.contains(element):
                new_set.add(element)

        return new_set
    
    def intersection(self, other_set):
        """return a new set that is the intersection of this set and other_set"""
        new_set = Set()
        for element in other_set.map.keys():
            if self.contains(element):
                new_set.add(element)

        return new_set

    def difference(self, other_set):
        """return a new set that is the difference of this set and other_set"""
        new_set = Set()
        for element in self.map.keys():
            if other_set.contains(element):
                new_set.add(element)

        return new_set
        
    def issubset(self, other_set):
        """Return true if all the elements in a set exist in the other set, False if not."""
        for item in self.map.keys():
            print('item in test_set:', item)
            if item not in other_set.map.keys():
                return False
        return True