Ejemplo n.º 1
0
 def get_sublist(self, letter):
     '''(PhoneBook, str) -> PhoneBook
     Returns the phonebook of the contacts starting all contact names with
     given string of letter from the phonebook of the  alphabetical order
     REQ: First Name or Surname must be given in the contact'''
     # create an empty sll
     sub = SingleLinkedList()
     # traverse through the list to find the contact whose primary name
     # start with the given string letter
     curr = self._head
     while (curr is not None):
         # get the name of the contacts from the phonebook
         new = curr.get_element().main_name()
         # if the first letter of the contact name matches with letter given
         if (str(new)[:1] == letter):
             # create a node and add that node in sll
             node = Node(curr.get_element())
             sub.add_last(node)
             # continue traversing
             curr = curr.get_next()
         else:
             # else continue traversing
             curr = curr.get_next()
     # return the sll with given letter
     return sub
Ejemplo n.º 2
0
 def __init__(self, contact=None):
     '''(PhoneBook, SingleLinkedList) -> NoneType
     initializes the attributes of a phonebook
     '''
     # Representation Invariant (RI)
     # PhoneBook contains contact(s) if it is not empty
     SingleLinkedList.__init__(self)
     # _size is non-negative
     # add the first contact during initialization if provided
     if contact is not None:
         self.add(contact)
Ejemplo n.º 3
0
    def rearrange_phonebook(self):
        '''(PhoneBook) -> NoneType
        Rearrages the phonebook in a way that brings all favourite contacts in
        front of the book in alphabetical order following with all unfavourit
        contacts from the phonebook that are present in the alphabetical order
        REQ: List must be given'''
        # Create an empty sll to store all favourite contacts
        emp = SingleLinkedList()
        # set prev to None to keep pointer of the previous node
        prev = None
        # set output to false
        output = False
        curr = self._head
        # traverse through the phonebook to get all favourite contacts
        while (curr is not None):
            # check if thecontact is fav
            if (curr.get_element().is_fav() is True):
                # create a new node and add the contact in new sll created
                new = Node(curr.get_element())
                emp.add_last(new)
                # if the element in head is fav, remove the node by moving
                # pointer to the next node and setting it to head
                if (prev is None):
                    self.set_head(self._head.get_next())
                # remove the node but setting next to current node
                else:
                    prev.set_next(curr.get_next())
                # continue traversing through the list
                curr = curr.get_next()
                output = True
            else:
                # else continue traversing through the list
                prev = curr
                curr = curr.get_next()

        # it there are favourite contacts present in the phonebook
        if (output is True):
            # use the helper method reverse, which changes the order of the
            # the nodes in reversed order
            rev_fav = self.reverse(emp)
            # traverse through the list to add the favourites in the phonebook
            # in front of the list
            curr = rev_fav.get_head()
            while (curr is not None):
                # using the method of sll, add first; add the contacts up front
                self.add_first(curr)
                curr = curr.get_next()
Ejemplo n.º 4
0
 def get_housemate(self, homephone):
     '''(PhoneBook, str) -> PhoneBook
     Returns the phonebook of the contacts whose homephones are same as
     given homephone from the phonebook of the  alphabetical order
     REQ: homephone must be given in the contact'''
     # Create an empty sll
     home = SingleLinkedList()
     # travrse through the list to get all the housemates who shares same hp
     curr = self._head
     while (curr is not None):
         # get the housephone of each contact
         new = curr.get_element().get_homephone()
         # check if housephone of contact matches given contact
         if (str(new) == homephone):
             # create a node and add that in the given list
             node = Node(curr.get_element())
             home.add_last(node)
             # continue traversing through the phonebook
             curr = curr.get_next()
         else:
             # else continue through traversing the phonebook
             curr = curr.get_next()
     # return the phonebook with housemates sharing same housephone
     return home
Ejemplo n.º 5
0
    def toronto_phone(self):
        '''(PhoneBook) -> (PhoneBook,PhoneBook)
        Returns the phonebook of the contact starting with 416 and 647 in tuple
        from the phonebook of the  alphabetical order
        REQ: Primary phone number must be given in the contact
        REQ: the given contact must start with the 647 or 416 '''
        # Create two empty linked list for storing different phone numbers
        four_one_six = SingleLinkedList()
        six_four_seven = SingleLinkedList()

        # traverse through the list to add the phone numbers in the linked list
        # according to their starting format
        curr = self._head
        while (curr is not None):
            # get the primary phone number of the contact in the phonebook
            new = curr.get_element().get_primaryphone()
            # If the phine number starts with 647
            if (str(new)[:3] == "647"):
                # create the new node of the contact
                node = Node(curr.get_element())
                # add that contact in six_four_seven phonebook sll
                six_four_seven.add_last(node)
                # continue traversing through the list
                curr = curr.get_next()
            # If the phine number starts with 416
            elif (str(new)[:3] == "416"):
                # create the new node of the contact
                node = Node(curr.get_element())
                # add that contact in four_one_six phonebook sll
                four_one_six.add_last(node)
                # continue traversing through the list
                curr = curr.get_next()
            # if the format of primary phone number is not correct
            else:
                # raise an exception
                raise InvalidFormatPhoneNumber("Invalid Format Phone Number")
        # return the tuple with both the sll
        return (four_one_six, six_four_seven)
Ejemplo n.º 6
0
 def reverse(self, phoneb):
     '''(PhoneBook,PhoneBook) -> PhoneBook
     Reverse the contacts from the phonebook that are present in the
     alphabetical order
     REQ: List must be given'''
     # create empty sll to return the reversed link
     rev = SingleLinkedList()
     # if the reversed sll is empty
     if (rev.get_head() is None):
         # create a node and set the element from given phonebook
         new = Node(phoneb.get_head().get_element())
         # set the node head
         rev.set_head(new)
     # Traverse through the given phonebook and continue traversing
     # through the list
     curr = phoneb._head.get_next()
     while (curr is not None):
         new = Node(curr.get_element())
         rev.add_first(new)
         curr = curr.get_next()
     # return the reversed phonebook
     return rev
Ejemplo n.º 7
0
 def reversed_phonebook(self):
     '''(PhoneBook) -> PhoneBook
     Returns the phonebook of the contacts in the reverse order from the
     phonebook of the  alphabetical order
     REQ: phonebook must not be empty'''
     # create the empty sll for reverse contacts
     rev = SingleLinkedList()
     # if the reverse phonebook is empty
     if (rev.get_head() is None):
         # create a new node and add that element in reverse phonebook
         new = Node(self.get_head().get_element())
         # set that node as a head of the reverse phonebook
         rev.set_head(new)
     # traverse through the list & keep adding the contact in rev phonebook
     curr = self._head.get_next()
     while (curr is not None):
         # create empty node and add the element in it
         new = Node(curr.get_element())
         # keep adding notes in the front in rev phonebook
         rev.add_first(new)
         # continue traversing through the list
         curr = curr.get_next()
     # return the reversed phonebook
     return rev
Ejemplo n.º 8
0
            tail_node.next = next_node
            head = next_node
            i = 0
        node = next_node
        i = i + 1
    return dummy.next


def reverse(node, k):
    tmp = node
    previous = None
    while tmp:
        tmp.next, previous, tmp = previous, tmp, tmp.next
        k = k - 1
        if k == 0:
            return previous, node


l = SingleLinkedList()
l.add_list_item(2)
l.add_list_item(1)

l.printall()

r = reverseKGroup(l.root, 2)
tmp = ""
while r is not None:
    tmp = tmp + str(r.data) + '->'
    r = r.next
print(tmp[:-2])
Ejemplo n.º 9
0
def printdata(head):
    temp = head
    while temp is not None:
        print(temp.data)
        temp = temp.next


chromedriver = "E:\Python36-32\chromedriver\chromedriver.exe"
driver = webdriver.Chrome(chromedriver)
driver.get("https://maps.google.com")
elem = driver.find_element_by_name("q")
time.sleep(.500)
elem.clear()
elem.send_keys("chennai to madurai")
elem.send_keys(Keys.RETURN)
assert "No results found." not in driver.page_source or "There is no Internet connection" not in driver.page_source
time.sleep(2)
link = driver.current_url
linkfile = requests.get(link)
content = linkfile.text
index = content.find(" km")
buscontent = content
SingleList = SingleLinkedList()
findbusroute(buscontent, SingleList)
printdata(SingleList)

driver.close()
driver.quit()
print("Finished")
Ejemplo n.º 10
0
# Series of steps involved to implement the requirement are -
# Build a single linked list by prompting user to enter values
# Traverse the list
# Convert the linked list to an array (this is to abide by the requirement which says linked list can be walked once,
# reverse, length or size cannot be used)

# class SearchFromEnd:


def kth_element_from_end(converted_list, k):
    return converted_list[-k]


if __name__ == '__main__':

    new_linked_list = SingleLinkedList()

    nums = int(input("How many nodes do you want to create: "))
    if nums <= 0:
        raise ValueError("Invalid number of nodes..")

    for i in range(nums):
        value = int(input("Enter the value for the node:"))
        new_linked_list.make_new_list(value)

    # get list from linked list
    converted_array = new_linked_list.traverse_and_convert()

    kth_value = int(input("Enter the kth value to be returned from the end:"))
    print(kth_element_from_end(converted_array, kth_value))