Tracks
/
Cairo
Cairo
/
Exercises
/
Gross Store
Gross Store

Gross Store

Work In Progress
Learning Exercise

Introduction

Cairo provides dictionaries as part of its core libraries represented by the Felt252Dict<T> type.

The dictionary key type in Cairo is restricted to felt252, which differs from other languages where it is possible to define the type of the key type.

To create a dictionary, you can use the default method of the Default trait, like:

let mut foo: Felt252Dict<u32> = Default::default();

The basic operations that are most often used are:

  1. insert(felt252, T) -> () to write values to a dictionary instance and
  2. get(felt252) -> T to read values from it.

And here's how you would use these operations:

let mut foo: Felt252Dict<u32> = Default::default();
// Add a value to the dictionariy
foo.insert('bar', 42);
// Here we update the element of 'bar'
foo.insert('bar', 73);
// To retrieve a dictionary value, you can use
baz = foo.get('bar');

If you try to retrieve the value for a key which does not exist in the dictionary, it will return the zero value of the value type.

This can confuse you, especially if the default value of your value type (for example, 0 for an integer), is a valid value.

This also means there is no way to delete data from a dictionary.

Something to take into account when incorporating this structure into your code.

Instructions

A friend of yours has an old wholesale store called Gross Store.

The name comes from the quantity of the item that the store sell: it's all in gross unit.

Your friend asked you to implement a point of sale (POS) system for his store.

First, you want to build a prototype for it.

In your prototype, your system will only record the quantity.

Your friend gave you a list of measurements to help you:

Unit Score
quarter_of_a_dozen 3
half_of_a_dozen 6
dozen 12
small_gross 120
gross 144
great_gross 1728

1. Store the unit of measurement in your program

In order to use the measurement, you need to store the measurement in your program.

let units = units();
// Felt252Dictionary with entries like { 'dozen': 12 }

2. Create a new customer bill

You need to implement a function that create a new (empty) bill for the customer.

let bill = new_bill();
// Empty Felt252Dictionary

3. Add an item to the customer bill

To implement this, you'll need to:

  • Return false if the given unit is not in the units map.
  • Otherwise add the item to the customer bill, indexed by the item name, then return true.
  • If the item is already present in the bill, increase its quantity by the amount that belongs to the provided unit.
let bill = new_bill();
let units = units();
let ok = add_item(bill, units, 'carrot', 'dozen');
println!(ok);
// Output: true (since dozen is a valid unit)

Note that the returned value is type bool.

4. Remove an item from the customer bill

To implement this, you'll need to:

  • Return false if the given item is not in the bill. An item is considered to not be in the bill if its quantity is 0.
  • Return false if the given unit is not in the units map.
  • Return false if the new quantity would be less than 0.
  • If the new quantity is 0, the item is considered removed from the bill, so you can return true.
  • Otherwise, reduce the quantity of the item and return true.
let bill = new_bill();
let units = units();
let ok = remove_item(bill, units, 'carrot', 'dozen');
println!(ok);
// Output: false (because there are no carrots in the bill)

Note that the returned value is type bool.

5. Return the quantity of a specific item that is in the customer bill

let mut bill: Felt252Dict<u32> = Default::default();
bill.insert('carrot', 12);
bill.insert('grapes', 3);
let qty = get_item(bill, 'carrot');
println!(qty);
// Output: 12

Note that the returned value is type u32.

Edit via GitHub The link opens in a new window or tab
Cairo Exercism

Ready to start Gross Store?

Sign up to Exercism to learn and master Cairo with 68 exercises, and real human mentoring, all for free.