Advertisement
horozov86

Final Exam Fundamentals Decrypting Commands

Apr 2nd, 2023
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.46 KB | None | 0 0
  1. text = input()
  2.  
  3. while True:
  4.     line = input()
  5.     if line == "Finish":
  6.         break
  7.     line_split = line.split()
  8.     command = line_split[0]
  9.  
  10.     if command == "Replace":
  11.         old_char = line_split[1]
  12.         new_char = line_split[2]
  13.         text = text.replace(old_char, new_char)
  14.         print(text)
  15.  
  16.     elif command == "Cut":
  17.         start_idx = int(line_split[1])
  18.         end_idx = int(line_split[2])
  19.         if 0 <= start_idx <= len(text) and 0 <= end_idx <= len(text):
  20.             text = text[:start_idx] + text[end_idx + 1:]
  21.             print(text)
  22.         else:
  23.             print("Invalid indices!")
  24.     elif command == "Make":
  25.         upper_lower = line_split[1]
  26.         if upper_lower == "Upper":
  27.             text = text.upper()
  28.             print(text)
  29.         else:
  30.             text = text.lower()
  31.             print(text)
  32.     elif command == "Check":
  33.         substring = line_split[1]
  34.         if substring in text:
  35.             print(f"Message contains {substring}")
  36.         else:
  37.             print(f"Message doesn't contain {substring}")
  38.     elif command == "Sum":
  39.         start_idx = int(line_split[1])
  40.         end_idx = int(line_split[2])
  41.         if 0 <= start_idx < len(text) and 0 <= end_idx < len(text):
  42.             substring = text[start_idx: end_idx + 1]
  43.             sum_char = 0
  44.             for ch in substring:
  45.                 sum_char += ord(ch)
  46.             print(sum_char)
  47.         else:
  48.             print("Invalid indices!")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement