Advertisement
simeonshopov

Nikulden’s Charity (Final Exams)

Feb 18th, 2020
184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.85 KB | None | 0 0
  1. text = [x for x in input()]
  2.  
  3.  
  4. def valid_index(g: int, b: list):
  5.     return 0 <= g < len(b)
  6.  
  7.  
  8. def replace(ags: list, b: list):
  9.     current = ags[1]
  10.     new = ags[2]
  11.     for i in range(len(b)):
  12.         if b[i] == current:
  13.             b[i] = new
  14.     return ''.join(b)
  15.  
  16.  
  17. def cut(a: list, b: list):
  18.     start = int(a[1])
  19.     end = int(a[2])
  20.     if valid_index(start, b) and valid_index(end, b):
  21.         to_remove = ''.join([b[x] for x in range(start, end + 1)])
  22.  
  23.         return ''.join(b).replace(to_remove, '')
  24.     else:
  25.         return 'Invalid indexes!'
  26.  
  27.  
  28. def make(a: list, b: list):
  29.     type_case = a[1]
  30.     if type_case == 'Upper':
  31.         return ''.join([x for x in (''.join(b).upper())])
  32.     else:
  33.         return ''.join([x.lower() for x in b])
  34.  
  35.  
  36. def check(a: list, b: list):
  37.     to_search_for = a[1]
  38.     if to_search_for in ''.join(b):
  39.         return f'Message contains {to_search_for}'
  40.     else:
  41.         return f'Message doesn\'t contain {to_search_for}'
  42.  
  43.  
  44. def sum_ascii(a: list, b: list):
  45.     start = int(a[1])
  46.     end = int(a[2])
  47.     if valid_index(start, b) and valid_index(end, b):
  48.         return sum([ord(b[x]) for x in range(start, end + 1)])
  49.     else:
  50.         return 'Invalid indexes!'
  51.  
  52. commands = {
  53.     'Replace': replace,
  54.     'Cut': cut,
  55.     'Make': make,
  56.     'Check': check,
  57.     'Sum': sum_ascii
  58. }
  59.  
  60. while True:
  61.     command = input()
  62.     if command == 'Finish':
  63.         break
  64.     args = command.split()
  65.     operation = args[0]
  66.     print(commands[operation](args, text))
  67.     # if operation == 'Replace':
  68.     #     print(replace(args, text))
  69.     # elif operation == 'Cut':
  70.     #     print(cut(args, text))
  71.     # elif operation == 'Make':
  72.     #     print(make(args, text))
  73.     # elif operation == 'Check':
  74.     #     print(check(args, text))
  75.     # elif operation == 'Sum':
  76.     #     print(sum_ascii(args, text))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement