Sunteți pe pagina 1din 7

Assignment

1. Introduction

Please complete the exercises described in the following sections using the Tcl or Python scripting

languages:

Python 2.x NOT Python 3.x (Python 2.7.x is the preferred version)

For each exercise, we would like to see the following:

An Python script.

Whatever test data has been used to test that script.

A text file with the following information:

Any assumptions you have made when writing your script.

How you have tested your script.

Instructions on how to run your script.

Environment information such as Windows/Unix and Tcl/Python versions.

The quality of your solution will be judged by the following criteria:

Correctness.

Maintainability.

Clarity and Readability.

Robustness.

Performance - which must be adequate and should not be an issue if you avoid gross

inefficiencies.

Completeness of test evidence.

Please use a file system directory per exercise solution. When you have completed the homework,

please send us your solutions as an archive file (e.g. ZIP, TAR).


2. Regular Expressions Warm-up Exercise
For this exercise please write a script that implements the following five regular expressions tasks:

W1:

Write a regular expression that only matches the string (wherever it occurs): ABC

W2:

Write a regular expression that matches any of the following, and no others: ABC ACC ADC

AXC

W3:

Write a regular expression that will only match the following string when it is on a line entirely

by itself: ABC

W4:

Write a regular expression that matches only the string that starts with 'A' and ends with 'B',

with anything in-between. Make a note of any assumptions you make.

W5:

Write a regular expression that matches only the string that starts with 'A' and ends with 'B',

with a run of one or more of either 'XO' or 'OX' in-between. Make a note of any assumptions

you make.

For each regular expression, the script should report:

The test data being evaluated.

If the test data matches the regular expression, by outputting: "MATCH:" followed by the

matched pattern.

If the test data does not match, by outputting: "NO MATCH".

The script should execute each regular expression sequentially (W1 to W5), announcing each one

before it is tested.
Example expected output:

Task W1:

ABC

MATCH: ABC

AABCC

MATCH: ABC

AAAC

NO MATCH

AXC

NO MATCH

Task W2:

...

It is up to you supply a suitable set of test data, and write the script that satisfies the requirements for

all five regular expressions.


3.File Searching for 'port #nnnn'
The scenario:

You have a big directory tree with many files and directories in it. Some of those files have the

extension '.inform'. Some of the files with that extension contain instances of the string 'port

#nnnn', where nnnn is a hexadecimal number of some length.

Document any assumptions you make when completing the tasks below. You may make assumptions

at any stage.

3.1 Task F1

Searching only the files with the '.inform' extension, count the number of instances of each different

'port #nnnn' string, where nnnn is a four or more digit hexadecimal number to a maximum of 32-digits.

Output a table of the port numbers against their frequency of occurrence.

Example expected output:

port freq

0000 1

1284 5

28f6 1

144ff 7

123abc 6

Ensure that each file (piece of data on the disk) is only scanned once, and that the strings in it are

only counted once.

Please include your test data.

3.2 Task F2 Extension

The Linux file system allow symbolic links to files in a different directory location or with a different
name.

For extra credit; modify your program to deal with directory structures that contain such links, such

that there may be more than one path from the root to a given '.inform' file.
4. Password Task
Document any assumptions you make when completing the tasks below. You may make assumptions

at any stage.

4.1 Task P1

Write a script that asks a user to set a password. It must:

Query the user for their user name.

Query the user for their password twice, making sure the user enters the same password

in both instances.

Allow only three attempts to set a correct password.

The rules for the password are as follows:

It must contain at least one number.

It must contain at least one lower case letter.

It must contain at least one upper case letter.

Allowed characters: numbers, letters, '_' (underscore), '-' (dash) and '.' (fullstop).

Other characters are not allowed.

4.2 Task P2 – using “expect”

For this task the “expect” module/package of your chosen language must be used.

It could be used to simply process and check the user inputting the password, or you could create a

separate script using “expect” that interacts with (and tests) your password script.

Write a script that asks the user for their password:

Query the user for their user name.

If the user name is not in the 'company database'(*), then reject it, otherwise continue.

Query the user for their password.


If this is the first time that the user has entered their password, ask for it twice.

If this is not the first time and if it is correct, ask the user to change their password.

(*)Note that we are not expecting a fully featured database design or password changing functionality.

The rules for the password are the same as for Task 1 (above), and in addition:

Do not allow incremental passwords when changing the password (e.g.

{PasswordRoot}{n} changed into {PasswordRoot}{n+1}, where 'n' is a number).

4.3 Notes on “expect”

Expect or pexpect for Python is available from various sources.

For *nix the pexpect module is available (http://www.noah.org/wiki/pexpect)

For Windows, the POSIX compatible pexpect module (as above) may be used with the Cygwin

environment. However, there is a port of pexpect for windows, called winpexpect

(https://pypi.Python.org/pypi/winpexpect).

Hint

When running a Python script as an Expect sub-process, the script may need to be executed in
unbuffered

mode:

child = winpexpect.winspawn("Python -u password.py")

S-ar putea să vă placă și