Saturday, May 30, 2026

Week 5 Assignments

Week 5 Notes



# Python Problem Solver
# Week 5 Example 2: Stargate Access Code 
#
# Get the 6-digit code
code = int(input("Input a 6-digit number: "))
# Flag if no combination can match the code
found = False
# 3-level for-loop to brute-force all 3-letter combination
for i in range(65, 91):
    for j in range(65, 91):
        for k in range(65, 91):
            product = i * j * k
            if product == code:
                print(f"Cracked the code: {chr(i)}{chr(j)}{chr(k)}")
                found = True
                break
if not found:
    print("There no combination that matches the code!")





# Python Problem Solver
# Week 5 Example 3: Uno Bank
#
# Generate a deck
deck = ["G+1", "G+5", "L*2", "B-4", "L+2", "G+3", "G+5", "B-6", "L/2", "G+1", "B-2", "G+3"]
draws = 10
bank = 10
# Keep drawing until no draw left
while draws > 0:
    # Draw 1
    draws -= 1
    # Draw 1 card from the deck, and remove it from the deck
    card = deck[0]
    del(deck[0])
    # Take actions according to card drawn
    if card[0] == 'G':
        bank += int(card[2])
    elif card[0] == 'B':
        bank -= int(card[2])
    elif card[0] == 'L':
        if card[1] == '+':
            draws += int(card[2])
        elif card[1] == '-':
            draws -= int(card[2])
        elif card[1] == '*':
            bank *= int(card[2])
        elif card[1] == '/':
            bank //= int(card[2])
print(f"Your final bank account balance is {bank}.")

Saturday, May 23, 2026

Week 4 Assignments

Week 4 Notes


# Python Problem Solver
# Week 4 Example 1: Gauss, method 1
#

# Define total
total = 0

# Iterative approach
# Note this loop runs 100 times
for i in range(1, 101):
    total += i

print(total)

# Python Problem Solver
# Week 4 Example 1: Gauss, method 2
#

# Define total
total = 0

# Gauss approach
# Note this is only one line, and runs only once
total = (1 + 100) * 100 / 2

print(total)

# Python Problem Solver
# Week 4 Example 2: Metronome App
#

import time
import os

def metronome(bpm, timeSignature):
   beatInterval = 60 / bpm
   beatsPerMeasure = timeSignature.split('/')[0]

   while True:
      for beat in range(1, int(beatsPerMeasure) + 1):
         if beat == 1:
            os.system("clear")
            print("*** Metrononome App ***")
            print("----------------------")
            print("BPM: "+ str(bpm))
            print("Time Signature: "+ str(timeSignature))
            print("----------------------")
            print("> tick (" + str(beat) + ")")
         else:
            print("> tock (" + str(beat) + ")")
         time.sleep(beatInterval)

#Main Program Starts Here...
print("*** Metrononome App ***")
print("----------------------")
bpm = int(input("Enter BPM: "))
timeSignature = input("Enter time signature (e.g., 4/4): ")
metronome(bpm, timeSignature)

# Python Problem Solver
# Week 4 Example 3: Election II (AIO 2022 P1)
#

import sys
sys.setrecursionlimit(1000000000)

# N is the number of votes.
N = 0

# votes contains the sequence of votes.
votes = ""

answer = None

# Read the value of N and the votes.
N = int(input().strip())
votes = input().strip()

# TODO: This is where you should compute your solution. Store the winning
# candidate ('A', 'B' or 'C'), or 'T' if there is a tie, into the variable
# answer.

a = 0
b = 0 
c = 0

for v in votes[:N]:
    if v == 'A':
        a += 1
    elif v == 'B':
        b += 1
    elif v == 'C':
        c += 1

if a > b and a > c:
    answer = 'A'
elif b > a and b > c:
    answer = 'B'
elif c > a and c > b:
    answer = 'C'
else:
    answer = 'T'

# Write the answer.
print(answer)

Saturday, May 16, 2026

Week 3 Assignments


Week 3 Notes


# Python Problem Solver
# Week 3 Example 1: the Smart Lift
#
# Get a room number
room = int(input("Room Number : "))
# Integer divide room number by 7 to get floor.  
# Note that 7//7 is not equal to 6//7, so room - 1
floor = (room - 1) // 7
# Reject invalid room number
if room < 1 or room > 707:
    print("Invalid Room Number!")
else:
    if floor == 0:
        print("Ground Floor")
    else: 
        print(floor)


# Python Problem Solver
# Week 3 Example 2: Teletrip (AIO 2023 P1)
#
# Get input, line 1: number N, line 2: string of N instructions
n = int(input())
s = input()
# Define variables, l for leftmost house, r for rightmost house, p for current house
l = 0
r = 0
p = 0
# Loop through all instructions
for i in s[0:n]:
    if i == 'L':
        p -= 1
        if p < l:
            l = p
    elif i == 'R':
        p += 1
        if p > r:
            r = p
    elif i == 'T':
        p = 0
# Output number of houses visited
print(r - l + 1)


Saturday, May 9, 2026

Week 2 Assignments

Week 2 Notes


# Python Problem Solver
# Week 2 Example 1: the Goldbach Conjecture
#

# Define function to determine whether a number is Prime number
def isPrime(num):
    for i in range(2,num):
        if num % i == 0:
            return False
    return True

# Get user to input an enen number larger than 2
n = int(input("Enter an even number greater than 2: "))
while (n % 2 == 1 or n <= 2):
    n = int(input("Enter an even number greater than 2: "))

# For any i less than n, if both i and n-i are prime, we find a match
for i in range (2, n):
    j = n - i
    if isPrime(i) and isPrime(j):
        print(f"{n} = {i} + {j}")
        break


# Python Problem Solver
# Week 2 Example 2: the Egg Farmer's Puzzle
#

# Get user to input a positive integer
n = int(input("Number of eggs: "))
while (n < 1):
    n = int(input("Number off eggs: "))

# Find number of large cartons
c1 = n // 12
if c1 > 0:
    print(f"You will need {c1} cartons of 12")
n -= c1 * 12

# Find number of small cartons
c2 = n // 6
if c2 > 0:
    print(f"You will need {c2} cartons of 6")
n -= c2 * 6 

# Find number of loose eggs
if n > 0:
    print(f"You will have {n} eggs for breakfast!")




Week 8 Assignments