Course 8.2Lesson 2 of 3
Every language comes with ready-made routines. This lesson uses the library routines of the syllabus, MOD, DIV, ROUND and RANDOM, and the four string routines LENGTH, SUBSTRING, UCASE and LCASE, in Cambridge pseudocode and in Python.
Try the code and the simulations beside the lesson.
Nobody writes their own square-root routine. Languages come with a library: routines that are ready to call. The syllabus names the ones you must know. You met many of them in Python already: // and % in "Numbers and Operators", round() and random in the Python Essentials chapter "Modules: Toolboxes You Can Import".
DIV(17, 5) returns 3, the whole part of the division.MOD(17, 5) returns 2, the remainder.ROUND(Value, Places) returns Value rounded to Places decimal places. Value is REAL and Places is an INTEGER.RANDOM() returns a random number between 0 and 1, both included.DECLARE Average : REAL
Average ← ROUND(17 / 3, 2)
OUTPUT Average, so the output is 5.67. The syllabus gives this example of RANDOM: Value ← ROUND(RANDOM() * 6, 0) returns a whole number between 0 and 6. Multiplying by 6 stretches the range to 0 to 6, and rounding to 0 places makes it whole.
MOD has two common uses. MOD(Number, 2) = 0 is TRUE for an even number. MOD(Number, 10) is the last digit of a whole number.
A string is a row of characters. In Cambridge pseudocode the first character is usually at position 1. The syllabus allows position 0 or 1, so a question will say which it uses.
LENGTH(Text) returns the number of characters, spaces included. LENGTH("Happy Days") is 10.SUBSTRING(Text, Start, Length) returns Length characters, starting at position Start. SUBSTRING("Happy Days", 1, 5) is "Happy".UCASE(Text) returns it in upper case, and LCASE(Text) in lower case. Both work on a STRING or a CHAR.The picture shows the positions. Position 6 holds the space. SUBSTRING("Happy Days", 7, 4) starts at the D and takes 4 characters: "Days". Python counts from 0, so the same piece is phrase[6:10].
DECLARE Phrase : STRING
Phrase ← "Happy Days"
OUTPUT LENGTH(Phrase)
OUTPUT SUBSTRING(Phrase, 7, 4)
OUTPUT UCASE(Phrase)
OUTPUT LCASE(SUBSTRING(Phrase, 1, 1))The outputs are 10, Days, HAPPY DAYS and h.
To look at each character in turn, loop from 1 to LENGTH(...) and take a substring of length 1. This program counts the vowels:
DECLARE Phrase : STRING
DECLARE Letter : CHAR
DECLARE Position : INTEGER
DECLARE Vowels : INTEGER
Phrase ← "Happy Days"
Vowels ← 0
FOR Position ← 1 TO LENGTH(Phrase)
Letter ← LCASE(SUBSTRING(Phrase, Position, 1))
CASE OF Letter
'a' : Vowels ← Vowels + 1
'e' : Vowels ← Vowels + 1
'i' : Vowels ← Vowels + 1
'o' : Vowels ← Vowels + 1
'u' : Vowels ← Vowels + 1
ENDCASE
NEXT Position
OUTPUT "Vowels: ", VowelsLCASE turns each character to lower case, so a capital vowel counts too. A CASE statement with no OTHERWISE does nothing for the other characters. The output is Vowels: 2, for the a of Happy and the a of Days. The y is not a vowel here.
The program writes SUBSTRING as a small Python function, then uses it to count vowels. It also shows DIV, MOD, ROUND and RANDOM.
SUBSTRING("Days", 3, 4) asks for characters that are not there.DIV whole part, MOD remainder, ROUND(Value, Places), RANDOM() from 0 to 1.LENGTH, SUBSTRING(Text, Start, Length), UCASE, LCASE.Try each one on your own first. If you are stuck, open a hint. Then check with Show the answer.
SUBSTRING("Notebook", 5, 4) return?LENGTH("A to Z") return?Number is odd.ROUND(2.718, 1) return?ROUND(RANDOM() * 3, 0) give?Every lesson is open to read. Enrolling also lets you:
Log in to join the discussion.
Loading…
This lesson is free to read. Enroll free to keep your place in the course.