Advertisement
Guest User

Untitled

a guest
Apr 2nd, 2020
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.71 KB | None | 0 0
  1. def encrypt(key_length, string):
  2.     new_string = ""
  3.  
  4.     n = len(string)
  5.     for i in range(n):
  6.         character_unicode = ord(string[i])
  7.  
  8.         if character_unicode == 32:  # whitespace
  9.             new_string += ' '
  10.             continue
  11.         elif character_unicode == 39:  # apostrophe
  12.             new_string += "'"
  13.             continue
  14.         elif 122 >= character_unicode and character_unicode >= 97:  # lowercase
  15.             new_character_code = (character_unicode + key_length) % 123
  16.             if new_character_code < 97:
  17.                 new_character_code += 97
  18.             new_string += chr(new_character_code)
  19.         elif 90 >= character_unicode and character_unicode >= 65:  # uppercase
  20.             new_character_code = (character_unicode + key_length) % 91
  21.             if new_character_code < 65:
  22.                 new_character_code += 65
  23.             new_string += chr(new_character_code)
  24.  
  25.     return new_string
  26.  
  27.  
  28. if __name__ == "__main__":
  29.     while True:
  30.         cmd = input()
  31.  
  32.         if cmd.lower() == "end":
  33.             break
  34.  
  35.         artist, song = cmd.split(':')
  36.         artist_len = len(artist)
  37.         song_len = len(song)
  38.  
  39.         # First check if the artist is valid.
  40.         first_letter_unicode = ord(artist[0])
  41.         if 91 <= first_letter_unicode or first_letter_unicode <= 64:
  42.             print("Invalid input!")
  43.             continue
  44.  
  45.         is_artist_valid = True
  46.         for i in range(1, artist_len):
  47.             character_unicode = ord(artist[i])
  48.  
  49.             if character_unicode == 32:  # whitespace
  50.                 continue
  51.             elif character_unicode == 39:  # apostrophe
  52.                 continue
  53.             elif 122 >= character_unicode and character_unicode >= 97:
  54.                 continue
  55.             else:
  56.                 print("Invalid input!")
  57.                 is_artist_valid = False
  58.                 break
  59.  
  60.         if not is_artist_valid:
  61.             continue
  62.  
  63.         # Then check the validity of the song.
  64.         is_song_valid = True
  65.         for i in range(song_len):
  66.             character_unicode = ord(song[i])
  67.  
  68.             if character_unicode == 32:  # whitespace
  69.                 continue
  70.             elif 90 >= character_unicode and character_unicode >= 65:
  71.                 continue
  72.             else:
  73.                 print("Invalid input!")
  74.                 is_song_valid = False
  75.                 break
  76.  
  77.         if not is_song_valid:
  78.             continue
  79.  
  80.         # If everything was alright, encrypt the information.
  81.         encrypted_artist = encrypt(artist_len, artist)
  82.         encrypted_song = encrypt(artist_len, song)
  83.         print("Successful encryption: %s@%s" %
  84.                 (encrypted_artist, encrypted_song))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement