Associate the name with the score

Using two arrays just won't cut it. You need some other data structure to hold your data in such a way that the association between the surfers' name and their score is maintained.

Heves -the away of sorted^ stoves-

9.12

Zack

8 . 65

Stacey

8.45

Juan

8.31

Joseph

8 . 05

Johnny

7.81

Aideen

7.21

Aaron

But the stores no longer match the names!

and here's array the of sorted names.

But the stores no longer match the names!

Mat you »«d is something that looks like this.

Many rows of data

Exactly two

Columns of matched data

Many rows of data

You need a different data structure. But which one?

-"ScMar's Comer

Exactly two

Columns of matched data

Data Structure A standard method of organizing a collection of data items in your Computer's memory.

You've already et one of the classic data structures: the array-

Match the data structure names on the left with their descriptions on the right. We've already done one for you. Which one do you think you'll need to use for the surfer data?

Array

A variable that allows data to enter at one end of a collection and leave at the other end, supporting a first-in, first-out mechanism

Linked list

A variable that contains data arranged as a matrix of multiple dimensions (but typically, only two)

Queue

A variable that has exactly two columns and (potentially) many rows of data

Hash

* A variable with multiple indexed slots for holding data

A variable that creates a chain of data where one data item points to another data item, which itself points to another data item, and another, and so on and so forth

Multi-dimensiona] array A variable that contains a collection of unique data items hash it out

Array

Here's the Queue one you need- ,

You were asked to match the data structure names on the left with their descriptions on the right. You were also to identify which one you thought you might need to use for the surfer data.

A variable that allows data to enter at one end of a collection and leave at the other end, supporting a first-in, first-out mechanism

A variable that contains data arranged as a matrix of multiple dimensions (but typically, only two)

A variable that has exactly two columns and (potentially) many rows of data

A variable with multiple indexed slots for holding data

A variable that creates a chain of data where one data item points to another data item, which itself points to another data item, and another, and so on and so forth

Array

Multi-dimensional array

A variable that contains a collection of unique data items

*„own in the Python world as a "dictionary-

Use a hash

You need to use a data structure that maintains the association between the surfer score and the surfer name, which is exactly what a hash gives you. There are lots of surfers with lots of scores, and you need to maintain the association between the two pieces of information.

Let's take a look at how hashes work.

Hashes go by different names in different programming languages: mapping, dictionary, associative array, and key-value list, to name a few. In this book, we'll stick to using the name hash.

Was this article helpful?

0 0

Post a comment