Programming Using Python



Similar documents
ML for the Working Programmer

COPYRIGHTED MATERIAL. Contents. List of Figures. Acknowledgments

Applied Computational Economics and Finance

Some programming experience in a high-level structured programming language is recommended.

SIMS 255 Foundations of Software Design. Complexity and NP-completeness

Charles Dierbach. Wiley

An Introduction to Programming and Computer Science

Computing Concepts with Java Essentials

PROBLEM SOLVING SEVENTH EDITION WALTER SAVITCH UNIVERSITY OF CALIFORNIA, SAN DIEGO CONTRIBUTOR KENRICK MOCK UNIVERSITY OF ALASKA, ANCHORAGE PEARSON

2. (a) Explain the strassen s matrix multiplication. (b) Write deletion algorithm, of Binary search tree. [8+8]

Contents. Introduction and System Engineering 1. Introduction 2. Software Process and Methodology 16. System Engineering 53

Intro to scientific programming (with Python) Pietro Berkes, Brandeis University

A Note for Students: How to Use This Book

William E. Hart Carl Laird Jean-Paul Watson David L. Woodruff. Pyomo Optimization. Modeling in Python. ^ Springer

Java Software Structures

5 INTEGER LINEAR PROGRAMMING (ILP) E. Amaldi Fondamenti di R.O. Politecnico di Milano 1

Java Modules for Time Series Analysis

CS 2302 Data Structures Spring 2015

Assignment 2: Option Pricing and the Black-Scholes formula The University of British Columbia Science One CS Instructor: Michael Gelbart

Software Testing. Definition: Testing is a process of executing a program with data, with the sole intention of finding errors in the program.

Computer Science/Software Engineering

Current Standard: Mathematical Concepts and Applications Shape, Space, and Measurement- Primary

Introduction to Programming System Design. CSCI 455x (4 Units)

Sample Syllabus (C++) CSCI 1301 Introduction to Programming Principles

Mathematics for Algorithm and System Analysis

CS5310 Algorithms 3 credit hours 2 hours lecture and 2 hours recitation every week

Basic Programming and PC Skills: Basic Programming and PC Skills:

SEQUENCES ARITHMETIC SEQUENCES. Examples

Computer programming course in the Department of Physics, University of Calcutta

A QUICK OVERVIEW OF THE OMNeT++ IDE

Texas Essential Knowledge and Skills Correlation to Video Game Design Foundations 2011 N Video Game Design

BCS2B02: OOP Concepts and Data Structures Using C++

: provid.ir

Java 6 'th. Concepts INTERNATIONAL STUDENT VERSION. edition

CONTENTS PREFACE 1 INTRODUCTION 1 2 DATA VISUALIZATION 19

Curriculum Map. Discipline: Computer Science Course: C++

Applied Algorithm Design Lecture 5

AP Computer Science A - Syllabus Overview of AP Computer Science A Computer Facilities

vii TABLE OF CONTENTS CHAPTER TITLE PAGE DECLARATION DEDICATION ACKNOWLEDGEMENT ABSTRACT ABSTRAK

Bootstrapping Big Data

Krishna Institute of Engineering & Technology, Ghaziabad Department of Computer Application MCA-213 : DATA STRUCTURES USING C

A Correlation of. to the. South Carolina Data Analysis and Probability Standards

Parallel Computing for Data Science

1. The memory address of the first element of an array is called A. floor address B. foundation addressc. first address D.

Persistent Binary Search Trees

PARALLEL PROGRAMMING

Institute of Actuaries of India Subject CT3 Probability and Mathematical Statistics

Data Preparation and Statistical Displays

Contextual-Bandit Approach to Recommendation Konstantin Knauf

New Hash Function Construction for Textual and Geometric Data Retrieval

AP Computer Science AB Syllabus 1

DATA STRUCTURES USING C

Course Overview and Approximate Time Allotments. First Semester

Computation and Economics - Spring 2012 Assignment #3: File Sharing

CRYPTOG NETWORK SECURITY

10CS35: Data Structures Using C

Data Structure [Question Bank]

ISSN: (Online) Volume 2, Issue 4, April 2014 International Journal of Advance Research in Computer Science and Management Studies

QUANTITATIVE METHODS. for Decision Makers. Mik Wisniewski. Fifth Edition. FT Prentice Hall

A Non-Linear Schema Theorem for Genetic Algorithms

CSE373: Data Structures and Algorithms Lecture 3: Math Review; Algorithm Analysis. Linda Shapiro Winter 2015

STATISTICA Formula Guide: Logistic Regression. Table of Contents

Algebra Academic Content Standards Grade Eight and Grade Nine Ohio. Grade Eight. Number, Number Sense and Operations Standard

M E M O R A N D U M. Faculty Senate Approved April 2, 2015

On the mathematical theory of splitting and Russian roulette

Using Web-based Tools to Enhance Student Learning and Practice in Data Structures Course

Notes from Week 1: Algorithms for sequential prediction

IMPROVING PERFORMANCE OF RANDOMIZED SIGNATURE SORT USING HASHING AND BITWISE OPERATORS

Classifying Large Data Sets Using SVMs with Hierarchical Clusters. Presented by :Limou Wang

Dynamic Programming. Lecture Overview Introduction

COSC 6397 Big Data Analytics. Mahout and 3 rd homework assignment. Edgar Gabriel Spring Mahout

SAS Software to Fit the Generalized Linear Model

Domains and Competencies

Theory of Computation Chapter 2: Turing Machines

CCA CYBER SECURITY TRACK

Model-Based Recursive Partitioning for Detecting Interaction Effects in Subgroups

Complexity Classes P and NP

Contents. List of Figures. List of Tables. List of Examples. Preface to Volume IV

Social Media Mining. Graph Essentials

How To Understand The Theory Of Probability

Home Page. Data Structures. Title Page. Page 1 of 24. Go Back. Full Screen. Close. Quit

Lecture 2: Data Structures Steven Skiena. skiena

TESTING JAVA MONITORS BY STATE SPACE EXPLORATION MONICA HERNANDEZ. Presented to the Faculty of the Graduate School of

Chapter ML:XI (continued)

Statistical Rules of Thumb

Introduction to Clustering

AI: A Modern Approach, Chpts. 3-4 Russell and Norvig

Introduction to Markov Chain Monte Carlo

The Network Structure of Hard Combinatorial Landscapes

Efficiency of algorithms. Algorithms. Efficiency of algorithms. Binary search and linear search. Best, worst and average case.

Master s Theory Exam Spring 2006

Partitioning and Divide and Conquer Strategies

Using Excel and VBA CHANDAN SENGUPTA SECOND EDITION. WILEY John Wiley & Sons, Inc.

Silvermine House Steenberg Office Park, Tokai 7945 Cape Town, South Africa Telephone:

096 Professional Readiness Examination (Mathematics)

Data Mining Project Report. Document Clustering. Meryem Uzun-Per

Transcription:

Introduction to Computation and Programming Using Python Revised and Expanded Edition John V. Guttag The MIT Press Cambridge, Massachusetts London, England

CONTENTS PREFACE xiii ACKNOWLEDGMENTS xv 1 GETTING STARTED 1 2 INTRODUCTION TO PYTHON 7 2.1 The Basic Elements of Python 8 2.1.1 Objects, Expressions, and Numerical Types 9 2.1.2 Variables and Assignment 11 2.1.3 IDLE 13 2.2 Branching Programs 2.3 Strings and Input 2.3.1 Input 14 16 18 2.4 Iteration 18 3 SOME SIMPLE NUMERICAL PROGRAMS 21 3.1 Exhaustive Enumeration 21 3.2 For Loops 23 3.3 Approximate Solutions and Bisection Search 25 3.4 A Few Words About Using Floats 29 3.5 Newton-Raphson 32 4 FUNCTIONS, SCOPING, and ABSTRACTION 34 4.1 Functions and Scoping 35 4.1.1 Function Definitions 35 4.1.2 Keyword Arguments and Default Values 36 4.1.3 Scoping 4.2 Specifications 37 41 4.3 Recursion 44 4.3.1 Fibonacci Numbers 45 4.3.2 Palindromes 48 4.4 Global Variables 50 4.5 Modules 51 4.6 Files 53

5 STRUCTURED TYPES, MUTABILITY, AND HIGHER-ORDER FUNCTIONS.. 56 5.1 Tuples 56 5.1.1 Sequences and Multiple Assignment 57 5.2 Lists and Mutability 58 5.2.1 Cloning 63 5.2.2 List Comprehension 63 5.3 Functions as Objects 64 5.4 Strings, Tuples, and Lists 66 5.5 Dictionaries 67 6 TESTING AND DEBUGGING 70 6.1 Testing 70 6.1.1 Black-Box Testing 71 6.1.2 Glass-Box Testing 73 6.1.3 Conducting Tests 74 6.2 Debugging 76 6.2.1 Learning to Debug 78 6.2.2 Designing the Experiment 79 6.2.3 When the Going Gets Tough 81 6.2.4 And When You Have Found "The" Bug 82 7 EXCEPTIONS AND ASSERTIONS 84 7.1 Handling Exceptions 84 7.2 Exceptions as a Control Flow Mechanism 87 7.3 Assertions 90 8 CLASSES AND OBJECT-ORIENTED PROGRAMMING 91 8.1 Abstract Data Types and Classes 91 8.1.1 Designing Programs Using Abstract Data Types 96 8.1.2 Using Classes to Keep Track of Students and Faculty 96 8.2 Inheritance 99 8.2.1 Multiple Levels of Inheritance 101 8.2.2 The Substitution Principle 102 8.3 Encapsulation and Information Hiding 103 8.3.1 Generators 106 8.4 Mortgages, an Extended Example 108

ix 9 A SIMPLISTIC INTRODUCTION TO ALGORITHMIC COMPLEXITY 113 9.1 Thinking About Computational Complexity 113 9.2 Asymptotic Notation 116 9.3 Some Important Complexity Classes 118 9.3.1 Constant Complexity 118 9.3.2 Logarithmic Complexity 118 9.3.3 Linear Complexity 119 9.3.4 Log-Linear Complexity 120 9.3.5 Polynomial Complexity 120 9.3.6 Exponential Complexity 121 9.3.7 Comparisons of Complexity Classes 123 10 SOME SIMPLE ALGORITHMS AND DATA STRUCTURES 125 10.1 Search Algorithms 126 10.1.1 Linear Search and Using Indirection to Access Elements 126 10.1.2 Binary Search and Exploiting Assumptions 128 10.2 Sorting Algorithms 131 10.2.1 Merge Sort 132 10.2.2 Exploiting Functions as Parameters 135 10.2.3 Sorting in Python 136 10.3 Hash Tables 137 11 PLOTTING AND MORE ABOUT CLASSES 141 11.1 Plotting Using PyLab 141 11.2 Plotting Mortgages, an Extended Example 146 12 STOCHASTIC PROGRAMS, PROBABILITY, AND STATISTICS 152 12.1 Stochastic Programs 153 12.2 Inferential Statistics and Simulation 155 12.3 Distributions 166 12.3.1 Normal Distributions and Confidence Levels 168 12.3.2 Uniform Distributions 170 12.3.3 Exponential and Geometric Distributions 171 12.3.4 Benford's Distribution 173 12.4 How Often Does the Better Team Win? 174 12.5 Hashing and Collisions 177

X 13 RANDOM WALKS AND MORE ABOUT DATA VISUALIZATION 179 13.1 The Drunkard's Walk 179 13.2 Biased Random Walks 186 13.3 Treacherous Fields 191 14 MONTE CARLO SIMULATION 193 14.1 Pascal's Problem 194 14.2 Pass or Don't Pass? 195 14.3 Using Table Lookup to Improve Performance 199 14.4 Finding * 200 14.5 Some Closing Remarks About Simulation Models 204 15 UNDERSTANDING EXPERIMENTAL DATA 207 15.1 The Behavior of Springs 207 15.1.1 Using Linear Regression to Find a Fit 210 15.2 The Behavior of Projectiles 214 15.2.1 Coefficient of Determination 216 15.2.2 Using a Computational Model 217 15.3 Fitting Exponentially Distributed Data 218 15.4 When Theory Is Missing 221 16 LIES. DAMNED LIES, AND STATISTICS 222 16.1 Garbage In Garbage Out (GIGO) 222 16.2 Pictures Can Be Deceiving 223 16.3 dim Hoc Ergo Propter Hoc 225 16.4 Statistical Measures Don't Tell the Whole Story 226 16.5 Sampling Bias 228 16.6 Context Matters 229 16.7 Beware of Extrapolation 229 16.8 The Texas Sharpshooter Fallacy 230 16.9 Percentages Can Confuse 232 16.10 Just Beware 233 17 KNAPSACK AND GRAPH OPTIMIZATION PROBLEMS 234 17.1 Knapsack Problems 234 17.1.1 Greedy Algorithms 235 17.1.2 An Optimal Solution to the 0/1 Knapsack Problem 238

17.2 Graph Optimization Problems 240 17.2.1 Some Classic Graph-Theoretic Problems 244 17.2.2 The Spread of Disease and Min Cut 245 17.2.3 Shortest Path: Depth-First Search and Breadth-First Search... 246 18 DYNAMIC PROGRAMMING 252 18.1 Fibonacci Sequences, Revisited 252 18.2 Dynamic Programming and the 0/1 Knapsack Problem 254 18.3 Dynamic Programming and Divide-and-Conquer 261 19 A QUICK LOOK AT MACHINE LEARNING 262 19.1 Feature Vectors 264 19.2 Distance Metrics 266 19.3 Clustering 270 19.4 Types Example and Cluster 272 19.5 K-means Clustering 274 19.6 A Contrived Example 276 19.7 A Less Contrived Example 280 19.8 Wrapping Up 286 PYTHON 2.7 QUICK REFERENCE 287 INDEX 289