Exemplo n.º 1
0
    def compare(self, other_person):
        this_birthday = str(self.get_birthday())
        this_year, this_month, this_day = this_birthday.split('-')
        this_year, this_month, this_day = int(this_year), int(this_month), int(this_day)
        this_name = self.get_name()
        other_birthday = str(other_person.get_birthday())
        other_year, other_month, other_day = other_birthday.split('-')
        other_year, other_month, other_day = int(other_year), int(other_month), int(other_day)
        other_name = other_person.get_name()
        Comparable.compare(other_person)

        if this_year > other_year:
            return 1
        elif this_year == other_year:
            if this_month > other_month:
                return 1
            elif this_month == other_month:
                if this_day > other_day:
                    return 1
                elif this_day == other_day:
                    if this_name > other_name:
                        return 1
                    elif this_name < other_name:
                        return -1
                    else:
                        return 0
                else:
                    return -1
            else:
                return -1
        else:
            return -1
Exemplo n.º 2
0
 def compare(self, person):
     Comparable.compare(self)
     if self.get_birthday().compare(person.get_birthday()) > 0:
         return 1
     elif self.get_birthday().compare(person.get_birthday()) == 0:
         if self.get_name() > person.get_name():
             return 1
         elif self.get_name() < person.get_name():
             return -1
         else:
             return 0
     else:
         return -1
def test(queueType):
    # Test any implementation with same code
    print("VERIFY THAT IT BEHAVES LIKE A REGULAR QUEUE.")
    q = queueType()
    print("Length:", len(q))
    print("Empty:", q.isEmpty())
    print("Add 1-10")
    for i in range(10):
        q.add(i + 1)
    print("Peeking:", q.peek())
    print("Items (front to rear):", q)
    print("Length:", len(q))
    print("Empty:", q.isEmpty())
    theClone = queueType(q)
    print("Items in clone (front to rear):", theClone)
    theClone.clear()
    print("Length of clone after clear:", len(theClone))
    print("Pop 3 items:", end=" ")
    for count in range(3):
        print(q.pop(), end=" ")
    print("\nQueue: ", q)
    print("Adding 11 and 12:")
    for item in range(11, 13):
        q.add(item)
    print("Queue: ", q)
    print("Popping items (front to rear): ", end="")
    while not q.isEmpty():
        print(q.pop(), end=" ")
    print("\nLength:", len(q))
    print("Empty:", q.isEmpty())
    print("Create with 11 items:")
    q = queueType(range(1, 12))
    print("Items (front to rear):", q)

    print("\nVERIFY THAT IT BEHAVES LIKE A PRIORITY QUEUE.")
    lyst = list(range(1, 20, 2))
    random.shuffle(lyst)
    print("The items added: ", lyst)
    q = LinkedPriorityQueue(lyst)
    print("The queue: ", q)
    q.add(16)
    q.add(17)
    print("Adding 16 and 17: ", q)
    print("Add some random strings with random priorities attached:")
    q.clear()
    for s in "VERIFY THAT":
        c = Comparable(s, random.randint(1, 3))
        print((c.getData(), c.getPriority()))
        q.add(c)
    print("The queue: ", q)
Exemplo n.º 4
0
def test(queueType):
    # Test any implementation with same code
    print("VERIFY THAT IT BEHAVES LIKE A REGULAR QUEUE.")
    q = queueType()
    print("Length:", len(q))
    print("Empty:", q.isEmpty())
    print("Add 1-10")
    for i in range(10):
        q.add(i + 1)
    print("Peeking:", q.peek())
    print("Items (front to rear):",  q)
    print("Length:", len(q))
    print("Empty:", q.isEmpty())
    theClone = queueType(q)
    print("Items in clone (front to rear):",  theClone)
    theClone.clear()
    print("Length of clone after clear:",  len(theClone))
    print("Pop 3 items:", end = " ")
    for count in range(3): print(q.pop(), end = " ")
    print("\nQueue: ", q)
    print("Adding 11 and 12:")
    for item in range(11, 13): q.add(item)
    print("Queue: ", q)    
    print("Popping items (front to rear): ", end="")
    while not q.isEmpty(): print(q.pop(), end=" ")
    print("\nLength:", len(q))
    print("Empty:", q.isEmpty())
    print("Create with 11 items:")
    q = queueType(range(1, 12))
    print("Items (front to rear):",  q)

    print("\nVERIFY THAT IT BEHAVES LIKE A PRIORITY QUEUE.")
    lyst = list(range(1, 20, 2))
    random.shuffle(lyst)
    print("The items added: ", lyst)
    q = LinkedPriorityQueue(lyst)
    print("The queue: ", q)
    q.add(16)
    q.add(17)
    print("Adding 16 and 17: ", q)
    print("Add some random strings with random priorities attached:")
    q.clear()
    for s in "VERIFY THAT":
        c = Comparable(s, random.randint(1, 3))
        print((c.getData(), c.getPriority()))
        q.add(c)
    print("The queue: ", q)
Exemplo n.º 5
0
    def compare(self, other_person):
        this_name = self.get_name()
        other_name = other_person.get_name()
        other_person_date = other_person.get_birthday()
        Comparable.compare(other_person)
        compare_result = self.__birthday.compare(other_person_date)

        if compare_result == 1:
            return 1
        elif compare_result == -1:
            return -1
        else:
            if this_name > other_name:
                return 1
            elif this_name < other_name:
                return -1
            else :
                return 0
Exemplo n.º 6
0
def main():

    p_list = PersonList()
    p_list.populate("Persons.csv")

    print("List searched using linear search")
    print()

    name = input("Who do you want to search for?")
    bdate = input("What is their birthdate? (mm-dd-yyyy)")
    print()

    month, day, year = bdate.split('-')
    p_obj = Person(name, int(month), int(day), int(year))

    index = p_list.search(linear_search, p_obj)

    if index == -1:
        print(name, "is not in the list")
    else:
        print(name, "is at position", index, "in the list")

    print()
    print("The number of compares are", Comparable.get_num_compares())
    print()

    Comparable.clear_compares()

    p_list.sort(bubble_sort)

    print("List sorted by bubble sort")
    for p in p_list:
        print(p)

    print()
    print("The number of compares are", Comparable.get_num_compares())
    print()

    Comparable.clear_compares()

    print("List searched using binary search")
    print()

    name = input("Who do you want to search for?")
    bdate = input("What is their birthdate? (mm-dd-yyyy)")
    print()

    month, day, year = bdate.split('-')
    p_obj = Person(name, int(month), int(day), int(year))

    index = p_list.search(binary_search, p_obj)
    if index == -1:
        print(name, "is not in the list")
    else:
        print(name, "is at position", index, "in the list")

    print()
    print("The number of compares are", Comparable.get_num_compares())
Exemplo n.º 7
0
    def test_basic_data_types(self):
        """
        Test that Comparable works with built-in data types.

        Builds Comparable objects with ordered integers and uses Comparable
        methods to assert that they are in the same order they were
        inserted.
        """
        for i in range(10):
            self.comparables.append(Comparable(i))

        for i in range(9):
            self.assertTrue(
                self.comparables[i].less_than(self.comparables[i+1]))
Exemplo n.º 8
0
    def compare(self, other_Person):

        Comparable.compare(other_Person)

        if self.__birthday.compare(other_Person.get_birthday()) > 0:

            return 1

        elif self.__birthday.compare(other_Person.get_birthday()) < 0:

            return -1

        elif self.__name > other_Person.get_name():

            return 1

        elif self.__name < other_Person.get_name():

            return -1

        else:

            return 0
Exemplo n.º 9
0
def test(stackType):
    s = stackType()
    print(f"Len: {len(s)}")
    print(f"empty? {s.isEmpty()}")
    print(f"add 1 - 10: ")
    for i in range(10):
        if stackType == PriorityLinkedQueue:
            s.add(Comparable(i + 1, i % 2))
        else:
            s.add(i + 1)
    print(f"peeking: {s.peek()}")
    print(f"Items (rear to front): {s}")
    print(f"Length: {len(s)}")
    print(f"Empty: {s.isEmpty()}")
    print(f"Popping items (front to rear):", end=" ")
    while not s.isEmpty():
        print(s.pop(), end=" ")
    print(f"\nLength: {len(s)}")
    print(f"Empty? {s.isEmpty()}")
Exemplo n.º 10
0
def main():
    """
    This main function creates a PersonList object and populates it
    with the contents of a data file containing 30 records each
    containing the first name and birthdate as month day year
    separated by commas:  Merli,9,10,1998

    Next, a request is made to search for a certain Person.
    A Person object is created from the input.
    Then a linear search is performed with the Person object
    If the Person is located in the list, its index is returned
    Otherwise, -1 is returned.
    The number of compares for the linear search is displayed

    Next, a bubble sort is executed to sort the PersonList.
    The sorted list is displayed, along with the number of
    compares it took to sort it

    Next, a request is made to search for another Person.
    A Person object is created from the input.
    Then a binary search is performed with the Person object
    If the Person is located in the sorted list, its index is returned
    Otherwise, -1 is returned.
    The number of compares for the binary search is displayed
    """
    
    #  Create a PersonList object and add the data
    p_list = PersonList()
    p_list.populate("Persons.csv")

    # Request a person to search
    print("List searched using linear search")
    print()
    
    name = input("Who do you want to search for?")
    bdate = input("What is their birthdate? (mm-dd-yyyy)")
    print()
    
    # Create a person object from the input
    month, day, year = bdate.split('-')
    p_obj = Person(name, year, month, day)

    # Do a linear search
    index = p_list.search(linear_search, p_obj)

    if index == -1:
        print(name, "is not in the list")
    else:
        print(name, "is at position", index, "in the list")

    # Display the number of compares
    print()
    print("The number of compares are", Comparable.get_num_compares())
    print()
    
    # Reset the compare count
    Comparable.clear_compares() 

    # Sort the list using bubble sort
    p_list.sort(bubble_sort)
    
    print("List sorted by bubble sort")
    print(p_list)

    # Display the number of compares
    print()
    print("The number of compares are", Comparable.get_num_compares())
    print()
    
    # Reset the compare count
    Comparable.clear_compares()
     
    # Request a person to search
    print("List searched using binary search")
    print()
    
    name = input("Who do you want to search for?")
    bdate = input("What is their birthdate? (mm-dd-yyyy)")
    print()
    
    # Create a person object from the input
    month, day, year = bdate.split('-')
    p_obj = Person(name, year, month, day)

    # Do a binary search
    index = p_list.search(binary_search, p_obj)
    if index == -1:
        print(name, "is not in the list")
    else:
        print(name, "is at position", index, "in the list")
    print(p_list)
    # Display the number of compares
    print()
    print("The number of compares are", Comparable.get_num_compares())
Exemplo n.º 11
0
def main():
    print("Sorting the 32 element Person file")
    print("==================================")
    print()

    func_list = [
        selection_sort, insertion_sort, merge_sort, quick_sort, heap_sort
    ]

    file_name = "Person32.csv"
    file_size = file_name[6:8]

    for func in func_list:
        p_list = PersonList()
        p_list.populate(file_name)
        p_list.sort(func)
        print("Sorting", file_size, "file", file_name, "using", func.__name__)
        print("The number of compares are", Comparable.get_num_compares())
        print()
        print("The sorted list is:")
        print(p_list)
        Comparable.clear_compares()

    print("Sorting the 4 different sized random Person files")
    print("=================================================")
    print()

    random_files = [
        "Person1Ka.csv", "Person2Ka.csv", "Person4Ka.csv", "Person8Ka.csv"
    ]

    for func in func_list:
        for file in random_files:
            file_size = file[6:8]
            p_list = PersonList()
            p_list.populate(file)
            p_list.sort(func)
            print("Sorting", file_size, "file", file, "using", func.__name__)
            print("The number of compares are", Comparable.get_num_compares())
            Comparable.clear_compares()
            print()

    print("Sorting the 4 different sized sorted Person files")
    print("=================================================")
    print()

    sorted_files = [
        "Person1Kb.csv", "Person2Kb.csv", "Person4Kb.csv", "Person8Kb.csv"
    ]

    for func in func_list:
        for file in sorted_files:
            file_size = file[6:8]
            p_list = PersonList()
            p_list.populate(file)
            p_list.sort(func)
            print("Sorting", file_size, "file", file, "using", func.__name__)
            print("The number of compares are", Comparable.get_num_compares())
            Comparable.clear_compares()
            print()

    print("Sorting the 4 different sized reverse sorted Person files")
    print("=========================================================")
    print()

    reverse_files = [
        "Person1Kc.csv", "Person2Kc.csv", "Person4Kc.csv", "Person8Kc.csv"
    ]

    for func in func_list:
        for file in reverse_files:
            file_size = file[6:8]
            p_list = PersonList()
            p_list.populate(file)
            p_list.sort(func)
            print("Sorting", file_size, "file", file, "using", func.__name__)
            print("The number of compares are", Comparable.get_num_compares())
            Comparable.clear_compares()
            print()
Exemplo n.º 12
0
            LinkedQueue.add(self, newItem)
        else:
            # search for position where the new item is less
            probe = self.front
            # step through nodes until newItem is less than or equal to probe.data
            while newItem >= probe.data:
                trailer = probe
                probe = probe.next
            # instantiate new Node for new item to go into the queue
            newNode = Node(newItem, probe)
            if probe == self.front:
                # new item goes to front
                self.front = newNode
            else:
                # new item goes between two nodes
                trailer.next = newNode
            self.size += 1


pq = PriorityLinkedQueue([
    Comparable("Third Class", 3),
    Comparable("Second Class", 2),
    Comparable("Third Class", 3),
    Comparable("First Class", 1),
    Comparable("Second Class", 2)
])

print(pq)
pq.pop()
print(pq)
Exemplo n.º 13
0
def main():

    sort_list = [
        selection_sort, insertion_sort, quick_sort, merge_sort, heap_sort
    ]
    #Sort the 32 element files
    for sort in sort_list:
        p_list = PersonList()
        p_list.populate("Person32.csv")
        p_list.sort(sort)

        print("Sorting 32 file " + p_list.name + " using " + p_list.func_name)
        print("The number of compares are", Comparable.get_num_compares())
        print()
        print("The sorted list is: ")
        print("==================================")
        print(p_list)
        Comparable.clear_compares()

    #Random list sorts
    print("Sorting the 4 different sized random Person files")
    print("==================================")
    print()
    random_lists = [
        "Person1Ka.csv", "Person2Ka.csv", "Person4Ka.csv", "Person8Ka.csv"
    ]

    for sort in sort_list:
        size = 1
        for list in random_lists:
            p_list = PersonList()
            p_list.populate(list)
            p_list.sort(sort)
            print("Sorting " + str(size) + "K file " + p_list.name +
                  " using " + p_list.func_name)
            print("The number of compares are", Comparable.get_num_compares())
            print()
            Comparable.clear_compares()
            size += size

    #Sorted list sorts
    print("Sorting the 4 different sized sorted Person files")
    print("==================================")
    print()
    random_lists = [
        "Person1Kb.csv", "Person2Kb.csv", "Person4Kb.csv", "Person8Kb.csv"
    ]

    for sort in sort_list:
        size = 1
        for list in random_lists:
            p_list = PersonList()
            p_list.populate(list)
            p_list.sort(sort)
            print("Sorting " + str(size) + "K file " + p_list.name +
                  " using " + p_list.func_name)
            print("The number of compares are", Comparable.get_num_compares())
            print()
            Comparable.clear_compares()
            size += size


#Reverse list sorts

    print("Sorting the 4 different sized reverse sorted Person files")
    print("==================================")
    print()
    random_lists = [
        "Person1Kc.csv", "Person2Kc.csv", "Person4Kc.csv", "Person8Kc.csv"
    ]

    for sort in sort_list:
        size = 1
        for list in random_lists:
            p_list = PersonList()
            p_list.populate(list)
            p_list.sort(sort)
            print("Sorting " + str(size) + "K file " + p_list.name +
                  " using " + p_list.func_name)
            print("The number of compares are", Comparable.get_num_compares())
            print()
            Comparable.clear_compares()
            size += size