1 / 34

COMPSCI 105 Principles of Computer Science

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.

gazit
Download Presentation

COMPSCI 105 Principles of Computer Science

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. COMPSCI 105Principles of Computer Science Introduction

  2. 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

  3. 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

  4. 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

  5. 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

  6. 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

  7. 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

  8. Empirical testing • How long will it take for fib_a(100) to execute? COMPSCI 105 - Principles of Computer Science

  9. 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

  10. Class Representative • Must elect a class rep • Attends 2 staff student meetings • Pass on student concerns to lecturers COMPSCI 105 - Principles of Computer Science

  11. 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

  12. 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

  13. 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

  14. 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

  15. 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

  16. 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

  17. 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

  18. Exercises • Write a function that calculates the area of a circle • area = π r2 COMPSCI 107 - Computer Science Fundamentals

  19. 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

  20. 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

  21. 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

  22. 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

  23. 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

  24. 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

  25. 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

  26. 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

  27. 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

  28. 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

  29. 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

  30. 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

  31. 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

  32. Exercise • Write a function that counts the number of vowels in a given string. COMPSCI 107 - Computer Science Fundamentals

  33. 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

  34. 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

More Related