NUSH CS1131 Revision Paper 2 Notes
Uploaded by lxysgp · 21 November 2025
Preview
Text from the first pagesCS1131 Notes Computational Thinking I Revision Paper 2 Worked Solutions Links of All LQ Notes: LQ Notes Links Document Topics Question 1 Question 2 Question 3 Question 4 Question 5 Disclaimer This is a set of notes going through Revision 2: More practices from NUS High Coursemology, since it seems many of you guys have a few concerns / are struggling a bit with it. Again, since this contains information from NUS High Coursemology, please do not share this set of notes outside of your NUS High Y1 Classmates. Thank you :D Good luck! ~LQ (25 Sep)
Solution First, we notice that this is just the sum of the cubes from 1 to 60 (i.e. 1 3 + 2 3 + … + 60 3 ). Let’s first define the variable resultA , which should start at 0. We use resultA = 0 . 1 2 resultA = 0 Now, since the sum starts with 1 3 and ends with 60 3 , we should loop over the values 1 to 60. In this case, we use range(1, 61) because range ends at one number before what’s specified. 1 2 3 4 resultA = 0 for n in range(1, 61): Now, since every term should be cubed, before being added to the resultA variable, we use the operation n ** 3 , then add that to resultA. 1 2 3 4 5 resultA = 0 for n in range(1, 61): resultA = resultA + n ** 3 Finally, we print out resultA. 1 resultA = 0
2 3 4 5 6 7 for n in range(1, 61): resultA = resultA + n ** 3 print(resultA) And that’s our solution.
Solution Let’s first define the variable resultB , which should start at 0. We use resultB = 0 . 1 2 resultB = 0 We then take a user input for n, and store it as a variable. Do recall that the input() function gives a string (i.e. text), so we should convert it into an integer before we can do much with it. 1 2 3 resultB = 0 n = int(input("Enter n: ")) Now, we see each term has alternating sign (i.e. plus, then minus, then plus, and so on…), we should define a variable sign to keep track of that. Since the first term is positive, we let sign be 1 . 1 2 3 4 resultB = 0 n = int(input("Enter n: ")) sign = 1 Since the first term is and the last term is , there are n terms in total, so we use 1 2 𝑛 𝑛 + 1 range(1, n+1) .
1 2 3 4 5 6 resultB = 0 n = int(input("Enter n: ")) sign = 1 for i in range(1, n + 1): Now, we recognise that the i-th term is just , multiplied by the sign variable. Then, we 𝑖 𝑖 + 1 add this to resultB . 1 2 3 4 5 6 7 resultB = 0 n = int(input("Enter n: ")) sign = 1 for i in range(1, n + 1): resultB = resultB + i * sign / (i + 1) But now recall that the sign alternates after every term. Hence, we need to change it from 1 to -1 or -1 to 1. In other words, we multiply sign by -1. 1 2 3 4 5 6 7 8 resultB = 0 n = int(input("Enter n: ")) sign = 1 for i in range(1, n + 1): resultB = resultB + i * sign / (i + 1) sign = (-1) * sign Lastly, we print resultB. 1 2 3 4 5 6 7 8 9 10 resultB = 0 n = int(input("Enter n: ")) sign = 1 for i in range(1, n + 1): resultB = resultB + i * sign / (i + 1) sign = (-1) * sign print(resultB)
Solution Let’s first write some code to calculate the factorial of any number. We store the value in a variable called fact , which will start at 1. 1 2 fact = 1 Let’s calculate the factorial for a number i , but let’s not worry about defining i first. We can just think of it as a placeholder. To calculate the factorial of a number i, we must multiply the numbers 1 to i, inclusive. Hence, we use range(1, n + 1) . 1 2 3 4 fact = 1 for j in range(1, i + 1): For every number j in this range, we multiply fact by j and update the value of fact . 1 2 3 4 5 fact = 1 for j in range(1, i + 1): fact = fact * j
Now, let’s consider how to add all the factorials. We take a user input for n, and define a variable resultC as 0. 1 2 3 resultC = 0 n = int(input("Enter n: ")) Now, we loop through the numbers from 1 to n. Hence, we use range(1, n + 1) . 1 2 3 4 5 resultC = 0 n = int(input("Enter n: ")) for i in range(1, n + 1): We then include the code we wrote just now. See how now we have defined the variable i ? This shows that in coding, sometimes you can leave things hanging for a while, then go back to it. 1 2 3 4 5 6 7 8 9 resultC = 0 n = int(input("Enter n: ")) for i in range(1, n + 1): fact = 1 for j in range(1, i + 1): fact = fact * j Then, we add the factorial we calculate to resultC . 1 2 3 4 5 6 7 8 9 10 11 resultC = 0 n = int(input("Enter n: ")) for i in range(1, n + 1): fact = 1 for j in range(1, i + 1): fact = fact * j resultC = resultC + fact
Lastly, we print resultC . 1 2 3 4 5 6 7 8 9 10 11 12 13 resultC = 0 n = int(input("Enter n: ")) for i in range(1, n + 1): fact = 1 for j in range(1, i + 1): fact = fact * j resultC = resultC + fact print(resultC) And we are done :D
We name the pattern above a flower. The flower is made up of a centre piece, in this case, a square. Each side of the square is 50 pixels in length. The flower has petals, in this case, 4 pentagons. Each side of the pentagon is 50 pixels in length. Write relevant python code to draw the diagram shown above. Solution First, let’s import turtle. (Read instructions carefully, since while coding you should still include this line, but don’t copy it into the submitted copy) 1 2 import turtle Now, to draw a pentagon, let’s use the code to draw any n-sided polygon. Note that the value 50 is given, and the angle 72° is obtained by 360° ÷ 5 = 72°. 1 2 3 import turtle for n in range(5):
4 5 6 turtle.forward(50) turtle.right(72) Now, we note that we have to draw 4 pentagons, and between each pentagon, we need to move the turtle forward by 50, then turn it left by 90 degrees. 1 2 3 4 5 6 7 8 9 import turtle for m in range(4): for n in range(5): turtle.forward(50) turtle.right(72) turtle.forward(50) turtle.left(90) Again, remember to delete import turtle , then copy to coursemology if the question requires you to. We should also check that the drawing looks right, and after running the code, we see that it is indeed correct. So, we’re done :D
Content continues in the PDF. Download PDF
Related notes
- NUSH CS1131 NotesNotes/Practices · 2025
- RI Y3 CEP Using DB Browser for SQLite 2021Notes/Practices · 2021
- RI Y3 CEP Using DB Browser for SQLite 2021Notes/Practices · 2021
- See all Computing notes

