Spec2Layout (LC 4): Python Programming Assignments 2

adventurescoldSoftware and s/w Development

Nov 7, 2013 (3 years and 11 months ago)

559 views

Spec2Layout (LC 4): Python Programming Assignments 2
Scope: Coverage in Day 2
· Modules and Packages
· Classes – Attributes, Objects & Methods
· Standard Library – os
Module, Files and Directories, Random Numbers, Date Time Manipulations &
Command Line Options
· Text Processing – re
Module

Installation: /software/Python/v2.6.6/<platform name> where <platform> is –
RHEL4-32bit, RHEL4-64bit, RHEL3-32bit, RHEL3-64bit or Sparc-sun-solaris2.9-64bit

Prepared by: Dr. Partha Pratim Das & Madhubanti Dasgupta
Prepared on: 02-Nov-10

1. Python Path Analysis
Write a Python program that:
a)

Shows the Current Working Directory and first 6 import paths

b)

Checks that all '.pyc' files at the CWD exists and are up-to-date

c)

Changes all ‘.py’ files in the CWD to use 4-space indents.

Ref:
http://www.java2s.com/Code/Python/CatalogPython.htm

2. Regular Expressions
Write a Python program (using Regular Expressions) that:
a)

Matches a Negative Floating Point Number and outputs ‘Yes’ if match is found.
Else it outputs ‘No’

Example:

Input

-2.45

Output

Yes

Input

2.45

Output

No

b)

Matches an Email Address and outputs ‘Yes’ if match is found. Else it outputs ‘No’

Example:

Input

ppd@cal.interrasystems.com

Output

Yes

Input

ppd@Interra

Output

No

c)

Matches a Valid Date (yyyy-mm-dd) and outputs ‘Yes’ if match is found. Else it
outputs ‘No’

Example:

Input

1999-01-01

Output

Yes

Input

1999-13-32

Output

No

Ref:
http://www.regular-expressions.info/floatingpoint.html

Ref:
http://www.regular-expressions.info/email.html

Ref:
http://www.regular-expressions.info/dates.html


3. Parsing Application File
Write a Python program that parses the given Command Line Options and returns a List
of their Values. If the Command Line Options List do not have all the Options then it
returns an empty List
Example:

Input

python <Application_Name> --Option1=A1 --Option2=A2 --Option3=A3

Output

[A1, A2, A3]

Ref: SOAP-Python\jira\JIRA-SOAP\Library\Utilities\ParserFunctions.py
4. Log File Handler
Write a Python program for a Log File Handler using Python Classes.
The given Log Levels are debug, info, warning, error and critical.
Now, implement the following Methods:
a)

initLog(logFile, logLevel)
￿
Initializes the Log File and sets the Default Log Level

b)

addLog(logFile, logLevel, logValue)
￿
Adds the Log to the Log File if the given
Log Level is more than or equal to the Default Log Level

c)

dumpLog(logFile)
￿
Dump the Logs from the Log File to the Screen
You may optionally use the Python ‘logging’ Module.
Ref: SOAP-Python\jira\JIRA-SOAP\Library\Utilities\LoggerFunctions.py
Or
http://code.activestate.com/recipes/102263-logfilepy-a-singleton-log-file-creator/


5. Date-Time Manipulation
Write a Python program that:
a)

Calculates the Time Difference between two Dates in Units of Days, Weeks,
Months and Years
Example:

Input

01/01/2010 & 31/12/2010

Output

Days = 364
Weeks = 52
Months = 11
Years = 0
b)

Finds the 'N'th Weekday in a month of a particular Year
Example:

Input

Second Tuesday 05 2005

Output

10

c)

Gets the Beginning Monday and Ending Sunday for a particular Week in a
particular Year
Example:

Input

Week 4 of Year 2010

Output

('Mon 25-Jan-2010', 'Sun 31-Jan-2010')

Ref: SOAP-Python\jira\JIRA-SOAP\Library\Utilities\DateTimeFunctions.py
6. Math Quiz
The following program runs a math quiz consisting of 10 multiplication problems
involving operands between 1 and 10:
from random import randint

correct = 0

for i in range(10):
n1 = randint(1, 10)
n2 = randint(1, 10)
prod = n1 * n2

ans = input("What's %d times %d? " % (n1, n2))
if ans == prod:
print "That's right -- well done.\n"
correct = correct + 1
else:
print "No, I'm afraid the answer is %d.\n" % prod

print "\nI asked you 10 questions. You got %d of them right." % correct
print "Well done!"
Your mission is to do the following:
1. Modify the program so that the user can choose how many questions they will be
asked.

2. Add levels to the program:
o Beginner - with operands between 1 and 10
o Intermediate - with operands between 1 and 25
o Advanced - with operands between 1 and 100

3. Modify the message at the end so that it says:
o “Well done!” if the user answered more than 2/3 of the questions
correctly.
o “You need more practice” if he gets between 1/3 and 2/3 of the questions
correct.
o “Please ask your math teacher for help!” if he gets less than 1/3 of the
questions correct.

4. Allow the user to start another quiz without restarting the program.

5. Let the user choose the question type: addition, subtraction, multiplication, or
mixed.

6. Add division as a question type, but only generate questions which have integral
solutions.

7. Maintain session totals for each type of question (the total number of questions,
and the total answered correctly; the total number of addition, subtraction,
multiplication, and division questions and the total correct for each of these
question types).

Ref:
http://openbookproject.net/pybiblio/practice/elkner/mathquiz.php