<filename>data_structures.py<gh_stars>0
"""
this file contains sample code for a 
simple introduction to working with data
structures in Python

The video corresponding to this code is called "Algorithms and Data Structures-Full Course for Beginners from Treehouse"
"""

# here we are creating a list which is also known as an array.
# in python the array doesnt actually store the values but rather
# stores the pointers to the values.
first_list = [1, 2, 3]


# lets get the first value in the list
first_idx = first_list[0]


# searching for elements in a list in python
# these two methods are how to do linear search
def linear_array_search():
    if 1 in first_list:
        print(True)


def linear_for_loop():
    for n in first_list:
        if n == 1:
            print(True)

            break


def append_value(list):
    """
    this adds a value to
    an existing list. this
    doesnt occure the same
    runtime as it just adds 
    the value to the end of 
    the list making is a constant
    runtime
    """
    list.append(2)
    print(list)


# this is extending which adds two
# list together.
first_list.extend([4, 5, 6, 8])


"""
this next section we will be building 
a singly linked list
"""


class Node:
    """
    An object for storing a single node of a linked list.
    Models two attributes - data and the link to the next
    node in the list.
    """

    data = None
    next_node = None

    def __init__(self, data):
        self.data = data

    def __repr__(self):
        return "<Node data: %s>" % self.data


class LinkedList:
    """
    singly linked list
    """

    def __init__(self):
        # this is only node that the linked list will have a reference to.
        self.head = None

    def is_empty(self):
        """
        this checks to see if
        the list is empty.
        """
        return self.head == None

    def size(self):
        """
        this is to check the size of
        our list. it has a linear runtime
        because it goes through each node in the list.
        """
        # get reference to the head
        current = self.head

        count = 0

        while current:
            count += 1
            current = current.next_node

        return count

    def add(self, data):
        """
        this is going to accept some data
        and add it to the list inside of a node.
        this has a constant runtime.
        """
        new_node = Node(data)
        new_node.next_node = self.head
        self.head = new_node

    def search(self, key):
        """
        Search for the first node containing data that matches the key
        returns node or None if not found.
        has a linear runtime
        """
        current = self.head

        while current:
            if current.data == key:
                return current
            else:
                current == current.next_node
        return None

    def __repr__(self):
        """
        return a string representation of the list
        Takes 0(n) time
        """

        nodes = []
        current = self.head

        while current:
            if current is self.head:
                nodes.append("[Head: %s]" % current.data)
            elif current.next_node is None:
                nodes.append("[Tail: %s]" % current.data)
            else:
                nodes.append("[%s]" % current.data)

            current = current.next_node
        # it should look like this [Head: 3]-> [2]-> [Tail: 1]
        return "->".join(nodes)

<filename>data_structures.py<gh_stars>0
"""
this file contains sample code for a 
simple introduction to working with data
structures in Python

The video corresponding to this code is called "Algorithms and Data Structures-Full Course for Beginners from Treehouse"
"""

# here we are creating a list which is also known as an array.
# in python the array doesnt actually store the values but rather
# stores the pointers to the values.
first_list = [1, 2, 3]


# lets get the first value in the list
first_idx = first_list[0]


# searching for elements in a list in python
# these two methods are how to do linear search
def linear_array_search():
    if 1 in first_list:
        print(True)


def linear_for_loop():
    for n in first_list:
        if n == 1:
            print(True)

            break


def append_value(list):
    """
    this adds a value to
    an existing list. this
    doesnt occure the same
    runtime as it just adds 
    the value to the end of 
    the list making is a constant
    runtime
    """
    list.append(2)
    print(list)


# this is extending which adds two
# list together.
first_list.extend([4, 5, 6, 8])


"""
this next section we will be building 
a singly linked list
"""


class Node:
    """
    An object for storing a single node of a linked list.
    Models two attributes - data and the link to the next
    node in the list.
    """

    data = None
    next_node = None

    def __init__(self, data):
        self.data = data

    def __repr__(self):
        return "<Node data: %s>" % self.data


class LinkedList:
    """
    singly linked list
    """

    def __init__(self):
        # this is only node that the linked list will have a reference to.
        self.head = None

    def is_empty(self):
        """
        this checks to see if
        the list is empty.
        """
        return self.head == None

    def size(self):
        """
        this is to check the size of
        our list. it has a linear runtime
        because it goes through each node in the list.
        """
        # get reference to the head
        current = self.head

        count = 0

        while current:
            count += 1
            current = current.next_node

        return count

    def add(self, data):
        """
        this is going to accept some data
        and add it to the list inside of a node.
        this has a constant runtime.
        """
        new_node = Node(data)
        new_node.next_node = self.head
        self.head = new_node

    def search(self, key):
        """
        Search for the first node containing data that matches the key
        returns node or None if not found.
        has a linear runtime
        """
        current = self.head

        while current:
            if current.data == key:
                return current
            else:
                current == current.next_node
        return None

    def __repr__(self):
        """
        return a string representation of the list
        Takes 0(n) time
        """

        nodes = []
        current = self.head

        while current:
            if current is self.head:
                nodes.append("[Head: %s]" % current.data)
            elif current.next_node is None:
                nodes.append("[Tail: %s]" % current.data)
            else:
                nodes.append("[%s]" % current.data)

            current = current.next_node
        # it should look like this [Head: 3]-> [2]-> [Tail: 1]
        return "->".join(nodes)

this file contains sample code for a  simple introduction to working with data structures in Python  The video corresponding to this code is called "Algorithms and Data Structures-Full Course for Beginners from Treehouse" # here we are creating a list which is also known as an array. # in python the array doesnt actually store the values but rather # stores the pointers to the values. # lets get the first value in the list # searching for elements in a list in python # these two methods are how to do linear search this adds a value to     an existing list. this     doesnt occure the same     runtime as it just adds      the value to the end of      the list making is a constant     runtime # this is extending which adds two # list together. this next section we will be building  a singly linked list An object for storing a single node of a linked list.     Models two attributes - data and the link to the next     node in the list. singly linked list # this is only node that the linked list will have a reference to. this checks to see if         the list is empty. this is to check the size of         our list. it has a linear runtime         because it goes through each node in the list. # get reference to the head this is going to accept some data         and add it to the list inside of a node.         this has a constant runtime. Search for the first node containing data that matches the key         returns node or None if not found.         has a linear runtime return a string representation of the list         Takes 0(n) time # it should look like this [Head: 3]-> [2]-> [Tail: 1]