Saturday, June 27, 2026

Week 9 Notes


# 
# Python Problem Solver
# Week 9 Example 1: Subbookkeeper 
#

N = int(input().strip())
line = input().strip()
newlist = []
answer = 0

for i in range(0,N):
    if line[i] == "?":
        if i == 0:
            newlist.append(line[i+1])
        else:
            newlist.append(line[i-1])
    else:
        newlist.append(line[i])

for i in range(0,N-1):
    if newlist[i] == newlist[i+1]:
        answer += 1

print(answer)


# 
# Python Problem Solver
# Week 9 Example 2: The Vigenere Cipher 1
#

original = input("Input the plain text: ").strip().upper()
key = int(input("Input the encryption key: ").strip())

cipher = ""

for c in original:
    asc = ord(c)
    asc += key
    if asc > 90:
        asc -= 26
    cipher += chr(asc)

print(f"The Caesar cipher of '{original}' is '{cipher}'.")


# 
# Python Problem Solver
# Week 9 Example 2: The Vigenere Cipher 2
#

original = input("Input the plain text: ").strip().upper()
key = input("Input the encryption key: ").strip().upper()

keylist = []
for k in key:
    keylist.append(ord(k) - 65)
keylist *= len(original)//len(key)+1
cipher = ""

for i in range(len(original)):
    asc = ord(original[i])
    asc += keylist[i]
    if asc > 90:
        asc -= 26
    cipher += chr(asc)

print(f"The Caesar cipher of '{original}' is '{cipher}'.")

No comments:

Post a Comment

Term 2 Term Test