def reduceGroups(groups):
    # Rule 1: HashSet cardinality and number of dups.
    for group in groups:
        for current_cell in group:
            dups = 0
            for other_cell in group:
                if other_cell == current_cell:
                    dups += 1

            if (dups == len(current_cell)) and (dups < 9):
                for other_cell in group:
                    if other_cell != current_cell and current_cell <= other_cell:
                        other_cell.difference_update(current_cell)
                        return True
    
    # Rule 2: HashSet difference when size = 1. Make a new copy of the item we are currently at.
    for group in groups:
        for i in range(0, len(group)):
            set_copy = HashSet(group[i])
            current_index = i
            for j in range(0, len(group)):
                if j != current_index:
                    set_copy.difference_update(group[j])

            if len(set_copy) == 1 and set_copy != group[i]:
                group[i].clear()
                group[i].update(set_copy)
                return True
                
    return False
Пример #2
0
 def test_contains(self):
     elements = [2, 4, 6, 8]
     hs = HashSet(elements)
     assert hs.contains(2) is True
     assert hs.contains(4) is True
     assert hs.contains(8) is True
     assert hs.contains(20) is False
Пример #3
0
def buildPuzzle(file):
    puzzle = []
    for line in file:
        line = line.strip("\n")
        line = line.split()
        isEmpty = False
        if len(line) == 0:
            isEmpty = True
        rowEntry = []
        if not isEmpty:
            counter = 0
            for item in line:
                counter += 1
                if item == "x":
                    rowEntry.append(HashSet([1, 2, 3, 4, 5, 6, 7, 8, 9]))
                else:
                    try:
                        rowEntry.append(HashSet([int(item)]))
                    except:
                        print(
                            "Your puzzle is in an invalid format. Please include only numbers seperated by a space."
                        )
                        file.close()
                        return
            puzzle.append(rowEntry)
            if counter != 9:
                print(
                    "Your puzzle is in an invalid format. Please include only numbers seperated by a space."
                )
                file.close()
                return
    file.close()
    return puzzle
Пример #4
0
 def test_init_with_list(self):
     data = [8, 5, 1]
     hs = HashSet(data)
     assert hs.size == 3
     assert hs.contains(8) == True
     assert hs.contains(5) == True
     assert hs.contains(1) == True
Пример #5
0
 def test_intersection(self):
     first_set = HashSet(['One', 'Two', 'Three', 'Four'])
     second_set = HashSet(['Red', 'Two', 'Three', 'Orange'])
     intersection = first_set.intersection(second_set)
     assert intersection.size == 2
     assert intersection.contains('Two')
     assert intersection.contains('Three')
Пример #6
0
def Q1(Random, capacity):
    """
    Parameter:
        Random takes in True/False
            if True, insert integers randomly. 
        Capacity takes in initial array capacity. 
    """
    hashSet = HashSet([], capacity)
    listOf400 = list(range(1, 401))
    numberLeft = 400
    elapsedTimeTotal = 0
    while numberLeft > 0:
        index = (random.randint(0, numberLeft - 1))
        if Random:
            addNumber = listOf400.pop(index)
        else:
            addNumber = listOf400.pop(0)
        startTime = time.time()
        hashSet.add(addNumber)
        endTime = time.time()
        elapsedTimeTotal += round(endTime - startTime, 10)
        numberLeft -= 1
    print("Initial array length:", capacity, "| Final length:",
          len(hashSet._array))
    print("Time taken for all add(): ", elapsedTimeTotal)
Пример #7
0
 def test_union(self):
     hs = HashSet(['one', 'fish', 'two', 'fish'])
     hs2 = HashSet(['red', 'fish', 'blue', 'fish'])
     hs_union = hs.union(hs2)
     assert hs_union.contains('red') is True
     assert hs_union.contains('blue') is True
     print(str(hs_union))
     assert hs_union.contains('purple') is False
Пример #8
0
 def test_init(self):
     hs = HashSet()
     assert hs.size == 0
     # Test elements with given sequence
     elements = [0, 1, 2, 1, 0]
     hs = HashSet(elements)
     assert hs.size == 3
     assert str(hs) == '{0, 1, 2}'
Пример #9
0
 def test_difference(self):
     first_set = HashSet(['One', 'Two', 'Three', 'Four'])
     second_set = HashSet(['Red', 'Two', 'Three', 'Orange'])
     difference = first_set.difference(second_set)
     print(difference.size)
     assert difference.contains('One') == True
     assert difference.contains('Two') == False
     assert difference.contains('Three') == False
Пример #10
0
 def test_intersection(self):
     data_a = ['A', 'B', 'C', 'D']
     hs_a = HashSet(data_a)
     data_b = ['C', 'D', 'E', 'F']
     hs_b = HashSet(data_b)
     intersection_hs = hs_a.intersection(hs_b)
     assert intersection_hs.contains('C') == True
     assert intersection_hs.contains('D') == True
Пример #11
0
 def test_difference(self):
     data_a = ['A', 'B', 'C', 'D']
     hs_a = HashSet(data_a)
     data_b = ['C', 'D', 'E', 'F']
     hs_b = HashSet(data_b)
     difference_set = hs_a.difference(hs_b)
     assert difference_set.contains('A') == True
     assert difference_set.contains('B') == True
Пример #12
0
 def test_intersection(self):
     hs = HashSet(['one', 'fish', 'two', 'fish'])
     hs2 = HashSet(['red', 'fish', 'blue', 'fish', 'two'])
     hs_intersect = hs.intersection(hs2)
     assert hs_intersect.contains('fish') is True
     assert hs_intersect.contains('two') is True
     print(str(hs_intersect))
     assert hs_intersect.contains('one') is False
     assert hs_intersect.contains('red') is False
     assert hs_intersect.contains('blue') is False
Пример #13
0
 def test_difference(self):
     hs = HashSet(['one', 'fish', 'two', 'fish'])
     hs2 = HashSet(['red', 'fish', 'blue', 'fish', 'two'])
     hs_difference = hs.difference(hs2)
     assert hs_difference.contains('red') is True
     assert hs_difference.contains('blue') is True
     assert hs_difference.contains('one') is True
     print(str(hs_difference))
     assert hs_difference.contains('fish') is False
     assert hs_difference.contains('two') is False
Пример #14
0
 def test_union(self):
     first_set = HashSet(['One', 'Two', 'Three', 'Four'])
     second_set = HashSet(['Red', 'Green', 'Blue'])
     union = first_set.union(second_set)
     assert union.size == 7
     assert union.contains('Two') == True
     assert union.contains('Three') == True
     assert union.contains('Blue') == True
     assert union.contains('Purple') == False
     assert union.contains('Six') == False
Пример #15
0
class HashMap:
    class __KVPair:
        def __init__(self, key, value):
            self.key = key
            self.value = value

        def __eq__(self, other):
            if type(self) != type(other):
                return False
            return self.key == other.key

        def get_key(self):
            return self.key

        def get_value(self):
            return self.value

        def __hash__(self):
            return hash(self.key)

    def __init__(self):
        self.hash_set = HashSet()

    def __len__(self):
        return len(self.hash_set)

    def __contains__(self, item):
        return HashMap.__KVPair(item, None) in self.hash_set

    def not__contains__(self, item):
        return item not in self.hash_set

    def __setitem__(self, key, value):
        self.hash_set.add(HashMap.__KVPair(key, value))

    def __getitem__(self, key):
        if HashMap.__KVPair(key, None) in self.hash_set:
            return self.hash_set[HashMap.__KVPair(key, None)].get_value()
        raise KeyError(f"Key '{key}' not in HashMap")

    def __iter__(self):
        for i in self.hash_set:
            yield i.get_key()

    def __str__(self):
        s = "{"
        items = []
        for i in self.hash_set:
            items.append(f"{repr(i.get_key())}: {repr(i.get_value())}")
        s += ", ".join(items)
        s += "}"
        return s

    def __repr__(self):
        return str(self)
Пример #16
0
 def test_add_twice_and_contains(self):
     hs = HashSet()
     hs.add('I')
     hs.add('V')
     hs.add('X')
     assert hs.size == 3
     hs.add('V')  # Update value
     hs.add('X')  # Update value
     assert hs.contains('I') is True
     assert hs.contains('V') is True
     assert hs.contains('X') is True
     assert hs.size == 3  # Check size is not overcounting
def form_puzzle(matrix):

    rows = len(matrix)
    cols = len(matrix[0])

    for row in range(rows):
        for col in range(cols):
            if matrix[row][col] == "x":
                matrix[row][col] = HashSet([1,2,3,4,5,6,7,8,9])
            else:
                matrix[row][col] = HashSet([matrix[row][col]])

    return matrix
Пример #18
0
 def test_add(self):
     hs = HashSet([1, 3, 5, 7])  # size 4
     assert hs.contains(23) == False
     hs.add(23)  #size 5
     assert hs.contains(23) == True
     hs.add(2)  #size 6
     assert hs.size == 6
Пример #19
0
    def test_is_subset(self):
        elements1 = ["1", "4", "6"]
        elements2 = ["1", "4"]

        hs = HashSet(elements1)
        other_hs = HashSet(elements2)

        assert other_hs.is_subset(hs) == True
        assert hs.is_subset(other_hs) == False
        other_hs.add("7")
        assert other_hs.is_subset(hs) == False
Пример #20
0
 def test_remove(self):
     elements = ["hola", "adios", "test", "test", "annie"]
     hs = HashSet(elements)
     assert hs.contains("annie") == True
     hs.remove("annie")
     assert hs.contains("annie") == False
     with self.assertRaises(ValueError):
         hs.remove("pamis")  # Element does not exist
Пример #21
0
    def test_union(self):
        elements1 = ["hola", "adios", "test", "test", "annie"]
        elements2 = ["pamis", "blah", "other", "test", "hola"]
        hs = HashSet(elements1)
        other_hs = HashSet(elements2)

        union = hs.union(other_hs)

        assert union.contains("hola") == True
        assert union.contains("adios") == True
        assert union.contains("test") == True
        assert union.contains("annie") == True
        assert union.contains("pamis") == True
        assert union.contains("blah") == True
        assert union.contains("other") == True
Пример #22
0
def Q2(highestInteger):
    """
    Parameter:
        highestInteger is the max integer to add to the HashSet. 1 - highestInteger. 
    """
    hashSet = HashSet([], 100)
    for number in range(1, highestInteger):
        hashSet.add(number)
    startTime = time.time()
    print("3 in hashSet: ", 3 in hashSet)
    endTime = time.time()
    elapsedTime = round(endTime - startTime, 10)
    print("Time to find 3: ", elapsedTime)
    print("Array length: ", len(hashSet._array))

    return hashSet
Пример #23
0
    def test_intersection(self):
        elements1 = ["1", "2", "5", "9"]
        elements2 = ["9", "2", "4", "3"]

        hs = HashSet(elements1)
        other_hs = HashSet(elements2)

        intersection = hs.intersection(other_hs)

        assert intersection.contains("9") == True
        assert intersection.contains("2") == True

        assert intersection.contains("1") == False
        assert intersection.contains("5") == False
        assert intersection.contains("4") == False
        assert intersection.contains("3") == False
Пример #24
0
    def test_difference(self):
        elements1 = ["1", "2", "5", "9"]
        elements2 = ["9", "2", "4", "3"]

        hs = HashSet(elements1)
        other_hs = HashSet(elements2)

        difference = hs.difference(other_hs)

        assert difference.contains("1") == True
        assert difference.contains("5") == True

        assert difference.contains("9") == False
        assert difference.contains("2") == False
        assert difference.contains("4") == False
        assert difference.contains("3") == False
Пример #25
0
def solve(matrix):
    """
    Solve the sudoku matrix.

    :param list[list[HashSet]] matrix: 2D matrix representing sudoku cells
    """
    for _ in range(5):
        reduce_(matrix)

    if not solution_viable(matrix):
        return None

    if solution_ok(matrix):
        return matrix

    print("searching...")

    for i in range(9):
        for j in range(9):
            if len(matrix[i][j]) > 1:
                for k in matrix[i][j]:
                    mcopy = copy.deepcopy(matrix)
                    mcopy[i][j] = HashSet([k])
                    result = solve(mcopy)
                    if result is not None:
                        return result

    return None
Пример #26
0
 def test_size(self):
     hs = HashSet()
     assert hs.size == 0
     hs.add('A')
     assert hs.size == 1
     hs.add('B')
     assert hs.size == 2
     hs.add('C')
     assert hs.size == 3
Пример #27
0
 def test_add(self):
     elements = ["hola", "adios", "test", "test"]
     hs = HashSet(elements)
     hs.add("annie")
     assert hs.size == 4
     assert hs.contains("annie") == True
     hs.add("hola")
     assert hs.size == 4
     assert hs.contains("hola") == True
Пример #28
0
 def test_add_and_contains(self):
     hs = HashSet()
     hs.add('I')
     hs.add('V')
     hs.add('X')
     assert hs.contains('I') is True
     assert hs.contains('V') is True
     assert hs.contains('X') is True
     assert hs.size == 3
Пример #29
0
 def test_contains(self):
     hs = HashSet()
     hs.add('I')
     hs.add('V')
     hs.add('X')
     assert hs.contains('I') is True
     assert hs.contains('V') is True
     assert hs.contains('X') is True
     assert hs.contains('A') is False
Пример #30
0
 def test_size(self):
     hs = HashSet()
     assert hs.size == 0
     hs.add('I')
     assert hs.size == 1
     hs.add('V')
     assert hs.size == 2
     hs.add('X')
     assert hs.size == 3