Task 1

A 1-dimensional array, products, stores the names of available products to be sent to good children all over the world:

  playhouse teddy barbie tank ... bike
index 0 1 2 3 ... 99

This is a global array in a computer program.

The program needs a function, isAvailable(), which:

  • asks the user to enter the name of a product
  • searches the global array for the product
  • returns TRUE if the product is in the array
  • returns FALSE if the product is not in the array

Implement this function. Use comments to state the datatype of any variable your solution uses.

Here is a program to help you test your solution:

products = ["playhouse", "teddy", "barbie", "water pistol", "car", "tricycle", "mp3 player", "playstation", "robot", "rabbit"]

#call the function and store the returned data

result = isAvailable()

if result == True:

print("Item available!")

else:

print("Item unavailable")

Task 2 - Adaptive maintenance

The 1-dimensional array, products, has been updated. Each element ends with a 3 character number indicating the amount of products in stock. eg:

 playhouse-010teddy-178barbie-002tank-453...bike-771
index0123...99

The program needs a function, outOfStock(), which:

  • counts how many products have 0 items in stock.
  • returns the count

Implement this function. Use comments to state the datatype of any variable your solution uses.

Here is a program to help you test your solution:

products = ["playhouse-101", "teddy-078", "barbie-012", "water pistol-566", "car-000", "tricycle-000", "mp3 player-000", "playstation-786", "robot-098", "rabbit-000"]

#call the function and store the returned data

result = outOfStock()

print(f"There are {result} products out of stock!")

Task 3 - Super tricky challenge

Implement a new function called outOfStockList()

This function will return an array of items that are out of stock.

For example, for the array in Task 2, if we call outOfStockList() and print the returned array, it will display:

["car-000", "tricycle-000", "mp3 player-000","rabbit-000"]

Each element of the array is added to a file, productData.txt, as a new line.

Christmas song

Jingle bells

Jingle bells

Jingle all the way

Oh what fun

It is to ride

On a one-horse

Open sleigh!