Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #---------------------------------
- # Q3: Block Rotate Cipher #
- #---------------------------------
- #-----------------------------------------------------------
- # Parameters: key (b,r)
- # Return: updatedKey (b,r)
- # Description: Assumes given key is in the format of (b(int),r(int))
- # Updates the key in three scenarios:
- # 1- The key is too big (use modulo)
- # 2- The key is negative
- # if an invalid key is given print error message and return (0,0)
- #-----------------------------------------------------------
- def adjustKey_blockRotate(key):
- updatedKey = ()
- error = 'Error (adjustKey_blockRotate): Invalid key'
- if type(key) is tuple:
- b=key[0]
- r=key[1]
- if b > 0:
- if type(b) is int and type(r) is int:
- updatedKey = (b,(r%b))
- else:
- print(error,end='')
- updatedKey = (0,0)
- else:
- print(error,end='')
- updatedKey = (0,0)
- else:
- print(error,end='')
- updatedKey = (0,0)
- return updatedKey
- #-----------------------------------
- # Parameters: text (string)
- # Return: nonalphaList (2D List)
- # Description: Analyzes a given string
- # Returns a list of non-alpha characters along with their positions
- # Format: [[char1, pos1],[char2,post2],...]
- # Example: get_nonalpha('I have 3 cents.') -->
- # [[' ', 1], [' ', 6], ['3', 7], [' ', 8], ['.', 14]]
- #-----------------------------------
- def get_nonalpha(text):
- location = -1
- nonalphaList = []
- for x in text:
- b = []
- if x.isalpha() == False:
- b.append(x)
- location = text.find(x,location+1)
- b.append(location)
- nonalphaList.append(b)
- return nonalphaList
- #-----------------------------------
- # Parameters: text (str)
- # 2D list: [[char1,pos1], [char2,pos2],...]
- # Return: modifiedText (string)
- # Description: inserts a list of nonalpha characters in the positions
- #-----------------------------------
- def insert_nonalpha(text, nonAlpha):
- # your code here
- textList = list(text)
- b = []
- for item in range(len(nonAlpha)):
- for char in nonAlpha[item]:
- b.append(char)
- for i in range(len(b)):
- if (i%2) == 0:
- elem = b[i]
- indx = b[i+1]
- textList.insert(indx,elem)
- modifiedText = ''.join(textList)
- return modifiedText
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement