Computer Project #4
This assignment focuses on the design, implementation, and testing of a Python program which is all about numbers' representation and converting or computing with them.
Assignment Overview
In this assignment, you will practice with functions, strings, controls.
Assignment Background
Number representation
When working with any kind of digital electronics in which numbers are being represented, it is important to understand how numbers are represented in these systems.
Human beings use decimal (base 10) and duodecimal (base 12) systems for counting and measurements (probably because we have 10 fingers and two big toes )1. Computers use binary (base 2) number system because they are made from electronics operating in two states: on and off (only two possible digits 0 or 1). In computing, we also use hexadecimal (base 16) or octal (base 8) systems to more easily represent groups of 4 and 3 binary digits (bits) respectively.
You should all have some familiarity with the decimal system as a positional notation. For instance, to represent the integer one hundred and twenty-three as a decimal number, we can write:
123= 1100 + 2 10 + 31 = 1102 + 2 101 + 3100
Binary number system has two symbols: 0 and 1, called bits. Eight bits is called a byte (why 8-bit unit? Probably because 8 = 23 ). It is also a positional notation, for example,
10110 = 1×24 + 0×23 + 1×22 + 1×21 + 0×20
Black and white images representation
Up to now we've explored how numbers are represented in binary, but in this problem we'll explore the representation of images using 0's and 1's. Let's begin by considering 8-by-8, black-and-white images such as the one below:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Each cell in the image is called a "pixel". A black pixel is represented by the digit 1 and a white pixel is represented by the digit 0. The first digit represents the pixel at the top left corner of the image. The next digit represents the pixel in the top row and the second column. The eighth bit (digit) represents the pixel at the right end of the top row. The next bit represents the leftmost pixel in the second row and so forth. Therefore, the image above is represented by the following binary string of length 64 (note the quotes for the string representation):
'1010101001010101101010100101010110101010010101011010101001010101'
Run-length image compression
Imagine that we have an 8x8 image that looks like this, for example:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Using our standard sequence of 64 bits, this image is represented by a binary string beginning with 16 consecutive 0's (for two rows of white pixels) followed by 16 consecutive 1's (for two rows of black pixels) followed by 16 consecutive 0's followed by 16 consecutive 1's.
Run-length encoding (which, by the way, is used as part of the JPEG image compression algorithm) says: Let's represent that image with the code "16 white, 16 black, 16 white, 16 black". That's a much shorter description than listing out the sequence of 64 pixels "white, white, white, white, ...".
In general, our run-length coding represents an image by a sequence of strings of length 8 (called a "run-length sequence"):
• The first character of the string represents the color that will appear next in the image, either 0 (for white) or 1 (for black).
• The final seven characters contain the number in binary of those bits that appear
consecutively at the current location in the image. If the binary number is less than 7 characters, append 0 to the left until it is 7 characters. For example, 16 in binary is ‘10000’ which is a 5-character long string. We extend the string to length 7 by adding 0’s to the left resulting in “0010000”. Note that in this project, the maximum length of the string representing an image is 64. Therefore, the binary number representation of each color count will never exceed 7 characters (because 7 bits can represent 0-127 since 27 is 128 and the number 64 is smaller than 127).
For example, the run-length sequence of the image above:
Original image:
'0000000000000000111111111111111100000000000000001111111111111111'
Intermediate results (number of consecutive whites and blacks):
"16 white, 16 black, 16 white, 16 black"
Final results (run-length sequence):
'00010000100100000001000010010000'
The character in ‘red’ is the color (0 or 1) representing white or black respectively.
Assignment Specifications
You have been hired by TASA ("Tatooine Air and Space Administration"). TASA has a deep- space satellite that takes 8-by-8 black-and-white images and sends them back to Tatooine as binary strings of 64 bits as described above. To save precious energy required for transmitting data, TASA would like to "compress" the images sent into a format that uses as few bits as possible. One way to do so is to use the run-length encoding algorithm.
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. You are not allowed to use advanced data structures such as lists, 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 (base 10, aka. decimal) and a “new” base B (an integer between 2 and 10 inclusive); it should return a string representing the number N in base B. 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 1 as the rightmost digit (least significant digit) of new base number.
Step 3 − Divide the quotient of the division from Step 1 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 Digit (MSD) of the new base number.
CS 340 Milestone One Guidelines and Rubric Overview: For this assignment, you will implement the fundamental operations of create, read, update,
Retail Transaction Programming Project Project Requirements: Develop a program to emulate a purchase transaction at a retail store. This
7COM1028 Secure Systems Programming Referral Coursework: Secure
Create a GUI program that:Accepts the following from a user:Item NameItem QuantityItem PriceAllows the user to create a file to store the sales receip
CS 340 Final Project Guidelines and Rubric Overview The final project will encompass developing a web service using a software stack and impleme