Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # 01. Decrypting Commands - Final exam 100/100
- ===============================================================================================
- # var1
- def is_index_valid(some_message, some_index):
- if 0 <= some_index < len(some_message):
- return True
- return False
- def string_sum_func(some_string):
- total_sum = 0
- for char in some_string:
- total_sum += ord(char)
- print(total_sum)
- message = input()
- command = input()
- while not command == 'Finish':
- details = command.split(" ")
- if details[0] == 'Replace':
- cur_char = details[1]
- new_char = details[2]
- message = message.replace(cur_char, new_char)
- print(message)
- elif details[0] == 'Cut':
- start_index = int(details[1])
- end_index = int(details[2])
- if is_index_valid(message, start_index) and is_index_valid(message, end_index):
- string_to_remove = message[start_index:end_index+1]
- message = message.replace(string_to_remove, "")
- print(message)
- else:
- print('Invalid indices!')
- elif details[0] == 'Make':
- make_to = details[1]
- if make_to == 'Upper':
- message = message.upper()
- elif make_to == 'Lower':
- message = message.lower()
- print(message)
- elif details[0] == 'Check':
- check_str = details[1]
- if check_str in message:
- print(f'Message contains {check_str}')
- else:
- print(f"Message doesn't contain {check_str}")
- elif details[0] == 'Sum':
- star_indx = int(details[1])
- end_indx = int(details[2])
- if is_index_valid(message, star_indx) and is_index_valid(message, end_indx):
- string_to_sum = message[star_indx:end_indx + 1]
- string_sum_func(string_to_sum)
- else:
- print('Invalid indices!')
- command = input()
- ====================================================================================================================
- # var2 functions
- def is_index_valid(some_message, some_index):
- if 0 <= some_index < len(some_message):
- return True
- return False
- def string_sum_func(some_string):
- total_sum = 0
- for char in some_string:
- total_sum += ord(char)
- print(total_sum)
- def replace_func(some_details, some_message):
- cur_char = some_details[1]
- new_char = some_details[2]
- some_message = some_message.replace(cur_char, new_char)
- print(some_message)
- return some_message
- def cut_func(some_details, some_message):
- start_index = int(some_details[1])
- end_index = int(some_details[2])
- if is_index_valid(some_message, start_index) and is_index_valid(some_message, end_index):
- string_to_remove = some_message[start_index:end_index + 1]
- some_message = some_message.replace(string_to_remove, "")
- print(some_message)
- else:
- print('Invalid indices!')
- return some_message
- def make_func(some_details, some_message):
- make_to = some_details[1]
- if make_to == 'Upper':
- some_message = some_message.upper()
- elif make_to == 'Lower':
- some_message = some_message.lower()
- print(some_message)
- return some_message
- def check_func(some_details, some_message):
- check_str = some_details[1]
- if check_str in some_message:
- print(f'Message contains {check_str}')
- else:
- print(f"Message doesn't contain {check_str}")
- def sum_func(some_details, some_message):
- star_indx = int(some_details[1])
- end_indx = int(some_details[2])
- if is_index_valid(some_message, star_indx) and is_index_valid(some_message, end_indx):
- string_to_sum = some_message[star_indx:end_indx + 1]
- string_sum_func(string_to_sum)
- else:
- print('Invalid indices!')
- message = input()
- command = input()
- while not command == 'Finish':
- details = command.split(" ")
- if details[0] == 'Replace':
- message = replace_func(details, message)
- elif details[0] == 'Cut':
- message = cut_func(details, message)
- elif details[0] == 'Make':
- message = make_func(details, message)
- elif details[0] == 'Check':
- check_func(details, message)
- elif details[0] == 'Sum':
- sum_func(details, message)
- command = input()
- ====================================================================================================================
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement