Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: daixieit

COMP(2041|9044) 22T2 — Week 06 Weekly Test Questions

Week 06 Weekly Test Questions

Test Conditions

These questions must be completed under self-administered exam-like conditions. You must time the test yourself and ensure you comply with the conditions below.

You may complete this test in CSE labs or elsewhere using your own machine.

You may complete this test at any time before Week 7 Thursday 18º00º00.

Weekly tests are designed to act like a past paper - to give you an idea of how well you are progressing in the course, and what you need to work on. Many of the questions in weekly tests are from past final exams.

Once the first hour has finished, you must submit all questions you've worked on.

You should then take note of how far you got, which parts you didn't understand.

You may choose then to keep working and submit test question anytime up to Week 7 Thursday 18º00º00 However the maximum mark for any question you submit after the first hour will be 50%

You may access this language documentation while attempting this test:

manual entries, via the man command.

Texinfo pages, via the info command.

Bash documentation via the  help command.

Shell/Regex quick reference

Python documentation via the  python3 -c 'help()' command.

Python quick reference

full Python 3.9 documentation

Any violation of the test conditions will results in a mark of zero for the entire weekly test component.

Set up for the test by creating a new directory called  test06 and changing to this directory.

$ mkdir test06

$ cd test06

There are no provided files for this test.

Test Complete!

Your time for this test has finished. You must submit your work now. You should

reflect on how you went in this hour, and discuss with your tutor if you have

concerns. You may choose to keep working, but the maximum mark for any

questions you submit later will be 50%.

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:

$ ./create_integers_file.py 40 42 fortytwo.txt

$ cat fortytwo.txt

40

41

42

$ ./create_integers_file.py -5 5 a.txt

$ cat a.txt

-5

-4

-3

-2

-1

0

1

2

3

4

5

$ ./create_integers_file.py 1 1000 a.txt

$ wc a.txt

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:


$ 2041 autotest python_create_integers_file

When you are finished working on this exercise you must submit your work by running give:


$ give cs2041 test06_python_create_integers_file create_integers_file.py


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

$ seq 42 99 > numbers.txt

$ head numbers.txt

42

43

44

45

46

47

48

49

50

51

$ tail numbers.txt

90

91

92

93

94

95

96

97

98

99

$ ./nth_line.py 1 numbers.txt

42

$ ./nth_line.py 20 numbers.txt

61

$ ./nth_line.py 1000 numbers.txt

$ echo this file has one line > file.txt

$ cat file.txt

this file has one line

$ ./nth_line.py 1 file.txt

this file has one line

$ ./nth_line.py 42 file.txt