Advertisement
Davitkova

Reg

Jun 17th, 2024
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.41 KB | None | 0 0
  1. import re
  2. username = input()
  3. commands = list()
  4.  
  5.  
  6. def replace_letters_function(name, letters_function):
  7.  
  8.     if letters_function.lower() == "lower":
  9.         name = name.lower()
  10.     elif letters_function.lower() == "upper":
  11.         name = name.upper()
  12.     return name
  13.  
  14.  
  15. def reverse_function(name, start_idx, end_idx):
  16.  
  17.     if start_idx < end_idx:
  18.         name_portion = name[start_idx:end_idx + 1]
  19.         print(name_portion[::-1])
  20.  
  21.  
  22. def substring_function(name, substring):
  23.  
  24.     if substring in name:
  25.         name = re.sub(substring, '', name)
  26.         print(name)
  27.     else:
  28.         print(f"The username {name} doesn't contain {substring}.")
  29.  
  30.  
  31. def replace_char(name, character):
  32.     new_name = []
  33.     for char_idx in range(len(name)):
  34.         if name[char_idx] != character:
  35.             new_name.append(name[char_idx])
  36.         else:
  37.             new_name.append('-')
  38.  
  39.     return ''.join(new_name)
  40.  
  41.  
  42. def is_name_valid(name, character):
  43.     if character in name:
  44.         result = "Valid username."
  45.     else:
  46.         result = f"{character} must be contained in your username."
  47.  
  48.     return result
  49.  
  50.  
  51. def process_commands(name):
  52.  
  53.     for current_command in commands:
  54.         if current_command[0] == "Letters":
  55.             command_attribute = current_command[1]
  56.             name = replace_letters_function(name, command_attribute)
  57.             print(name)
  58.  
  59.         elif current_command[0] == "Reverse":
  60.             command_attribute_1 = int(current_command[1])
  61.             command_attribute_2 = int(current_command[2])
  62.             if min(command_attribute_1, command_attribute_2) >= 0 \
  63.                     and max(command_attribute_1, command_attribute_2) < len(name):
  64.                 reverse_function(name, command_attribute_1, command_attribute_2)
  65.  
  66.         elif current_command[0] == "Substring":
  67.             command_attribute = current_command[1]
  68.             substring_function(name, command_attribute)
  69.  
  70.         elif current_command[0] == "Replace":
  71.             command_attribute = current_command[1]
  72.             name = replace_char(name, command_attribute)
  73.             print(name)
  74.  
  75.         elif current_command[0] == "IsValid":
  76.             command_attribute = current_command[1]
  77.             print(is_name_valid(name, command_attribute))
  78.  
  79.  
  80. while True:
  81.     command = input()
  82.  
  83.     if command == "Registration":
  84.         break
  85.  
  86.     commands.append(command.split())
  87.  
  88. process_commands(username)
  89.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement