Programming Exercises

P1:

Write a Python function called reduceWhitespace that is given a line read from a text file and returns the line with all extra whitespace characters between words removed,

    ‘This  line  has  extra  space  characters’ ➝ ‘This line has extra space characters’

In [ ]:

P2:

Write a Python function named extractTemp that is given a line read from a text file and displays the one number (integer) found in the string,

                            ‘The high today will be 75 degrees’ ➝ 75

In [ ]:

P3:

Write a Python function named checkQuotes that is given a line read from a text file and returns True if each quote characters in the line has a matching quote (of the same type), otherwise returns False.

                        ‘Today’s high temperature will be 75 degrees’ ➝ False

In [ ]:

P4:

Write a Python function named countAllLetters that is given a line read from a text file and returns a list containing every letter in the line and the number of times that each letter appears (with upper/lower case letters counted together),

    ‘This is a short line’ ➝ [('t', 2), ('h', 2), ('i', 3), ('s', 3), ('a', 1),('o', 1), ('r', 1), ('l', 1),                                     ('n', 1), ('e', 1)]

In [ ]:

P5:

Write a Python function named interleaveChars that is given two lines read from a text file, and returns a single string containing the characters of each string interleaved,

                                   ‘Hello’, ‘Goodbye’ ➝ 'HGeololdobye' 

In [ ]:

P6:

Write a program segment that opens and reads a text file and displays how many lines of text are in the file.

In [ ]:

P7:

   Write a program segment that reads a text file named original_text, and writes every other line, starting with the first line, to a new file named half_text.

In [ ]:

P8:

Write a program segment that reads a text file named original_text, and displays how many times the letter ‘e’ occurs.

In [ ]:

Modification Problem

Modify the Sparse Text program you wrote in Apply It! section, so that instead of the letter ‘e’ being removed, the user is prompted for the letter to remove.


In [ ]:

Development Problems

D1: Sentence, Word, and Character Count Program

Develop and test a Python program that reads in any given text file and displays the number of lines, words, and total number of characters there are in the file, including spaces and special characters, but not the newline character, '\n'.

In [ ]:

D2: Variation on a Sparsity Program

Develop and test a program that reads the text in a given file, and produces a new file in which the first occurrence only of the vowel in each word is removed, unless the removal would leave an empty word (for example, for the word “I”). Consider how readable the results are for various sample text.

In [ ]:

D3: Message Encryption/Decryption Program

Develop and test a Python program that reads messages contained in a text file, and encodes the messages saved in a new file. For encoding messages, a simple substitution key should be used. 


Each letter in the left column is substituted with the corresponding letter in the right column when encod- ing. Thus, to decode, the letters are substituted the opposite way. Unencrypted message files will be simple text files with file extension .txt. Encrypted message files will have the same file name, but with file extension .enc. For each message encoded, a new substitution key should be randomly generated and saved in a file with the extension '.key'. Your program should also be able to decrypt messages given a specific encoded message and the corresponding key.

In [ ]:

D4: Universal Product Code Check Digit Verification Program

A check digit is a digit added to a string of digits that is derived from other digits in the string. Check digits provide a form of redundancy of information, used for determining if any of the digits in the string are incorrect or misread.

The Universal Product Code on almost all purchase items utilizes a bar code to allow for the scanning of items. Below the bar code is the sequence of digits that the bar code encodes, as illustrated below.

The last digit of the product code (2) is a check digit computed as follows,

1. Add up all digits in the odd numbered positions (first, third, fifth, etc., starting with the leftmost digit) excluding the last check digit, and multiply the result by 3:

                                    0 + 6 + 0 + 2 + 1 + 5 = 14
                                            14 * 3 = 42

2. Add up all digits in the even numbered positions (second, fourth, etc.) excluding the last check digit,

                                      3 + 0 + 0 + 9 + 4 = 16

3. Take the sum of the two previous results mod 10,

                                       (42 + 16) mod 10 = 8

4. Subtract the result from 10 to get the checksum digit. 

                                            10 - 8 = 2


Develop and test a Python program that verifi es the check digit of Universal Product Codes.

In [ ]: