Exercise #40: Merging Two Sorted Lists def mergeTwoLists(list1, list2):
# Create an empty list to hold the final sorted results: result = []
# Start i1 and i2 at index 0, the start of list1 and list2: i1 = 0
i2 = 0
# Keeping moving up i1 and i2 until one reaches the end of its list: while i1 < len(list1) and i2 < len(list2):
# Add the smaller of the two current items to the result: if list1[i1] < list2[i2]:
# Add list1's current item to the result: result.append(list1[i1])
# Increment i1: i1 += 1
else:
# Add list2's current item to the result: result.append(list2[i2])
# Increment i2: i2 += 1
# If i1 is not at the end of list1, add the remaining items from list1: if i1 < len(list1):
for j in range(i1, len(list1)):
result.append(list1[j])
# If i2 is not at the end of list2, add the remaining items from list2: if i2 < len(list2):
for j in range(i2, len(list2)):
result.append(list2[j])
# Return the merged, sorted list: return result
Exercise #41: Rot 13 Encryption def rot13(text):
# Create an encryptedText variable to store the encrypted string: encryptedText = ''
# Loop over each character in the text: for character in text:
# If the character is not a letter, add it as-is to encryptedText: if not character.isalpha():
encryptedText += character
# Otherwise calculate the letter's "rotated 13" letter: else:
rotatedLetterOrdinal = ord(character) + 13
# If adding 13 pushes the letter past Z, subtract 26: if character.islower() and rotatedLetterOrdinal > 122:
rotatedLetterOrdinal -= 26
if character.isupper() and rotatedLetterOrdinal > 90:
rotatedLetterOrdinal -= 26
# Add the encrypted letter to encryptedText: