340 likes | 523 Views
COMPSCI 105 Principles of Computer Science. Introduction. Lecturers. Andrew Luxton-Reilly Office Hours: Monday 1-3pm Rm 303.479 Phone: 373-7599 x 85654 andrew@cs.auckland.ac.nz Also teaching: Angela Chang Adriana Ferraro (Course coordinator). Topics.
E N D
COMPSCI 105Principles of Computer Science Introduction
Lecturers • Andrew Luxton-Reilly • Office Hours: Monday 1-3pm • Rm 303.479 • Phone: 373-7599 x 85654 • andrew@cs.auckland.ac.nz • Also teaching: • Angela Chang • Adriana Ferraro (Course coordinator) COMPSCI 105 - Principles of Computer Science
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 COMPSCI 105 - Principles of Computer Science
Assessment • Laboratories every week (starting next week) ………. 10% • 1 hour duration – must be completed by Tuesday midday • Note: First lab due Tuesday 5th August • Assignments most weeks ……………………….……………. 15% • Should take 2 - 5 hours – all due Thursday midday • Note: First assignment due Thursday 7th August Note: Assignments and labs to be submitted using CodeRunner • Mid-semester Test ……………………………………………... 15% • Thurs 28th Aug, 6:25pm – 7:45 pm • Final Exam………………………………………………………….. 60% • Date to be announced COMPSCI 105 - Principles of Computer Science
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 COMPSCI 105 - Principles of Computer Science
Fibonacci numbers • Next number is sum of previous two numbers • 1, 1, 2, 3, 5, 8, 13, 21 … • Mathematical definition COMPSCI 105 - Principles of Computer Science
Question • Which of the following would you choose? deffib_a(n): if n == 0 or n == 1: return n if n >= 2: return fib_a(n - 1) + fib_a(n - 2) deffib_b(n): if n == 0 or n == 1: return n prev = 1 prev_prev = 0 for i in range(2, n+1): temp = prev + prev_prev prev_prev = prev prev = temp return prev COMPSCI 105 - Principles of Computer Science
Empirical testing • How long will it take for fib_a(100) to execute? COMPSCI 105 - Principles of Computer Science
Fibonacci numbers 2 1 3 2 4 3 5 5 6 8 7 13 8 21 9 34 10 55 20 6,765 30 832,040 40 102,334,155 50 12,586,269,025 60 1,548,008,755,920 70 190,392,490,709,135 80 23,416,728,348,467,685 90 2,880,067,194,370,816,120 100 354,224,848,179,261,915,075 COMPSCI 105 - Principles of Computer Science
Class Representative • Must elect a class rep • Attends 2 staff student meetings • Pass on student concerns to lecturers COMPSCI 105 - Principles of Computer Science
Revision – Python Programs When 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 COMPSCI 105 - Principles of Computer Science
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, string x = 34 x = 34.5 x = True x = 'Hello' COMPSCI 107 - Computer Science Fundamentals
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 100 length_in_cms: 254.0 COMPSCI 107 - Computer Science Fundamentals
Exercise • What is the output of the following code? Perform a code trace. a = 7 b = 3 c = 2 d = 4 e = a a = b b = e e = c c = d d = e print(a, b, c, d, e) COMPSCI 107 - Computer Science Fundamentals
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 COMPSCI 107 - Computer Science Fundamentals
Exercise • 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 COMPSCI 107 - Computer Science Fundamentals
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 defrectangle_area(width, height): return width * height COMPSCI 107 - Computer Science Fundamentals
Exercises • Write a function that calculates the area of a circle • area = π r2 COMPSCI 107 - Computer Science Fundamentals
Boolean values and related operators • Boolean values • True • False • Relational operators • >, >=, <, <=, == • Boolean operators • and, or, not >>> 2 == 3 False >>> 2 == 3 or 4 < 5 True COMPSCI 107 - Computer Science Fundamentals
Conditionals • Code is executed if the condition is true if name == "Andrew": print("Hi Andrew") if n % 2 == 0: print("Even number") else: print("Odd number") ifn < 0: print("Negative number") elif n > 0: print("Positive number") else: print("Zero") COMPSCI 107 - Computer Science Fundamentals
Exercise • Given a table of grades as follows, write a function that accepts a mark out of 100 and returns a string containing the grade COMPSCI 107 - Computer Science Fundamentals
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 COMPSCI 107 - Computer Science Fundamentals
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] 'A' >>> 'd' in name True >>> len(name) 6 >>> name + ' ' + 'Luxton-Reilly' 'Andrew Luxton-Reilly' COMPSCI 107 - Computer Science Fundamentals
Exercise • 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. COMPSCI 107 - Computer Science Fundamentals
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 data >>> [1, 2, 3] [1, 2, 3] >>> ['Hello', 'Is', 'there', 'anybody', 'out', 'there?'] ['Hello', 'Is', 'there', 'anybody', 'out', 'there?'] >>> [1, 5.899, 'Hello'] [1, 5.899, 'Hello'] COMPSCI 107 - Computer Science Fundamentals
List functions • Numerous list functions are supported • Use help(list) to find out the functions • Use Python.org to find out more >>> x = [1, 2, 3] >>> len(x) 3 >>> x + [4] [1, 2, 3, 4] >>> x += [5] [1, 2, 3, 5] >>> 3 in x True >>> x[0] 1 COMPSCI 107 - Computer Science Fundamentals
Exercises • Write a function that sums the elements of a list that contains numbers. • Write a function that accepts a list and returns a new list with the same contents, but in reverse order. COMPSCI 107 - Computer Science Fundamentals
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] '' >>> name[0:1] 'A' >>> name[1:4] 'ndr' COMPSCI 107 - Computer Science Fundamentals
Slice step value • Actually, 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] 'ad' >>> name = 'Andrew' >>> name[::-1] 'werdnA' COMPSCI 107 - Computer Science Fundamentals
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) COMPSCI 107 - Computer Science Fundamentals
Exercise • Write a function that returns a list containing the squares of all the values between 1 and n (inclusive). >>> squares(5) [1, 4, 9, 16, 25] COMPSCI 107 - Computer Science Fundamentals
Exercise • Write a function that counts the number of vowels in a given string. COMPSCI 107 - Computer Science Fundamentals
While loops • Used to execute code when the end condition is unknown name = "Andrew Luxton-Reilly" i = 0 while name[i] != ' ': i += 1 print('Space is at position:', i) COMPSCI 107 - Computer Science Fundamentals
Range • Range is a special object in Python • Used to generate integer numbers within a range of values • Can iterate through the range for x in range(0, 10): print(x) COMPSCI 107 - Computer Science Fundamentals