Name - Grade - / 32: National 5 Computing Homework
Name - Grade - / 32: National 5 Computing Homework
CPU
All programming languages have built-in functions that allow basic tasks to be carried out without the need
for additional code.
A few examples of text functions (in the language Python 3.3) are shown below:
len( ) The Length function counts the number of characters in a string
word = “Century” Output from Program
print(len(word)) 7
count( ) The Count function returns the number of times one string occurs in another string
Note - The count function is case sensitive so in the example below only 2
occurrences of “s” are found. The capital S is not counted.
sentence = “Six rain ridden summers” Output from Program
print(sentence.count(“s”)) 2
replace( ) The Replace function finds some given text in a string and replaces it with alternative text.
motto = “Work Hard, Study Hard” Output from Program
newMotto = motto.replace(“Hard”,”Well”) Work Well, Study Well
print(newMotto)
1. For each of the small programs below write down the expected output.
a) word = “Processor” Output from Program (1)
print(len(word)) 9
c) wordOne = “Tailor”
wordTwo = “Made” Output from Program (1)
wordsTogether = wordOne + wordTwo 10
print(len(wordsTogether))
d) lyrics = “Don't criticize what you can't understand” Output from Program (1)
print(lyrics.count(“c”)) 3
e) quotation = “There are only two kinds of people who are really Output from Program (1)
fascinating: people who know absolutely everything, and people 4
who know absolutely nothing. - Oscar Wilde”
print(quotation.count(“re”))
Output from Program
f) quotation = “One day I will find the right words.” One day I will discover the right (1)
quotationTwo = quotation.replace(“find”,”discover”)
words.
print(quotationTwo)
g) quotation = “I met an old lady once, almost a hundred years old, and she told me…”
quotation = quotation.replace(“lady”,”gentleman”) Output from Program
quotation = quotation.replace(“she”,”he”) I met an old gentleman once, almost (1)
print(quotation) a hundred years old, and he told
me…
Created by Mr G Reid, PTC Technologies, Buckhaven High School, September 2013
C
2. The following examples combine two or more of the text functions. Like before, try and predict the
output from the following. You may wish to use a bit of scrap paper to scribble your working on.
a) word = “Code & Coding” Output from Program
word = word.replace(“Code”,”Programs”) Programs & Coding (1)
print(word) 17
print(len(word))
b) quotationPart1 = “Nobody” Output from Program
quotationPart2 = “can make you feel inferior without your” 17 (1)
quotationPart3 = “consent”
total = len(quotationPart1) + len(quotationPart3) + quotationPart2.count(“e”)
print(total)
c) password = “hheeh_shh01” Output from Program
password = password.replace(“hh”,“fff”) Number of characters in password = 12 (1)
password = password.replace(“ee”,“e”)
print(“Number of characters in password =”,len(password))
d) sentenceOne = “The to boys learned to new skills” Output from Program
sentenceTwo = sentenceOne.replace(“to”,“two”) 9 (1)
sentenceThree = sentenceTwo.replace(“boy”,“girls”)
totalLetterW = sentenceOne.count(“w”) + sentenceTwo.count(“w”) + sentenceThree.count(“w”)
totalLetterB = sentenceOne.count(“b”) + sentenceTwo.count(“b”) + sentenceThree.count(“b”)
print(totalLetterW + totalLetterB)
e) messageScrambled = “t s d 20:30”
if len(messageScrambled) >= 15:
messageScrambled = messageScrambled.replace(“t”,“Don’t”)
messageScrambled = messageScrambled.replace(“s”,“Arrive”)
messageScrambled = messageScrambled.replace(“d”,“at”)
else:
messageScrambled = messageScrambled.replace(“t”,“Meet”)
messageScrambled = messageScrambled.replace(“s”,“me”)
messageScrambled = messageScrambled.replace(“d”,“at”)
if (len(messageScrambled) >= 15: Output from Program
message = messageScrambled.replace(“20”,“18”) Meet me at 18:30 (1)
else:
message = messageScrambled.replace(“30”,“15”)
print(message)
3. Using the inputs given, predict what the output will be from the following small program.
usersWord = str(input(“Please enter a word of your choice”))
a = usersWord.count(“a”) Input Entered Output from Program
The most common type of built-in functions found in programming languages are mathematical functions.
A few examples of mathematical functions (in the language Python 3.3) are shown below:
round( ) The Round function reduces the number of decimal places in a number
rounding up or down as required. In the example below 4.6783 is rounded to 1
decimal place.
Output from Program
number = round(4.6783,1)
print(number) 4.7
int( ) The Integer function changes a real number into an integer by removing all the
decimal places, without rounding up or down.
number = 4.6783 Output from Program
print(int(number)) 4
ceil( ) The Ceiling function round a real number up to the nearest integer. Note that this
function requires “import math” at the top of your program.
import math
Output from Program
number = math.ceil(4.6783)
5
print(number)
% The Modulus function calculates the remainder when one number is divided by another.
leftOver = 13%5 Output from Program
print(number) 3
pow( ) The Power multiplies one number to the power of another number.
The example below would calculate 4 to the power of 2 (or 4 squared).
answer = pow(4,2) Output from Program
16
4. For each of the small programs below write down the expected output.
a) height = 193.734 Output from Program (1)
print(round(height,2) 193.73
5. Using the inputs given, predict what the output will be from the following program.
import math
usersOption = str(input(“Please enter a mathematical function of your choice”))
numberOne = float(input(“Please enter the first number”))
numberTwo = float(input(“Please enter the second number”))
if usersOption == “integer”:
answer = int(numberOne + numberTwo)
elif usersOption == “modulus”:
answer = numberOne%numberTwo
answer = numberOne%answer
elif usersOption == “ceiling”:
answer = math.ceil(numberOne + numberTwo)
answer = math.ceil(answer/2)
elif usersOption == “square”:
answer = pow((int(numberOne) + int(numberTwo)),2)
answer = pow(int(numberOne),2) + answer
elif usersOption == “round”:
answer = round((numberOne + numberTwo),1)
answer = round(answer + 0.7,0)
else:
answer = int(numberOne)+pow(numberTwo,2)+math.ceil(numberOne)+round(numberTwo,2)
print(answer)