• Homework
  • Thirds
  • Upper Thirds
  • Fourth
  • Divisions
    • ICT
    • Computer Science
  • Fifth
  • Lower Sixth
  • Upper Sixth
    • Upper Sixth Theory
  • Blog
  • Careers
  • Unity
  • Networking

An Algorithm for Quests

An algorithm is a set of specific steps or instructions for solving a problem.  For example there are algorithms to sort numbers, compute mathematical results and render images.
Sir Galwin has some advice for Ann.

Follow the established Algorithm for quests and you'll be alright.

"If you have one or more leads, then you follow the best one.  Otherwise, if you don't have any leads, you travel to where you can find more information.  Break any ties by flipping a coin"


Sir Galwin thinks of this as more of a 'heuristic'.  What does 'heuristic' mean?
Picture

Examples of Algorithms

Make a cup of tea:
  • Turn on kettle
  • Wait till water boils
  • Add tea to teapot
  • Pour water into teapot
  • Wait for three minutes
  • Serve with milk or lemon
Picture

Algorithm to find the largest number
  • input a number
  • if the number is 0 then stop
  • if the number is bigger than the largest number
  • then set the largest number to the current number
  • repeat
We will make this algorithm work in Python.  Here's what we'll need:

  • a variable for the current number
  • a variable for the largest number
  • a loop that will repeat until the current number is 0
  • an input statement that will read the user input
  • a statement that will convert input to a number
  • a comparison of the current number with the largest number
  • an assignment to set the largest number to the current number
Picture
Hints:

Set up your variables by giving them a name and setting them to 1.  eg:
currentnumber = 1
Create a loop using while.  Use the 'not equal to' symbol != to check for 0
Use raw_input() to get keyboard input.  Don't forget a prompt for the user.
raw_input() will give you text rather than a number.  Convert it using the int() function, eg:
currentnumber = int(currentnumber)
Compare numbers with the greater than > or less than < symbols.  Use an if statement here

See your program work - paste it into http://www.pythontutor.com/visualize.html

Powered by Create your own unique website with customizable templates.
Photo used under Creative Commons from dumbledad