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 »
how much will it cost to complete this programming assignment?


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 (arbitrary) error 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 (arbitrary) error message and exits. • The contents of the file consists of some number of lines, each line being a sequence of the same number 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 2 2 2 2 2 1 3 4 3 2 2 3 5 3 2 2 4 1 1 2 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 2 2 2 2 2 1 3 4 3 2 2 3 5 3 2 2 4 1 1 2 $ 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 arbitrary 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 incorrect, then the program outputs an error 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 incorrect, then the program outputs another error 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 power of nb_of_switches many heroes, a given hero seeing its power being switched an arbitrary number 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 − 1 + 2 + 7 = 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 be achieved is 17; this can be done by switching -7 once, 1 twice, and 3 twice: 7 + 1 + 2 + 3 + 4 = 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 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 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 power 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 power 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 arbitrarily 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 power 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: 7 + 1 + 2 + 3 + 4 = 17. 4 Here is a possible interaction: $ python3 superpower.py Please input the heroes' powers: 3 5 8.5 Sorry, 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 Sorry, this is not a valid number of power flips. $ python3 superpower.py Please input the heroes' powers: 4 -3 1 2 -7 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 arbitrarily many consecutive heroes, the greatest achievable power is 11. $ python3 superpower.py Please input the heroes' powers: -7 1 2 3 4 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 arbitrarily many consecutive heroes, the greatest achievable power is 17. $ python3 superpower.py Please input the heroes' powers: 1 2 3 4 5 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 arbitrarily many consecutive heroes, the greatest achievable power is 15. 5 4. Cable car 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 ride if the slope does not change over the corresponding 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 latter 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 DayAug 28, 2020COMP9021

Answer To: Assignment 1 COMP9021, Session 2, 2018 1. General matters 1.1. Aims. The purpose of the assignment...

Akash answered on Aug 30 2020
145 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
assignm
ent/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 FileNotFoundError:
print('Sorry, there is no such file.')
sys.exit()
try:
text = file.read()
A = [int(i) for i in text.split()]
except ValueError:
print('Sorry, 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('Sorry, input file does not store valid data.')
sys.exit()
for i in range(len(B)):
if C[i] >= B[i]:
print('Sorry, 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:
break
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:
break
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 ValueError:
print('Sorry, 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 ValueError
except ValueError:
print('Sorry, 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
b = copy.deepcopy(number_list)
b_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