Advertisement
GeorgiLukanov87

01. Decrypting Commands Final exam 100/100

Aug 7th, 2022
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.46 KB | None | 0 0
  1. # 01. Decrypting Commands - Final exam 100/100
  2.  
  3. ===============================================================================================
  4.  
  5. # var1
  6.  
  7.  
  8. def is_index_valid(some_message, some_index):
  9.     if 0 <= some_index < len(some_message):
  10.         return True
  11.     return False
  12.  
  13.  
  14. def string_sum_func(some_string):
  15.     total_sum = 0
  16.     for char in some_string:
  17.         total_sum += ord(char)
  18.     print(total_sum)
  19.  
  20.  
  21. message = input()
  22. command = input()
  23. while not command == 'Finish':
  24.     details = command.split(" ")
  25.     if details[0] == 'Replace':
  26.         cur_char = details[1]
  27.         new_char = details[2]
  28.         message = message.replace(cur_char, new_char)
  29.         print(message)
  30.  
  31.     elif details[0] == 'Cut':
  32.         start_index = int(details[1])
  33.         end_index = int(details[2])
  34.         if is_index_valid(message, start_index) and is_index_valid(message, end_index):
  35.             string_to_remove = message[start_index:end_index+1]
  36.             message = message.replace(string_to_remove, "")
  37.             print(message)
  38.         else:
  39.             print('Invalid indices!')
  40.  
  41.     elif details[0] == 'Make':
  42.         make_to = details[1]
  43.         if make_to == 'Upper':
  44.             message = message.upper()
  45.         elif make_to == 'Lower':
  46.             message = message.lower()
  47.         print(message)
  48.  
  49.     elif details[0] == 'Check':
  50.         check_str = details[1]
  51.         if check_str in message:
  52.             print(f'Message contains {check_str}')
  53.         else:
  54.             print(f"Message doesn't contain {check_str}")
  55.  
  56.     elif details[0] == 'Sum':
  57.         star_indx = int(details[1])
  58.         end_indx = int(details[2])
  59.         if is_index_valid(message, star_indx) and is_index_valid(message, end_indx):
  60.             string_to_sum = message[star_indx:end_indx + 1]
  61.             string_sum_func(string_to_sum)
  62.         else:
  63.             print('Invalid indices!')
  64.  
  65.     command = input()
  66.  
  67.  
  68. ====================================================================================================================
  69.  
  70. # var2 functions
  71.  
  72.  
  73. def is_index_valid(some_message, some_index):
  74.     if 0 <= some_index < len(some_message):
  75.         return True
  76.     return False
  77.  
  78.  
  79. def string_sum_func(some_string):
  80.     total_sum = 0
  81.     for char in some_string:
  82.         total_sum += ord(char)
  83.     print(total_sum)
  84.  
  85.  
  86. def replace_func(some_details, some_message):
  87.     cur_char = some_details[1]
  88.     new_char = some_details[2]
  89.     some_message = some_message.replace(cur_char, new_char)
  90.     print(some_message)
  91.     return some_message
  92.  
  93.  
  94. def cut_func(some_details, some_message):
  95.     start_index = int(some_details[1])
  96.     end_index = int(some_details[2])
  97.     if is_index_valid(some_message, start_index) and is_index_valid(some_message, end_index):
  98.         string_to_remove = some_message[start_index:end_index + 1]
  99.         some_message = some_message.replace(string_to_remove, "")
  100.         print(some_message)
  101.     else:
  102.         print('Invalid indices!')
  103.     return some_message
  104.  
  105.  
  106. def make_func(some_details, some_message):
  107.     make_to = some_details[1]
  108.     if make_to == 'Upper':
  109.         some_message = some_message.upper()
  110.     elif make_to == 'Lower':
  111.         some_message = some_message.lower()
  112.     print(some_message)
  113.     return some_message
  114.  
  115.  
  116. def check_func(some_details, some_message):
  117.     check_str = some_details[1]
  118.     if check_str in some_message:
  119.         print(f'Message contains {check_str}')
  120.     else:
  121.         print(f"Message doesn't contain {check_str}")
  122.  
  123.  
  124. def sum_func(some_details, some_message):
  125.     star_indx = int(some_details[1])
  126.     end_indx = int(some_details[2])
  127.     if is_index_valid(some_message, star_indx) and is_index_valid(some_message, end_indx):
  128.         string_to_sum = some_message[star_indx:end_indx + 1]
  129.         string_sum_func(string_to_sum)
  130.     else:
  131.         print('Invalid indices!')
  132.  
  133.  
  134. message = input()
  135. command = input()
  136. while not command == 'Finish':
  137.     details = command.split(" ")
  138.     if details[0] == 'Replace':
  139.         message = replace_func(details, message)
  140.  
  141.     elif details[0] == 'Cut':
  142.         message = cut_func(details, message)
  143.  
  144.     elif details[0] == 'Make':
  145.         message = make_func(details, message)
  146.  
  147.     elif details[0] == 'Check':
  148.         check_func(details, message)
  149.  
  150.     elif details[0] == 'Sum':
  151.         sum_func(details, message)
  152.  
  153.     command = input()
  154.  
  155.  
  156. ====================================================================================================================
  157.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement