Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- text = [x for x in input()]
- def valid_index(g: int, b: list):
- return 0 <= g < len(b)
- def replace(ags: list, b: list):
- current = ags[1]
- new = ags[2]
- for i in range(len(b)):
- if b[i] == current:
- b[i] = new
- return ''.join(b)
- def cut(a: list, b: list):
- start = int(a[1])
- end = int(a[2])
- if valid_index(start, b) and valid_index(end, b):
- to_remove = ''.join([b[x] for x in range(start, end + 1)])
- return ''.join(b).replace(to_remove, '')
- else:
- return 'Invalid indexes!'
- def make(a: list, b: list):
- type_case = a[1]
- if type_case == 'Upper':
- return ''.join([x for x in (''.join(b).upper())])
- else:
- return ''.join([x.lower() for x in b])
- def check(a: list, b: list):
- to_search_for = a[1]
- if to_search_for in ''.join(b):
- return f'Message contains {to_search_for}'
- else:
- return f'Message doesn\'t contain {to_search_for}'
- def sum_ascii(a: list, b: list):
- start = int(a[1])
- end = int(a[2])
- if valid_index(start, b) and valid_index(end, b):
- return sum([ord(b[x]) for x in range(start, end + 1)])
- else:
- return 'Invalid indexes!'
- commands = {
- 'Replace': replace,
- 'Cut': cut,
- 'Make': make,
- 'Check': check,
- 'Sum': sum_ascii
- }
- while True:
- command = input()
- if command == 'Finish':
- break
- args = command.split()
- operation = args[0]
- print(commands[operation](args, text))
- # if operation == 'Replace':
- # print(replace(args, text))
- # elif operation == 'Cut':
- # print(cut(args, text))
- # elif operation == 'Make':
- # print(make(args, text))
- # elif operation == 'Check':
- # print(check(args, text))
- # elif operation == 'Sum':
- # print(sum_ascii(args, text))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement