Advertisement
Guest User

Untitled

a guest
Oct 15th, 2019
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.57 KB | None | 0 0
  1. #---------------------------------
  2. #   Q3:  Block Rotate Cipher     #
  3. #---------------------------------
  4. #-----------------------------------------------------------
  5. # Parameters:   key (b,r)
  6. # Return:       updatedKey (b,r)
  7. # Description:  Assumes given key is in the format of (b(int),r(int))
  8. #               Updates the key in three scenarios:
  9. #               1- The key is too big (use modulo)
  10. #               2- The key is negative
  11. #               if an invalid key is given print error message and return (0,0)
  12. #-----------------------------------------------------------
  13. def adjustKey_blockRotate(key):
  14.  
  15.     updatedKey = ()
  16.     error = 'Error (adjustKey_blockRotate): Invalid key'
  17.  
  18.     if type(key) is tuple:
  19.         b=key[0]
  20.         r=key[1]
  21.  
  22.         if b > 0:
  23.             if type(b) is int and type(r) is int:
  24.                 updatedKey = (b,(r%b))
  25.             else:
  26.                 print(error,end='')
  27.                 updatedKey = (0,0)
  28.         else:
  29.             print(error,end='')
  30.             updatedKey = (0,0)
  31.            
  32.     else:
  33.         print(error,end='')
  34.         updatedKey = (0,0)
  35.  
  36.     return updatedKey
  37.  
  38. #-----------------------------------
  39. # Parameters:   text (string)
  40. # Return:       nonalphaList (2D List)
  41. # Description:  Analyzes a given string
  42. #               Returns a list of non-alpha characters along with their positions
  43. #               Format: [[char1, pos1],[char2,post2],...]
  44. #               Example: get_nonalpha('I have 3 cents.') -->
  45. #                   [[' ', 1], [' ', 6], ['3', 7], [' ', 8], ['.', 14]]
  46. #-----------------------------------
  47. def get_nonalpha(text):
  48.  
  49.     location = -1
  50.     nonalphaList = []
  51.     for x in text:
  52.         b = []
  53.         if x.isalpha() == False:
  54.             b.append(x)
  55.             location = text.find(x,location+1)
  56.             b.append(location)
  57.             nonalphaList.append(b)
  58.     return nonalphaList
  59.  
  60. #-----------------------------------
  61. # Parameters:   text (str)
  62. #               2D list: [[char1,pos1], [char2,pos2],...]
  63. # Return:       modifiedText (string)
  64. # Description:  inserts a list of nonalpha characters in the positions
  65. #-----------------------------------
  66. def insert_nonalpha(text, nonAlpha):
  67.     # your code here
  68.  
  69.     textList = list(text)
  70.     b = []
  71.     for item in range(len(nonAlpha)):
  72.         for char in nonAlpha[item]:
  73.             b.append(char)
  74.  
  75.     for i in range(len(b)):
  76.         if (i%2) == 0:
  77.             elem = b[i]
  78.             indx = b[i+1]
  79.             textList.insert(indx,elem)
  80.  
  81.     modifiedText = ''.join(textList)
  82.    
  83.     return modifiedText
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement