Week 06 Weekly Test Questions
Getting Started
Set up for the test by creating a new directory called test06 and changing to this directory.
1 $ mkdir test06
2 $ cd test06
There are no provided files for this test.
WEEKLY TEST QUESTION:
Create A File of Integers In Python
Write a Python program,
create_integers_file.py
which takes 3 arguments.
The first and second arguments will specify a range of integers.
The third argument will specify a filename.
Your program should create a file of this name containing the specified integers.
For example:
1 $ ./create_integers_file.py 40 42 fortytwo.txt
2 $ cat fortytwo.txt
3 40
4 41
5 42
6 $ ./create_integers_file.py -5 5 a.txt
7 $ cat a.txt
8 -5
9 -4
10 -3
11 -2
12 -1
13 0
14 1
15 2
16 3
17 4
18 5
19 $ ./create_integers_file.py 1 1000 a.txt
20 $ wc a.txt
21 1000 1000 3893 a.txt
NOTE:
You assume your program is give three valid arguments.
Your program can assume its first and second arguments will be integers.
Your program can assume its second argument is greater than or equal to its first argument.
Your answer must be Python only. You can not use other languages such as Shell, Perl or C.
Make the first line of your script #!/usr/bin/env python3
You may not run external programs. For example, you can't run seq.
No error checking is necessary.
When you think your program is working you can autotest to run some simple automated tests:
1 $ 2041 autotest python_create_integers_file
When you are finished working on this exercise you must submit your work by running give:
1 $ give cs2041 test06_python_create_integers_file create_integers_file.py
WEEKLY TEST QUESTION:
Print the N-th Line of a File
Write a Python program, nth_line.py which prints the n-th line of a file.
It will be given two arguments n and the file name.
Your program should print nothing if the file does not have an n-th line
1 $ seq 42 99 > numbers.txt
2 $ head numbers.txt
3 42
4 43
5 44
6 45
7 46
8 47
9 48
10 49
11 50
12 51
13 $ tail numbers.txt
14 90
15 91
16 92
17 93
18 94
19 95
20 96
21 97
22 98
23 99
24 $ ./nth_line.py 1 numbers.txt
25 42
26 $ ./nth_line.py 20 numbers.txt
27 61
28 $ ./nth_line.py 1000 numbers.txt
29 $ echo this file has one line > file.txt
30 $ cat file.txt
31 this file has one line
32 $ ./nth_line.py 1 file.txt
33 this file has one line
34 $ ./nth_line.py 42 file.txt
NOTE:
You assume your program is give two valid arguments.
Your program can assume its first argument will be a positive integer.
You should not assume anything about the lines in the file.
You can assume the file contains ASCII text.
Your answer must be Python only. You can not use other languages such as Shell, Perl or C.
Make the first line of your script #!/usr/bin/env python3
You may not run external programs. For example, you can't run sed.
No error checking is necessary.
When you think your program is working you can autotest to run some simple automated tests:
1 $ 2041 autotest python_nth_line
When you are finished working on this exercise you must submit your work by running give:
1 $ give cs2041 test06_python_nth_line nth_line.py
WEEKLY TEST QUESTION:
Reverse the Lines of a File into a New File
Write a Python program, reverse_file.py which creates a file that is the reverse of another files.
It will be given two arguments the file name of the source file, and the file name of the destination file.
Your program should print nothing.
1 $ seq 42 99 > numbers.txt
2 $ head numbers.txt
3 42
4 43
5 44
6 45
7 46
8 47
9 48
10 49
11 50
12 51
13 $ tail numbers.txt
14 90
15 91
16 92
17 93
18 94
19 95
20 96
21 97
22 98
23 99
24 $ ./reverse_file.py numbers.txt new_numbers.txt
25 $ head new_numbers.txt
26 99
27 98
28 97
29 96
30 95
31 94
32 93
33 92
34 91
35 90
36 $ tail new_numbers.txt
37 51
38 50
39 49
40 48
41 47
42 46
43 45
44 44
45 43
46 42
47 $ echo this file has one line > file.txt
48 $ cat file.txt
49 this file has one line
50 $ ./reverse_file.py file.txt reverse.txt
51 $ cat reverse.txt
52 this file has one line
When you think your program is working you can autotest to run some simple automated tests:
1 $ 2041 autotest python_reverse_file
When you are finished working on this exercise you must submit your work by running give:
1 $ give cs2041 test06_python_reverse_file reverse_file.py