Intro to Programming Lecture 5

Slides:



Advertisements
Similar presentations
R for Macroecology Aarhus University, Spring 2011.
Advertisements

Recursion. Recursion is a powerful technique for thinking about a process It can be used to simulate a loop, or for many other kinds of applications In.
CIT 590 Intro to Programming Lecture 7. Agenda Configuring IDLE (now that your code is getting huge) Exceptions Testing for exceptions – the weird self.assertRaises.
Lists Introduction to Computing Science and Programming I.
Data Collections: Dictionaries CSC 161: The Art of Programming Prof. Henry Kautz 11/4/2009.
CIT 590 Intro to Programming Lecture 4. Agenda Doubts from HW1 and HW2 Main function Break, quit, exit Function argument names, scope What is modularity!
Recap form last time How to do for loops map, filter, reduce Next up: dictionaries.
Data Collections: Lists CSC 161: The Art of Programming Prof. Henry Kautz 11/2/2009.
CIT 590 Intro to Programming Lecture 6 (dictionaries)
Chapter 9 Dictionaries and Sets.
14. DICTIONARIES AND SETS Rocky K. C. Chang 17 November 2014 (Based on from Charles Dierbach, Introduction to Computer Science Using Python and Punch and.
CSC Introduction to Data Structures Devon M. Simmonds Computer Science Department University of North Carolina Wilmington Wilmington, NC 28403
CIT 590 Intro to Programming Lecture 4. How are assignments evaluated Pay attention to what the HW says it is trying to teach you about ‘modular programming’
CSE 130 : Winter 2009 Programming Languages Lecture 11: What’s in a Name ?
CIT 590 Intro to Programming Lecture 4. Random utilities to improve efficiency Search everything! Beyond Compare Keyboard shortcuts Not essentially but.
Intro to Data Structures Concepts ● We've been working with classes and structures to form linked lists – a linked list is an example of something known.
COMPSCI 107 Computer Science Fundamentals
10 - Python Dictionary John R. Woodward.
Functions and Subroutines
Topics Dictionaries Sets Serializing Objects. Topics Dictionaries Sets Serializing Objects.
CSE341: Programming Languages Lecture 14 Thunks, Laziness, Streams, Memoization Dan Grossman Spring 2017.
CMSC201 Computer Science I for Majors Lecture 21 – Dictionaries
Containers and Lists CIS 40 – Introduction to Programming in Python
CSE341: Programming Languages Lecture 15 Macros
Arrays in PHP are quite versatile
C++ Standard Library.
Announcements Project 4 due Wed., Nov 7
Dictionaries, File operations
Lecture 10 Data Collections
CSE341: Programming Languages Lecture 14 Thunks, Laziness, Streams, Memoization Dan Grossman Winter 2013.
Python is a general-purpose interpreted, interactive, object-oriented, and high-level programming language. It was created by Guido van Rossum during.
Algorithm Analysis CSE 2011 Winter September 2018.
Intro to Computer Science CS1510 Dr. Sarah Diesburg
CSE341: Programming Languages Lecture 14 Thunks, Laziness, Streams, Memoization Zach Tatlock Winter 2018.
Hash table another data structure for implementing a map or a set
CSE341: Programming Languages Lecture 14 Thunks, Laziness, Streams, Memoization Dan Grossman Spring 2013.
CISC101 Reminders Slides have changed from those posted last night…
Winter 2018 CISC101 12/1/2018 CISC101 Reminders
Chapter 8 Arrays Objectives
CSE341: Programming Languages Lecture 14 Thunks, Laziness, Streams, Memoization Dan Grossman Spring 2016.
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Recitation Outline C++ STL associative containers Examples
CSE341: Programming Languages Lecture 14 Thunks, Laziness, Streams, Memoization Dan Grossman Autumn 2017.
Forth A stack language.
Sparse matrices, hash tables, cell arrays
Introduction to Dictionaries
CSE341: Programming Languages Lecture 14 Thunks, Laziness, Streams, Memoization Dan Grossman Autumn 2018.
Python Tutorial for C Programmer Boontee Kruatrachue Kritawan Siriboon
6. Implementation of Vector-Space Retrieval
Topics Dictionaries Sets Serializing Objects. Topics Dictionaries Sets Serializing Objects.
Dictionaries Dictionary: object that stores a collection of data
Asst. Dr.Surasak Mungsing
6. Dictionaries and sets Rocky K. C. Chang 18 October 2018
Programming Languages
CSE341: Programming Languages Lecture 15 Macros
Recursion Taken from notes by Dr. Neil Moore
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Chapter 8 Arrays Objectives
CISC101 Reminders Assignment 2 due today.
CHAPTER 4: Lists, Tuples and Dictionaries
Using Script Files and Managing Data
Data Structures & Algorithms
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Winter 2019 CISC101 5/26/2019 CISC101 Reminders
Introduction to Dictionaries
EE 194/BIO 196: Modeling biological systems
Brett Wortzman Summer 2019 Slides originally created by Dan Grossman
CSE341: Programming Languages Lecture 14 Thunks, Laziness, Streams, Memoization Dan Grossman Spring 2019.
CSE341: Programming Languages Lecture 15 Macros
Introduction to Computer Science
Presentation transcript:

Intro to Programming Lecture 5 CIT 590 Intro to Programming Lecture 5

One more unit testing example Let’s write a unit test for something in HW3 checkRacko(hand) – remember this returns True or False Assume also some added specification – throw errors if the rack being passed in does not have 10 cards the rack has cards that are not in [1,60]

Dictionary Dictionaries consist of key, value pairs Also know as Hashmaps, associative arrays in other languages Initialized with dictionary = {} And then we can add key, value pairs as follows dictionary[‘name’] = ‘Arvind’ dictionary[‘age’] = 92 Very efficient way of storing sparse data A lot of matrices and vectors that come up in probability are sparse, so you could use an integer key and store values in that manner

Dictionary update operation. Copying dictionaries Dictone = {‘abc’: 3, ‘def’:7, ‘xyz’: 9} Dicttwo = {‘def’: 5, ‘pdq’ : 4} Dictone.update(dicttwo) As with lists or for that matter any kind of assignment, you need to be careful when you do assignment, since that is done by reference So dict1 = dict2 will make both dictionaries refer to the same value If you want to make what is generally called a ‘shallow copy’, you need to use the copy method Dict 2 = dict1.copy()

Altering dictionaries by passing them into functions Remember that arguments are passed into functions by reference, so you could get some unintended consequences if you are not careful

Initializing a dictionary version 2 The dict() function can convert a list of 2 element tuples into dictionaries Very useful when you want to initialize a dictionary by using 2 lists, one of which has keys = [‘Arvind’, ‘Aashish’] values = [33, 28] dict(zip(keys, values))

Looping over a dictionary For e in dict: This will loop over the keys For e in dict.values(): As is somewhat obvious, this will loop over the values Since there is no guarantee about what order the keys are looped over, you might sometimes want to call the sorted function

Fibonacci sequence The naïve recursive version is just Fib(n) = Fib(n-1) + Fib(n-2) Why does this take so long??

Dynamic programming The central principle is that you delay the computation of the function as much as possible When you compute a result, store it somewhere in memory Retrieve the results from memory

Persistent variables The shelve module allows data to persist without using explicit file commands import shelve Data = shelve.open(“database”) #and this will make an implicit file called database And then data from that point can basically be treated just as any other dictionary Remember to close the shelf before quitting by using Data.close()

The internal dictionaries Locals() gives you the local variables Correspondingly global() gives you the global variables