Upload
silas-gregory
View
217
Download
1
Tags:
Embed Size (px)
Citation preview
Introduction
COMPSCI 105 SS 2015Principles of Computer Science
COMPSCI 1052
Lecturers Lecturers
Angela Chang (Course coordinator) Email: [email protected] Phone: 3737599 ext 86620 Room: 303S.585 Office hours: Tue, Wed, Thurs 11am-12noon
Or whenever the office door is open
Dr Mike Barley Email: [email protected] Phone: 3737599 ext 86133 Room: 303S.394 Office hours: TBA
Lecture 01
• Teaching Assistants• Steven Hu• [email protected]
• Monica Bian• [email protected]
COMPSCI 1053
Topics Making informed choices about programming
Understanding the trade-offs
Focus on ways of storing and manipulating data Different ways of structuring data storage Efficiency Searching Sorting
Some new programming ideas Importance of Abstraction Recursion, regular expressions, exceptions, data
interchange
Lecture 01
COMPSCI 1054
Assessment Note: Students must obtain a pass in both the
practical (assignments) and non-practical work (test + exam) in order to pass as a whole
Laboratories every week (starting next week) ………. 25% Pre-lab exercises & two laboratories per week
dues on every Thursday OR Saturday Note: labs to be submitted using CodeRunner 5 Pre-lab exercises (7%) 9 Laboratories (18%)
Mid-semester Test …………………………………... 10% Thurs 29th Jan, 10:00am – 11:00 am
Final Exam ………………………………………….. 65% Date to be announced
Lecture 01
COMPSCI 1055
Laboratories All Laboratories will be started from Monday 12 Jan. Two Laboratories per week
First Lab (Lab)/ Venue: G75 Streams:
Monday 12:00noon – 1:00pm Monday 1:00pm-2:00pm Tuesday 12:00noon – 1:00pm Tuesday 1:00pm-2:00pm Tuesday 2:00pm-3:00pm
Second Lab (Tut)/ Venue: B75 Streams:
Thursday 12:00noon – 1:00pm Thursday 1:00pm-2:00pm Thursday 2:00pm-3:00pm Friday 12:00noon – 1:00pm Friday 1:00pm-2:00pm
Lecture 01
COMPSCI 1056
Resources Lectures
Overheads and recordings Forum
Question and answers – peers, tutors and lecturers http://forums.cs.auckland.ac.nz/
Textbook Problem Solving with Algorithms and Data Structures Online, free, open source
Additional resources Python.org PythonTutor.com
Lecture 01
COMPSCI 1057
CalendarWk Mo Tu We Th Fr
1
6/JanIntro, Python lists,List Comprehension
7Mutable objectsReferences and Equality
8Classes 1
9Classes 2
2
12Exceptions
13Exceptions 2
14JSON
15Algorithm Analysis
16Abstract Data Types, Stack ADT
3
19Stacks
20Queues
21Lists
22Lists 2
23Recursion 1
4
26PUBLIC HOLIDAY
27Recursion 2
28Searching
29TERM TEST
30Hash functions
52 Hash functions
3Sorting: Simple Sorts
4Sorting: Better Sorts
5Introduction to Trees
6PUBLIC HOLIDAY
6
9Priority Queues and Binary Heaps
10Using Trees - Expressions
11Binary Search Trees
12Binary Search Trees and RegularExpressions
13More Regular Expressions
Lecture 01
COMPSCI 1058
LaboratoriesWk Mo Tu We Th Fr
1
6 Jan 7 8 9
2
12Lab1
13Lab1
14 15Lab2
16Lab2
3
19Lab3
20Lab3
21 22Lab4
23Lab4
4
26PUBLIC HOLIDAY
27 28 29Lab5
30Lab5
5
2 FebLab6
3Lab6
4Lab7
5Lab7
6PUBLIC HOLIDAY
6
9Lab8
10Lab8
11 12Lab9
13Lab9
Lecture 01
COMPSCI 1059
AssessmentsWk Mo Tu We Th Fr Sat
1
6 Jan 7 8 9 10
2
12Due: prelab1 (1%)
13 14Due: Lab1 (2%)
15 16 17Due: Lab2 (2%)
3
19Due: prelab2 (1.5%)
20 21Due: Lab3 (2%)
22 23 24Due: Lab4 (2%)
4
26PUBLIC HOLIDAY
27Due: prelab3 1.5%)
28 29TERM TEST
30 31Due: Lab5 (2%)
5
2 FebDue: prelab4 1.5%)
3 4Due: Lab6 (2%)
5 6PUBLIC HOLIDAY
7Due: Lab7 (2%)
6
9Due: prelab5 1.5%)
10 11Due: Lab8 (2%)
12 13 14Due: Lab9 (2%)
Lecture 01
COMPSCI 10510
Class Representative Must elect a class rep Attends 2 staff student meetings Pass on student concerns to lecturers
Lecture 01
COMPSCI 10511
Revision – Python ProgramsWhen the steps in a process are well defined, we can store them in the computers memory in a way that the computer can follow them. The language we store the instructions must be
formal language Reduces ambiguity
Python is a programming language designed to be easy to read Each step in the program is known as a statement A program is a sequence of statements
Ways of running a program Interactive execution – great for learning Creating a module (file) and executing the module
Lecture 01
COMPSCI 10512
Variables Variables store information
Information is divided into different types Python is dynamically typed Variables do not need to be declared before they are
used Basic types
int, float, boolean, stringx = 34x = 34.5x = Truex = 'Hello'
Lecture 01
COMPSCI 10513
Tracing code Keep track of the contents of variables
Write down the name of each variable Change the value when (and only when) an
assignment occurs When you change a value, cross out the old one and
write a new one
length_in_inches: 50 100length_in_cms: 254.0
Lecture 01
COMPSCI 10514
Example 1 What is the output of the following code?
Perform a code trace.
a = 7b = 3c = 2d = 4e = a
a = bb = ee = cc = dd = eprint(a, b, c, d, e)
a
b
c
d
e
7
3
2
4
7
3
2
2
7
4
Lecture 01
Example01.py
COMPSCI 10516
Expression An expression is part of the program that can be
evaluated (i.e. when the computer follows the rules that define the instructions, an expression turns into a value).
An expression can be used anywhere that a value is used
x = 3 + 4
3 + 4 is an expression
Lecture 01
COMPSCI 10517
Example Floor division and modulus
Integer part of a division, and remainder after division
What do each of the following expressions evaluate to? 10 + 4 10 - 4 10 * 4 10 / 4 10 ** 4 10 // 4 10 % 4
14
6
40
2.5
10000
2
2
Lecture 01
Example01.py
COMPSCI 10518
Functions A function is a sequence of instructions designed
to perform a task, and is packaged as a unit. Functions have a name Functions accept arguments Functions return values
Syntax Indentation rather than braces are used to signify
blocks of code Variables defined within the scope of a function are
not available outside the function
def rectangle_area(width, height): return width * height
Lecture 01
COMPSCI 10519
Exercise 2 Write a function that calculates the area of a
circle area = π r2
Lecture 01
COMPSCI 10520
Boolean values and related operators Boolean values
True False
Relational operators >, >=, <, <=, ==
Boolean operators and, or, not
>>> 2 == 3
Lecture 01
False
True>>> 2 == 3 or 4 < 5
COMPSCI 10521
Conditionals Code is executed if the condition is true
if n < 0: print("Negative number")elif n > 0: print("Positive number")else: print("Zero")
if name == "Andrew": print("Hi Andrew")
if n % 2 == 0: print("Even number")else: print("Odd number")
Lecture 01
name
Andrew
n 6Hi Andrew
Even number
Positive number
COMPSCI 10522
Sequences Sequences allow you to store values in an
organized fashion. strings, lists, tuples, dictionaries, and sets http://en.wikibooks.org/wiki/Python_Programming/
Sequences
Lecture 01
COMPSCI 10523
Strings Strings are a sequence of characters
Strings also have a number of other functions that can be used split() is especially useful
>>> name = 'Andrew'>>> name[0]
Lecture 01
name
Andrew'A'
True
6>>> len(name)
>>> 'd' in name
>>> name + ' ' + 'Luxton-Reilly'
'Andrew Luxton-Reilly'
COMPSCI 10524
Exercise 3 Write a function that determines whether a given
string of text contains a vowel or not. The function should return True if the text contains a vowel.
Lecture 01
COMPSCI 10525
Lists Lists are a built-in type in Python
Use square brackets to signify a list Lists can contain any type of data, or any mixture of
datamy_list1 = [1, 2, 3]
my_list2 = ['Hello', 'Is', 'there', 'anybody', 'out', 'there?']
my_list3 = [1, 5.899, 'Hello']
my_list4 = [4, 2, 6, 9, 3]
my_list 4 2 6 9 3
Lecture 01
COMPSCI 10526
List functions Numerous list functions are supported
Use help(list) to find out the functions Examples:
>>> x = [1, 2, 3]>>> len(x)
Lecture 01
3
[1, 2, 3, 4]
[1, 2, 3, 5]>>> x += [5]
>>> x + [4]
>>> 3 in xTrue
1>>> x[0]
x 1 2 3
x 1 2 3 5
COMPSCI 10527
Lists of lists Since a list can contain anything, it can of course
contain a list
In memory
my_list = [[1, 2, 3], [4, 5, 6], [7], [8, 9]]
my_list0 1 2 3
1 2 30 1 2
4 5 60 1 2
70
8 90 1
Lecture 01
COMPSCI 10528
Exercise 4 Draw a diagram showing how the following list is
structured in memory:
my_list = [[1, 2], [[3, 4], [5, 6, 7]]]
Lecture 01
COMPSCI 10529
Slices of sequences A piece of a sequence can be obtained using the
following syntax sequence_name[x:y] where x is the index of the first element and y is the
index after the last element
>>> name = 'Andrew'>>> name[0:0]
Lecture 01
''
'A'
'ndr'
>>> name[0:1]
>>> name[1:4]
COMPSCI 10530
Slice step valueActually, the syntax allows for a third value, used to define the step size between elements included in the slice. If a value if omitted, it defaults to [start:end:1]
If the step size is negative, it starts at the end and steps backward towards the start.
>>> name = 'Andrew'>>> name[:4:2]
>>> name = 'Andrew'>>> name[::-1]
name A n d r e w
Positive Index
0 1 2 3 4 5
Negative index
-6 -5 -4 -3 -2 -1
Lecture 01
'ad'
'werdnA'
COMPSCI 10531
For loops Used to iterate through a sequence
numbers = [2, 3, 5, 7, 11]for i in numbers: print(i)
name = "Andrew"for c in name: print(c)
Lecture 01
235711
Andrew
Example01.py
COMPSCI 10532
While loops Used to execute code when the end condition is
unknownname = "Andrew Luxton-Reilly"i = 0while name[i] != ' ': i += 1print('Space is at position:', i)
Space is at position: 6
Lecture 01
Example01.py
COMPSCI 10533
Range Range is a special object in Python
Used to generate integer numbers within a range of values
Can iterate through the rangefor x in range(0, 5): print(x)
Lecture 01
01234
Example01.py
COMPSCI 10534
List comprehensions A list can be created using instructions that
appear within the square brackets
The general format is as follows:
Examples: To convert a string to a list of characters:
To generate all the even numbers between 1 and 20
my_list = [x for x in range(0, 10)]
[expression for variable in sequence]
my_list = [c for c in 'Andrew']
my_list = [n * 2 for n in range(1, 10)]
Lecture 01
COMPSCI 10535
Exercise 5 Write a list comprehension that generates all the
odd numbers between 1 and 50
Lecture 01
COMPSCI 10536
We can extend the syntax for a list comprehension to include a condition:
The general format is as follows:
Examples: Generate a list of the non-vowel letters that appear in
a string:
List comprehensions that use conditions
my_list = [x for x in range(0, 10) if x % 2 == 0]
[expression for variable in sequence if condition]
Lecture 01
>>> name = 'Andrew Luxton-Reilly'>>> vowels = 'aeiou‘>>> my_list = [c for c in name if c not in vowels]
['A', 'n', 'd', 'r', 'w', ' ', 'L', 'x', 't', 'n', '-', 'R', 'l', 'l', 'y']
COMPSCI 10537
Information in a list is stored contiguously in memory location of the information can be calculated location = start of the list + index * size of each
element
Efficiency issues It takes the same time to access any of the elements Slow to move elements around (i.e. add and delete
elements from within the list)
Features of lists
Lecture 01