LING 388 Computers and Language Lecture 7 Administrivia

  • Slides: 13
Download presentation
LING 388: Computers and Language Lecture 7

LING 388: Computers and Language Lecture 7

Administrivia • Last time: Strings • Terminal log for lecture 6 available • This

Administrivia • Last time: Strings • Terminal log for lecture 6 available • This time: Lists • Quick Homework 5

Python • https: //docs. python. org/3/tutorial/introduction. html • Numbers • Strings • Lists •

Python • https: //docs. python. org/3/tutorial/introduction. html • Numbers • Strings • Lists • Dictionaries

Python Lists Recall strings from last time? Some similarities: indexing, slicing Think of those

Python Lists Recall strings from last time? Some similarities: indexing, slicing Think of those as lists too

Python Lists vs. Sets • in – membership • not in – not a

Python Lists vs. Sets • in – membership • not in – not a member of • set(List) - produces a set, no duplicates permitted • set is faster than list for lookup (hashtable)

Python Lists

Python Lists

Python Lists https: //visualgo. net/en/list? slide=4 • Lists as stacks • Lists as queues

Python Lists https: //visualgo. net/en/list? slide=4 • Lists as stacks • Lists as queues • List Comprehensions (advanced topic) https: //www. appcoda. com/ios-concurrency/

Python List as a Stack • Note that. pop() removes from the right end

Python List as a Stack • Note that. pop() removes from the right end and. append() adds to the right end. • How to save the element of a list popped off the stack? Use a variable, e. g. x: • x = list. pop() • Stacking operations are: • list. append() • list. pop()

Python List as a Stack • Suppose we have list = ['a', 'c', 'b']

Python List as a Stack • Suppose we have list = ['a', 'c', 'b'] • How do we flip the order of b and c using stack operations? • Answer: >>> x 1 = list. pop() >>> x 2 = list. pop() >>> x 1 = list. append(x 1) >>> x 1 = list. append(x 2)

Python List as a Queue EXAMPLE: • Method append() to add to right end

Python List as a Queue EXAMPLE: • Method append() to add to right end of the queue • list[0] gives us the head, i. e. left end, of the queue • Note: x = list[0] saves the head of the queue into variable x • list = list[1: ] deletes the head of the queue from the queue • Also can use del list[0]

Python List as a Queue • Queuing operations are: • list. append() • list[0]

Python List as a Queue • Queuing operations are: • list. append() • list[0] • del list[0]

Quick Homework 5 • Suppose we define a variable list as follows: • list

Quick Homework 5 • Suppose we define a variable list as follows: • list = [4, 2, 3, 1] • Use only the operations demonstrated in class. • Q 1: Show a shortest sequence of stacking operations that sorts list into a list in ascending order, i. e. you should end up with: • list = [1, 2, 3, 4] • Q 2: How many stacking operations are needed? • Q 3: Show a shortest sequence of queuing operations that sorts list into a list in ascending order, i. e. you should end up with: • list = [1, 2, 3, 4] • Q 4: How many queuing operations are needed?

Python List as a Queue FYI: • A faster implementation according to section 5.

Python List as a Queue FYI: • A faster implementation according to section 5. 1. 2 • Don't use for your homework