algorithms and data structures for data science lists

38
Department of Computer Science Algorithms and Data Structures for Data Science CS 277 Brad Solomon September 9, 2021 Lists

Upload: others

Post on 19-Feb-2022

1 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Algorithms and Data Structures for Data Science Lists

Department of Computer Science

Algorithms and Data Structures for Data Science

CS 277 Brad Solomon

September 9, 2021

Lists

Page 2: Algorithms and Data Structures for Data Science Lists

lab_parsing Feedback

Average completion time looks to be ~3 hours

Not everyone found the lecture helpful

All students agreed lab matched learning objectives

Avg: 13.89 / 15

Page 3: Algorithms and Data Structures for Data Science Lists

Planning out a problem

Write down the input and output (make sure you know it!)

Work out by hand the steps you need to get the output

Figure out how to do the necessary steps

Write the code — and then test it!

Page 4: Algorithms and Data Structures for Data Science Lists

Reflecting on lab_parsing

bradsol,10,8,5,7,8 hdeep,10,8,5,7,8,9

Input: Output:

Page 5: Algorithms and Data Structures for Data Science Lists

Open Dialogue about CS 277

Page 6: Algorithms and Data Structures for Data Science Lists

lab_cipher adjustments

Vigenere Decode now extra credit

CaesarEncode / Decode significantly up-weighted

a b c d e f g h iEncode

Decode

Page 7: Algorithms and Data Structures for Data Science Lists

MPs designed as mini-data science projects

mp_racing adjustments

(Most) content is reasonable — clarity not so much

Future assignments will be simpler and shorter

PuzzleMovement extra credit — for now

Page 8: Algorithms and Data Structures for Data Science Lists

mp_racing: loadMap()

0 0

1 1 1

1 0 1

1 1 1_map.txt

Input: Output:

Page 9: Algorithms and Data Structures for Data Science Lists

3

5

3

7_cargo.txt

Input: Output:

mp_racing: loadCargo()

Page 10: Algorithms and Data Structures for Data Science Lists

mp_racing: checkSquare()

Input: Output:

Page 11: Algorithms and Data Structures for Data Science Lists

mp_racing: checkAvail() def checkAvail(self): if self.curr == None: return []

# racingBot's current position row = self.curr[0] col = self.curr[1]

# The return list avail = []

north = (0,0) if self.checkSquare(north): avail.append(north)

return avail

1 2 3 4 5 6 7 8 9

10 11 12 13 14 15 16 17 18 19 20 21 22 23

Page 12: Algorithms and Data Structures for Data Science Lists

Takeaway thoughts

There will be a proficiency exam in the near future

Course curriculum will slow down

Pacing and Content survey on Moodle

Page 13: Algorithms and Data Structures for Data Science Lists

Learning Objectives

Define the list abstract data type

Discuss implementation strategies for lists

Debate which strategy is better (and how to judge ‘better’)

Page 14: Algorithms and Data Structures for Data Science Lists

List Abstract Data TypeValues:

Functions:

Page 15: Algorithms and Data Structures for Data Science Lists

List Implementations

1.

2.

Page 16: Algorithms and Data Structures for Data Science Lists

Linked List

C S 2 7 7None

Head

Page 17: Algorithms and Data Structures for Data Science Lists

Creating a linked listclass Node: def __init__(self, data, next=None): self.data = data self.next = next

class linkedList: def __init__(self, head=None): self.head = head

if __name__ == ‘__main__': n1 = Node(3) n2 = Node(5) n3 = Node(7)

n1.next = n2 n2.next = n3 n3.next = n1

ll = linkedList(n1)

1 2 3 4 5 6 7 8 9

10 11 12 13 14 15 16 17 18 19 20 21 22 23

Page 18: Algorithms and Data Structures for Data Science Lists

Linked List

C S 2 7 7None

Add(B)Head

Page 19: Algorithms and Data Structures for Data Science Lists

Linked List Addclass Node: def __init__(self, data, next=None): self.data = data self.next = next

class linkedList: def __init__(self, head=None): self.head = head

def add(self, data): temp = self.head self.head = Node(data, temp)

if __name__ == ‘__main__': ll = linkedList()

for i in range(3): ll.add(i)

1 2 3 4 5 6 7 8 9

10 11 12 13 14 15 16 17 18 19 20 21 22 23

Page 20: Algorithms and Data Structures for Data Science Lists

Linked List

C S 2 7 7None

Index(3)Head

Page 21: Algorithms and Data Structures for Data Science Lists

Linked List Indexclass linkedList:

def index(self, pos): curr = self.head

i = 0 while(curr and i < pos): curr = curr.next i+=1 if i == pos: return curr else: raise ValueError(“Out of bounds”) return None

1 2 3 4 5 6 7 8 9

10 11 12 13 14 15 16 17 18 19 20 21 22 23

Page 22: Algorithms and Data Structures for Data Science Lists

Linked List

C S 2 7 7None

Insert(X,2)Head

Page 23: Algorithms and Data Structures for Data Science Lists

Linked List Insertclass linkedList:

def insert(self, data, pos=0): if (pos == 0): self.add(data) else: prev = self.index(pos-1)

temp = prev.next

prev.next = Node(data,temp)

1 2 3 4 5 6 7 8 9

10 11 12 13 14 15 16 17 18 19 20 21 22 23

Page 24: Algorithms and Data Structures for Data Science Lists

Linked List

C S 2 7 7None

Remove(7)Head

Page 25: Algorithms and Data Structures for Data Science Lists

Linked List Removeclass linkedList: def remove(self, data): prev = None curr = self.head while(curr): if curr.data == data: if prev == None: self.head = self.head.next break else: prev.next = curr.next break else: prev = curr curr = curr.next

1 2 3 4 5 6 7 8 9

10 11 12 13 14 15 16 17 18 19 20 21 22 23

Page 26: Algorithms and Data Structures for Data Science Lists

Linked List

C S 2 7 7None

Pop(4)Head

Page 27: Algorithms and Data Structures for Data Science Lists

Linked List Popclass linkedList: def pop(self, pos): if(pos == 0): temp = self.head.data self.head = self.head.next return temp else: prev = self.index(pos-1) temp = prev.next prev.next = temp.next return temp.data

1 2 3 4 5 6 7 8 9

10 11 12 13 14 15 16 17 18 19 20 21 22 23

Page 28: Algorithms and Data Structures for Data Science Lists

Exercise 1ll = linkedList()

for i in range(3): ll.add(i)

ll.insert(6,1) ll.insert(3,1) ll.insert(10,3)

ll.remove(6) ll.remove(2) ll.remove(12)

ll.pop(2) ll.pop(0)

1 2 3 4 5 6 7 8 9

10 11 12 13 14 15 16 17 18 19 20 21 22 23

Page 29: Algorithms and Data Structures for Data Science Lists

List Implementations

1. Linked List

2. Arrays

Page 30: Algorithms and Data Structures for Data Science Lists

Array

Page 31: Algorithms and Data Structures for Data Science Lists

Creating an array listclass arrayList: def __init__(self, array=[]): self.array = array

1 2 3

Page 32: Algorithms and Data Structures for Data Science Lists

Array Adddef add(self, data): self.array.insert(0,data)

1 2 3

Page 33: Algorithms and Data Structures for Data Science Lists

Array Indexdef index(self, pos): return self.array[pos]

1 2 3

Page 34: Algorithms and Data Structures for Data Science Lists

Array Insertdef insert(self, data, pos=0): self.array.insert(pos, data)

1 2 3

Page 35: Algorithms and Data Structures for Data Science Lists

Array Removedef remove(self, data): self.array.remove(data)

1 2 3

Page 36: Algorithms and Data Structures for Data Science Lists

Array Popdef pop(self, pos): return self.array.pop(pos)

1 2 3

Page 37: Algorithms and Data Structures for Data Science Lists

Which implementation is better?

Page 38: Algorithms and Data Structures for Data Science Lists

What do we care about when we write code?