Lecture 14 - JSON.  Text-based notation for data interchange  Human readable  Object  Unordered set of name-value pairs  { name1 : value1, name2.

Slides:



Advertisements
Similar presentations
§3 The Stack ADT 1. ADT A stack is a Last-In-First-Out (LIFO) list, that is, an ordered list in which insertions and deletions are.
Advertisements

Stacks & Their Applications COP Stacks  A stack is a data structure that stores information arranged like a stack.  We have seen stacks before.
Prefix, Postfix, Infix Notation
Computer Science 112 Fundamentals of Programming II Applications of Stacks.
Computer Science 2 Data Structures V section 2 Recitation 1.
COSC 2006 Chapter 7 Stacks III
COMPSCI 105 S Principles of Computer Science 13 Stacks.
C o n f i d e n t i a l Developed By Nitendra NextHome Subject Name: Data Structure Using C Title : Overview of Stack.
CS 206 Introduction to Computer Science II 03 / 04 / 2009 Instructor: Michael Eckmann.
COMPSCI 105 SS 2015 Principles of Computer Science Queues.
 Balancing Symbols 3. Applications
Lecture 12 – ADTs and Stacks.  Modularity  Divide the program into smaller parts  Advantages  Keeps the complexity managable  Isolates errors (parts.
Topic 15 Implementing and Using Stacks
CS 206 Introduction to Computer Science II 03 / 06 / 2009 Instructor: Michael Eckmann.
Stacks & Queues Infix Calculator CSC 172 SPRING 2002 LECTURE 5.
Infix to postfix conversion Process the tokens from a vector infixVect of tokens (strings) of an infix expression one by one When the token is an operand.
Stacks & Queues Infix Calculator CSC 172 SPRING 2004 LECTURE 13.
CS 206 Introduction to Computer Science II 10 / 17 / 2008 Instructor: Michael Eckmann.
Infix, Postfix, Prefix.
Fall 2007CS 2251 Stacks Chapter 5. Fall 2007CS 2252 Chapter Objectives To learn about the stack data type and how to use its four methods: push, pop,
Fall 2007CS 2251 Stacks Chapter 5. Fall 2007CS 2252 Chapter Objectives To learn about the stack data type and how to use its four methods: push, pop,
CS 206 Introduction to Computer Science II 03 / 16 / 2009 Instructor: Michael Eckmann.
TCSS 342, Winter 2005 Lecture Notes
Topic 15 Implementing and Using Stacks
CS 206 Introduction to Computer Science II 10 / 28 / 2009 Instructor: Michael Eckmann.
The Stack and Queue Types Lecture 10 Hartmut Kaiser
Objectives of these slides:
Fundamentals of Python: From First Programs Through Data Structures Chapter 14 Linear Collections: Stacks.
Comp 245 Data Structures Stacks. What is a Stack? A LIFO (last in, first out) structure Access (storage or retrieval) may only take place at the TOP NO.
Week7 Stack Data Structures & Algorithms. Introduction to Stacks and Queues Widely used data structures Ordered List of element Easy to implement Easy.
CSC 205 Programming II Postfix Expressions. Recap: Stack Stack features Orderly linear structure Access from one side only – top item Stack operations.
CHAPTER 05 Compiled by: Dr. Mohammad Omar Alhawarat Stacks & Queues.
Computer Science Department Data Structure & Algorithms Problem Solving with Stack.
Data Structures. The Stack: Definition A stack is an ordered collection of items into which new items may be inserted and from which items may be deleted.
COP3530 Data Structures600 Stack Stack is one the most useful ADTs. Like list, it is a collection of data items. Supports “LIFO” (Last In First Out) discipline.
Stack Any Other Data Structure Array Linked List
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 18: Stacks and Queues (part 2)
Lecture 13 – Queues.  Ordered collection of data  Items are added to the back of the queue  Items are removed from the front of the queue  Remove.
Lecture 10 – Algorithm Analysis.  Next number is sum of previous two numbers  1, 1, 2, 3, 5, 8, 13, 21 …  Mathematical definition 2COMPSCI Computer.
JSON COMPSCI 105 SS 2015 Principles of Computer Science.
COMPSCI 105 SS 2015 Principles of Computer Science 09 ADT & Stacks.
JSON COMPSCI 105 S Principles of Computer Science.
Prefix, Postfix, Infix Notation. Infix Notation  To add A, B, we write A+B  To multiply A, B, we write A*B  The operators ('+' and '*') go in between.
Stacks A stack is a linear data structure that can be accessed only at one of its ends for storing and retrieving data LIFO (Last In First Out) structure.
CC 215 DATA STRUCTURES MORE ABOUT STACK APPLICATIONS Dr. Manal Helal - Fall 2014 Lecture 6 AASTMT Engineering and Technology College 1.
Applications of Stack Maitrayee Mukerji. Stacks Last In First Out (LIFO List) ◦ FILO? Insertions and Deletions from the same end called the Top Push(),
CE 221 Data Structures and Algorithms Chapter 3: Lists, Stacks, and Queues - II Text: Read Weiss, §3.6 1Izmir University of Economics.
CSC 172 DATA STRUCTURES. A TALE OF TWO STRUCTURES.
BCA II Data Structure Using C
Stacks Access is allowed only at one point of the structure, normally termed the top of the stack access to the most recently added item only Operations.
Revised based on textbook author’s notes.
COMPSCI 107 Computer Science Fundamentals
Infix to postfix conversion
Objectives In this lesson, you will learn to: Define stacks
CSC 172 DATA STRUCTURES.
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Stack application: postponing data usage
PART II STACK APPLICATIONS
Stacks Chapter 5 Adapted from Pearson Education, Inc.
More About Stacks: Stack Applications
Stack A data structure in which elements are inserted and removed only at one end (called the top). Enforces Last-In-First-Out (LIFO) Uses of Stacks Evaluating.
Stacks and Queues 1.
Queue Applications Lecture 31 Mon, Apr 9, 2007.
Topic 15 Implementing and Using Stacks
Queue Applications Lecture 31 Tue, Apr 11, 2006.
Applications of Stacks and Queues
More About Stacks: Stack Applications
Chapter 7 (continued) © 2011 Pearson Addison-Wesley. All rights reserved.
Presented by : Aman Gupta PGT CS KV No.1, Narimedu, Madurai
Stacks A stack is an ordered set of elements, for which only the last element placed into the stack is accessible. The stack data type is also known as.
Presentation transcript:

Lecture 14 - JSON

 Text-based notation for data interchange  Human readable  Object  Unordered set of name-value pairs  { name1 : value1, name2 : value2, …, nameN : valueN }  Array  Ordered list of values  [ value1, value2, … valueN ] 2COMPSCI Computer Science Fundamentals

 json.dumps( data )  Accepts Python object as an argument  Returns a string containing the information in JSON format  Typically write this string to a file 3COMPSCI Computer Science Fundamentals import json def write(data, filename): file = open(filename, 'w') str_out = json.dumps(data) file.write(str_out) file.close()

 json.loads( data )  Accepts string as an argument  The string should be in JSON format  Returns a Python object corresponding to the data 4COMPSCI Computer Science Fundamentals import json def read(filename): file = open(filename) str_in = file.read() file.close() data = json.loads(str_in) return data

 json.dumps( data )  json.dumps( data, indent=4, sort_keys=True )  Formats the output over multiple lines 5COMPSCI Computer Science Fundamentals {'b': ['HELLO', 'WORLD'], 'a': ['hello', 'world']} { "a": [ "hello", "world" ], "b": [ "HELLO", "WORLD" ] }

 Point class  Can create a dictionary to store state information then use json  Can use json to read dictionary and extract the state information 6COMPSCI Computer Science Fundamentals class Point: def __init__(self, loc_x, loc_y): self.x = loc_x self.y = loc_y def generate_json(p): out = {'_Point' : True, 'x' : p.x, 'y' : p.y} return json.dumps(out, sort_keys=True) def generate_point(txt): inp = json.loads(txt) result = Point( inp['x'], inp['y'] ) return result

 Start by thinking of the different kinds of input and the output  Test Cases  Work on the solution, keeping the test cases in mind  Test your code after each development advance 7COMPSCI Computer Science Fundamentals

 Debugging and tracing code are closely linked skills  To debug your code, you need to know:  what your code *should* produce  what your code *does* produce  why is there is a difference  Use text output to determine data  Test functions at entry and exit points  Test loops at entry and exit points  If data is large or complex, save output to a file  JSON may help 8COMPSCI Computer Science Fundamentals

 A stack can be used in the algorithm to convert infix to postfix  Divide expression into tokens  Operators: +. -, *, /  Operands: single digits  Other tokens: brackets 9COMPSCI Computer Science Fundamentals

 Create a stack to store operators and a list for the output tokens  Scan the tokens from left to right  If the token is an operand, add it to the output list  If the token is a left parenthesis, push it to the operator stack  If the token is a right parenthesis, pop the operator stack until the left parenthesis is removed. Append each operator to the output list  If the token is an operator, push it onto the operator stack. But first, remove any operators that have higher or equal precedence and append them to the output list  When there are no more tokens, remove operators on the stack and append to the output list 10COMPSCI Computer Science Fundamentals

 Show the operator stack and the output list at every step as the following infix expression is converted to postfix 11COMPSCI Computer Science Fundamentals 12 / ( ) * 2 + 4

 Create an empty stack  Scan the list of tokens from left to right  If the token is an operand, push it to the operand stack  If the token is an operator, pop the stack twice  The first element popped is the right operand  The second element popped is the left operand  Apply the operator to the operands and push the result onto the stack  When there are no more tokens, the stack should contain the result. 12COMPSCI Computer Science Fundamentals

13COMPSCI Computer Science Fundamentals  Following the algorithm to evaluate postfix expressions, show the operand stack, and the token being processed (at each step) as the following postfix expression is evaluated: * 3 / +

 How does a user know if the circular_queue is full? What should happen when the circular_queue is full? Discuss 14COMPSCI Computer Science Fundamentals class circular_queue: def __init__(self, capacity): #creates empty list, count, front, back def is_empty(self): def enqueue(self, item): def dequeue(self): def size():

 A Double-Ended Queue or Deque (pronounced ‘Deck’)  An ordered collection of items where items are added and removed from either end, either front or back  add_front()  add_rear()  remove_front()  remove_rear()  is_empty()  size() 15COMPSCI Computer Science Fundamentals

 Use a double ended queue to write a function that determines if a string is a palindrome.  A palindrome is a sentence in which the letters appear in the same order forwards and reverse. Punctuation is ignored. 16COMPSCI Computer Science Fundamentals >>> is_palindrome(‘bob’) True

17COMPSCI Computer Science Fundamentals I, man, am regal - a German am I Never odd or even If I had a hi-fi Madam, I'm Adam Too hot to hoot No lemons, no melon Too bad I hid a boot Lisa Bonet ate no basil Warsaw was raw Was it a car or a cat I saw? Rise to vote, sir Do geese see god? "Do nine men interpret?" "Nine men," I nod Rats live on no evil star Won't lovers revolt now? Race fast, safe car Pa's a sap Ma is as selfless as I am May a moody baby doom a yam? Ah, Satan sees Natasha No devil lived on Lonely Tylenol Not a banana baton No "x" in "Nixon" O, stone, be not so O Geronimo, no minor ego "Naomi," I moan "A Toyota's a Toyota" A dog, a panic in a pagoda Oh no! Don Ho! Nurse, I spy gypsies - run! Senile felines Now I see bees I won UFO tofu We panic in a pew Oozy rat in a sanitary zoo God! A red nugget! A fat egg under a dog! Go hang a salami, I'm a lasagna hog

def exampleA(n): s = "PULL FACES" for i in range(n): print("I must not ", s) for j in range(n, 0, -1): print("I must not ", s)  What is the big-O running time for the following function? 18COMPSCI Computer Science Fundamentals

def exampleB(n): s = "JUMP ON THE BED" for i in range(n): for j in range(i): print("I must not ", s)  What is the big-O running time for the following function? 19COMPSCI Computer Science Fundamentals

def exampleC(n): s = "WHINGE" i = 1 while i < n: for j in range(n): print("I must not ", s) i = i * 2  What is the big-O running time for the following function? 20COMPSCI Computer Science Fundamentals

def exampleD(n): s = "PROCRASTINATE" for i in range(n): for j in range(n, 0, -1): outD(s, n / 2) def outD(s, b): number_of_times = int(b % 10) for i in range(number_of_times): print(i, "I must not ", s)  What is the big-O running time for the following function? 21COMPSCI Computer Science Fundamentals

def exampleF(n): s = "FORGET MY MOTHER’S BIRTHDAY" i = n while i > 0: outF(s) i = i // 2 def outF(s): for i in range(25, 0, -1): print(i, "I must not ", s)  What is the big-O running time for the following function? 22COMPSCI Computer Science Fundamentals

 If a particular quadratic time algorithm uses 300 elementary operations to process an input of size 10, what is the most likely number of elementary operations it will use if given an input of size  (a)  (b)  (c)  (d)  (e) COMPSCI Computer Science Fundamentals

 You know that a given algorithm runs in O(2 n ) time. If your computer can process input of size in one year using an implementation of this algorithm, approximately what size input could you solve in one year with a computer 1000 times faster? 24COMPSCI Computer Science Fundamentals

25COMPSCI Computer Science Fundamentals