Write a function that returns the sum of two largest values

Assignment Help Python Programming
Reference no: EM132360777

PART 1

For this part, you will be solving some problems and implementing the solutions. The idea is to design and implement solutions that are as fast as possible, and to analyze these solutions as well. You will submit two files:

a3_part2_xxxxxx.py contains the functions you are asked to write for each question.

a3_part2_xxxxxx.txt contains, for each function, a rough analysis of its running time when the argument, n, or the length of the list, a, is 10,000. For example, if your function looks like this:

def riddle(a):
s=0
for x in a:
for y in a:
s = s+ x*y
return s

Then you would write: This function has two nested for loops. The inner loop executes 10,000 times for each step of the outer loop, which also executes 10,000 times. Therefore, this function performs roughly 10,000*10,000=100,000,000 operations. If your function uses
the sort() method to sort a, just say that this uses about about 140,000 operations (since sort uses roughly n log_2 n operations).

In all of the questions you can assume that a is a list containing numbers.

2a) Write a function, largest_two(a), that returns the sum of the two largest values in the list a.

2b) Write a function, smallest_half(a), that computes the sum of the len(a)//2 smallest values in the list a.

2c) Write a function, median(a), that returns a value, x, such that at least half the elements in a are less than or equal to x and at least half the elements in a are greater than or equal to x.

2d) Write a function, at_least_third(a), that returns a value in a that occurs at least len(a)//3 + 1 times. If no such element exists in a, then this function returns None.

2e) Write a function, triple_sum(a,x), that takes a list ,a, as input and returns True if there exists i, j and k (where i and j and k are not necessarily distinct)

such that a[i]+a[j]+a[k]=x. Otherwise it returns False. For example, if a=[1, 5, 8, 2, 6, 55, 90] and x=103, then triple_sum(a, x) would return True since

a[1]+a[2]+a[6]=5+8+90=103. If a=[-1, 1, 5, 8, 2, 6] and x=-3, triple_sum(a, x) would return True since a[0]+a[0]+a[0]=-1+ -1 + -1 = -3. If a=[-10,2] and x=-18, triple_sum(a, x) would return True since a[0]+a[0]+a[1]=-10+-10+2=18. If a=[1, 1, 5, 8, 2, 6] and
x=1000 would return False, since there are not indices i, j and k such that a[i]+a[j]+a[k]=1000.

PART 2

For this part, I provided two files, called a5_part3_xxxxxx.py and a3_part3_testing_given.txt

File a5_part2_xxxxxx.py already contains a class Point that we developed in class. For this part, you will need to develop and add two more classes to a5_part2_xxxxxx.py: class Rectangle and class Canvas.

To understand how they should be designed and how they should behave, you must study in detail the test cases provided in a3_part3_testing_given.txt. These tests are your main resource in understanding what methods your two classes should have and what their input parameters are. I will explain few methods below in detail, but only those whose behaviour may not be clear from the test cases.
As in Assignment 1 and Assignment 2, for part 3 of this assignment you will need to also submit your own text file called a3_part3_testing_xxxxxx.txt demonstrating that you tested your two classes and their methods (in particular, demonstrating that you tested them by running all the calls made in a3_part3_testing_given.txt)

Details about the two classes:
=====
Class Rectangle represents a 2D (axis-parallel) rectangle that a user can draw on a computer screen. Think of a computer screen as a 2D map where each position has an x and a y coordinate.

The data that each object of type Rectangle should have (and that should be populated in the constructor, i.e., __init__ method of the class Rectangle) are:
* two Points: the first point representing the bottom left corner of the rectangle and the second representing the top right corner of the rectangle; and,
* the color of the rectangle

Note that the two points (bottom left and top right) completely determine (the axis parallel) rectangle and its position on the map. There is no default rectangle.

The __init__ method of Rectangle (that is invoked by the constructor Rectangle) will take two objects of class Point as input and a string for the color). You may assume that the first point (sent to the constructor, i.e. __init__) will always have smaller than or equal x coordinate than the x coordinate of the second point and smaller than or equal y coordinate than the y coordinate of the second point.

Class Rectangle should have 13 methods. In particular, in addition to the constructor (i.e. __init__ method) and three methods that override python's object methods (and make your class user friendly as suggested by the test cases), your class should contain the following 9 methods:

get_bottom_left, get_top_right, get_color, reset_color, get_perimeter, get_area, move, intersects, and contains.

Here is a description of three of those methods whose job may not be obvious from the test cases.

* Method move: given numbers dx and dy this method moves the calling rectangle by dx in the x direction and by dy in the y-direction. This method should not change directly the coordinates of the two corners of the calling rectangle, but must instead call move method
from the Point class.

* Method intersects: returns True if the calling rectangle intersects the given rectangle and False otherwise. Definition: two rectangles intersect if they have at least one point in common, otherwise they do not intersect.

* Method contains: given an x and a y coordinate of a point, this method tests if that point is inside of the calling rectangle. If yes it returns True and otherwise False. (A point on the boundary of the rectangle is considered to be inside).

Class Canvas represents a collection of Rectangles. It has 8 methods. In addition, to the constructor (i.e. __init__ method) and two methods that override python's object methods (and make your class user friendly as suggested by the test cases), your class should
contain 5 more methods:

add_one_rectangle, count_same_color, total_perimeter, min_enclosing_rectangle, and common_point.

Here is a description of those methods whose job may not be obvious from the test cases.

* The method total_perimeter: returns the sum of the perimeters of all the rectangles in the calling canvas. To compute total perimeter do not compute a perimeter of an individual rectangle in the body of total_perimeter method. Instead use get_perimeter method from the
Rectangle class.

* Method min_enclosing_rectangle: calculates the minimum enclosing rectangle that contains all the rectangles in the calling canvas. It returns an object of type Rectangle of any colour you prefer. To find minimum enclosing rectangle you will need to find the minimum x
coordinate of all rectangles, the maximum x coordinate for all rectangles, the minimum y coordinate and the maximum y coordinate of all rectangles.

* Method common_point: returns True if there exists a point that intersects all rectangles in the calling canvas. To test this (for axis parallel rectangles like ours), it is enough to test if every pair of rectangles intersects (according to a Helly's theorem).

Finally recall, from the beginning of the description of this assignment that each of your methods should have a type contract.

Reference no: EM132360777

Questions Cloud

Calculate the syndrome : what is the loss in terms of mean SNR compared with full MRC and Derive the pdf of the SNR at the output of the combiner for the given cases.
Compute the value of e using all three of the methods : Write a program that computes the value of e using all three of the methods described. Use values of 1, 2, 5, 10, 100, 1000, and 10000 for the first method.
How does one define a new class and its methods : What is meant by the methods associated with a class/object? How do they relate to object-oriented programming? How does one define a new class and its methods?
What impact does organizational systems : What impact does organizational systems have on its unethical behavior? How important is organizational culture and code of conduct on ethical behavior?
Write a function that returns the sum of two largest values : Write a function, median(a), that returns a value, x, such that at least half the elements in a are less than or equal to x and at least half the elements.
Describe evolution for scientist point of view : What are three key factors that influence species richness in ecosystem? Describe evolution for scientist's point of view. Explain diversity of species
How can you modify strategies in consultation : How can you modify strategies in consultation with key organisational and stakeholder personnel?
Write report about advantages of renewable energy sources : Write a report about the advantages and disadvantages of various renewable energy sources: solar PV, wind, hydroelectric, biomass, waste incineration
Context is the key dimension in culture : Context is the key dimension in culture what strategy would you use to communicate as a manager to someone from a Japanese culture?

Reviews

Write a Review

Python Programming Questions & Answers

  Write a python program to implement the diff command

Without using the system() function to call any bash commands, write a python program that will implement a simple version of the diff command.

  Write a program for checking a circle

Write a program for checking a circle program must either print "is a circle: YES" or "is a circle: NO", appropriately.

  Prepare a python program

Prepare a Python program which evaluates how many stuck numbers there are in a range of integers. The range will be input as two command-line arguments.

  Python atm program to enter account number

Write a simple Python ATM program. Ask user to enter their account number, and print their initail balance. (Just make one up). Ask them if they wish to make deposit or withdrawal.

  Python function to calculate two roots

Write a Python function main() to calculate two roots. You must input a,b and c from keyboard, and then print two roots. Suppose the discriminant D= b2-4ac is positive.

  Design program that asks user to enter amount in python

IN Python Design a program that asks the user to enter the amount that he or she has budget in a month. A loop should then prompt the user to enter his or her expenses for the month.

  Write python program which imports three dictionaries

Write a Python program called hours.py which imports three dictionaries, and uses the data in them to calculate how many hours each person has spent in the lab.

  Write python program to create factors of numbers

Write down a python program which takes two numbers and creates the factors of both numbers and displays the greatest common factor.

  Email spam filter

Analyze the emails and predict whether the mail is a spam or not a spam - Create a training file and copy the text of several mails and spams in to it And create a test set identical to the training set but with different examples.

  Improve the readability and structural design of the code

Improve the readability and structural design of the code by improving the function names, variables, and loops, as well as whitespace. Move functions close to related functions or blocks of code related to your organised code.

  Create a simple and responsive gui

Please use primarily PHP or Python to solve the exercise and create a simple and responsive GUI, using HTML, CSS and JavaScript.Do not use a database.

  The program is to print the time

The program is to print the time in seconds that the iterative version takes, the time in seconds that the recursive version takes, and the difference between the times.

Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd