Advertisement
Kamend1

09_anonymous_threat_Kamen_Dimitrov

Oct 8th, 2023
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.97 KB | None | 0 0
  1. def merge_elements(start_idx: int, end_idx: int, element_list: list):
  2.     concatenated_string = ""
  3.     removed_strings = []
  4.     if start_idx < 0:
  5.         start_idx = 0
  6.     if start_idx > len(element_list) - 1:
  7.         start_idx = len(element_list) - 2
  8.     if end_idx >= len(element_list) - 1:
  9.         end_idx = len(element_list) - 1
  10.     if end_idx < 0:
  11.         end_idx = 1
  12.     for i in range(start_idx, end_idx + 1):
  13.         removed_strings.append(element_list[i])
  14.     concatenated_string = "".join(removed_strings)
  15.     for item in removed_strings:
  16.         element_list.remove(item)
  17.     element_list.insert(start_index, concatenated_string)
  18.     return element_list
  19.  
  20.  
  21. def divide_element(idx: int, number_parts: int, element_list: list):
  22.     string_to_be_divided = element_list[idx]
  23.     part_length = len(string_to_be_divided) // number_parts
  24.     parts_of_string = []
  25.     idx_start = 0
  26.     idx_end = 0
  27.     for i in range(number_parts - 1):
  28.         idx_start = i * part_length
  29.         idx_end = (i + 1) * part_length
  30.         part = string_to_be_divided[idx_start:idx_end]
  31.         parts_of_string.append(part)
  32.     if idx_end <= len(string_to_be_divided):
  33.         last_part = string_to_be_divided[idx_end:]
  34.         parts_of_string.append(last_part)
  35.  
  36.     element_list.pop(idx)
  37.     for j in range(len(parts_of_string)):
  38.         element_list.insert(idx + j, parts_of_string[j])
  39.     return element_list
  40.  
  41. data_elements = input().split()
  42.  
  43. user_command = input()
  44.  
  45. while user_command != "3:1":
  46.     user_command_list = user_command.split()
  47.     if user_command_list[0] == "merge":
  48.         start_index = int(user_command_list[1])
  49.         end_index = int(user_command_list[2])
  50.         merge_elements(start_index, end_index, data_elements)
  51.     elif user_command_list[0] == "divide":
  52.         index = int(user_command_list[1])
  53.         parts = int(user_command_list[2])
  54.         divide_element(index, parts, data_elements)
  55.     user_command = input()
  56.  
  57. print(*data_elements, sep=" ")
  58.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement