CS074-Digital World

Review



Summary of Course Contents


Units of Information

Binary Encoding of Numbers

Binary Encoding of Text

Binary Encoding of Images

Binary Encoding of Sound

Compression Basics


Python Programming


Internet Basics




Problems


1. 'Textese', or 'SMS language' refers to the style in which text messages are written.  Since people firing off text messages tend to ignore capitalization and punctuation, and since line breaks are handled automatically, textese in effect uses a character set consisting of just 26 letters, the 10 digits, and a space.  Suppose we want to devise a binary encoding of this character set. 
    (a) What is the least number of bits required to encode each character, assuming that we use the same number of bits for each character? 
    (b) In the earliest days of SMS, text messages were limited to 160 characters.  Assuming you use the encoding of part (a), what is the maximum number of bytes in such a text message?

2.  Write in both decimal and hex the number whose binary representation is 1101011. 

3. Write two hundred thirty three in both binary and hex.

4. Describe the steps you would have to perform in the Binary Editor to convert a color bitmap image into a grayscale negative.  (Pictured below.)


                       

5. The steps you carried out in Problem 4 involve a certain operation, or sequence of operations, applied to the bytes of a file.  Suppose you applied exactly the same operations to the audio samples in a .wav file.  What would be the effect on the sound?

6.  This problem describes a data compression technique used to compress black-and-white images, in which each pixel is either black or white, and thus represented by a single bit.  It is well suited for things like fax messages that consist of text and simple line drawings.

(a) Just to get things started off, before we describe the compression technique, how many bytes are required to encode a 400 pixel x 400 pixel image when each pixel is represented by a single bit.

The data compression method looks at each row of the image.  Suppose a row consist of 280 white pixels, followed by 3 black pixels, followed by 74 white pixels, then 2  black pixels, then 41 white pixels.  This row will be encoded as the sequence of numbers 280,2,74,2,41.  We will use two bytes for each number (why?)  Thus the number of bytes used to encode each row is twice the number of alternations between black and white that you find as you scan along the row. This compression method is called run-length encoding.  The full encoding of the image will contain the encoding of each row, together with two bytes telling the number of pixels in each row.

(b) Is this a lossless or lossy compression method? Explain.

(c) Suppose that, on average, each row is encoded by five numbers, as in our example above.  What compression ratio is achieved in encoding a 400x400 pixel image in this fashion?

(d)  Which of these two images will be compressed more by this method:  A brief handwritten note (about 50 words) or a typewritten page of text (about 200 words)?  Explain.

7. What are the values of the variables x and y after the following Python statements are executed?


    x=7/2*3.5
    y="boston"+str(x)

8. Write a python program that draws a house, but paints it in randomly chosen colors.  Thus you should get a different color scheme  each time you run the program. Here's a much trickier question:  Then try to rewrite the program in a fashion so that the difference between the largest and smallest components of the color is at least 150---this should give you a highly saturated color.

9. Write a python program that opens an image file and displays a grayscale negative of the image---essentially question 4 above, but now you have to write it in python.

10. (a) The following python function convert is meant to take an integer argument in the range 0 to 255 and returns a string of length 2.  What string is returned when the argument is 97?  What useful thing is this function doing in general?  What happens when you pass this function an argument outside the prescribed range (like -2 or 256)?

    def convert(num):
      code="0123456789ABCDEF"
      result=code[num/16]+code[num%16]
      return result

(b) Write a python function that prompts the user to enter an integer at the keyboard and then prints the value returned when this integer is passed to convert as an argument.  In other words, your function should call convert.  Your function should also check to see if the integer is in the appropriate range, and print an error message if it is not.  You can use the built-in JES function requestInteger: this will check that the input really is an integer.

11.  Write a python function that does the reverse of the function convert in 8(a):  This should take a string of length 2 as an argument and return the corresponding integer.  (To do this, you really need to understand the useful thing that convert does.)  There are several approaches to this problem.  It can be useful to know (a) you can compare characters using < (for instance 'a'<'d' is true); (b) the ord function gives the ASCII code of a character; (c) the chr function reverses the ord function, i.e., it returns the character whose ASCII code is the argument.