Saturday, May 16, 2026

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)


No comments:

Post a Comment

Week 8 Assignments