Saturday, August 29, 2026

Week 15 Assignments

Week 15 Notes


# 
# Python Problem Solver
# Week 15 Example 1: Level Ground (2022 AIO P2) 
#

N = 0
A = []
answer = 0

# Read the value of N.
N = int(input().strip())
# Read the altitudes.
A = list(map(int, input().strip().split()))

lastalt = 0
intensity = 0
for alt in A:
    if alt == lastalt:
        intensity += alt
    else: 
        intensity = alt
    answer = max(answer, intensity)
    lastalt = alt

# Write the answer.
print(answer)

  
#
# Python Problem Solver
# Week 15 Example 2: Spider web with straight threads
#

import turtle

n = int(input("Input number of layers in the web: "))

spider = turtle.Turtle()
spider.pensize(2)
spider.pencolor("black")

for i in range(8):
    spider.fd(200)
    spider.bk(200)
    spider.lt(45)

for i in range(1, n+1):
    spider.fd(50)
    spider.lt(180 - 67.5)
    for j in range(8):
        spider.fd(50*i*0.765)
        spider.lt(45)
    spider.setheading(0)

input()

Sunday, August 23, 2026

Week 14 Assignments

Week 14 Notes


# 
# Python Problem Solver
# Week 14 Example 1: Email Address Validator
#
def validate_email():
    # Step 1: Input a string
    email = input("Enter an email address: ")
    # Step 2: Scan for "@" signs and count them
    at_count = email.count('@')
    if at_count != 1:
        print("Invalid: email must contain exactly one '@' sign.")
        return
    at_pos = email.index('@')
    # Step 3: Scan for "." before and after the "@"
    #   The rules require at least one "." AFTER the "@".
    dot_after = '.' in email[at_pos + 1:]
    if not dot_after:
        print("Invalid: email must contain a '.' after the '@' sign.")
        return
    # Step 4: Scan for invalid characters (spaces and "#")
    #if " " in email or "#" in email:
    for ch in email:
        if ch == ' ' or ch == '#':
            print("Invalid: email cannot contain spaces or '#' signs.")
            return
    # Step 5: Check "@" position and its relative position with "."
    #   - "@" cannot be first
    #   - at least 1 character between "@" and the next "."
    if at_pos == 0:
        print("Invalid: '@' cannot be in the first position.")
        return
    next_dot = email.index('.', at_pos + 1)   # first "." after the "@"
    if next_dot - at_pos < 2:                 # e.g. "a@.com" -> nothing between
        print("Invalid: there must be at least 1 character between '@' and '.'.")
        return
    # Step 6: Check "." position (cannot end with ".")
    if email.endswith('.'):
        print("Invalid: email cannot end with a '.' sign.")
        return
    # Step 7: Confirm valid
    print("Valid email address.")

validate_email()



# 
# Python Problem Solver
# Week 14 Example 2: Sieve of Eratosthenes
#
def sieve(n):
   # Create a boolean list to track the prime status of all numbers pup to n
   numbers = [True] * (n + 1)
   # 0 and 1 are not prime numbers  
   numbers[0] = False
   numbers[1] = False
   
   # Sieve of Eratosthenes algorithm
   for i in range(2,n//2 + 1):
      if numbers[i]:
         # Mark all multiples of i as non-prime
         for j in range(i*2, n + 1, i):
            numbers[j] = False
   # Combine all prime numbers in a list
   primes = []
   for i in range(2, n + 1):
      if numbers[i]:
         primes.append(i)
   
   return primes
# Main program starts here
n = 100
primes = sieve(n)
print("Prime numbers up to " + str(n) + ":")
for prime in primes:
   print(prime, end=' ')


Saturday, August 15, 2026

Week 13 Assignments

Week 13 Notes


# 
# Python Problem Solver
# Week 13 Example 1: Art Class (2021 AIO P2)
#
N = int(input())
minx, maxx, miny, maxy = 0, 10001, 0, 10001
for i in range(N):
    x, y = map(int, input().strip().split())
    maxx = min(maxx, x)
    minx = max(minx, x)
    maxy = min(maxy, y)
    miny = max(miny, y)
print((maxx - minx) * (maxy - miny))

Term 2 Term Test