Lecture
Hi
everyone!
So this
week’s lectures were based on learning about the Linked List class. This class
is pretty similar to a nested list, except the LinkedLists have a first
parameter as an item, and the second as a LinkedList. Here is an example of the
class LinkedList:
class LinkedList:
"""Linked list class"""
def __init__(self: 'LinkedList', item: object=None,
rest: 'LinkedList'=None) ->
None:
"""Create a new LinkedList
item - The first element of the linked list,
Not present if self will be the empty linked list.
rest - The linked list that contains the elements after item.
Not present if self will be the empty linked list."""
self.empty = (item is None) and (rest is None)
if not self.empty:
self.item = item
if rest is None:
self.rest = LinkedList()
else:
self.rest = rest
As the description
says, the first item in the LinkedList is either a value, or it’s empty, and “rest”
is either a LinkedList or empty. Some methods that we’ve created for the class
LinkedList include:
Prepend (Put an
item at the front of the list, and move the previous first item into “rest”)
__repr__(Returns a
string representation of LinkedList)
Decapitate (Delete
the first item in the LinkedList)
This week we learnt
how to implement some of these methods in the LinkedList class, for example:
def prepend(self: 'LinkedList', head: object) -> None:
old_head = copy(self)
self.rest = old_head
self.head = head
self.empty = False
We also learnt how to implement other methods, such as insertion and
deletion, basically how to add and remove values from the LinkedList. Luckily,
I found it pretty easy to understand since it’s similar to how nested lists
work, and the recursive functions we’d have to write would be similar to nested
list functions.
Lab 7
In this week’s lab,
we had to implement a few methods into the LinkedList class such as:
__len__
__setitem__
__delitem__
insert
After last week’s
lab where we had to write our old class Queue and it’s methods by using
LinkedList methods, this week’s lab seemed a lot easier since I now had a
better understanding of LinkedList and how it’s methods worked. Although we had
to use a lot of recursion to implement these methods, my partner and I ended up
completing this lab without too much difficulty.
Assignment 2 Part 1
And finally the
Assignment update! The link for Assignment 2 is here.
At first, when my partner and I started reading the assignment, it seemed a bit
complicated, especially since trees were a new concept to us. We were confused
about why we’d need multiple classes represent the trees, however, after a bit
more reading and coding, we found it actually made things a lot easier if we
made classes for each different type of regex, and using a Regex superclass
which we could inherit __init__ and __eq__method, and then simply write out the
__repr__ method for each class (since each one would start differently,
inheritance wouldn’t be effective here).
That’s pretty much
it for this week!
No comments:
Post a Comment