8_1_6

WHAT: Create and use data structures in a programming language

SMART START:

Perform these binary calculations in your head:

  • 11 + 10
  • 11 + 1
  • 10 + 1
  • 11 - 1

Extra Mile:

Investigate how computers are programmed to structure data.

HOW:

Activity 1

Have a go at this challenge taken from here: http://www.folj.com/puzzles/

The Camels

Four tasmanian camels traveling on a very narrow ledge encounter four tasmanian camels coming the other way.

As everyone knows, tasmanian camels never go backwards, especially when on a precarious ledge. The camels will climb over each other, but only if there is a camel sized space on the other side.

The camels didn't see each other until there was only exactly one camel's width between the two groups.

How can all camels pass, allowing both groups to go on their way, without any camel reversing?

Activity 2

Write an algorithm for the camel challenge. Can you structure the data to make it easier to write your algorithm?

Activity 3

A common method for structuring data in a programming language is called an Array.

In Python, you can create an array by using the structure below:

myArray = ["Camel A","Camel B","Camel C", "Camel D", "SPACE", "Camel 1", "Camel 2", "Camel 3", " Camel 4"]
print (myArray)

Copy and paste this code into Python and see what happens.

(Download Python from here: https://www.python.org/downloads/)

Activity 4

The table shows how the array has been structured. Each location in the array has a number associated with it. Camel 1 is in location [0]. In Python, and most programming languages, the first item in the array is at location [0].

Add the following line of code to your Python program:

print (myArray[0])

Run the code now and see what happens.

Try and print Camel B and Camel C to see if you can get it to work.

Activity 5

If we wanted to test out our algorithm then we would need to know how to swap positions of the camels.

Try this piece of code and see what happens:

myArray[0], myArray[1] = myArray[1], myArray[0]
print (myArray)

You should notice that it has swapped Camel A and Camel B over.

Challenge

Try and figure out how to code the solution to the Camel Challenge in Python!

CHECK:

EMBED:

Learn more about arrays by using this worksheet:

Manipulating Arrays The Basics.doc

Code for this task is here:

# defines an array called animals
animals=["Dog","Cat","Pig","Sheep","Elephant","Aardvark"]
# prints the array
print (animals)
# sorts the array into alphabetical order
animals.sort()
# prints the array
print (animals)
# adds an extra item to the array
animals.append("Cow")
# re-sorts the array
animals.sort()
print (animals)
# removes an item from an array
animals.remove("Dog")
print (animals)
# we can also do all of this with integers
numbers = [4,5,6,2,4,5,7,8]
numbers.sort()
print (numbers)
Manipulating Arrays INTERMEDIATE.doc

Code for this task is here:

# this defines an array
numbers=[]
# this fills that array with numbers automatically
x=0
while x<10:
  numbers.append(x) #whatever is stored in x will be added to the array as an item
  x=x+1
print (numbers)
x=0
while x<10:
  numbers.remove(x) #whatever is stored in x will be removed from the array as an item
  x=x+2 #because we added 2 it will only remove every other item
print (numbers)
# we can do the same for string and input
players=[]
x=0
while x<5:
  name=input("Enter a player name: ") # this will ask for 5 player names
  players.append(name) # each one will be stored as a list item
  x=x+1
print (players) # this will display all the names stored

CLASSROOM IDEAS

The logic puzzle used for this learning outcome is a classic computational thinking exercise. Students may find it easier to understand if they have a physical version of it. They could even try it in the playground with 8 students and some chalk to draw out the grid.