Answered step by step
Verified Expert Solution
Question
1 Approved Answer
please only answer in python Tatooine planet is under attack from stormtroopers, and there is only one line of defense remaining. You have been hired
please only answer in python
Tatooine planet is under attack from stormtroopers, and there is only one line of defense remaining. You have been hired by TASA (Tatooine Air and Space Administration). TASA has a deep-space satellite that takes 8-by8 black-and-white images and sends them back to Tatooine as binary strings described above. To save the planet, the freedom fighters needs to be able to embed messages into those image. You will develop a Python program which allows the user to select from a menu of options and which performs the requested calculations. You must use at least the four functions specified below. You are encouraged to use more functions of your own design. Global variables are not allowed. That is, any variable names used within any function must be parameters or be created in the function unless those variables are constants. You are not allowed to use advanced data structures such as list, dictionaries, classes, etc. However, you are allowed to read ahead and use try-except. You are also not allowed to use built-in functions that are not specified in the function descriptions below: numtobase (N,B) str: a. This function accepts as input a non-negative integer N and a base B (no error checking); it should retum a string representing the number N in base B. The string should always be of length multiple of 8 unless the number is 0 . Your function must output the empty string when the input value of N is 0 . (This avoids leading zeros!) a. Parameters: N (int), B (int) b. Returns : str c. The function displays nothing. d. Steps to convert decimal to other base system: Step 1 - Divide the decimal number to be converted by the value of the new base. Step 2 - Get the remainder from Step I as the rightmost digit (least significant digit) of new base number. Step 3 - Divide the quotient of the previous divide by the new base. Step 4 -Record the remainder from Step 3 as the next digit (to the left) of the new base number. Step 5 - Repeat Steps 3 and 4 , getting remainders from right to left, until the quotient becomes zero in Step 3. The last remainder thus obtained will be the Most Significant Bit (MSB) of the new base number. Example on how to convert from decimal to binary base system: Decimal Number: 29 Calculating Binarv Eduivalent - Decimal Number - 29 = Binary Number - 00011101 As you can see, the remainders have to be arranged in the reverse order so that the first remainder becomes the Least Significant Bit (LSB) and the last remainder becomes the Most Significant Bit (MSB). Now ask yourself... what has to change in order to output the number in base B instead of base 2 ? In [1]/ numtobase (29,2) Out [1]: 00011101 ' basetonum (S,B) int: a. This function accepts as input a string S and a base B where S represents a number in base B where B is between 2 and 10 inclusive. It should then return an integer in base 10 representing the same number as S. It should output 0 when S is the empty string. b. Parameters: S (string), B (int) c. Retums : integer d. The function displays nothing. e. Steps to convert any base system to decimal: Step 1 - Deternine the column (positional) value of each digit (this depends on the position of the digit and the base of the number system). Step 2 - Multiply the obtained column values (in Step I) by the digits in the corresponding columns. Step 3 - Sum the products calculated in Step 2 . The total is the equivalent value in decimal. Example Binary Number - 00011101 Calculating Decimal Equivalent - Binary Number - 00011101 - Decimal Number - 29 Again, the key is to ask yourself... what has to change in order to output base B instead of base 2? In [1]: hasetonum ('00011101', 2) Out [1]+29 In [21) basetonun ('', 4) out 12 l: 0 In 131 : basetonum ('00004321', 5) Out {3):586 basetobase (B1,B2,SB1) str: a. Now, we can assemble what we've written to write a function that takes three inputs: a base B1, a base B2 and s. B1, which is a string representing a number in base B1. Then, your function should return a string representing the same number in base B2.SB1 is always of length multiple of 8 . b. Parameters: B1 (int), B2 (int), S_B1 (str) c. Returns:string d. The function displays nothing. e. Don't rewrite any conversions at all! Instead, convert to decimal and then back to the desired final base! a. This function takes a binary string image representing the image, text representing the message to be hidden in the image and N representing how many bits represent each pixel, and retums another binary string as output. The output binary string should be the original image with the text embedded using the LSB algorithm defined earlier. If image is empty, the function should return an empty string. If text is empty, the image should not change. If the image is not big enough to hold all the text, the function should return None. b. You may need a helper function or two - you may name these whatever you like. Also, you may want to use some of the functions previously defined. c. Parameters: Image (str), text (str), N (int) d. Returns:string or None e. The function displays nothing. Let's begin by considering a 5-by-5 colored image. The image is represented by the following binary string of length 50: image = '10101010101010001010001010111010111010101010101010' This image uses two bits to store the colors: N=2. Let's say we want to embed the text ' CS ' in the image above: text = 'cs' Using the above algorithm: - Step 2: Convert text message into binary. You need to convert each character in the text to binary string then concatenate all binary strings. To convert each character to binary, you need to first convert the character to its decimal number (Hint: ord () ) then from decimal to binary using the function defined in this project. 'cs' .... 0100001101010011 ' 'S' ' 83 ' 1010011 ' Here are a couple of examples of encode_image( in action: In [1]: encode image ( image, text, N ) Out [1]: '10111010101011011011001110101111111010101010101010' In [2] : encode_imagel ", text, N ] Out [2]: In [3]: encode imagel image, ", N ] Out [3]: '10101010101010001010001010111010111010101010101010" In [4]: encode_image ( '101010101010", text, N ) Out [4]: decode image (stego, N) string: a. This function takes a binary string and N representing how many bits represent each pixel as input and returns a string as output, which is the hidden text. The function "inverts" or "undoes" the encoding in your encode_texto function. That is, decode_image (encode_image (image, text, N ), N) should give back text and some more characters as gibberish. b. You may need a helper function or two. Also, you may want to use some of the functions previously defined. c. Parameters: stego (str), N (int) d. Retums:string e. The function displays nothing. 1. Hints: Remember that each character is represented by an 8 bit binary number (i.e., "A" "0100000" ). So the length of the binary string that represents the text should always be a multiple of 8(cg,0,8,16,). Here are a couple of examples of decode_image 0 in action: stego - '101110101010110110110011101011111011101010111011100101010110101010101010' In [2] : text_out = decode_image ( stego, 2) Out [2]= 'CSEx' main(): a. This function is used to interact with the user. It takes no input and returns nothing. Call the functions from here. The program should prompt the user to choose between 6 options (capitalization does not matter) until the user enters ' X ': ' A ' - Convert a decimal number to another base system. ' B ' - Convert decimal number from another base. 'C' - Convert from one representation system to another. ' E ' - Encode an image with a text. ' D ' - Decode an image. ' M ' - Display the menu of options. ' x ' - Exit from the program. b. The program will repeatedly prompt the user to enter a letter (upper or lower case) which corresponds to one of the supported options. For example, the user will enter the letter A (or the letter a) to select Option A (Convert a decimal number in another base system). c. The program will display the menu of options once when execution begins, whenever the user selects Option M, and whenever the user selects an invalid menu option. d. If the user enters option A, the program will ask the user to enter a numeric value N and a base number B in the range between 2 and 10 inclusive. If N is a non-negative number and B is valid (an integer between 2 and 10 ), the program will calculate and display the string representation of the number in base B. Otherwise, the program will display an appropriate message and reprompt to enter the invalid input. Note that the string method .isdigit () is useful here. e. If the user enters option B, the program will ask the user to enter a string S and a base number B in the range between 2 and 10 inclusive. If B is valid, it will calculate and display the decimal representation of the string. Otherwise, the program will display an appropriate message and reprompt to enter the invalid input f. If the user enters option C, the program will ask the user to enter three inputs: a base B1, a base B2 (both of which are between 2 and 10 , inclusive) and S_B1, which is a string representing a number in base B1. If B1 and B2 are valid, it will calculate and display a string representing S_B1 in base B2. Otherwise, the program will display an appropriate message and reprompt to enter the invalid input. g. If the user enters option E, the program will ask the user to enter: (1) a binary string for the image, (2) then, a text to hide in the image, and (3) finally, a number of bits representing the pixels. The program will then encode the image and display the new encoded string of the image. h. If the user enters option D, the program will ask the user to enter. (1) a binary string representing an encoded image, and (2) a number of bits representing the pixels. The program will then decode the image and displays the hidden text. i. If the user enters option M, the program will display the menu. j. If the user enters X, the user wants to quit. The program should display a goodbye message, MEiu = "'nPlease choose one of the options belowa A. Convert a decinat nusber to another base systen B. Convert decinal number fron another base. C. Convert fron one representation systen to another. F. Encode an inage with a text. D. Decode an insge. M. Display the Benu of options. X. Exit fron the progras, "* def nuetobase( N,) : " "Insert docstring bere." pass insert your code here def basetonun( 5,8 ): A long time ago in a gataxy far, far away.. A terrible civil war burns throughout the gataxy Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started