Built-in functions from the insert
A receipt joins a number to text
- Every Paper 2 has an insert of built-in functions. Anything not in the pseudocode guide is printed there, and an answer may call it.
LEFT(Text, N)copies the firstNcharacters andRIGHT(Text, N)the lastN.MID(Text, Start, N)copiesNcharacters from positionStart. Positions start at 1.NUM_TO_STR(4.5)is the string"4.5". Join it to words with&:&joins strings only.
DECLARE Code : STRING
DECLARE Price : REAL
Code ← "LON-2026-B"
OUTPUT "City: " & LEFT(Code, 3)
OUTPUT "Year: " & MID(Code, 5, 4)
OUTPUT "Gate: " & RIGHT(Code, 1)
Price ← 3 * 1.5
OUTPUT "Total: " & NUM_TO_STR(Price)
"Total: " & Priceis wrong:Priceis aREAL, so turn it into text withNUM_TO_STRfirst.
Check before you convert
- Text typed by a user, or read from a file, is a
STRING, even when it looks like a number. IS_NUM(Text)isTRUEonly when the whole string is a number:"12a"and""giveFALSE.STR_TO_NUM(Text)turns the string into a number you can calculate with. Call it only afterIS_NUMsays the text is safe.
Text |
IS_NUM(Text) |
STR_TO_NUM(Text) |
|---|---|---|
"42" |
TRUE |
42 |
"3.5" |
TRUE |
3.5 |
"12a" |
FALSE |
do not call it |
DECLARE Entry : STRING
DECLARE Index : INTEGER
DECLARE Total : REAL
DECLARE Entries : ARRAY[1:4] OF STRING
Entries[1] ← "12"
Entries[2] ← "seven"
Entries[3] ← "30"
Entries[4] ← "4.5"
Total ← 0
FOR Index ← 1 TO 4
Entry ← Entries[Index]
IF IS_NUM(Entry) THEN
Total ← Total + STR_TO_NUM(Entry)
ELSE
OUTPUT "Skipped: " & Entry
ENDIF
NEXT Index
OUTPUT "Total: " & NUM_TO_STR(Total)
ASC and CHR
ASC('A')is the integer code65;'B'is66, and so on.CHR(72)is the character'H'.- Adding to a code moves along the alphabet:
CHR(ASC('C') + 2)is'E'.
DECLARE Letter : CHAR
Letter ← 'C'
OUTPUT ASC(Letter)
OUTPUT CHR(ASC(Letter) + 2)
OUTPUT CHR(ASC('a') - 32)
- Lower-case codes are 32 more than upper-case ones (
'a'is 97), so- 32gives the capital.TO_UPPERfrom the insert does this for you.
Common mistakes
| Wrong | Right | Why |
|---|---|---|
"Score: " & 87.5 |
"Score: " & NUM_TO_STR(87.5) |
& joins strings only |
STR_TO_NUM(Text) with no check |
IF IS_NUM(Text) THEN … first |
text such as "12a" is not a number |
UCASE("hello") |
TO_UPPER("hello") |
UCASE takes one character |
LEFT(Code, 0) for the first letter |
LEFT(Code, 1) |
the second value is how many characters |
MID(Code, 0, 3) |
MID(Code, 1, 3) |
positions start at 1 |
Now you try
- The tasks use
"Computer",87.5and"23". Choose the function, then check that the output has exactly the words and number asked for.
Independent transfer
The final task has no supplied algorithm. Plan its inputs, processing and outputs, then solve it before opening support. Check more than the sample case.
Độ khó 1/5 · 100 xu + phần thưởng nhỏ khi hoàn thành lần đầu tiên hôm nay
Đầu ra LEFT("Computer", 3).
Nhấn Chạy để xem kết quả ở đây.
Độ khó 1/5 · 100 xu + phần thưởng nhỏ khi hoàn thành lần đầu tiên hôm nay
Đầu ra Score: 87.5 on one line by joining the word and NUM_TO_STR(87.5) with &.
Nhấn Chạy để xem kết quả ở đây.
Độ khó 1/5 · 100 xu + phần thưởng nhỏ khi hoàn thành lần đầu tiên hôm nay
Text là (is) "23". If IS_NUM(Text), output STR_TO_NUM(Text) + 1. Otherwise output bad.
Nhấn Chạy để xem kết quả ở đây.
Độ khó 3/5 · 200 xu + phần thưởng nhỏ khi hoàn thành lần đầu tiên hôm nay
Read a string containing a numeric value or invalid text. If IS_NUM is true, output twice its numeric value; otherwise output invalid.
Nhấn Chạy để xem kết quả ở đây.