Computing Science 1P Large Group Tutorial 14 Simon Gay Department of Computing Science University of Glasgow 2006/07.

Slides:



Advertisements
Similar presentations
Computing Science 1P Large Group Tutorial 19 Simon Gay Department of Computing Science University of Glasgow 2006/07.
Advertisements

AESuniversity Ad hoc Reporting. Ad hoc Reports What are ad hoc reports? Why would you use ad hoc reports? Creating an ad hoc report from a query Building.
Formulas, Ranges, and Functions. Formulas n Formulas perform operations such as addition, multiplication, and comparison on worksheet values. n Formulas.
Recursion Genome 559: Introduction to Statistical and Computational Genomics Elhanan Borenstein.
Lecture 4 Sept 8 Complete Chapter 3 exercises Chapter 4.
18-Jun-15 Arrays. 2 A problem with simple variables One variable holds one value The value may change over time, but at any given time, a variable holds.
Richard Fateman CS 282 Lecture 111 Determinants Lecture 11.
CS107 Introduction to Computer Science Lecture 5, 6 An Introduction to Algorithms: List variables.
26-Jun-15 Arrays. 2 A problem with simple variables One variable holds one value The value may change over time, but at any given time, a variable holds.
Arrays. A problem with simple variables One variable holds one value –The value may change over time, but at any given time, a variable holds a single.
Lecture 4 Sept 7 Chapter 4. Chapter 4 – arrays, collections and indexing This chapter discusses the basic calculations involving rectangular collections.
Module 6 Matrices & Applications Chapter 26 Matrices and Applications I.
Computing Science 1P Large Group Tutorial 17 Simon Gay Department of Computing Science University of Glasgow 2006/07.
Python Programming Chapter 10: Dictionaries Saad Bani Mohammad Department of Computer Science Al al-Bayt University 1 st 2011/2012.
Computing Science 1P Lecture 21: Friday 20 th April Simon Gay Department of Computing Science University of Glasgow 2006/07.
General Programming Introduction to Computing Science and Programming I.
CS1Q Computer Systems Lecture 8
Computing Science 1P Lecture 13: Friday 26 th January Simon Gay Department of Computing Science University of Glasgow 2006/07.
Data Structures and Algorithm Analysis Hashing Lecturer: Jing Liu Homepage:
Computing Science 1P Lecture 18: Friday 2 nd March Simon Gay Department of Computing Science University of Glasgow 2006/07.
1 Use a cell value as a KEY to a table to find and return a specific element to another cell or use in a calculation.
By : Afifah Elhan & Noah Joseph
Hashing Chapter 20. Hash Table A hash table is a data structure that allows fast find, insert, and delete operations (most of the time). The simplest.
Yaomin Jin Design of Experiments Morris Method.
AP Comp Sci A Chapter 12 - Arrays. Ch 12 Goals: Goals: Declare and create arrays Declare and create arrays Access elements in arrays Access elements in.
COMPSCI 101 Principles of Programming Lecture 25 – Nested loops, passing mutable objects as parameters.
Computing Science 1P Lecture 15: Friday 9 th February Simon Gay Department of Computing Science University of Glasgow 2006/07.
Computing Science 1P Lecture 21: Wednesday 18 th April Simon Gay Department of Computing Science University of Glasgow 2006/07.
Computing Science 1P Large Group Tutorial 20 Simon Gay Department of Computing Science University of Glasgow 2006/07.
Error Control Code. Widely used in many areas, like communications, DVD, data storage… In communications, because of noise, you can never be sure that.
Arranging and Choosing © Christine Crisp “Teach A Level Maths” Statistics 1.
Computing Science 1P Lecture 14: Friday 2 nd February Simon Gay Department of Computing Science University of Glasgow 2006/07.
CS201: Data Structures and Discrete Mathematics I Hash Table.
Computing Science 1P Large Group Tutorial: Lab Exam & Class Test Simon Gay Department of Computing Science University of Glasgow 2006/07.
Computing Science 1P Large Group Tutorial 13 Simon Gay Department of Computing Science University of Glasgow 2006/07.
Chapter 11Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 11 l Basics of Recursion l Programming with Recursion Recursion.
Matrices: Simplifying Algebraic Expressions Combining Like Terms & Distributive Property.
Mean, Median, Mode and Range
CS1Q Computer Systems Lecture 8
Python Programing: An Introduction to Computer Science
Table of Contents Matrices - Definition and Notation A matrix is a rectangular array of numbers. Consider the following matrix: Matrix B has 3 rows and.
Today… Python Keywords. Iteration (or “Loops”!) Winter 2016CISC101 - Prof. McLeod1.
Chapter 4 Section 1 Organizing Data into Matrices.
MDFP Mathematics and Statistics 1. Univariate Data – Today’s Class 1.STATISTICS 2.Univariate (One Variable) Data 1.Definition 2.Mean, Median, Mode, Range.
Reliability of Disk Systems. Reliability So far, we looked at ways to improve the performance of disk systems. Next, we will look at ways to improve the.
Python Fundamentals: Complex Data Structures Eric Shook Department of Geography Kent State University.
Introduction to Computing Science and Programming I
CS1022 Computer Programming & Principles
Algorithmic complexity: Speed of algorithms
Computer Programming Fundamentals
Representing characters
CSc 110, Spring 2017 Lecture 28: Sets and Dictionaries
Introduction to Python
CISC101 Reminders Quiz 1 grading underway Assn 1 due Today, 9pm.
Systems of equations When we have two equations with two unknowns we solve them using the methods from simultaneous equations. Either by elimination.
Bellwork Change to Addition
Arrays 6-Dec-18.
CSCI N207 Data Analysis Using Spreadsheet
ARRAYS 1 GCSE COMPUTER SCIENCE.
Algorithmic complexity: Speed of algorithms
Further Matrix Algebra
Reference semantics, variables and names
CISC101 Reminders All assignments are now posted.
15-110: Principles of Computing
Algorithmic complexity: Speed of algorithms
Adjacency Matrices and PageRank
Arrays 2-May-19.
Department of Computing Science
Matrix A matrix is a rectangular arrangement of numbers in rows and columns Each number in a matrix is called an Element. The dimensions of a matrix are.
Presentation transcript:

Computing Science 1P Large Group Tutorial 14 Simon Gay Department of Computing Science University of Glasgow 2006/07

Computing Science 1P Tutorial 14 - Simon Gay2 If d = { "John" : 5, "Helen" : 7 }, what do we call "John" ? A key A value Don't know

2006/07Computing Science 1P Tutorial 14 - Simon Gay3 Question Suppose we define a dictionary by d = { "Fred":10, "Joe":15, "Anne": 23, "Clare":14 } and then run the following code: for x in d.keys(): print x In which order will the keys of the dictionary appear?

2006/07Computing Science 1P Tutorial 14 - Simon Gay4 What is the order? Alphabetical order of keys Numerical order of values We can't tell; it looks random The order in the definition Don't know

2006/07Computing Science 1P Tutorial 14 - Simon Gay5 Calculating the mode In statistics, the most frequently occurring element of a list of data is called the mode. It's a kind of average which does not require the data to have any structure at all. e.g. mode([1,2,1,1,3,4,3,2]) should return 1. We want to define a function to calculate the mode of a list of numbers. We can do this by combining two ideas we already know: - frequency analysis - finding the largest value among data

2006/07Computing Science 1P Tutorial 14 - Simon Gay6 Calculating the mode To calculate the mode, we need to calculate how many times each number occurs in the data. How should we do this? (Example: [1,2,1,1,3,4,3,2] ) 1.Use a list in which position i contains the number of occurrences of i Example: [ 0,3,2,2,1 ] 2.Use a dictionary in which an item with key i has a value which is the number of occurrences of i Example: { 1:3, 2:2, 3:2, 4:1 }

2006/07Computing Science 1P Tutorial 14 - Simon Gay7 What is your preferred data structure for this problem? The list The dictionary Don't know

2006/07Computing Science 1P Tutorial 14 - Simon Gay8 Discussion Think about what will happen in the following function call: mode([1,2,1,1,3,4,3,2, ]) Do you still like your choice of data structure? Discuss it with your neighbours.

2006/07Computing Science 1P Tutorial 14 - Simon Gay9 When a dictionary is better When keys are integers and the set of keys is sparse (i.e. there is a large range of possible keys but relatively few are actually used), a dictionary is better than a list. When keys are some other type (e.g. strings), so that we can't use positional lookup, a dictionary is better than a nested list because lookup is faster.

2006/07Computing Science 1P Tutorial 14 - Simon Gay10 Mode: counting occurrences def mode(x): d = {} for n in x: if d.has_key(n): d[n] = d[n] + 1 else: d[n] = 1 # now find the most common number

2006/07Computing Science 1P Tutorial 14 - Simon Gay11 What do you think of this code? It's correct: in fact, ideal It's correct but can be simplified It contains errors Don't know

2006/07Computing Science 1P Tutorial 14 - Simon Gay12 Mode: counting occurrences def mode(x): d = {} for n in x: d[n] = d.get(n,0)+1 # now find the most common number The code is correct but it can be simplified a little by using the technique from page 76 of the book: Exercise: complete the definition so that the most common number is returned.

2006/07Computing Science 1P Tutorial 14 - Simon Gay13 Using nested lists to represent a matrix A matrix (plural: matrices) is a rectangular grid of numbers Represent it by a list of lists: [ [1,2,3], [2,4,5], [3,1,2] ] Let's think about an n  n matrix: n rows and n columns, represented by a list of n lists, each of n elements.

2006/07Computing Science 1P Tutorial 14 - Simon Gay14 Opposite diagonal of an n  n matrix Which of the following code fragments prints the "opposite" diagonal of an n  n matrix m ? n = len(m) for i in range(n): print m[i][i] n = len(m) for i in range(n): print m[i][n-i] n = len(m) for i in range(n): print m[i][n-i-1] n = len(m) for i in range(n): print m[i][n-i+1]

2006/07Computing Science 1P Tutorial 14 - Simon Gay15 Which piece of code? None of them Don't know