0% found this document useful (0 votes)
78 views

Python Assignments Python ADT Rev 1.0

The document describes 9 tasks involving text processing and analysis using Python: 1) Read text from a file, tokenize it, display and modify the tokenized list, and write the result to an output file. 2) Write a function to check if a value is a member of a list. 3) Write a function to check if two lists have any common members. 4) Map a list of words to a list of their lengths. 5) Find the longest word in a list. 6) Implement a palindrome checker that ignores punctuation, case and spacing. 7) Build a character frequency table from a string. 8) Implement encoding and decoding for the ROT

Uploaded by

Anuj
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
78 views

Python Assignments Python ADT Rev 1.0

The document describes 9 tasks involving text processing and analysis using Python: 1) Read text from a file, tokenize it, display and modify the tokenized list, and write the result to an output file. 2) Write a function to check if a value is a member of a list. 3) Write a function to check if two lists have any common members. 4) Map a list of words to a list of their lengths. 5) Find the longest word in a list. 6) Implement a palindrome checker that ignores punctuation, case and spacing. 7) Build a character frequency table from a string. 8) Implement encoding and decoding for the ROT

Uploaded by

Anuj
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 2

1) Get an input file name and an output file name from command line where the input file

has some text. Then do the following:

a. Read the text, tokenize and store all tokens in a python list. (Tokens can be
separated with space, tab, comma, semicolon).

b. Display the list of tokens.

c. Search for those tokens which are of even length and display them.

d. Delete the list elements having the tokens of even length

e. Display the modified list.

f. Update the output file with the contents of the modified list.

(You need to refer the python file handling functions too to solve this question)

2) Write a function is_member() that takes a value (i.e. a number, string, etc) x and a list of
values a, and returns True if x is a member of a, False otherwise. (Note that this is
exactly what the in operator does.)

3) Define a function overlapping() that takes two lists and returns True if they have at least
one member in common, False otherwise. You may use your is_member() function, or
the in operator, but for the sake of the exercise, you should (also) write it using two
nested for-loops.

4) Write a program that maps a list of words into a list of integers representing the lengths
of the corresponding words.

5) Write a function find_longest_word() that takes a list of words and returns the length of
the longest one.

6) Write a version of a palindrome recognizer that also accepts phrase palindromes such
as "Go hang a salami I'm a lasagna hog.", "Was it a rat I saw?", "Step on no pets", "Sit
on a potato pan, Otis", "Lisa Bonet ate no basil", "Satan, oscillate my metallic sonatas",
"I roamed under it as a tired nude Maori", "Rise to vote sir", or the exclamation "Dammit,
I'm mad!". Note that punctuation, capitalization, and spacing are usually ignored.

7) Write a function char_freq() that takes a string and builds a frequency listing of the
characters contained in it. Represent the frequency listing as a Python dictionary. Try it
with something like char_freq("abbabcbdbabdbdbabababcbcbab").

8) In cryptography, a Caesar cipher is a very simple encryption techniques in which each


letter in the plain text is replaced by a letter some fixed number of positions down the
alphabet. For example, with a shift of 3, A would be replaced by D, B would become E,
and so on. The method is named after Julius Caesar, who used it to communicate with
his generals. ROT-13 ("rotate by 13 places") is a widely used example of a Caesar
cipher where the shift is 13. In Python, the key for ROT-13 may be represented by
means of the following dictionary:

key = {'a':'n', 'b':'o', 'c':'p', 'd':'q', 'e':'r', 'f':'s', 'g':'t', 'h':'u', 'i':'v', 'j':'w', 'k':'x', 'l':'y', 'm':'z', 'n':'a',
'o':'b', 'p':'c', 'q':'d', 'r':'e', 's':'f', 't':'g', 'u':'h', 'v':'i', 'w':'j', 'x':'k', 'y':'l', 'z':'m', 'A':'N', 'B':'O',
'C':'P', 'D':'Q', 'E':'R', 'F':'S', 'G':'T', 'H':'U', 'I':'V', 'J':'W', 'K':'X', 'L':'Y', 'M':'Z', 'N':'A',
'O':'B', 'P':'C', 'Q':'D', 'R':'E', 'S':'F', 'T':'G', 'U':'H', 'V':'I', 'W':'J', 'X':'K', 'Y':'L', 'Z':'M'}

Your task in this exercise is to implement an encoder/decoder of ROT-13. Once you're


done, you will be able to read the following secret message:

Pnrfne pvcure? V zhpu cersre Pnrfne fnynq!

Note that since English has 26 characters, your ROT-13 program will be able to both
encode and decode texts written in English.

9) Take an input file name and an output file name from command line where the input file
has some text. Then do the following:

a) Read the text, tokenize and store all tokens in a suitable python data type, along
with their frequency of occurrence.

(Tokens can be separated by space, tab, comma, and semicolon)

b) Display the list of tokens, along with their frequency of occurrence.

c) Update the output file with tokens and their frequency (one record in one line)

(Also the output should be sorted in descending order of frequency)

(You need to refer the python file handling functions too to solve this question)

You might also like