Homework 2- Programming, Assignments of Computer Science

Programming, also known as coding, is the process of creating instructions for computers to follow. It involves writing code in a specific language that a computer can understand and execute to perform tasks or solve problems. Programmers use programming languages to build software, applications, and systems.

Typology: Assignments

2024/2025

Uploaded on 06/27/2025

bananana-apple
bananana-apple 🇬🇧

4

(1)

25 documents

1 / 4

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Homework 2 Sequence and selection
Unit 7 Programming
Name:............................................................................. Class:................... Mark:................
1. The following pseudocode tests how quickly a user can type a given sentence without
making any mistakes.
The statement time.clockTick() returns the number of seconds elapsed since the program
started followed by the number of characters in the sentence typed.
1 sentence = "The quick brown fox jumped over the lazy dog"
2 n = len(sentence)
3 print("Sentence to type: " + sentence)
4
5 mistakeMade = False
6
7 print("Press Enter when you're ready to start typing! Press Enter
when finished")
8 ready = input()
9 print("Go!")
10
11 startTime = time.clockTick()
12 mySentence = input()
13 finishTime = time.clockTick()
14 totalTime = finishTime - startTime
15
16 if mySentence != sentence:
17 mistakeMade = True
18 endif
19 if mistakeMade:
20 print("You made one or more errors")
21 else:
22 print(totalTime)
23 print(n)
24 endif
(a) What type of variable is each of the following? [4]
(i) mistakeMade
(ii) n
(iii) totalTime
(iv) sentence
(b) What does line 16 do? [2]
(c) Alter the program so that instead of storing the sentence “The quick brown fox jumped
over the lazy dog”, the user can enter the sentence on which they will be timed. [3]
1
pf3
pf4

Partial preview of the text

Download Homework 2- Programming and more Assignments Computer Science in PDF only on Docsity!

Unit 7 Programming

Name: ............................................................................. Class: ................... Mark: ................

  1. The following pseudocode tests how quickly a user can type a given sentence without making any mistakes. The statement time.clockTick() returns the number of seconds elapsed since the program started followed by the number of characters in the sentence typed. 1 sentence = "The quick brown fox jumped over the lazy dog" 2 n = len(sentence) 3 print("Sentence to type: " + sentence) 4 5 mistakeMade = False 6 7 print("Press Enter when you're ready to start typing! Press Enter when finished") 8 ready = input() 9 print("Go!") 10 11 startTime = time.clockTick() 12 mySentence = input() 13 finishTime = time.clockTick() 14 totalTime = finishTime - startTime 15 16 if mySentence != sentence: 17 mistakeMade = True 18 endif 19 if mistakeMade: 20 print("You made one or more errors") 21 else: 22 print(totalTime) 23 print(n) 24 endif (a) What type of variable is each of the following? [4] (i) mistakeMade (ii) n (iii) totalTime (iv) sentence (b) What does line 16 do? [2] (c) Alter the program so that instead of storing the sentence “The quick brown fox jumped over the lazy dog”, the user can enter the sentence on which they will be timed. [3]

Unit 7 Programming

  1. A customer entering security details when logging in to a bank website is asked to enter three random characters from their password, which must be at least 8 characters long and contain no spaces. Assume that in this case the password is HC&dt2016. The letters are then compared to those in the password held on file for that customer. Kelly has a first attempt at writing pseudocode for the algorithm. 1 customerPassword = "HC&dt2016" 2 numChars = customerPassword.length 3 index1 = random.randint(0, numChars-1) 4 index2 = random.randint(0, numChars-1) 5 index3 = random.randint(0, numChars-1) 6 7 print("Please enter characters ") 8 print(index1+1) 9 print(index2+1) 10 print(index3+1) 11 print("Press enter after each character: ") 12 char1 = input("Enter character " + str(index1+1) + ": ") 13 char2 = input("Enter character " + str(index2+1) + ": ") 14 char3 = input("Enter character " + str(index3+1) + ": ") 15 16 match1 = char1 == customerPassword[index1] 17 match2 = char2 == customerPassword[index2] 18 match3 = char3 == customerPassword[index3] 19 20 if 21 print("Welcome") 22 else: 23 print("Password incorrect") 24 endif (a) What value is assigned to numChars at line 2? [1]

Unit 7 Programming

  1. Write pseudocode for one or more selection statements to decide whether a year is a Leap year. The rules are: A year is generally a Leap Year if it is divisible by 4, except that if the year is divisible by 100, it is not a Leap year, unless it is also divisible by 400. Thus 1900 was not a Leap Year, but 2000 was a Leap year. [4] [Total 20 marks]