Great Deal! Get Instant $10 FREE in Account on First Order + 10% Cashback on Every Order Order Now

Assignment 1 COMP9021, Session 2, 2018 1. General matters 1.1. Aims. The purpose of the assignment is to: • let you design solutions to simple problems; • let you implement these solutions in the form...

1 answer below »
Assignment 1
COMP9021, Session 2, 2018
1. General matters
1.1. Aims. The purpose of the assignment is to:
• let you design solutions to simple problems;
• let you implement these solutions in the form of short Python programs;
• practice the use of arithmetic computations, tests, repetitions, lists, tuples, dictionaries, deques, reading
from files.
1.2. Submission. Your programs will be stored in a number of files, with one file per exercise, of the appropriate
name. After you have developed and tested your programs, upload your files using Ed. Assignments can be
submitted more than once: the last version is marked. Your assignment is due by September 2, 11:59pm.
1.3. Assessment. Each of the 4 exercises is worth 2.5 marks. For all exercises, the automarking script will let
each of your programs run for 30 seconds. Still you should not take advantage of this and strive for a solution
that gives an immediate output for “reasonable” inputs.
Late assignments will be penalised: the mark for a late submission will be the minimum of the awarded mark
and 10 minus the number of full and partial days that have elapsed from the due date.
The outputs of your programs should be exactly as indicated.
1.4. Reminder on plagiarism policy. You are permitted, indeed encouraged, to discuss ways to solve the
assignment with other people. Such discussions must be in terms of algorithms, not code. But you must
implement the solution on your own. Submissions are routinely scanned for similarities that occur when students
copy and modify other people’s work, or work very closely together on a single implementation. Severe penalties
apply.
1
2
2. Rain
Write a program, stored in a file named rain.py, that performs the following task.
• The program prompts the user to input a file name. If there is no file with that name in the working
directory, then the program outputs an (a
itrary) e
or message and exits.
• The program prompts the user to input a nonnegative integer. If the input is not a nonnegative integer,
then the program outputs an (a
itrary) e
or message and exits.
• The contents of the file consists of some number of lines, each line being a sequence of the same numbe
of nonnegative integers separated by at least one space, with possibly spaces before and after the first
and last number, respectively. These numbers represent the height in centimetres of a block with a
square base of 10 square centimetres. For instance, the contents of the file land.txt can be represented
as
XXXXXXXXXX
XXXXXXXXXX
XXXXXXXXXX
XXXXXXXXXX
and would represent a land covering an area of 50 centimetres by 40 centimetres, with a block of height 2
centimetres in each corner, etc. The number input by the user represents a quantity of rain in decilitres,
to be poured down on the land.
• The program outputs the height in centimetres, with a precision of 2 digits after the decimal point, that
is reached by the water.
Here is a possible interaction:
$ cat land.txt
XXXXXXXXXX
XXXXXXXXXX
XXXXXXXXXX
XXXXXXXXXX
$ python3 rain.py
Which data file do you want to use? land.txt
How many decilitres of water do you want to pour down? 1
The water rises to 1.33 centimetres.
$ $ python3 rain.py
Which data file do you want to use? $ land.txt
How many decilitres of water do you want to pour down? 33
The water rises to 4.00 centimetres.
$ $ python3 rain.py
Which data file do you want to use? $ land.txt
How many decilitres of water do you want to pour down? 50
The water rises to 4.89 centimetres.
You can assume that the contents of any test file is as expected, you do not have to check that it is as
expected.
3
3. Superpowers
Write a program, stored in a file named superpower.py, that does the following.
• The program prompts the user for an a
itrary number of (positive or negative) integers, all provided
on one line and separated by at least one space, with possibly extra spaces anywhere. The user can
input no integer at all. If the input is inco
ect, then the program outputs an e
or message and exits.
These integers are meant to represent the superpower of a bunch of heroes (positive power is good, all
the better that it is larger; negative power is bad, all the worse that it is smaller).
• Then the program prompts the user for a nonnegative integer (possibly equal to 0), nb_of_switches, at
most equal to the number of integers previously input (the number of heroes). If the input is inco
ect,
then the program outputs another e
or message and exits. This number is meant to represent the
number of switches (multiplication by -1) to the power of some heroes; for instance, a hero with positive
power 7 can see its power switched to -7, and a hero with negative power -5 can see its power switched
to 5.
• First, the program determines the maximum overall power that can be achieved by switching the powe
of nb_of_switches many heroes, a given hero seeing its power being switched an a
itrary numbe
of times (not switched at all, or switched once, or switched more than once). For instance, with the
sequence (4, -3, 1, 2, -7) and nb_of_switches set to 3, the maximum overall power that can be achieved
is 15; this can be done by switching -3, 1 and -7 once: 4 + 3 − XXXXXXXXXX = 15. For another example,
with the sequence (-7, 1, 2, 3, 4) and nb_of_switches set to 5, the maximum overall power that can
e achieved is 17; this can be done by switching -7 once, 1 twice, and 3 twice: XXXXXXXXXX = 17.
• Second, the program determines the maximum overall power that can be achieved by switching the
power of nb_of_switches many heroes, a given hero seeing its power being switched at most once (not
switched at all, or switched once). For instance, with the sequence (4, -3, 1, 2, -7) and nb_of_switches
set to 3, the maximum overall power that can be achieved is 15, as previously described. For anothe
example, with the sequence (-7, 1, 2, 3, 4) and nb_of_switches set to 5, the maximum overall powe
that can be achieved is -3, as each power has to be switched: 7− 1− 2− 3− 4 = −3.
• Third, the program determines the maximum overall power that can be achieved by switching the powe
of nb_of_switches many consecutive heroes (note that first and last heroes are not consecutive, the
heroes are aligned on a line). For instance, with the sequence (4, -3, 1, 2, -7) and nb_of_switches set
to 3, the maximum overall power that can be achieved is 5; this can be done by switching the powe
of the last 3 heroes: 4 − 3 − 1 − 2 + 7 = 5. For another example, with the sequence (-7, 1, 2, 3, 4)
and nb_of_switches set to 5, the maximum overall power that can be achieved is -3, as previously
described.
• Fourth, the program determines the maximum overall power that can be achieved by switching the
power of a
itrarily many (possibly none) consecutive heroes. For instance, with the sequence (4, -3, 1,
2, -7), the maximum overall power that can be achieved is 17; this can be done by switching the powe
of the last 4 heroes: 4 + 3− 1− 2 + 7 = 11. For another example, with the sequence (-7, 1, 2, 3, 4), the
maximum overall power that can be achieved is 17; this can be done by switching the power of the first
hero: XXXXXXXXXX = 17.
4
Here is a possible interaction:
$ python3 superpower.py
Please input the heroes' powers: XXXXXXXXXX
So
y, these are not valid power values.
$ python3 superpower.py
Please input the heroes' powers: 3 5 8
Please input the number of power flips: 4
So
y, this is not a valid number of power flips.
$ python3 superpower.py
Please input the heroes' powers: XXXXXXXXXX
Please input the number of power flips: 3
Possibly flipping the power of the same hero many times, the greatest achievable power is 15.
Flipping the power of the same hero at most once, the greatest achievable power is 15.
Flipping the power of nb_of_flips many consecutive heroes, the greatest achievable power is 5.
Flipping the power of a
itrarily many consecutive heroes, the greatest achievable power is 11.
$ python3 superpower.py
Please input the heroes' powers: XXXXXXXXXX
Please input the number of power flips: 5
Possibly flipping the power of the same hero many times, the greatest achievable power is 17.
Flipping the power of the same hero at most once, the greatest achievable power is -3.
Flipping the power of nb_of_flips many consecutive heroes, the greatest achievable power is -3.
Flipping the power of a
itrarily many consecutive heroes, the greatest achievable power is 17.
$ python3 superpower.py
Please input the heroes' powers: XXXXXXXXXX
Please input the number of power flips: 2
Possibly flipping the power of the same hero many times, the greatest achievable power is 15.
Flipping the power of the same hero at most once, the greatest achievable power is 9.
Flipping the power of nb_of_flips many consecutive heroes, the greatest achievable power is 9.
Flipping the power of a
itrarily many consecutive heroes, the greatest achievable power is 15.
5
4. Cable ca
Consider the following picture of the pillars and cable that provide the structure for a cable car. Successive
pillars are equidistant. The height of each pillar is indicated above it. Continuous pillars make up for a good
ide if the slope does not change over the co
esponding section. Here the longest good ride is between the
pillar of height 25 and the pillar of height 28; it is depicted in blue. It has a length of 3 (units, whatever it is).
5
10
14 15
20
25 26
27 28
30 31
If one gets rid of the black pillars and keeps only the remaining (red) ones, then one can use use the latte
and create a new structure (with successive pillars being equidistant) which makes up for a perfect ride, that
is, good from first to last pillar:
Answered Same Day Aug 28, 2020 COMP9021

Solution

Akash answered on Aug 30 2020
147 Votes
assignment/cable_car1.txt
5
10 14 15
20 25 26 27 28
30
31
assignment/cable_car.txt
10 13 20 30 40 42
44 46 48 50 60 70 80 82 85 87
90 100 101 110 113 117
121
assignment/tunnel_wrong1.txt
7 3 9
1 3 4
assignment/tunnel_wrong.txt
7 3
1 0 4
assignment/tunnel.txt
7 9 6 8
6 2 1 5
assignment/tunnel.py
import os.path
import sys
from collections import defaultdict
import copy
try:
file = open(input('Please enter the name of the file you want to get data from: '))
except FileNotFoundE
or:
print('So
y, there is no such file.')
sys.exit()
try:
text = file.read()
A = [int(i) for i in text.split()]
except ValueE
or:
print('So
y, input file does not store valid data.')
sys.exit()
finally:
file.close()
B = A[0:int(len(A)/2)]
C = A[int(len(A)/2):len(A)]
if len(A) % 2 != 0 or len(A)<4:
print('So
y, input file does not store valid data.')
sys.exit()
for i in range(len(B)):
if C[i] >= B[i]:
print('So
y, input file does not store valid data.')
sys.exit()
deep = 1
deep_max = 1
for j in range(0,B[0]-C[0]):
deep = 1
for i in range(1, len(B)):
if C[i]<=(C[0]+j) and B[i]>(C[0]+j):
deep += 1
if deep > deep_max:
deep_max = deep
else:

eak
print('From the west, one can into the tunnel over a distance of',deep_max)
deep1 = 1
deep1_max = 1
for x in range(len(B)):
if x < len(B)-1:
for j in range(B[x]-C[x]):
deep1 = 1
for i in range(x+1, len(B)):
if C[i]<=(C[x]+j) and B[i]>(C[x]+j):
deep1 += 1
if deep1 > deep1_max:
deep1_max = deep1
else:

eak
print('Inside the tunnel, one can into the tunnel over a maximum distance of',deep1_max)
assignment/cable.txt
5
10 14 15

20 25 26 27 28 30
31
assignment/superpower.py
import sys
import copy
try:
number_list = [int(x) for x in input('Please input the heroes\' powers: ').split()]
except ValueE
or:
print('So
y, these are not valid power values.')
sys.exit()
try:
nb_of_swiches = input('Please input the number of power flips: ')
nb_of_swiches = int(nb_of_swiches)
if nb_of_swiches < 0 or nb_of_swiches > len(number_list):
raise ValueE
o
except ValueE
or:
print('So
y, this is not a valid number of power flips.')
sys.exit()
a = nb_of_swiches
a_1 = nb_of_swiches
a_2 = nb_of_swiches
= copy.deepcopy(number_list)
_1...
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here