SlideShare a Scribd company logo
Mod 4 Homeork Enhanced DoublyLinkedList
Starter Code:
# Do not modify this class
class Node:
'Node object to be used in DoublyLinkedList'
def __init__(self, item, _next=None, _prev=None):
'initializes new node objects'
self.item = item
self._next = _next
self._prev = _prev
def __repr__(self):
'String representation of Node'
return f"Node({self.item})"
class DoublyLinkedList:
def __init__(self, items=None):
'Construct a new DLL object'
self._head = None
self._tail = None
self._len = 0
self._nodes = dict() # dictionary of item:node pairs
# initialize list w/ items if specified
if items is not None:
for item in items:
self.add_last(item)
def __len__(self):
'returns number of nodes in DLL'
return self._len
# TODO: Modify the 4 methods below to keep `self._nodes` up-to-date
def add_first(self, item):
'adds item to front of dll'
# add new node as head
self._head = Node(item, _next=self._head, _prev=None)
self._len += 1
# if that was the first node
if len(self) == 1: self._tail = self._head
# otherwise, redirect old heads ._tail pointer
else: self._head._next._prev = self._head
def add_last(self, item):
'adds item to end of dll'
# add new node as head
self._tail = Node(item, _next=None, _prev=self._tail)
self._len += 1
# if that was the first node
if len(self) == 1: self._head = self._tail
# otherwise, redirect old heads ._tail pointer
else: self._tail._prev._next = self._tail
def remove_first(self):
'removes and returns first item'
if len(self) == 0: raise RuntimeError("cannot remove from empty dll")
# extract item for later
item = self._head.item
# move up head pointer
self._head = self._head._next
self._len -= 1
# was that the last node?
if len(self) == 0: self._tail = None
else: self._head._prev = None
return item
def remove_last(self):
'removes and returns last item'
if len(self) == 0: raise RuntimeError("cannot remove from empty dll")
# extract item for later
item = self._tail.item
# move up tail pointer
self._tail = self._tail._prev
self._len -= 1
# was that the last node?
if len(self) == 0: self._head = None
else: self._tail._next = None
return item
# TODO: Add a docstring and implement
def __contains__(self, item):
raise NotImplementedError
# TODO: Add a docstring and implement
def neighbors(self, item):
raise NotImplementedError
# TODO: Add a docstring and implement
def remove_node(self, item):
raise NotImplementedError
Mod 4 Homework - Enhanced DoublyLinkedList Doubly Linked Lists (DLLs) support O ( 1 )
addition and removal from either end, making them a great choice for queues or deques. A
downside of DLLs (and other linked data structures) is that it takes linear time ( O ( n )) to access
nodes in the middle - in a traditional DLL, we have to start at the beginning and continually call
node._next until we get to the node we're looking for. Here, we'll enhance a DLL with a
dictionary of item:node pairs to give us O ( 1 ) access to interior nodes. This will speed up
membership testing, node insertion at arbitrary locations, and removal of arbitrary nodes to O ( 1
) . This is a common design pattern we'll see with other linked data structures like heaps and
graphs. Some downsides of this approach to keep in mind: - Dictionary keys must be unique, so
this will only work if node items are unique. - Storing and maintaining the dictionary will not
asymptotically worsen our O ( 1 ) running times or O ( n ) memory usage, but it will negatively
impact both. Figure 1: (a) In a standard DLL, we can only access the head and tail nodes in O ( 1
) . (b) By using a dictionary of item:node pairs, we can immediately jump to any node, as long as
we know which item we are interested in. 1) Add a dictionary DoublyLinkedList.py provides
basic Node and DoublyLinkedList classes that supports typical DLL operations. Modify these
methods to keep the nodes dictionary up to date with item:node pairs as you add and remove
from the DLL so you can access any node in O ( 1 ) . 2) Implement ___ contains__(item) _nodes
is a private attribute, and thus should not be called explicilty in your tests. Instead, use _nodes to
> dll = DoublyLinkedList (range (5)) 3 in dll # O ( 1 ) , even though 3 is in the middle True 5 in
dll # Note the use of 'in' to call the dunder method __contains__() False Write a unittest that
verifies contains works as expected as you add and remove nodes. 3) Implement neighbors
(item) Add a method neighbors (item) to your DoublyLinkedList class that returns the items
immediately before and after the node with item: dll = DoublyLinkedList(range (5)) dll
.neighbors(3) ( 2 , 4 ) dll .neighbors(0) # Edge case - head (None, 1) dll.neighbors(4) # Edge
case - tail (3, None) Add a method neighbors (item) to your DoublyLinkedList class that returns
the items immediately before and after the node with item: > d l 1 = DoublyLinkedList(range
(5)) > d ll .neighbors(3) ( 2 , 4 ) > d ll .neighbors(0) # Edge case - head (None, 1)
dll.neighbors(4) # Edge case - tail (3, None) - Should be O ( 1 ) - When called on the item stored
in the head/tail, return None as the previous/next item, as shown above - Raise a RuntimeError if
someone searches for an item not in the DLL Include unittests for the above behavior (except the
running time). 4) Implement remove_node (item) Add a method that removes the node
containing an item from your DLL. - O ( 1 ) - Make sure to "stitch together" the adjacent nodes -
Edge cases you should be able to handle: - Raise a RuntimeError if someone tries to remove an
item not in the DLL - Removing the head - Removing the tail Include unittests for the above
behavior (except the running time).

More Related Content

Similar to Mod 4 Homeork Enhanced DoublyLinkedList Starter Code- # Do not modify.docx

[Python] [Singly-linked list] [Element insertion] Consider the follo.pdf
[Python] [Singly-linked list] [Element insertion] Consider the follo.pdf[Python] [Singly-linked list] [Element insertion] Consider the follo.pdf
[Python] [Singly-linked list] [Element insertion] Consider the follo.pdf
isenbergwarne4100
 
How to capture a variable in C# and not to shoot yourself in the foot
How to capture a variable in C# and not to shoot yourself in the footHow to capture a variable in C# and not to shoot yourself in the foot
How to capture a variable in C# and not to shoot yourself in the foot
PVS-Studio
 
How to capture a variable in C# and not to shoot yourself in the foot
How to capture a variable in C# and not to shoot yourself in the footHow to capture a variable in C# and not to shoot yourself in the foot
How to capture a variable in C# and not to shoot yourself in the foot
Sofia Fateeva
 
1183 c-interview-questions-and-answers
1183 c-interview-questions-and-answers1183 c-interview-questions-and-answers
1183 c-interview-questions-and-answers
Akash Gawali
 
advancedR.pdf
advancedR.pdfadvancedR.pdf
advancedR.pdf
RukhsanaTaj2
 
Advanced r
Advanced rAdvanced r
Basic R Learning
Basic R LearningBasic R Learning
Basic R Learning
Kumar P
 
Advanced R cheat sheet
Advanced R cheat sheetAdvanced R cheat sheet
Advanced R cheat sheet
Dr. Volkan OBAN
 
The Ring programming language version 1.4.1 book - Part 29 of 31
The Ring programming language version 1.4.1 book - Part 29 of 31The Ring programming language version 1.4.1 book - Part 29 of 31
The Ring programming language version 1.4.1 book - Part 29 of 31
Mahmoud Samir Fayed
 
UNIT 3a.pptx
UNIT 3a.pptxUNIT 3a.pptx
UNIT 3a.pptx
jack881
 
singly link list project in dsa.....by rohit malav
singly link list project in dsa.....by rohit malavsingly link list project in dsa.....by rohit malav
singly link list project in dsa.....by rohit malav
Rohit malav
 
The Ring programming language version 1.8 book - Part 7 of 202
The Ring programming language version 1.8 book - Part 7 of 202The Ring programming language version 1.8 book - Part 7 of 202
The Ring programming language version 1.8 book - Part 7 of 202
Mahmoud Samir Fayed
 
Scala Intro
Scala IntroScala Intro
The Ring programming language version 1.5.2 book - Part 37 of 181
The Ring programming language version 1.5.2 book - Part 37 of 181The Ring programming language version 1.5.2 book - Part 37 of 181
The Ring programming language version 1.5.2 book - Part 37 of 181
Mahmoud Samir Fayed
 
07slide.ppt
07slide.ppt07slide.ppt
07slide.ppt
NuurAxmed2
 
linked.ppt
linked.pptlinked.ppt
linked.ppt
ssuser90b1872
 
These questions will be a bit advanced level 2
These questions will be a bit advanced level 2These questions will be a bit advanced level 2
These questions will be a bit advanced level 2
sadhana312471
 
Polymorphism.pptx
Polymorphism.pptxPolymorphism.pptx
Polymorphism.pptx
Vijaykota11
 
C++ Interview Question And Answer
C++ Interview Question And AnswerC++ Interview Question And Answer
C++ Interview Question And Answer
Jagan Mohan Bishoyi
 
C++ questions And Answer
C++ questions And AnswerC++ questions And Answer
C++ questions And Answer
lavparmar007
 

Similar to Mod 4 Homeork Enhanced DoublyLinkedList Starter Code- # Do not modify.docx (20)

[Python] [Singly-linked list] [Element insertion] Consider the follo.pdf
[Python] [Singly-linked list] [Element insertion] Consider the follo.pdf[Python] [Singly-linked list] [Element insertion] Consider the follo.pdf
[Python] [Singly-linked list] [Element insertion] Consider the follo.pdf
 
How to capture a variable in C# and not to shoot yourself in the foot
How to capture a variable in C# and not to shoot yourself in the footHow to capture a variable in C# and not to shoot yourself in the foot
How to capture a variable in C# and not to shoot yourself in the foot
 
How to capture a variable in C# and not to shoot yourself in the foot
How to capture a variable in C# and not to shoot yourself in the footHow to capture a variable in C# and not to shoot yourself in the foot
How to capture a variable in C# and not to shoot yourself in the foot
 
1183 c-interview-questions-and-answers
1183 c-interview-questions-and-answers1183 c-interview-questions-and-answers
1183 c-interview-questions-and-answers
 
advancedR.pdf
advancedR.pdfadvancedR.pdf
advancedR.pdf
 
Advanced r
Advanced rAdvanced r
Advanced r
 
Basic R Learning
Basic R LearningBasic R Learning
Basic R Learning
 
Advanced R cheat sheet
Advanced R cheat sheetAdvanced R cheat sheet
Advanced R cheat sheet
 
The Ring programming language version 1.4.1 book - Part 29 of 31
The Ring programming language version 1.4.1 book - Part 29 of 31The Ring programming language version 1.4.1 book - Part 29 of 31
The Ring programming language version 1.4.1 book - Part 29 of 31
 
UNIT 3a.pptx
UNIT 3a.pptxUNIT 3a.pptx
UNIT 3a.pptx
 
singly link list project in dsa.....by rohit malav
singly link list project in dsa.....by rohit malavsingly link list project in dsa.....by rohit malav
singly link list project in dsa.....by rohit malav
 
The Ring programming language version 1.8 book - Part 7 of 202
The Ring programming language version 1.8 book - Part 7 of 202The Ring programming language version 1.8 book - Part 7 of 202
The Ring programming language version 1.8 book - Part 7 of 202
 
Scala Intro
Scala IntroScala Intro
Scala Intro
 
The Ring programming language version 1.5.2 book - Part 37 of 181
The Ring programming language version 1.5.2 book - Part 37 of 181The Ring programming language version 1.5.2 book - Part 37 of 181
The Ring programming language version 1.5.2 book - Part 37 of 181
 
07slide.ppt
07slide.ppt07slide.ppt
07slide.ppt
 
linked.ppt
linked.pptlinked.ppt
linked.ppt
 
These questions will be a bit advanced level 2
These questions will be a bit advanced level 2These questions will be a bit advanced level 2
These questions will be a bit advanced level 2
 
Polymorphism.pptx
Polymorphism.pptxPolymorphism.pptx
Polymorphism.pptx
 
C++ Interview Question And Answer
C++ Interview Question And AnswerC++ Interview Question And Answer
C++ Interview Question And Answer
 
C++ questions And Answer
C++ questions And AnswerC++ questions And Answer
C++ questions And Answer
 

More from Jason0x0Scottw

A company's stock price rose 3-4- in 2011 - and in 2012 - it increased.docx
A company's stock price rose 3-4- in 2011 - and in 2012 - it increased.docxA company's stock price rose 3-4- in 2011 - and in 2012 - it increased.docx
A company's stock price rose 3-4- in 2011 - and in 2012 - it increased.docx
Jason0x0Scottw
 
A company has the following- Dec- 1 Beginning inventory 15 units at $6.docx
A company has the following- Dec- 1 Beginning inventory 15 units at $6.docxA company has the following- Dec- 1 Beginning inventory 15 units at $6.docx
A company has the following- Dec- 1 Beginning inventory 15 units at $6.docx
Jason0x0Scottw
 
A company must perform a maintenance project consisting of seven activ.docx
A company must perform a maintenance project consisting of seven activ.docxA company must perform a maintenance project consisting of seven activ.docx
A company must perform a maintenance project consisting of seven activ.docx
Jason0x0Scottw
 
A company has paid out all its current and accumulated earnings a prof.docx
A company has paid out all its current and accumulated earnings a prof.docxA company has paid out all its current and accumulated earnings a prof.docx
A company has paid out all its current and accumulated earnings a prof.docx
Jason0x0Scottw
 
New Public Management- clearly provide the following- (1) establish th.docx
New Public Management- clearly provide the following- (1) establish th.docxNew Public Management- clearly provide the following- (1) establish th.docx
New Public Management- clearly provide the following- (1) establish th.docx
Jason0x0Scottw
 
New Corporation- a C corporation- had the following items for the curr.docx
New Corporation- a C corporation- had the following items for the curr.docxNew Corporation- a C corporation- had the following items for the curr.docx
New Corporation- a C corporation- had the following items for the curr.docx
Jason0x0Scottw
 
networking networking Which of the following Topologies are available.docx
networking networking Which of the following Topologies are available.docxnetworking networking Which of the following Topologies are available.docx
networking networking Which of the following Topologies are available.docx
Jason0x0Scottw
 
Neighborhood Effects 1- What is the neighborhood effect- 2- How to ide.docx
Neighborhood Effects 1- What is the neighborhood effect- 2- How to ide.docxNeighborhood Effects 1- What is the neighborhood effect- 2- How to ide.docx
Neighborhood Effects 1- What is the neighborhood effect- 2- How to ide.docx
Jason0x0Scottw
 
need help with filling out the results table!! ( provided pictures of.docx
need help with filling out the results table!!  ( provided pictures of.docxneed help with filling out the results table!!  ( provided pictures of.docx
need help with filling out the results table!! ( provided pictures of.docx
Jason0x0Scottw
 
need help with both please Methanogens are a group of archaea that ge.docx
need help with both please  Methanogens are a group of archaea that ge.docxneed help with both please  Methanogens are a group of archaea that ge.docx
need help with both please Methanogens are a group of archaea that ge.docx
Jason0x0Scottw
 
Need help finishing this doubly linked list code- Commented lines are.docx
Need help finishing this doubly linked list code- Commented lines are.docxNeed help finishing this doubly linked list code- Commented lines are.docx
Need help finishing this doubly linked list code- Commented lines are.docx
Jason0x0Scottw
 
Need help getting past an error in C++! I have all my code pasted down.docx
Need help getting past an error in C++! I have all my code pasted down.docxNeed help getting past an error in C++! I have all my code pasted down.docx
Need help getting past an error in C++! I have all my code pasted down.docx
Jason0x0Scottw
 
Narrow visible light emission Below left is a familiar visible-light v.docx
Narrow visible light emission Below left is a familiar visible-light v.docxNarrow visible light emission Below left is a familiar visible-light v.docx
Narrow visible light emission Below left is a familiar visible-light v.docx
Jason0x0Scottw
 
Name the three major hormones produced by thyroid tissues and give spe.docx
Name the three major hormones produced by thyroid tissues and give spe.docxName the three major hormones produced by thyroid tissues and give spe.docx
Name the three major hormones produced by thyroid tissues and give spe.docx
Jason0x0Scottw
 
Name the type and tunics of blood vessels and the major tissue typers).docx
Name the type and tunics of blood vessels and the major tissue typers).docxName the type and tunics of blood vessels and the major tissue typers).docx
Name the type and tunics of blood vessels and the major tissue typers).docx
Jason0x0Scottw
 
name it Provide the names of Bone A and Bone D.docx
name it Provide the names of Bone A and Bone D.docxname it Provide the names of Bone A and Bone D.docx
name it Provide the names of Bone A and Bone D.docx
Jason0x0Scottw
 
name it Provide the name of Letter A - Letter B and Letter C Be sure t.docx
name it Provide the name of Letter A - Letter B and Letter C Be sure t.docxname it Provide the name of Letter A - Letter B and Letter C Be sure t.docx
name it Provide the name of Letter A - Letter B and Letter C Be sure t.docx
Jason0x0Scottw
 
Name and define each component of the preparedness cycle- What is a HV.docx
Name and define each component of the preparedness cycle- What is a HV.docxName and define each component of the preparedness cycle- What is a HV.docx
Name and define each component of the preparedness cycle- What is a HV.docx
Jason0x0Scottw
 
n-.docx
n-.docxn-.docx
Myopia (nearsightedness) Rays focus in front of retina Concave lens co.docx
Myopia (nearsightedness) Rays focus in front of retina Concave lens co.docxMyopia (nearsightedness) Rays focus in front of retina Concave lens co.docx
Myopia (nearsightedness) Rays focus in front of retina Concave lens co.docx
Jason0x0Scottw
 

More from Jason0x0Scottw (20)

A company's stock price rose 3-4- in 2011 - and in 2012 - it increased.docx
A company's stock price rose 3-4- in 2011 - and in 2012 - it increased.docxA company's stock price rose 3-4- in 2011 - and in 2012 - it increased.docx
A company's stock price rose 3-4- in 2011 - and in 2012 - it increased.docx
 
A company has the following- Dec- 1 Beginning inventory 15 units at $6.docx
A company has the following- Dec- 1 Beginning inventory 15 units at $6.docxA company has the following- Dec- 1 Beginning inventory 15 units at $6.docx
A company has the following- Dec- 1 Beginning inventory 15 units at $6.docx
 
A company must perform a maintenance project consisting of seven activ.docx
A company must perform a maintenance project consisting of seven activ.docxA company must perform a maintenance project consisting of seven activ.docx
A company must perform a maintenance project consisting of seven activ.docx
 
A company has paid out all its current and accumulated earnings a prof.docx
A company has paid out all its current and accumulated earnings a prof.docxA company has paid out all its current and accumulated earnings a prof.docx
A company has paid out all its current and accumulated earnings a prof.docx
 
New Public Management- clearly provide the following- (1) establish th.docx
New Public Management- clearly provide the following- (1) establish th.docxNew Public Management- clearly provide the following- (1) establish th.docx
New Public Management- clearly provide the following- (1) establish th.docx
 
New Corporation- a C corporation- had the following items for the curr.docx
New Corporation- a C corporation- had the following items for the curr.docxNew Corporation- a C corporation- had the following items for the curr.docx
New Corporation- a C corporation- had the following items for the curr.docx
 
networking networking Which of the following Topologies are available.docx
networking networking Which of the following Topologies are available.docxnetworking networking Which of the following Topologies are available.docx
networking networking Which of the following Topologies are available.docx
 
Neighborhood Effects 1- What is the neighborhood effect- 2- How to ide.docx
Neighborhood Effects 1- What is the neighborhood effect- 2- How to ide.docxNeighborhood Effects 1- What is the neighborhood effect- 2- How to ide.docx
Neighborhood Effects 1- What is the neighborhood effect- 2- How to ide.docx
 
need help with filling out the results table!! ( provided pictures of.docx
need help with filling out the results table!!  ( provided pictures of.docxneed help with filling out the results table!!  ( provided pictures of.docx
need help with filling out the results table!! ( provided pictures of.docx
 
need help with both please Methanogens are a group of archaea that ge.docx
need help with both please  Methanogens are a group of archaea that ge.docxneed help with both please  Methanogens are a group of archaea that ge.docx
need help with both please Methanogens are a group of archaea that ge.docx
 
Need help finishing this doubly linked list code- Commented lines are.docx
Need help finishing this doubly linked list code- Commented lines are.docxNeed help finishing this doubly linked list code- Commented lines are.docx
Need help finishing this doubly linked list code- Commented lines are.docx
 
Need help getting past an error in C++! I have all my code pasted down.docx
Need help getting past an error in C++! I have all my code pasted down.docxNeed help getting past an error in C++! I have all my code pasted down.docx
Need help getting past an error in C++! I have all my code pasted down.docx
 
Narrow visible light emission Below left is a familiar visible-light v.docx
Narrow visible light emission Below left is a familiar visible-light v.docxNarrow visible light emission Below left is a familiar visible-light v.docx
Narrow visible light emission Below left is a familiar visible-light v.docx
 
Name the three major hormones produced by thyroid tissues and give spe.docx
Name the three major hormones produced by thyroid tissues and give spe.docxName the three major hormones produced by thyroid tissues and give spe.docx
Name the three major hormones produced by thyroid tissues and give spe.docx
 
Name the type and tunics of blood vessels and the major tissue typers).docx
Name the type and tunics of blood vessels and the major tissue typers).docxName the type and tunics of blood vessels and the major tissue typers).docx
Name the type and tunics of blood vessels and the major tissue typers).docx
 
name it Provide the names of Bone A and Bone D.docx
name it Provide the names of Bone A and Bone D.docxname it Provide the names of Bone A and Bone D.docx
name it Provide the names of Bone A and Bone D.docx
 
name it Provide the name of Letter A - Letter B and Letter C Be sure t.docx
name it Provide the name of Letter A - Letter B and Letter C Be sure t.docxname it Provide the name of Letter A - Letter B and Letter C Be sure t.docx
name it Provide the name of Letter A - Letter B and Letter C Be sure t.docx
 
Name and define each component of the preparedness cycle- What is a HV.docx
Name and define each component of the preparedness cycle- What is a HV.docxName and define each component of the preparedness cycle- What is a HV.docx
Name and define each component of the preparedness cycle- What is a HV.docx
 
n-.docx
n-.docxn-.docx
n-.docx
 
Myopia (nearsightedness) Rays focus in front of retina Concave lens co.docx
Myopia (nearsightedness) Rays focus in front of retina Concave lens co.docxMyopia (nearsightedness) Rays focus in front of retina Concave lens co.docx
Myopia (nearsightedness) Rays focus in front of retina Concave lens co.docx
 

Recently uploaded

The Diamonds of 2023-2024 in the IGRA collection
The Diamonds of 2023-2024 in the IGRA collectionThe Diamonds of 2023-2024 in the IGRA collection
The Diamonds of 2023-2024 in the IGRA collection
Israel Genealogy Research Association
 
DRUGS AND ITS classification slide share
DRUGS AND ITS classification slide shareDRUGS AND ITS classification slide share
DRUGS AND ITS classification slide share
taiba qazi
 
MASS MEDIA STUDIES-835-CLASS XI Resource Material.pdf
MASS MEDIA STUDIES-835-CLASS XI Resource Material.pdfMASS MEDIA STUDIES-835-CLASS XI Resource Material.pdf
MASS MEDIA STUDIES-835-CLASS XI Resource Material.pdf
goswamiyash170123
 
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
Nguyen Thanh Tu Collection
 
Landownership in the Philippines under the Americans-2-pptx.pptx
Landownership in the Philippines under the Americans-2-pptx.pptxLandownership in the Philippines under the Americans-2-pptx.pptx
Landownership in the Philippines under the Americans-2-pptx.pptx
JezreelCabil2
 
PIMS Job Advertisement 2024.pdf Islamabad
PIMS Job Advertisement 2024.pdf IslamabadPIMS Job Advertisement 2024.pdf Islamabad
PIMS Job Advertisement 2024.pdf Islamabad
AyyanKhan40
 
PCOS corelations and management through Ayurveda.
PCOS corelations and management through Ayurveda.PCOS corelations and management through Ayurveda.
PCOS corelations and management through Ayurveda.
Dr. Shivangi Singh Parihar
 
A Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in EducationA Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in Education
Peter Windle
 
Delivering Micro-Credentials in Technical and Vocational Education and Training
Delivering Micro-Credentials in Technical and Vocational Education and TrainingDelivering Micro-Credentials in Technical and Vocational Education and Training
Delivering Micro-Credentials in Technical and Vocational Education and Training
AG2 Design
 
How to Build a Module in Odoo 17 Using the Scaffold Method
How to Build a Module in Odoo 17 Using the Scaffold MethodHow to Build a Module in Odoo 17 Using the Scaffold Method
How to Build a Module in Odoo 17 Using the Scaffold Method
Celine George
 
South African Journal of Science: Writing with integrity workshop (2024)
South African Journal of Science: Writing with integrity workshop (2024)South African Journal of Science: Writing with integrity workshop (2024)
South African Journal of Science: Writing with integrity workshop (2024)
Academy of Science of South Africa
 
Executive Directors Chat Leveraging AI for Diversity, Equity, and Inclusion
Executive Directors Chat  Leveraging AI for Diversity, Equity, and InclusionExecutive Directors Chat  Leveraging AI for Diversity, Equity, and Inclusion
Executive Directors Chat Leveraging AI for Diversity, Equity, and Inclusion
TechSoup
 
Thesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.pptThesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.ppt
EverAndrsGuerraGuerr
 
Acetabularia Information For Class 9 .docx
Acetabularia Information For Class 9  .docxAcetabularia Information For Class 9  .docx
Acetabularia Information For Class 9 .docx
vaibhavrinwa19
 
Normal Labour/ Stages of Labour/ Mechanism of Labour
Normal Labour/ Stages of Labour/ Mechanism of LabourNormal Labour/ Stages of Labour/ Mechanism of Labour
Normal Labour/ Stages of Labour/ Mechanism of Labour
Wasim Ak
 
Best Digital Marketing Institute In NOIDA
Best Digital Marketing Institute In NOIDABest Digital Marketing Institute In NOIDA
Best Digital Marketing Institute In NOIDA
deeptiverma2406
 
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama UniversityNatural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
Akanksha trivedi rama nursing college kanpur.
 
Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.
Ashokrao Mane college of Pharmacy Peth-Vadgaon
 
2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...
Sandy Millin
 
World environment day ppt For 5 June 2024
World environment day ppt For 5 June 2024World environment day ppt For 5 June 2024
World environment day ppt For 5 June 2024
ak6969907
 

Recently uploaded (20)

The Diamonds of 2023-2024 in the IGRA collection
The Diamonds of 2023-2024 in the IGRA collectionThe Diamonds of 2023-2024 in the IGRA collection
The Diamonds of 2023-2024 in the IGRA collection
 
DRUGS AND ITS classification slide share
DRUGS AND ITS classification slide shareDRUGS AND ITS classification slide share
DRUGS AND ITS classification slide share
 
MASS MEDIA STUDIES-835-CLASS XI Resource Material.pdf
MASS MEDIA STUDIES-835-CLASS XI Resource Material.pdfMASS MEDIA STUDIES-835-CLASS XI Resource Material.pdf
MASS MEDIA STUDIES-835-CLASS XI Resource Material.pdf
 
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
 
Landownership in the Philippines under the Americans-2-pptx.pptx
Landownership in the Philippines under the Americans-2-pptx.pptxLandownership in the Philippines under the Americans-2-pptx.pptx
Landownership in the Philippines under the Americans-2-pptx.pptx
 
PIMS Job Advertisement 2024.pdf Islamabad
PIMS Job Advertisement 2024.pdf IslamabadPIMS Job Advertisement 2024.pdf Islamabad
PIMS Job Advertisement 2024.pdf Islamabad
 
PCOS corelations and management through Ayurveda.
PCOS corelations and management through Ayurveda.PCOS corelations and management through Ayurveda.
PCOS corelations and management through Ayurveda.
 
A Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in EducationA Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in Education
 
Delivering Micro-Credentials in Technical and Vocational Education and Training
Delivering Micro-Credentials in Technical and Vocational Education and TrainingDelivering Micro-Credentials in Technical and Vocational Education and Training
Delivering Micro-Credentials in Technical and Vocational Education and Training
 
How to Build a Module in Odoo 17 Using the Scaffold Method
How to Build a Module in Odoo 17 Using the Scaffold MethodHow to Build a Module in Odoo 17 Using the Scaffold Method
How to Build a Module in Odoo 17 Using the Scaffold Method
 
South African Journal of Science: Writing with integrity workshop (2024)
South African Journal of Science: Writing with integrity workshop (2024)South African Journal of Science: Writing with integrity workshop (2024)
South African Journal of Science: Writing with integrity workshop (2024)
 
Executive Directors Chat Leveraging AI for Diversity, Equity, and Inclusion
Executive Directors Chat  Leveraging AI for Diversity, Equity, and InclusionExecutive Directors Chat  Leveraging AI for Diversity, Equity, and Inclusion
Executive Directors Chat Leveraging AI for Diversity, Equity, and Inclusion
 
Thesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.pptThesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.ppt
 
Acetabularia Information For Class 9 .docx
Acetabularia Information For Class 9  .docxAcetabularia Information For Class 9  .docx
Acetabularia Information For Class 9 .docx
 
Normal Labour/ Stages of Labour/ Mechanism of Labour
Normal Labour/ Stages of Labour/ Mechanism of LabourNormal Labour/ Stages of Labour/ Mechanism of Labour
Normal Labour/ Stages of Labour/ Mechanism of Labour
 
Best Digital Marketing Institute In NOIDA
Best Digital Marketing Institute In NOIDABest Digital Marketing Institute In NOIDA
Best Digital Marketing Institute In NOIDA
 
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama UniversityNatural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
 
Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.
 
2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...
 
World environment day ppt For 5 June 2024
World environment day ppt For 5 June 2024World environment day ppt For 5 June 2024
World environment day ppt For 5 June 2024
 

Mod 4 Homeork Enhanced DoublyLinkedList Starter Code- # Do not modify.docx

  • 1. Mod 4 Homeork Enhanced DoublyLinkedList Starter Code: # Do not modify this class class Node: 'Node object to be used in DoublyLinkedList' def __init__(self, item, _next=None, _prev=None): 'initializes new node objects' self.item = item self._next = _next self._prev = _prev def __repr__(self): 'String representation of Node' return f"Node({self.item})" class DoublyLinkedList: def __init__(self, items=None): 'Construct a new DLL object' self._head = None self._tail = None self._len = 0 self._nodes = dict() # dictionary of item:node pairs # initialize list w/ items if specified if items is not None: for item in items: self.add_last(item) def __len__(self): 'returns number of nodes in DLL' return self._len # TODO: Modify the 4 methods below to keep `self._nodes` up-to-date def add_first(self, item): 'adds item to front of dll' # add new node as head self._head = Node(item, _next=self._head, _prev=None) self._len += 1 # if that was the first node if len(self) == 1: self._tail = self._head
  • 2. # otherwise, redirect old heads ._tail pointer else: self._head._next._prev = self._head def add_last(self, item): 'adds item to end of dll' # add new node as head self._tail = Node(item, _next=None, _prev=self._tail) self._len += 1 # if that was the first node if len(self) == 1: self._head = self._tail # otherwise, redirect old heads ._tail pointer else: self._tail._prev._next = self._tail def remove_first(self): 'removes and returns first item' if len(self) == 0: raise RuntimeError("cannot remove from empty dll") # extract item for later item = self._head.item # move up head pointer self._head = self._head._next self._len -= 1 # was that the last node? if len(self) == 0: self._tail = None else: self._head._prev = None return item def remove_last(self): 'removes and returns last item' if len(self) == 0: raise RuntimeError("cannot remove from empty dll") # extract item for later item = self._tail.item # move up tail pointer self._tail = self._tail._prev self._len -= 1 # was that the last node? if len(self) == 0: self._head = None
  • 3. else: self._tail._next = None return item # TODO: Add a docstring and implement def __contains__(self, item): raise NotImplementedError # TODO: Add a docstring and implement def neighbors(self, item): raise NotImplementedError # TODO: Add a docstring and implement def remove_node(self, item): raise NotImplementedError Mod 4 Homework - Enhanced DoublyLinkedList Doubly Linked Lists (DLLs) support O ( 1 ) addition and removal from either end, making them a great choice for queues or deques. A downside of DLLs (and other linked data structures) is that it takes linear time ( O ( n )) to access nodes in the middle - in a traditional DLL, we have to start at the beginning and continually call node._next until we get to the node we're looking for. Here, we'll enhance a DLL with a dictionary of item:node pairs to give us O ( 1 ) access to interior nodes. This will speed up membership testing, node insertion at arbitrary locations, and removal of arbitrary nodes to O ( 1 ) . This is a common design pattern we'll see with other linked data structures like heaps and graphs. Some downsides of this approach to keep in mind: - Dictionary keys must be unique, so this will only work if node items are unique. - Storing and maintaining the dictionary will not asymptotically worsen our O ( 1 ) running times or O ( n ) memory usage, but it will negatively impact both. Figure 1: (a) In a standard DLL, we can only access the head and tail nodes in O ( 1 ) . (b) By using a dictionary of item:node pairs, we can immediately jump to any node, as long as we know which item we are interested in. 1) Add a dictionary DoublyLinkedList.py provides basic Node and DoublyLinkedList classes that supports typical DLL operations. Modify these methods to keep the nodes dictionary up to date with item:node pairs as you add and remove from the DLL so you can access any node in O ( 1 ) . 2) Implement ___ contains__(item) _nodes is a private attribute, and thus should not be called explicilty in your tests. Instead, use _nodes to > dll = DoublyLinkedList (range (5)) 3 in dll # O ( 1 ) , even though 3 is in the middle True 5 in dll # Note the use of 'in' to call the dunder method __contains__() False Write a unittest that verifies contains works as expected as you add and remove nodes. 3) Implement neighbors (item) Add a method neighbors (item) to your DoublyLinkedList class that returns the items immediately before and after the node with item: dll = DoublyLinkedList(range (5)) dll .neighbors(3) ( 2 , 4 ) dll .neighbors(0) # Edge case - head (None, 1) dll.neighbors(4) # Edge case - tail (3, None) Add a method neighbors (item) to your DoublyLinkedList class that returns the items immediately before and after the node with item: > d l 1 = DoublyLinkedList(range (5)) > d ll .neighbors(3) ( 2 , 4 ) > d ll .neighbors(0) # Edge case - head (None, 1) dll.neighbors(4) # Edge case - tail (3, None) - Should be O ( 1 ) - When called on the item stored in the head/tail, return None as the previous/next item, as shown above - Raise a RuntimeError if someone searches for an item not in the DLL Include unittests for the above behavior (except the
  • 4. running time). 4) Implement remove_node (item) Add a method that removes the node containing an item from your DLL. - O ( 1 ) - Make sure to "stitch together" the adjacent nodes - Edge cases you should be able to handle: - Raise a RuntimeError if someone tries to remove an item not in the DLL - Removing the head - Removing the tail Include unittests for the above behavior (except the running time).