Question

In: Computer Science

in Python3 The ord function in Python takes a character and return an integer that represents...

in Python3

The ord function in Python takes a character and return an integer that represents that character.
It does not matter what the integer representing the character actually is, but what matters is this:

ord('a') is 1 less than ord('b'), so that:

x=ord('a')
thisLetter = x+1 # thisLetter is the ord('b')

This is a powerful fact that is used in encryption techniques, so data transferred over the web is 'ciphered so it is unreadable to others.

To decipher data, we take a string and change it into another string by adding a constant (called the key) to each of its letters' ord value.
See the book's Case study: word play.

To cipher and decipher data, we need two functions: one to take a string, and change it to something else. For this we need to use the ord function, and add the 'key' to result in a new string. For example, say x is one letter to be ciphered, and the key is 3. We can use:

Newx=ord(x)+3

Newx will be an integer. To find out what letter that integer represents you can use the chr function as in:
actualLetter = chr(x)

Write a function named cipher that takes a string.
The function returns that string in a ciphered form by using the ord of the first letter of the string to cipher each letter including the first letter. Hence for abc, use the ord of 'a', and add it to the ord of 'a' and convert that result to a character use the chr function. This character should be concatenated to the same action on the letter b and so on. Hence the function returns:

chr(ord('a')+ord('a')) + chr(ord('a')+ord('b')) + chr(ord('a')+ord('c')).
Obviously you need a loop to iterate on each letter.

Write another function to decipher (do the opposite of the previous function), given a string and returns the deciphered string. Obviously, the first letter's ord is halved to find the first letter, and that value is used to decipher the remaining letters.

From main, write code to get a string, as input, then call the cipher function and print its output.
Then call the decipher function and display its output. The decipher output should match the original string.

For help on this see the book's Case study: word play.

Add comments as needed but make sure to add top level comments.

Solutions

Expert Solution

#source code in python:

def cipher(strc,keyc):

c=""

for i in strc: #iterate over the charecter

v=ord(i)+keyc #add key to the character

c=c+chr(v) #here convert to char

return c #here return the cipher text

def decipher(strd,keyd):

d=""

for i in strd: #iterate over the character

v=ord(i)-keyd #here substract key

d=d+chr(v) #here convert to char

return d #here return the decrypted text

if __name__=="__main__":

strn=input("Enter String Here:")

k=int(input("Enter Key Here:"))

cipher_text=cipher(strn,k) #call the cipher function

decipher_text=decipher(cipher_text,k) #here call the decipher function

print("Enter Text:",strn)

print("Cipher Text:",cipher_text)

print("Decipher Test:",decipher_text)

#output:

#if you have any doubt or more information needed comment below..i will respond as possible as soon..thanks....


Related Solutions

Define a function average_entries_for(integer) that takes in an integer than represents the number of times the...
Define a function average_entries_for(integer) that takes in an integer than represents the number of times the function will ask the user to input a number. The function will return the average of all the numbers entered. You must use a for loop to implement this function. (Hint: use range)
In Python, write a function one_bit_NOT that takes one argument, a character that is either '0'...
In Python, write a function one_bit_NOT that takes one argument, a character that is either '0' or '1'. It should perform the NOT operation and return a string with a single character as the result. I.e., if the character argument is "0", it returns a "1"'. If the character argument is "1", it returns a "0".
Write a function in python that takes in an integer n and computes the left hand...
Write a function in python that takes in an integer n and computes the left hand Riemann sum of the function f(x) = x^2 on the interval [0,1]. Hint: compute the error from the true answer
write a function named as cubeCalculator that takes an integer pointer as function and return its...
write a function named as cubeCalculator that takes an integer pointer as function and return its cube value , you are required to compute the cube of a number using pointer notation , return the result as an integer value , use c++
python exercise: a. Write a function sumDigits that takes a positive integer value and returns the...
python exercise: a. Write a function sumDigits that takes a positive integer value and returns the total sum of the digits in the integers from 1 to that number inclusive. b. Write a program to input an integer n and call the above function in part a if n is positive, else give ‘Value must be Positive’ message. sample: Enter a positive integer: 1000000 The sum of the digits in the number from 1 to 1000000 is 27000001 Enter a...
(In python) Write a function calc_pizza_charge that takes four integer arguments, one for the size (1...
(In python) Write a function calc_pizza_charge that takes four integer arguments, one for the size (1 small, 2 medium, 3 large), one for the number of meat toppings, one for the number of other toppings and another for the quantity of pizzas ordered. It should calculate and return the total due for that pizza order based on the following information: Small pizza base price: $6.50 Medium pizza base price: $9.50 Large pizza base price: $11.50 The base pizza price includes...
PYTHON 3: Write a recursive function that takes a non-negative integer n as input and returns...
PYTHON 3: Write a recursive function that takes a non-negative integer n as input and returns the number of 1's in the binary representation of n. Use the fact that this is equal to the number of 1's in the representation of n//2 (integer division) plus 1 if n is odd. >>>numOnes(0) 0 >>>numOnes(1) 1 >>>numOnes(14) 3
I have a python coding question: Write the function: eAapproximately (n), that takes a positive integer...
I have a python coding question: Write the function: eAapproximately (n), that takes a positive integer value n and returns an approximation of e as (1 + 1/n)^n I am not even sure what the question is asking me to do but I think it is asking me to code that function. Does anyone know how to do this?
Write a function that takes a number as input, and returns the character A if the...
Write a function that takes a number as input, and returns the character A if the input is 90 and above, B if it’s 80 and above but less than 90, C if it’s at least 70 but less than 80, D if it’s at least 60 but less than 70, and F if it’s less than 60. If the input is not a number or is negative, the function should exit 1 with an error (by calling the Matlab...
IN PYTHON Write a program that takes in a positive integer as input, and outputs a...
IN PYTHON Write a program that takes in a positive integer as input, and outputs a string of 1's and 0's representing the integer in binary. For an integer x, the algorithm is: As long as x is greater than 0 Output x % 2 (remainder is either 0 or 1) x = x // 2 Note: The above algorithm outputs the 0's and 1's in reverse order. You will need to write a second function to reverse the string....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT