Pages

20.8.11

Exercises on Section 1

Exercise 1

  • (a) How many characters are there in the string “This text.”?

  • (b) Which of the following are integers: 3, 0, 98, 4, –22,, ‘7’, [56]?

  • (c) How many integers are there in the sequence [11, 23, 4, 56, 32]?

    Answer

    Solution

    • (a) There are ten characters: four in each word, a space, and a full stop.

    • (b) Each of 3, 0, 98, 4 and −22 is an integer. is not an integer because it is not a whole number. ‘7’ is a character, and is not an integer. [56] is a sequence containing one integer, but is not an integer.

    • (c) There are five integers in the sequence.



    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