120 likes | 208 Views
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)
E N D
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) 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)
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?
Your challenge … With knobs on The Spiral
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.
Where am I? myposition = position() print myposition What kind of thing is this?
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
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.
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