1 / 11

Introduction to Algorithms

Introduction to Algorithms. What is an algorithm?. ‘A process that performs some sequence of operations’ Sequence Rigorously defined Discrete, mechanical steps. What’s the difference?. NumberOfSides  4 Length  100 For each Side in NumberOfSides Draw line(length)

Download Presentation

Introduction to Algorithms

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. Introduction to Algorithms

  2. What is an algorithm? • ‘A process that performs some sequence of operations’ • Sequence • Rigorously defined • Discrete, mechanical steps.

  3. What’s the difference? NumberOfSides  4 Length  100 For each Side in NumberOfSides Draw line(length) Rotate Anticlockwise(360/NumberOfSides) • Algorithm • Program var length : integer; nsides : integer; begin length := 100; nsides := 4; for i := 1 to nsides do line(length); aclock(360/nsides); end do; end; dim length as integer = 100 dim nsides as integer = 4 for i = 1 to nsides turtle.forward(length) turtle.left(360/nsides) next i nsides = 4 length = 100 for i in range(0,nsides) forward(length) left(360/nsides)

  4. 2 The main principles of computation Problem Abstraction What are the key features of this problem? How can we express those features precisely? How can we systematically solve all problems like this? Automation How do we turn our solution into something that this particular machine can do?

  5. Your challenge … With knobs on The Spiral

  6. How are you going to do this? • First think through the algorithm • What set of rules create a spiral? • Think of it from the turtle’s point of view • Set out your algorithm in pseudo-code • Then think about how you will make it work in Python.

  7. Where am I? myposition = position() print myposition What kind of thing is this?

  8. Tuples • Singleton • Double • Triple • Quadruple • Quintuple • Sextuple • Septuple • Octuple • N-tuple • A tuple is a sequence • This position value is a two part sequence, an x value and a y value • We can pull out the components with indexing – eg: pos[0] • But we can’t change them

  9. Lists • Here’s a tuple • Point = (0,0) • Here’s a list • Point = [0,0] You can’t change Tuples You can change lists. Why would you use a Tuple? What are they there for? If a Python list were like a pencil, then a Python Tuple would be like a pen.

  10. Chessboard Noughts and Xs

  11. Start Flowchart Set row = 0 Set column = 0 Is row greater than 10? Is column greater than 10? Add one to row Set column = 0 Stop Add one to column Move to Row,Column and Draw Circle

More Related