Exercise 2
Suppose that the variable
till2 holds data in the form of a sequence of barcodes. When an item is read at the checkout, the state of
till2 is changed by appending the barcode of the item to the end of the sequence giving the state of
till2. Suppose that
till2 currently has the state [50222, 50345], and that an item with barcode 50111 is read, and then an item with barcode 50456 is read. Write out the state of the variable
till2 at each stage.
Answer
Solution
In turn, the state takes the following values:
[50222, 50345] | (at the start). |
[50222, 50345, 50111] | (after 50111 is read). |
[50222, 50345, 50111, 50456] | (after 50456 is read). |
Exercise 3
With item prices as given in Table 1, what is the sequence of prices corresponding to the sequence of barcodes [33050, 33050, 77502, 53151, 77502]? What is the total price of the items given by this sequence?
Answer
Solution
The sequence of prices (given as integer numbers of pence, as in Table 1) is: [299, 299, 89, 440, 89]. The total price (in pence) is 1216 (or £12.16).
No comments:
Post a Comment