จากตำแหน่งที่กำหนด ให้ mirror ตัวอักษรที่เหลือในลำดับอัลฟาเบต (a<->z, b<->y, ...)
ตัวอย่าง
Input: position=3, string="python" Output: pygslm
อัลกอริทึม
- สร้างสตริงอัลฟาเบตย้อนกลับ: "zyxwvutsrqponmlkjihgfedcba"
- คัดลอกตัวอักษรแรก n ตัวเดิม
- จาก n เป็นต้นไป แปลงแต่ละตัวอักษรด้วย mirror mapping
โค้ดตัวอย่าง
def mirror(str1, n):
alphaset = "zyxwvutsrqponmlkjihgfedcba"
result = ""
for i in range(n):
result += str1[i]
for i in range(n, len(str1)):
result += alphaset[ord(str1[i]) - ord('a')]
return result
str1 = input("Enter the string: ")
n = int(input("Enter the position: "))
print("The Result:", mirror(str1, n-1))
ผลลัพธ์
Enter the string: python Enter the position: 3 The Result: pygslm